Source of file SecurePayTechPayment.php
Size: 10,628 Bytes - Last Modified: 2021-12-23T10:45:55+00:00
/var/www/docs.ssmods.com/process/src/code/SecurePayTechPayment.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 | <?php /** * @author Nicolaas [at] sunnysideup.co.nz * Sub-class of Payment that supports SecurePayTech as its payment processor * * Note: You must have the cURL extension installed in PHP in order to use * this class * @reference: http://www.securepaytech.com/developers/documentation/ * **/ /** * Configuration * ============= * You need to define the merchant id and key in the _config.php of your * project */ class SecurePayTechPayment extends Payment { protected static $credit_cards = array( 'Visa' => 'payment/images/payments/methods/visa.jpg', 'MasterCard' => 'payment/images/payments/methods/mastercard.jpg', //These two are usually not supported //'Amex' => 'payment/images/payments/methods/american-express.gif', //'Diners' => 'payment/images/payments/methods/dinners-club.jpg', ); protected static $spt_merchant_id; public static function set_spt_merchant_id($spt_merchant_id) { self::$spt_merchant_id = $spt_merchant_id; } public static function get_spt_merchant_id() { if (Director::isDev()) { return "TESTDIGISPL1"; } else { return self::$spt_merchant_key; } } protected static $spt_merchant_key; public static function set_spt_merchant_key($spt_merchant_key) { self::$spt_merchant_key = $spt_merchant_key; } public function getPaymentFormFields() { $site_currency = Payment::site_currency(); $paymentsList = '<div id="SecurePayTechCardsAvailable">'; $count = 0; foreach (self::$credit_cards as $name => $image) { $count++; $class = ''; if ($count == 1) { $class = "first"; } if ($count % 2) { $class .= " even"; } else { $class .= " odd"; } $paymentsList .= '<img src="' . $image . '" alt="' . $name . '" class="SecurePayTechCardImage'.$count.'" />'; } Requirements::customScript(' function paymark_verify(merchant) { window.open ("http://www.paymark.co.nz/dart/darthttp.dll?etsl&tn=verify&merchantid=" + merchant, "verify", "scrollbars=yes, width=400, height=400"); } '); $paymentsList.='<img height="30" src="payment_securatech/images/paymark_small.png" alt="Paymark Certified" onclick="paymark_verify (' . "'" . self::get_spt_merchant_id() . "'" . ')" class="last" /></div>'; $fieldSet = new FieldSet(); if (Director::isDev()) { $fieldSet->push( new DropdownField( $name = "SecurePayTechTestAmountValue", $title = "Test", $source = $this->testCodesInCents(), $value = "", $form = null, $emptyString = " --- select test - if any ---" ) ); $fieldSet->push( new DropdownField( $name = "SecurePayTechCardsToUse", $title = "Card", $source = $this->cardsToUse(), $value = "", $form = null, $emptyString = " --- choose card - if any ---" ) ); $fieldSet->push( new LiteralField( $name = "SecurePayTechCardsExplained", $title = '<p id="SecurePayTechCardsExplained" class="middleColumn"><i>NB: The option to select a test type and test card is ONLY availabe in a development environment.</i></p>' ) ); Requirements::javascript("payment_NZ_gateways/javascript/SecurePayTechPayment.js"); } $fieldSet->push(new TextField('SecurePayTechCardHolderName', 'Card Holder Name:')); $fieldSet->push(new TextField('SecurePayTechCreditCardNumber', 'Credit Card Number:')); $fieldSet->push(new NumericField('SecurePayTechCardExpiry', 'Credit Card Expiry (MMYY):', '', 4)); $fieldSet->push(new LiteralField('SPTInfo', $paymentsList)); return $fieldSet; } /** * Returns the required fields to add to the order form, when using this * payment method. */ public function getPaymentFormRequirements() { return array(); return array( "js" => " require('SecurePayTechCardHolderName'); require('SecurePayTechCreditCardNumber'); require('SecurePayTechCardExpiry'); ", "php" => ' $this->requireField("SecurePayTechCardHolderName", $data); $this->requireField("SecurePayTechCreditCardNumber", $data); $this->requireField("SecurePayTechCardExpiry", $data); ' ); } /** * Process payment using HTTPS POST */ public function processPayment($data, $form) { $data = Convert::raw2sql($data); $realPayment = $this->Amount->Amount; if (Director::isDev()) { if (isset($data["SecurePayTechTestAmountValue"])) { if ($data["SecurePayTechTestAmountValue"] !== "") { if ($data["SecurePayTechTestAmountValue"] == 0) { $numberString = "99"; } else { $numberString = "0.".$data["SecurePayTechTestAmountValue"]; } $nicelyFormatted = number_format($numberString, 2); $this->Amount->Amount = floatval($nicelyFormatted); $this->Amount->Currency = Payment::site_currency(); } } if (isset($data["SecurePayTechCardsToUse"])) { if ($data["SecurePayTechCardsToUse"] !== "") { $cardArray = explode(",", $this->getCardData($data["SecurePayTechCardsToUse"])); $data['SecurePayTechCreditCardNumber'] = trim($cardArray[1]); $data['SecurePayTechCardExpiry'] = trim($cardArray[2]).trim($cardArray[3]); } } } $orderRef = $this->ID; $cardNo = $data['SecurePayTechCreditCardNumber']; $cardExp = $data['SecurePayTechCardExpiry']; $cardHolder = $data['SecurePayTechCardHolderName']; $cardType = 0; $amt = $this->Amount->Amount; $currency = $this->Amount->Currency; $postvars = array( 'OrderReference' => $orderRef, 'CardNumber' => $cardNo, 'CardExpiry' => $cardExp, 'CardHolderName' => $cardHolder, 'CardType' => $cardType, 'MerchantID' => self::get_spt_merchant_id(), 'MerchantKey' => self::$spt_merchant_key, 'Amount' => $amt, 'Currency' => $currency ); $this->Amount->setAmount($realPayment); $response = $this->http_post('https', 'tx.securepaytech.com', 8443, '/web/HttpPostPurchase', $postvars); if (!$response) { $this->Status = 'Failure'; $this->Message = "Communication Failure"; if (Director::isDev()) { $this->Message .= " (".curl_error($ch).")"; } $this->write(); $result = EcommercePayment_Failure::create(); return $result; } $responses = explode(',', $response); //var_dump ($responses); if (!isset($responses[0])) { $responses[0] = 0; } $ok = false; if ($responses[0] == 1) { $ok = true; } if ($ok) { $this->Status = 'Success'; $result = EcommercePayment_Success::create(); } else { $this->Status = 'Failure'; $this->Message = $this->getResponseMessage($responses [0]); $result = EcommercePayment_Failure::create(); } $this->write(); return $result; } /* $vars is an associative array containing the post variables */ public function http_post($method, $server, $port, $url, $vars) { $postdata = ""; foreach ($vars as $key => $value) { $postdata .= urlencode($key) . "=" . urlencode($value) . "&"; } $postdata = substr($postdata, 0, -1); $content_length = strlen($postdata); $headers = "POST $url HTTP/1.1\r\n". "Accept: */*\r\n". "Accept-Language: en-nz\r\n". "Content-Type: application/x-www-form-urlencoded\r\n". "Host: $server\r\n". "Connection: Keep-Alive\r\n". "Cache-Control: no-cache\r\n". "Content-Length: $content_length\r\n\r\n"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $method . '://' . $server .":". $port . $url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata); $ret = curl_exec($ch); curl_close($ch); return $ret; } protected function responseCodes() { $array = array( 0 => "Could not reach server", 1 => 'Transaction Approved', 2 => 'Insufficient funds', 3 => 'Card expired', 4 => 'Card declined', 5 => 'Server error occurred', 6 => 'Communication error', 7 => 'Unsupported transaction type', 8 => 'Bad or malformed request', 9 => 'Invalid card number' ); return $array; } protected function getResponseMessage($number) { $array = $this->responseCodes(); if (isset($array[$number])) { return $array[$number]; } else { return "unknown error"; } } protected function testCodesInCents() { $array = array( 00 => "Transaction OK", 10 => "Insufficient Funds", 54 => "Card Expired", 57 => "Unsupported Transaction Type", 75 => "Card Declined", 91 => "Communications Error" ); return $array; } protected function cardsToUse() { return array( 0 => "Visa, 4987654321098769, 05, 13", 1 => "MasterCard, 5123456789012346, 05, 13", 2 => "Amex, 345678901234564, 05, 13", 3 => "Dinersclub, 30123456789019, 05, 13" ); } protected function getCardData($cardNumber) { $array = $this->cardsToUse(); return $array[$cardNumber]; } } |