Source of file DPSAdapter.php
Size: 12,861 Bytes - Last Modified: 2021-12-23T10:33:28+00:00
/var/www/docs.ssmods.com/process/src/code/DPSPayment/DPSAdapter.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 | <?php /** */ class DPSAdapter extends Controller { public static $allowed_actions = array( 'AuthForm', 'CompleteForm', 'RefundForm', 'PurchaseForm', 'DPSHostedForm', 'DPSRecurringPayment', 'DPSHostedRecurringForm', 'processDPSHostedResponse', ); protected static $pxPost_Username; protected static $pxPost_Password; protected static $pxPay_Userid; protected static $pxPay_Key; private static $mode = 'Normal'; private static $receipt_from; // DPS Informations public static $privacy_link = 'http://www.paymentexpress.com/privacypolicy.htm'; public static $logo = 'payment/images/payments/dps.gif'; // URLs public static $pxPost_Url = 'https://sec.paymentexpress.com/pxpost.aspx'; public static $pxPay_Url = 'https://sec.paymentexpress.com/pxpay/pxaccess.aspx'; public static $using_transaction = true; public static $allowed_currencies = array( 'CAD'=>'Canadian Dollar', 'CHF'=>'Swiss Franc', 'EUR'=>'Euro', 'FRF'=>'French Franc', 'GBP'=>'United Kingdom Pound', 'HKD'=>'Hong Kong Dollar', 'JPY'=>'Japanese Yen', 'NZD'=>'New Zealand Dollar', 'SGD'=>'Singapore Dollar', 'USD'=>'United States Dollar', 'ZAR'=>'Rand', 'AUD'=>'Australian Dollar', 'WST'=>'Samoan Tala', 'VUV'=>'Vanuatu Vatu', 'TOP'=>"Tongan Pa'anga", 'SBD'=>'Solomon Islands Dollar', 'PGK'=>'Papua New Guinea Kina', 'MYR'=>'Malaysian Ringgit', 'KWD'=>'Kuwaiti Dinar', 'FJD'=>'Fiji Dollar' ); protected static $cvn_mode = true; protected static $credit_cards = array( 'Visa' => 'payment/images/payments/methods/visa.jpg', 'MasterCard' => 'payment/images/payments/methods/mastercard.jpg', 'American Express' => 'payment/images/payments/methods/american-express.gif', 'Dinners Club' => 'payment/images/payments/methods/dinners-club.jpg', 'JCB' => 'payment/images/payments/methods/jcb.jpg' ); public static function set_pxpost_account($pxPost_Username, $pxPost_Password) { self::$pxPost_Username = $pxPost_Username; self::$pxPost_Password = $pxPost_Password; } public static function set_pxpay_account($pxPay_Userid, $pxPay_Key) { self::$pxPay_Userid = $pxPay_Userid; self::$pxPay_Key = $pxPay_Key; } public static function get_pxpost_username() { return self::$pxPost_Username; } public static function get_pxpost_password() { return self::$pxPost_Password; } public static function get_pxpay_userid() { return self::$pxPay_Userid; } public static function get_pxpay_key() { return self::$pxPay_Key; } public static function remove_credit_card($creditCard) { unset(self::$credit_cards[$creditCard]); } public static function set_receipt_from($address) { self::$receipt_from = $address; } public static function get_receipt_from() { return self::$receipt_from; } public static function unset_cvn_mode() { self::$cvn_mode = false; } public static function set_mode($mode) { self::$mode = $mode; } public function postConnect() { $inputs['PostUsername'] = self::$pxPost_Username; $inputs['PostPassword'] = self::$pxPost_Password; $transaction = "<Txn>"; foreach ($inputs as $name => $value) { $XML_name = Convert::raw2xml($name); $XML_value = Convert::raw2xml($value); $transaction .= "<$XML_name>$XML_value</$XML_name>"; } $transaction .= "</Txn>"; $client = $this->getHTTPClient(); $resultXml = $client->post(self::$pxPost_Url, $transaction); $match = preg_match('/^\<Txn\>(.*)\<\/Txn\>$/i', $resultXml, $matches); return $match; } //Payment Function public function doPayment($inputs, $payment) { // Main Settings //$inputs = array(); $inputs['PostUsername'] = self::$pxPost_Username; $inputs['PostPassword'] = self::$pxPost_Password; //$inputs = array(); // Transaction Creation $transaction = "<Txn>"; foreach ($inputs as $name => $value) { if ($name == "Amount") { $value = number_format($value, 2, '.', ''); } $XML_name = Convert::raw2xml($name); $XML_value = Convert::raw2xml($value); $transaction .= "<$XML_name>$XML_value</$XML_name>"; } $transaction .= "</Txn>"; $client = $this->getHTTPClient(); $resultXml = $client->post(self::$pxPost_Url, $transaction); $payment->ResponseXML = $resultXml; // XML Parser Creation $xmlParser = xml_parser_create(); $values = null; $indexes = null; xml_parse_into_struct($xmlParser, $resultXml, $values, $indexes); xml_parser_free($xmlParser); // XML Result Parsed In A PHP Array $resultPhp = array(); $level = array(); foreach ($values as $xmlElement) { if ($xmlElement['type'] == 'open') { if (array_key_exists('attributes', $xmlElement)) { list($level[$xmlElement['level']], $extra) = array_values($xmlElement['attributes']); } else { $level[$xmlElement['level']] = $xmlElement['tag']; } } elseif ($xmlElement['type'] == 'complete') { $startLevel = 1; $phpArray = '$resultPhp'; while ($startLevel < $xmlElement['level']) { $phpArray .= '[$level['. $startLevel++ .']]'; } $phpArray .= '[$xmlElement[\'tag\']] = array_key_exists(\'value\', $xmlElement)? $xmlElement[\'value\'] : null;'; eval($phpArray); } } $responseFields = $resultPhp['TXN']; // DPS Response Management if ($responseFields['SUCCESS']) { $payment->Status = 'Success'; if ($authcode = $responseFields['1']['AUTHCODE']) { $payment->AuthCode = $authcode; } if ($dpsBillingID = $responseFields['1']['DPSBILLINGID']) { $payment->DPSBillingID = $dpsBillingID; } $dateSettlement = $responseFields['1']['DATESETTLEMENT']; $payment->SettlementDate = substr($dateSettlement, 0, 4) ."-".substr($dateSettlement, 4, 2)."-".substr($dateSettlement, 6, 2); $payment->Amount->Currency = $responseFields['1']['INPUTCURRENCYNAME']; $payment->DateExpiry = $responseFields['1']['DATEEXPIRY']; $payment->CardNumberTruncated = $responseFields['1']['CARDNUMBER']; $payment->CardHolderName = $responseFields['1']['CARDHOLDERNAME']; } else { $payment->Status = 'Failure'; } if ($transactionRef = @$responseFields['DPSTXNREF']) { $payment->TxnRef = $transactionRef; } if ($helpText = @$responseFields['HELPTEXT']) { $payment->Message = $helpText; } elseif ($responseText = @$responseFields['RESPONSETEXT']) { $payment->Message = $responseText; } $payment->write(); if (self::$mode === 'Rolling_Back_Mode' && self::$using_transaction) { DB::getConn()->transactionRollback(); } } public function getPxpay() { return new PxPay(self::$pxPay_Url, self::$pxPay_Userid, self::$pxPay_Key); } public function doDPSHostedPayment($inputs, $payment) { $request = new PxPayRequest(); foreach ($inputs as $element => $value) { $funcName = 'set' . $element; if (!is_object($value)) { $value = Convert::raw2xml($value); } $request->$funcName($value); } // submit payment request to get the URL for redirection $pxpay = $this->getPxpay(); $requestXML = $pxpay->makeRequest($request); $xml = new SimpleXMLElement($requestXML); $requestElement = $xml->xpath('//Request'); $valid = (bool) $requestElement[0]['valid']; if ($valid) { $urls = $xml->xpath('//URI'); $url = (string) $urls[0]; if (self::$using_transaction) { DB::getConn()->transactionEnd(); } if (self::$mode == 'Unit_Test_Only') { return $url; } else { if (Director::is_ajax()) { echo $url; die(); } else { header('Location: ' . $url); die(); } } } else { $payment->Message = 'Invalid Request String'; $payment->write(); } } /** * Action called by DPS right after a payment operation, being a success or a failure * Will update the status of the payment accordingly, and redirect to the success/failure url * where more domain specific operations can be performed * @see {http://www.paymentexpress.com/technical_resources/ecommerce_hosted/pxaccess.html#ResultNotification} */ public function processDPSHostedResponse() { $pxpay = new PxPay(self::$pxPay_Url, self::$pxPay_Userid, self::$pxPay_Key); $enc_hex = $_REQUEST["result"]; $rsp = $pxpay->getResponse($enc_hex); $paymentID = $rsp->getTxnData1(); $SQL_paymentID = (int)$paymentID; if ($dpsBillingID = $rsp->getDpsBillingId()) { $payment = DataObject::get_by_id('DPSRecurringPayment', $SQL_paymentID); $payment->DPSBillingID = $dpsBillingID; } else { $payment = DataObject::get_by_id("DPSPayment", $SQL_paymentID); } if ($payment) { if (self::$using_transaction) { DB::getConn()->transactionStart(); } try { $payment->ResponseXML = $rsp->toXml(); $success = $rsp->getSuccess(); if ($success =='1') { // @todo Use AmountSettlement for amount setting? $payment->Status="Success"; } else { $payment->Status="Failure"; } $payment->TxnRef = $rsp->getDpsTxnRef(); $payment->AuthCode = $rsp->getAuthCode(); $payment->DateExpiry = $rsp->getDateExpiry(); $payment->CardNumberTruncated = $rsp->getCardNumber(); $payment->CardHolderName = $rsp->getCardHolderName(); $payment->Message=$rsp->getResponseText(); $payment->write(); if (self::$using_transaction) { DB::getConn()->transactionEnd(); } } catch (Exception $e) { if (self::$using_transaction) { DB::getConn()->transactionRollback(); } $payment->handleError($e); } Director::redirect($payment->DPSHostedRedirectURL); } else { Director::redirect(Director::baseURL()); } } protected $httpClient; public function setHTTPClient($client) { $this->httpClient = $client; } public function getHTTPClient() { if (!$this->httpClient) { $this->httpClient = new DPSAdapter_HTTPClient(); } return $this->httpClient; } } /** * Crude HTTP client to allow for basic mocking of HTTP responses. */ class DPSAdapter_HTTPClient { public function post($url, $data) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //curl_setopt($clientURL, CURLOPT_SSL_VERIFYPEER, 0); //Needs to be included if no *.crt is available to verify SSL certificates if (defined('CAINFO')) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1); curl_setopt($ch, CURLOPT_CAINFO, CAINFO); } else { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); } $result = curl_exec($ch); if (curl_error($ch)) { throw new Exception(curl_error($ch)); } curl_close($ch); return $result; } } |