Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
nPay Integration Example
<?php
/**
* Created by PhpStorm.
* User: bhaktaraz
* Date: 6/13/18
* Time: 2:47 PM
*/
namespace Fundprabhu\Bundle\PaymentBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Fundprabhu\Bundle\MainBundle\Entity\Donation;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class NpayController extends Controller
{
/**
* @param Donation $donation
* @return array
*/
public function validateMerchant($donation)
{
$merchantId = $this->getParameter('npay_merchant_id');
$merchantUserName = $this->getParameter('npay_merchant_username');
$merchantPassword = $this->getParameter('npay_merchant_password');
$merchantSignaturePassCode = $this->getParameter('npay_merchant_signature');
$merchantValidateEndpoint = $this->getParameter('npay_validate_merchant_url');
$merchantTransactionId = $donation->getId();
$amount = $donation->getAmount();
$paymentMode = 'SCT';
$merchantPasswordEncoded = hash('sha256', $merchantUserName.$merchantPassword);
$signature = hash('sha256', $merchantSignaturePassCode.$merchantUserName.$merchantTransactionId);
# Initialize webservice with WSDL
$client = new \SoapClient($merchantValidateEndpoint);
# Set your parameters for the request
$params = array(
"MerchantId" => $merchantId,
"MerchantTxnId" => $merchantTransactionId,
"MerchantUserName" => $merchantUserName,
"MerchantPassword" => $merchantPasswordEncoded,
"Signature" => $signature,
"AMOUNT" => $amount,
"purchaseDescription" => '',
"PaymentMode" => $paymentMode,
);
# Invoke webservice method with parameters, Function Name: ValidateMerchant
$response = $client->__soapCall("ValidateMerchant", array($params));
if($response->ValidateMerchantResult->STATUS_CODE != "0")
{
$STATUS_CODE = $response->ValidateMerchantResult->STATUS_CODE;
# Error occured while validating merchant. End process.
die("Error on validating merchant. <br> Error Code: " . $STATUS_CODE . " <br>MESSAGE: " . $response->ValidateMerchantResult->MESSAGE);
}
$data = array();
$data['processID'] = $response->ValidateMerchantResult->PROCESSID;
$data['MerchantID'] = $merchantId;
$data['MerchantTxnID'] = $merchantTransactionId;
$data['PayAmount'] = $amount;
$data['MerchantUsername'] = $merchantUserName;
$data['PaymentMode'] = $paymentMode;
return $data;
}
public function redirectAction(Request $request)
{
$donationId = $request->get('donation_id');
/** @var Donation $donation */
$donation = $this->getDoctrine()->getRepository(Donation::class)->find($donationId);
$data = $this->validateMerchant($donation);
$data['description'] = 'Donation for campaign '.$donation->getCampaign()->getTitle();
return $this->render('FundprabhuPaymentBundle:Npay:redirect.html.twig', $data);
}
public function failureAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$donationId = $this->get('session')->get('donation_id');
$donation = $em->getRepository(Donation::class)->find($donationId);
$em = $this->getDoctrine()->getManager();
$em->persist($donation);
$em->flush();
$this->addFlash('error', 'Unable to make donation for campaign ' . $donation->getCampaign()->getTitle());
return $this->redirectToRoute('fundprabhu_web_user_donations');
}
public function successAction(Request $request)
{
$transactionId = $request->get('MERCHANTTXNID');
$gateWayRefNo = $request->get('GTWREFNO');
$em = $this->getDoctrine()->getManager();
$donation = $em->getRepository(Donation::class)->find($transactionId);
$merchantId = $this->getParameter('npay_merchant_id');
$merchantUserName = $this->getParameter('npay_merchant_username');
$merchantPassword = $this->getParameter('npay_merchant_password');
$merchantSignaturePassCode = $this->getParameter('npay_merchant_signature');
$merchantValidateEndpoint = $this->getParameter('npay_validate_merchant_url');
$merchantTransactionId = $donation->getId();
$merchantPasswordEncoded = hash('sha256', $merchantUserName.$merchantPassword);
$signature = hash('sha256', $merchantSignaturePassCode.$merchantUserName.$merchantTransactionId);
# Initialize webservice with WSDL
$client = new \SoapClient($merchantValidateEndpoint);
# Set your parameters for the request
$params = array(
"MerchantId" => $merchantId,
"MerchantTxnId" => $transactionId,
"MerchantUserName" => $merchantUserName,
"MerchantPassword" => $merchantPasswordEncoded,
"Signature" => $signature,
"GTWREFNO" => $gateWayRefNo,
);
# Merchant must send query to gateway SOAP to get STATUS against GTWREFNO for transaction verification
# Invoke webservice method with parameters, Function Name: CheckTransactionStatus
$response = $client->__soapCall("CheckTransactionStatus", array($params));
# Print webservice response
// print_r($response); // Uncomment to print
# if successfully query executed, STATUS_CODE is returned as 0 (ZERO) else validation error.
if($response->CheckTransactionStatusResult->STATUS_CODE != "0")
{
$STATUS_CODE = $response->CheckTransactionStatusResult->STATUS_CODE;
# Error occured while validating merchant. End process.
// die("Error occured while getting Transaction STATUS . <br> Error Code: " . $STATUS_CODE . " <br>MESSAGE: " . $response->CheckTransactionStatusResult->MESSAGE);
return false;
}
# Proceed as query execution is successful.
$STATUS_CODE = $response->CheckTransactionStatusResult->STATUS_CODE;
$TRANSACTION_STATUS = $response->CheckTransactionStatusResult->TRANSACTION_STATUS;
$AMOUNT = $response->CheckTransactionStatusResult->AMOUNT;
$MERCHANT_TRANSACTIONID = $response->CheckTransactionStatusResult->MERCHANT_TRANSACTIONID;
$REMARKS = $response->CheckTransactionStatusResult->REMARKS;
$GTWREFNO = $response->CheckTransactionStatusResult->GTWREFNO;
if($TRANSACTION_STATUS == "SUCCESS")
{
$transactionAmount = $donation->getAmount();
if($transactionAmount != $AMOUNT){
throw new \Exception('Man in the middle attack');
}
$donation->setStatus(Donation::DONATION_STATUS_SUCCESS);
$em->persist($donation);
$em->flush();
$this->addFlash('success', 'You have successfully donated for campaign ' . $donation->getCampaign()->getTitle());
}
else
{
$this->addFlash('error', 'Unable to make donation for campaign ' . $donation->getCampaign()->getTitle());
}
return $this->redirectToRoute('fundprabhu_web_user_donations');
}
public function deliveryAction(Request $request)
{
$transactionId = $request->get('MERCHANTTXNID');
$gateWayRefNo = $request->get('GTWREFNO');
$em = $this->getDoctrine()->getManager();
$donation = $em->getRepository(Donation::class)->find($transactionId);
$merchantId = $this->getParameter('npay_merchant_id');
$merchantUserName = $this->getParameter('npay_merchant_username');
$merchantPassword = $this->getParameter('npay_merchant_password');
$merchantSignaturePassCode = $this->getParameter('npay_merchant_signature');
$merchantValidateEndpoint = $this->getParameter('npay_validate_merchant_url');
$merchantTransactionId = $donation->getId();
$merchantPasswordEncoded = hash('sha256', $merchantUserName.$merchantPassword);
$signature = hash('sha256', $merchantSignaturePassCode.$merchantUserName.$merchantTransactionId);
# Initialize webservice with WSDL
$client = new \SoapClient($merchantValidateEndpoint);
# Set your parameters for the request
$params = array(
"MerchantId" => $merchantId,
"MerchantTxnId" => $transactionId,
"MerchantUserName" => $merchantUserName,
"MerchantPassword" => $merchantPasswordEncoded,
"Signature" => $signature,
"GTWREFNO" => $gateWayRefNo,
);
# Merchant must send query to gateway SOAP to get STATUS against GTWREFNO for transaction verification
# Invoke webservice method with parameters, Function Name: CheckTransactionStatus
$response = $client->__soapCall("CheckTransactionStatus", array($params));
# Print webservice response
// print_r($response); // Uncomment to print
# if successfully query executed, STATUS_CODE is returned as 0 (ZERO) else validation error.
if($response->CheckTransactionStatusResult->STATUS_CODE != "0")
{
$STATUS_CODE = $response->CheckTransactionStatusResult->STATUS_CODE;
# Error occured while validating merchant. End process.
die("Error occured while getting Transaction STATUS . <br> Error Code: " . $STATUS_CODE . " <br>MESSAGE: " . $response->CheckTransactionStatusResult->MESSAGE);
}
# Proceed as query execution is successful.
$STATUS_CODE = $response->CheckTransactionStatusResult->STATUS_CODE;
$TRANSACTION_STATUS = $response->CheckTransactionStatusResult->TRANSACTION_STATUS;
$AMOUNT = $response->CheckTransactionStatusResult->AMOUNT;
$MERCHANT_TRANSACTIONID = $response->CheckTransactionStatusResult->MERCHANT_TRANSACTIONID;
$REMARKS = $response->CheckTransactionStatusResult->REMARKS;
$GTWREFNO = $response->CheckTransactionStatusResult->GTWREFNO;
# Check Transaction Status and proceed required steps
if($TRANSACTION_STATUS == "SUCCESS")
{
// execute transaction success steps
}
else
{
// execute transaction failure steps
}
# Print '0' (ZERO) value to let gateway know you have received Delivery status successfully.
return new Response("0", 200);
}
public function getSctNpayBankListAction(Request $request)
{
$merchantId = $this->getParameter('npay_merchant_id');
$merchantUserName = $this->getParameter('npay_merchant_username');
$merchantPassword = $this->getParameter('npay_merchant_password');
$signaturePassCode = $this->getParameter('npay_merchant_signature');
$merchantValidateEndpoint = $this->getParameter('npay_validate_merchant_url');
$dateTime = date('Y-m-d h:i:s');
# Initialize webservice with WSDL
$client = new \SoapClient($merchantValidateEndpoint);
$merchantPasswordEncoded = hash('sha256', $merchantUserName.$merchantPassword);
$signature = hash('sha256', $merchantUserName.$signaturePassCode.$dateTime);
# Set your parameters for the request
$params = array(
"MerchantId" => $merchantId,
"MerchantUserName" => $merchantUserName,
"MerchantPassword" => $merchantPasswordEncoded,
"Signature" => $signature,
"Datetime" => $dateTime,
);
# Invoke webservice method with parameters, Function Name: ValidateMerchant
$response = $client->__soapCall("EBankLists", array($params));
$banks = json_decode(json_encode($response->EBankListsResult), True);
if(array_key_exists('STATUS_CODE', $banks['EBankList'])){
return null;
}
$this->get('logger')->error(json_encode($response->EBankListsResult));
return $banks['EBankList'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment