Merx

Custom Payment Gateways

Use the gateway option to add your own payment gateway.

initializePayment and completePayment must return a OrderPage object.

In initializePayment you can set a redirect url. The user will be redirected to this url. If you don’t set redirect the user will be sent to successPage.

site/config/config.php

return [
  'ww.merx.gateways' => [
    'my-payment-provider' => [
      'initializePayment' => function(OrderPage $virtualOrderPage): OrderPage
      {
        $sum = $virtualOrderPage->cart()->getSum();
        // do something to get a redirect url
        $redirectUrl = 'https://mypaymentprovider.com/payment/mlDquvqMQK85M1Pw',
        if (version_compare(App::version(), '5.0.0', '>=')) {
            $virtualOrderPage->version('latest')->update([
                'redirect' => $redirectUrl,
            ]);
        } else {
            $virtualOrderPage->content()->update([
              'redirect' => $redirectUrl,
            ]);
        }
        return $virtualOrderPage;
      },
      'completePayment' => function(OrderPage $virtualOrderPage, array $data): OrderPage
      {
        if (version_compare(App::version(), '5.0.0', '>=')) {
          $virtualOrderPage->version('latest')->update([
            'paymentComplete' => true,
            'paidDate' => date('c'),
          ]);
        } else {
          $virtualOrderPage->content()->update([
            'paymentComplete' => true,
            'paidDate' => date('c'),
          ]);
        }
        return $virtualOrderPage;
      }
    ],
  ],
];

Be careful. Since $virtualOrderPage is just virtual and not yet written to the content folder you can't use $page->update() (which would write a content file). Instead you can use $page->version('latest')->update() to update the virtual page’s content.