Skip to content

Instantly share code, notes, and snippets.

@bhaktaraz
Created July 26, 2018 05:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bhaktaraz/5348d0289a5908a519c551f6bb041b0a to your computer and use it in GitHub Desktop.
Save bhaktaraz/5348d0289a5908a519c551f6bb041b0a to your computer and use it in GitHub Desktop.
eSewa 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 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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment