The payment process consists of two steps: initializePayment
with validation and completePayment
. Before we can order something, we have to put something into the cart.
First Order
Add product to cart
$merx = merx();
$merx->cart()->add(['id' => 'nice-shoes']);
The id
has to be a valid page. The page must have a price field.
On another page you should show the customer’s cart.
site/templates/cart.php
$merx = merx();
foreach($merx->cart() as $item) {
$itemPage = page($item['id']);
echo $itemPage->title(); // Nice shoes
echo $itemPage->price(); // 99.99
}
Initialize Payment
Provide a paymentMethod
and other data you need to handle the order (email, name, billing / shipping address etc.).
The payment is validated by the Order Page. Read more about validation.
site/templates/checkout.php
$data = [
'paymentMethod' => 'paypal',
'email' => 'chuck@example.com',
'name' => 'Chuck Norris',
…
]
try {
$redirect = merx()->initializePayment($data);
go($redirect);
} catch (Exception $ex) {
echo $ex->getMessage();
}
Complete Payment
Depending on the payment method the customer is redirected directly or after the customer completed the payment on the payment provider’s external webpage (e.g. PayPal) to the success page.
site/templates/success.php
try {
$orderPage = merx()->completePayment();
echo 'Payment completed.';
} catch (Exception $ex) {
echo $ex->getMessage();
}