Example #1
0
 function setTransaction($data)
 {
     //$data should contain: 'amountInCents'; 'description'; 'returnUrl', 'cancelUrl', 'merchantReference'
     //email; firstName; lastName; mobile
     //---Location of radclient----
     $this->settings = Configure::read('payu');
     // 1. Building the Soap array  of data to send
     $setTransactionArray = array();
     $setTransactionArray['Api'] = $this->settings['apiVersion'];
     $setTransactionArray['Safekey'] = $this->settings['safeKey'];
     $setTransactionArray['TransactionType'] = 'PAYMENT';
     //---Additional information---
     $setTransactionArray['AdditionalInformation']['merchantReference'] = $data['merchantReference'];
     $setTransactionArray['AdditionalInformation']['cancelUrl'] = $data['cancelUrl'];
     $setTransactionArray['AdditionalInformation']['returnUrl'] = $data['returnUrl'];
     $setTransactionArray['AdditionalInformation']['supportedPaymentMethods'] = 'CREDITCARD';
     $setTransactionArray['AdditionalInformation']['notificationUrl'] = $this->settings['notificationUrl'];
     //---Basket---
     $setTransactionArray['Basket']['description'] = $data['description'];
     $setTransactionArray['Basket']['amountInCents'] = $data['amountInCents'];
     $setTransactionArray['Basket']['currencyCode'] = 'ZAR';
     //----Customer---
     $setTransactionArray['Customer']['email'] = $data['email'];
     $setTransactionArray['Customer']['firstName'] = $data['firstName'];
     $setTransactionArray['Customer']['lastName'] = $data['lastName'];
     $setTransactionArray['Customer']['mobile'] = $data['mobile'];
     // 2. Creating a XML header
     $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>' . $this->settings['soapUsername'] . '</wsse:Username>';
     $headerXml .= '<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">' . $this->settings['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($this->settings['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);
     if (isset($returnData['return']['successful']) && $returnData['return']['successful'] === true && isset($returnData['return']['payUReference'])) {
         return array('success' => true, 'payUReference' => $returnData['return']['payUReference']);
     } else {
         //TODO Here we need to tell them what whent wrong!
         $this->log("PAYU: " . $returnData['return']['displayMessage']);
         $this->log("PAYU: " . $returnData['return']['resultMessage']);
         $this->log("PAYU: " . $returnData['return']['resultCode']);
         return array('success' => false, 'Error' => $returnData['return']['displayMessage']);
     }
 }
 $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'])) {
         //Redirecting to payment page
         header('Location: ' . $payuRppUrl . $returnData['return']['payUReference']);
         die;
     }
 }
 private function _do_payu_transaction($voucher_value, $voucher_price, $price_in_cent, $product_id)
 {
     error_reporting(E_ALL);
     ini_set('display_errors', 1);
     ob_start();
     //-------------------------------------------------------------------
     //-------------------------------------------------------------------
     //-------
     //-------      Configs comes here
     //-------
     //-------------------------------------------------------------------
     //-------------------------------------------------------------------
     $baseUrl = 'https://staging.payu.co.za';
     //staging 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 = '{45D5C765-16D2-45A4-8C41-8D6F84042F8C}';
     $soapUsername = '******';
     $soapPassword = '******';
     try {
         $merchantReference = time();
         $description = "{$voucher_value} Internet voucher";
         // 1. Building the Soap array  of data to send
         $setTransactionArray = array();
         $setTransactionArray['Api'] = $apiVersion;
         $setTransactionArray['Safekey'] = $safeKey;
         $setTransactionArray['TransactionType'] = 'PAYMENT';
         $setTransactionArray['AdditionalInformation']['merchantReference'] = $merchantReference;
         $setTransactionArray['AdditionalInformation']['cancelUrl'] = 'http://127.0.0.1/cake2/rd_cake/financials/completed_transaction';
         $setTransactionArray['AdditionalInformation']['returnUrl'] = 'http://127.0.0.1/cake2/rd_cake/financials/completed_transaction';
         $setTransactionArray['AdditionalInformation']['supportedPaymentMethods'] = 'CREDITCARD';
         $setTransactionArray['Basket']['description'] = $description;
         $setTransactionArray['Basket']['amountInCents'] = "{$price_in_cent}";
         $setTransactionArray['Basket']['currencyCode'] = 'ZAR';
         $setTransactionArray['Customer']['merchantUserId'] = "500";
         $setTransactionArray['Customer']['email'] = "*****@*****.**";
         $setTransactionArray['Customer']['firstName'] = 'Renier';
         $setTransactionArray['Customer']['lastName'] = 'Viljoen';
         $setTransactionArray['Customer']['mobile'] = '0725995050';
         $setTransactionArray['Customer']['regionalId'] = '7701015055012';
         $setTransactionArray['Customer']['countryCode'] = '27';
         $setTransactionArray['Customfield']['key'] = "ProductID";
         $setTransactionArray['Customfield']['value'] = "{$product_id}";
         // 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);
         if (isset($doAutoRedirectToPaymentPage) && $doAutoRedirectToPaymentPage == 1) {
             if (isset($returnData['return']['successful']) && $returnData['return']['successful'] === true && isset($returnData['return']['payUReference'])) {
                 //Add an entry since we could get a payUReference
                 $data = array();
                 $data['merchant_reference'] = $merchantReference;
                 $data['payu_reference'] = $returnData['return']['payUReference'];
                 $data['amount_in_cents'] = $price_in_cent;
                 $data['description'] = $description;
                 $data['product_code'] = $product_id;
                 $transaction = ClassRegistry::init('FinPayuTransaction');
                 $transaction->create();
                 $transaction->save($data);
                 //Redirecting to payment page
                 header('Location: ' . $payuRppUrl . $returnData['return']['payUReference']);
                 die;
             } else {
                 //TODO Here we need to tell them what whent wrong!
             }
         }
     } catch (Exception $e) {
         var_dump($e);
     }
 }