Nepals Popular Payment Gateway eSewa Integration – Symfony 3 Example

Here today I’m going to post a piece of code to integrate Nepal’s one of popular payment gateway eSewa.

<?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 Fundprabhu\Bundle\MainBundle\Entity\Donation;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class EsewaController extends Controller
{
public function redirectAction(Request $request)
{
$donationId = $request->get('donation_id');
$donation = $this->getDoctrine()->getRepository(Donation::class)->find($donationId);
$amt = $donation->getAmount();
$txAmt = 0;
$psc = 0;
$pdc = 0;
$tAmt = $amt + $txAmt + $psc + $pdc;
$pid = $donation->getId();
$data['donation'] = $donation;
$data['tAmt'] = $tAmt;
$data['pid'] = $pid;
return $this->render('FundprabhuPaymentBundle:Esewa:redirect.html.twig', $data);
}
public function successAction(Request $request)
{
$oid = $request->get('oid');
$pid = $oid;
$rid = $request->get('refId');
$amt = $request->get('amt');
$esewaUrl = $this->container->getParameter('esewa_tra_veryfy_url');
$scd = $this->container->getParameter('esewa_service_code');
$url = $esewaUrl . '?' . 'rid=' . $rid . '&pid=' . $pid . '&amt=' . $amt . '&scd=' . $scd;
$em = $this->getDoctrine()->getManager();
$donationId = $this->get('session')->get('donation_id');
$donation = $em->getRepository(Donation::class)->find($donationId);
//verify transaction
if ($this->transactionVerify($url)) {
$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 failureAction()
{
$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');
}
private function transactionVerify($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$dom = new \DOMDocument();
$dom->loadXML($response);
$result = simplexml_import_dom($dom);
if (trim($result[0]->response_code) == 'Success') {
return true;
}
return false;
}
}

view raw
EsewaController.php
hosted with ❤ by GitHub

<h1>Redirecting…</h1>
<div style="display: none;">
<form action="{{ esewa_payment_url }}" method="POST" target="_parent" name="esewa">
<input value="{{ tAmt }}" name="tAmt" type="hidden">
<input value="{{ tAmt }}" name="amt" type="hidden">
<input value="0" name="txAmt" type="hidden">
<input value="0" name="psc" type="hidden">
<input value="0" name="pdc" type="hidden">
<input value="{{ esewa_service_code }}" name="scd" type="hidden">
<input value="{{ pid }}" name="pid" type="hidden">
<input value="{{ esewa_success_url }}" type="hidden" name="su">
<input value="{{ esewa_failure_url }}" type="hidden"
name="fu">
<input value="Continue to Payment" type="submit" style="border-radius:8px; background:#3C0; font-size:20px; color:#fff; font-weight:bold; padding:6px; width:60%;">
</form>
</div>
<script>
window.onload = function () {
document.forms['esewa'].submit()
}
</script>

view raw
redirect.html.twig
hosted with ❤ by GitHub

Leave a Reply

Your email address will not be published. Required fields are marked *