I need to integrate a new payment gateway to our corporate website, which is based on Social Engine. There is an extension for this CMS
called Advanced Payment Gateways which allows integration of new gateways. In fact, it gets your gateway name and generates a skeleton structure zipped as a file so you can unzip and upload to your server and thus merge with the application directory.
I'm going to explain how I implement my gateway without Social Engine, and I hope someone can tell me how I can incorporate that into Social Engine.
First I connect to my
PSP
service:$client = new nusoap_client('https://example.com/pgwchannel/services/pgw?wsdl');
I prepare the following parameters in an array to send to
bpPayRequest
:$parameters = array( 'terminalId' => $terminalId, 'userName' => $userName, 'userPassword' => $userPassword, 'orderId' => $orderId, 'amount' => $amount, 'localDate' => $localDate, 'localTime' => $localTime, 'additionalData' => $additionalData, 'callBackUrl' => $callBackUrl, 'payerId' => $payerId); // Call the SOAP method $result = $client->call('bpPayRequest', $parameters, $namespace);
If payment request is accepted, the result is a comma separated string, with the first element being 0.
Then we can send the second element (reference id) to payment gateway as follows viaPOST
method:echo "<script language='javascript' type='text/javascript'>postRefId('" . $res[1] . "');</script>"; <script language="javascript" type="text/javascript"> function postRefId (refIdValue) { var form = document.createElement("form"); form.setAttribute("method", "POST"); form.setAttribute("action", "https://example.com/pgwchannel/startpay"); form.setAttribute("target", "_self"); var hiddenField = document.createElement("input"); hiddenField.setAttribute("name", "RefId"); hiddenField.setAttribute("value", refIdValue); form.appendChild(hiddenField); document.body.appendChild(form); form.submit(); document.body.removeChild(form); } </script>
The gateway will return the following parameters via
POST
method to the call backURL
that we provided in payment request:
RefId
(reference id as produced in previous steps)
ResCode
(Result of payment: 0 denotes success)
saleOrderId
(order id as passed during payment request)
SaleReferenceId
(sale reference code is given by PSP to the merchant)If
ResCode
in the previous step was 0, then we'd need to pass the callbpVerifyRequest
with the following parameters to verify payment, otherwise the payment will be canceled.$parameters = array( 'terminalId' => $terminalId, 'userName' => $userName, 'userPassword' => $userPassword, 'orderId' => $orderId, 'saleOrderId' => $verifySaleOrderId, 'saleReferenceId' => $verifySaleReferenceId); // Call the SOAP method $result = $client->call('bpVerifyRequest', $parameters, $namespace);
In case the result of
bpVerifyRequest
is zero, payment is certain and the merchant has to provide goods or services purchased. However, there is an optional methodbpSettleRequest
, which is used to request a settlement. It is called as follows:
$parameters = array(
'terminalId' => $terminalId,
'userName' => $userName,
'userPassword' => $userPassword,
'orderId' => $orderId,
'saleOrderId' => $settleSaleOrderId,
'saleReferenceId' => $settleSaleReferenceId);
// Call the SOAP method
$result = $client->call('bpSettleRequest', $parameters, $namespace);
I get confused by looking at default gateways in the Payment Gateways plugin e.g. PayPal, Stripe, 2Checkout, etc. How am I incorporate this code logic into the newly created gateway skeleton? (the structure is shown below):
You can check out the complete source code here:
default.php
callback.php
I solved this by adding the payment code inside the
Engine_Payment_Gateway_MyGateway
class:Once the user confirms on the SocialEngine page that they want to pay, the method
processTransaction()
inside the mentioned class is called and the user is redirected to the PSP's payment secure page. Once they are done with the payment, i.e. paid successfully or failed or canceled the transaction, they PSP's page redirects them to the page we had sent to it earlier as a parameter called callBackUrl. There, you will receive PSP-specific parameters which helps you decide whether the payment was successful and to ask the PSP with another SOAP call to confirm the payment and then optionally ask it to settle (deposit money ASAP into the seller's account):Add to processTransaction():
Add to your callBack action: