Quantcast
Channel: Yudiz Solutions Ltd.
Viewing all articles
Browse latest Browse all 595

PayUMoney Payment Integration with IPN in Codeigniter

$
0
0

Overview

Payment gateway offers the desired and vital link between the online seller (merchant), its customer, the customer’s online payment instrument provider and the bank account of the online merchant.

  • First create merchant account in PayUMoney.
  • The PayU Redirect Payment Page (RPP) allows merchants to integrate into PayU by redirecting their consumers to PayU’s hosted payment page.
  • This is accomplished by using a combination of server side API calls and HTTPS redirects.
  • The setTransaction API call is used to setup/initiate transactions between the merchant and PayU using server to server requests.
  • Now first do setTransaction API call into your controller.

error_reporting(E_ALL);
			ini_set('display_errors', 1);
			ob_start();

			//-------------------------------------------------------------------
			//-------------------------------------------------------------------
			//-------
			//-------      Configs comes here
			//-------
			//-------------------------------------------------------------------
			//-------------------------------------------------------------------
			$baseUrl = 'https://staging.payu.co.za'; // testing environment URL
			// $baseUrl = 'https://secure.payu.co.za'; //production environment URL

			$soapWdslUrl = $baseUrl . '/service/PayUAPI?wsdl';
			$payuRppUrl = $baseUrl . '/rpp.do?PayUReference=';
			$apiVersion = 'ONE_ZERO';

			//set value != 1 if you dont want to auto redirect topayment page
			$doAutoRedirectToPaymentPage = 1;

			/*
			Store config details
 			*/
			$safeKey = '{CE62CE80-0EFD-4035-87C1-8824C5C46E7F}'; // safe key provided in merchant account
			$soapUsername = '100032'; // SOAP username provided in merchant account
			$soapPassword = 'PypWWegU'; // SOAP password provided in merchant account

			try {

				// 1. Building the Soap array  of data to send
				$setTransactionArray = array();
				$setTransactionArray['Api'] = $apiVersion;
				$setTransactionArray['Safekey'] = $safeKey;
				$setTransactionArray['TransactionType'] = 'PAYMENT';

				$setTransactionArray['AdditionalInformation']['merchantReference'] = 203269; //provided in merchant account
				$setTransactionArray['AdditionalInformation']['cancelUrl'] = 'http://your-cancel-url-comes-here';
				$setTransactionArray['AdditionalInformation']['returnUrl'] = 'http://your-return-url-comes-here';
				$setTransactionArray['AdditionalInformation']['notificationUrl'] = 'http://your-notification-url-comes-here';
				$setTransactionArray['AdditionalInformation']['supportedPaymentMethods'] = 'CREDITCARD';

				$setTransactionArray['Basket']['description'] = "Load Funds";
				$setTransactionArray['Basket']['amountInCents'] = 1000 // amount in cents
				$setTransactionArray['Basket']['currencyCode'] = 'ZAR';

				$setTransactionArray['Customer']['email'] = “john@doe.com”;
				$setTransactionArray['Customer']['firstName'] = 'John';
				$setTransactionArray['Customer']['lastName'] = 'Doe';
				$setTransactionArray['Customer']['mobile'] = '0211234567';
				$setTransactionArray['Customer']['regionalId'] = '1234512345122';
				$setTransactionArray['Customer']['countryCode'] = '27';

				// 2. Creating a XML header for sending in the soap heaeder (creating it raw a.k.a xml mode)
				$headerXml = '<wsse:Security SOAP-ENV:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">';
				$headerXml .= '<wsse:UsernameToken wsu:Id="UsernameToken-9" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">';
				$headerXml .= '<wsse:Username>' . $soapUsername . '</wsse:Username>';
				$headerXml .= '<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">' . $soapPassword . '</wsse:Password>';
				$headerXml .= '</wsse:UsernameToken>';
				$headerXml .= '</wsse:Security>';
				$headerbody = new SoapVar($headerXml, XSD_ANYXML, null, null, null);

				// 3. Create Soap Header.
				$ns = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'; //Namespace of the WS.
				$header = new SOAPHeader($ns, 'Security', $headerbody, true);

				// 4. Make new instance of the PHP Soap client
				$soap_client = new SoapClient($soapWdslUrl, array("trace" => 1, "exception" => 0));

				// 5. Set the Headers of soap client.
				$soap_client->__setSoapHeaders($header);

				// 6. Do the setTransaction soap call to PayU
				$soapCallResult = $soap_client->setTransaction($setTransactionArray);

				// 7. Decode the Soap Call Result
				$returnData = json_decode(json_encode($soapCallResult), true);
				print "<br />-----------------------------------------------<br />\r\n";
				print "Return data decoded:<br />\r\n";
				print "-----------------------------------------------<br />\r\n";
				print "<pre>";
				var_dump($returnData);
				print "</pre>";

				if (isset($doAutoRedirectToPaymentPage) && ($doAutoRedirectToPaymentPage == 1)) {

					if ((isset($returnData['return']['successful']) && ($returnData['return']['successful'] === true) && isset($returnData['return']['payUReference']))) {
						// do some database insertion if you want to do
						// Redirecting to payment page
						header('Location: ' . $payuRppUrl . $returnData['return']['payUReference']);
						die();
					}
				}
			} catch (Exception $e) {
				var_dump($e);
			}

			//-------------------------------------------------------------------
			//-------------------------------------------------------------------
			//-------
			//-------      Checking response
			//-------
			//-------------------------------------------------------------------
			//-------------------------------------------------------------------
			if (is_object($soap_client)) {

				print "<br />-----------------------------------------------<br />\r\n";
				print "Request in XML:<br />\r\n";
				print "-----------------------------------------------<br />\r\n";
				echo str_replace('&gt;&lt;', '&gt;<br />&lt;', htmlspecialchars($soap_client->__getLastRequest(), ENT_QUOTES));
				print "\r\n<br />";
				print "-----------------------------------------------<br />\r\n";
				print "Response in XML:<br />\r\n";
				print "-----------------------------------------------<br />\r\n";
				echo str_replace('&gt;&lt;', '&gt;<br />&lt;', htmlspecialchars($soap_client->__getLastResponse(), ENT_QUOTES));
				// echo "test";exit;
			}
			die();

  • After completion this, your page redirect to “returnURL” .
  • Internally it also fire IPN (Instant Payment Notification). The Instant Payment Notification (IPN) is an asynchronous notification mechanism where PayU sends transaction results/information to a specified URL without any dependency on a customer’s browser.
  • Notifications are sent in XML format using POST method.
  • In the case of approval the IPN will fire after the payment has been attempted and will return that result.

IPN called under following conditions:

  1. When a payment on the PayU redirect either fails or succeeds.
  2. When a user session times out on the PayU redirect with no chance for the user to finish payment.
  3. When a transaction, pending review for fraud, is either approved or rejected by case managers.

In your notification page write below code

$postdata = file_get_contents("php://input"); // get post data
		$xml = json_decode(json_encode(simplexml_load_string($postdata)), true); // convert it into array

		$paureference = $xml['PayUReference']; // unique payU reference
		$transaction = $xml['Secure3D']['lkpTransactionId']; // unique transaction id

		if ($xml['TransactionState'] == 'SUCCESSFUL' && isset($xml['Secure3D']['lkpTransactionId'])) {
			// do something if transaction succeed.
		} else if ($xml['TransactionState'] == 'FAILED') {
			// do something if transaction fails.
		} else if (empty($xml['Secure3D'] && $xml['Secure3D'] == '')) {
			//do something if transaction succeed but Issuer/Cardholder not participated.
		}

Bravo!!! Now you can collect payment from your customers.


Viewing all articles
Browse latest Browse all 595

Trending Articles