When a payment method is selected that uses the gateway driver, the register
method is called. Within this method, the bootstrapping of the transaction should be carried out. This may involve connecting to the payment gateway's API to register the transaction. For example, the gateway may need to know the total amount to request from the customer's card, along with where to redirect the browser once the transaction has been processed.
The register
method should return a Aero\Payment\Responses\PaymentResponse
instance.
public function register()
{
// make an API call to create the transaction with Acme Payments (the fake 3rd party gateway company)
$request = AcmePaymentsInc::createTransaction([
'merchant_reference' => $this->order->reference,
'amount' => $this->order->total_rounded,
'currency' => $this->order->currency_code,
'billing_address' => [
'line_1' => $this->order->billingAddress->line_1,
'postcode' => $this->order->billingAddress->postcode,
'country' => $this->order->billingAddress->country_code,
],
'return_url' => route("payments-{$this->getMerchantMode()}.complete-payment"),
]);
$response = new \Aero\Payment\Responses\PaymentResponse();
// set the response as successful if the transaction status is "created"
$response->setSuccessful($request->status === AcmePaymentsInc::STATUS_CREATED);
// set the data to pass to the view
$response->setData([
'transaction_reference' => $request->reference,
]);
// set the view to use (this is typically an iframe or JavaScript)
$response->setView("acme-payments::{$this->getMerchantMode()}-form");
return $response;
}
The callback URL(s) may differ depending on the operating mode. If the driver supports both ECOM
and MOTO
modes, the getMerchantMode
method can be used:
$request = AcmePaymentsInc::createTransaction([
// ...
'return_url' => route("payments-{$this->getMerchantMode()}.complete-payment"),
]);