<?php session_start(); // Determine whether to show the default page or get a transaction started $transaction_id = $_SESSION['txnid']; $session_id = isset($_GET['sessionid']) ? $_GET['sessionid'] : false; if (!$transaction_id and !$session_id) { die('Nothing to do...'); } if ($transaction_id) { // Make sure you have entered your Px Fusion credentials in PxFusion.php require_once 'PxFusion.php'; $pxf = new PxFusion($_SESSION['userid'], $_SESSION['pwd']); # handles most of the Px Fusion magic $response = $pxf->get_transaction($transaction_id); $transaction_details = get_object_vars($response->GetTransactionResult); if ($transaction_details['responseCode'] == '00') { header('Location: ' . $_SESSION['succurl'] . '?statuscode=' . $transaction_details['responseCode'] . '&statustext=' . $transaction_details['responseText'] . '&txnid=' . $transaction_details['transactionId'] . '&txnref=' . $_SESSION['txnref']); //success url } else { header('Location: ' . $_SESSION['failurl'] . '?statuscode=' . $transaction_details['responseCode'] . '&statustext=' . $transaction_details['responseText'] . '&txnid=' . $transaction_details['transactionId'] . '&txnref=' . $_SESSION['txnref']); //failure url } } ?> <html> <head> <title>Alis PX Fusion Testing</title> <style type="text/css"> body {font-family: Arial, Verdana, Serif; font-size: .9em; margin: 1em;} h1 {font-family: Verdana;}
public function process_payment($order_id) { global $woocommerce; // Get this Order's information so that we know // who to charge and how much $customer_order = new WC_Order($order_id); // Are we testing right now or is it a real transaction //$environment = ( $this->environment == "yes" ) ? 'TRUE' : 'FALSE'; // Decide which URL to post to $environment_url = 'https://sec.paymentexpress.com/pxmi3/pxfusionauth'; if ($this->environment == "yes") { $pxuser = $this->api_login_test; $pxpwd = $this->api_pwd_test; } else { $pxuser = $this->api_login; $pxpwd = $this->api_pwd; } //mail("*****@*****.**","PXfusion credentials ","user="******" pwd=".$pxpwd); require_once 'PxFusion.php'; $pxf = new PxFusion($pxuser, $pxpwd); # handles most of the Px Fusion magic // Work out the probable location of return.php since this sample // code could be anywhere on a development server. $returnUrl = 'http://' . $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['PHP_SELF']), '/\\') . '/return.php'; // Set some transaction details $pxf->set_txn_detail('txnType', 'Purchase'); # required $pxf->set_txn_detail('currency', 'NZD'); # required $pxf->set_txn_detail('returnUrl', $returnUrl); # required $pxf->set_txn_detail('amount', $customer_order->order_total); # required $pxf->set_txn_detail('merchantReference', 'Order#' . $order_id . "-" . $customer_order->billing_first_name . " " . $customer_order->billing_last_name); // Some of the many optional settings that could be specified: $pxf->set_txn_detail('enableAddBillCard', 0); $pxf->set_txn_detail('txnRef', substr(uniqid() . rand(1000, 9999), 0, 16)); # random 16 digit reference); // Make the request for a transaction id $response = $pxf->get_transaction_id(); if (!$response->GetTransactionIdResult->success) { //die('There was a problem getting a transaction id from DPS'); throw new Exception(__('There was a problem getting a transaction id from DPS', 'ali_payfusion')); } else { // You should store these values in a database // ... they are needed to query the transaction's outcome $transaction_id = $response->GetTransactionIdResult->transactionId; $session_id = $response->GetTransactionIdResult->sessionId; } // We've got everything we need to generate a payment form... // ... check the HTML further down $expdate = explode("/", $_POST['ali_payfusion-card-expiry']); //mail("*****@*****.**","woo txn details 0","txn_id=".$transaction_id." SessionId=".$session_id." expiry=".trim($expdate[0]).trim($expdate[1])); // This is where the fun stuff begins $payload = array("SessionId" => $session_id, "Action" => 'Add', "Object" => "DpsPxPay", "CardNumber" => str_replace(array(' ', '-'), '', $_POST['ali_payfusion-card-number']), "Cvc2" => isset($_POST['ali_payfusion-card-cvc']) ? $_POST['ali_payfusion-card-cvc'] : '', "ExpiryMonth" => trim($expdate[0]), "ExpiryYear" => trim($expdate[1])); // Send this payload to Payment Express for processing /*$response = wp_remote_post( $environment_url, array( 'method' => 'POST', 'body' => http_build_query( $payload ), 'timeout' => 90, 'sslverify' => false, ) );*/ //print_r($payload); //mail("*****@*****.**","woo txn details date","date=".$payload['ExpiryMonth'].$payload['ExpiryYear']); //echo "<br>"; //echo "<br>"; $environment_url = 'https://sec.paymentexpress.com/pxmi3/pxfusionauth'; $ch = curl_init($environment_url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload)); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //curl_setopt ($ch, CURLOPT_COOKIEFILE, $cookieJar); curl_setopt($ch, CURLOPT_HEADER, 1); //curl_setopt($ch, CURLINFO_HEADER_OUT, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data')); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); //print_r($response); $info = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); //CURLINFO_EFFECTIVE_URL //CURLINFO_REDIRECT_URL //echo "<br>"; //echo "<br>"; //print_r($info); //mail("*****@*****.**","woo txn details","txn_id=".$transaction_id." SessionId=".$session_id); if ($transaction_id) { // Make sure you have entered your Px Fusion credentials in PxFusion.php //require_once 'PxFusion.php'; $pxf2 = new PxFusion($pxuser, $pxpwd); # handles most of the Px Fusion magic $response = $pxf2->get_transaction($transaction_id); $transaction_details = get_object_vars($response->GetTransactionResult); //foreach ($transaction_details as $key => $value) : //echo $key; //echo $value; //echo '<BR>'; //endforeach; } //mail("*****@*****.**","woo txn details 2","txn_id=".$transaction_id." SessionId=".$session_id." txnresptext=".$transaction_details['responseText']); //responseTextAPPROVED if ($transaction_details['responseCode'] != 00) { throw new Exception(__('Sorry, the card issuer returned an error: ' . $transaction_details['responseText'], 'ali_payfusion')); } //if ( empty( $response['body'] ) ) // throw new Exception( __( 'Payment Express\'s Response was empty.', 'ali_payfusion' ) ); // Retrieve the body's resopnse if no errors found //$response_body = wp_remote_retrieve_body( $response ); // Parse the response into something we can read //foreach ( preg_split( "/\r?\n/", $response_body ) as $line ) { // $resp = explode( "|", $line ); //} // Get the values we need $r['response_code'] = $transaction_details['responseCode']; //$r['response_sub_code'] = $resp[1]; //$r['response_reason_code'] = $resp[2]; $r['response_reason_text'] = $transaction_details['responseText']; // Test the code to know if the transaction went through or not. if ($r['response_code'] == '00') { // Payment has been successful $customer_order->add_order_note(__('Payment Express payment completed.', 'ali_payfusion')); // Mark order as Paid $customer_order->payment_complete(); // Empty the cart (Very important step) $woocommerce->cart->empty_cart(); mail("*****@*****.**", "woo txn details status", "txn_id=" . $transaction_id . " SessionId=" . $session_id . " responseText=" . $r['response_reason_text']); // Redirect to thank you page return array('result' => 'success', 'redirect' => $this->get_return_url($customer_order)); } else { // Transaction was not succesful // Add notice to the cart wc_add_notice($r['response_reason_text'], 'error'); // Add note to the order for your reference $customer_order->add_order_note('Error: ' . $r['response_reason_text']); } }
function px_process_transaction($transaction_id, $try = 0) { global $purchase_log; if (@extension_loaded('soap')) { $pxf = new PxFusion(); # handles most of the Px Fusion magic $response = $pxf->get_transaction($transaction_id); $transaction_details = get_object_vars($response->GetTransactionResult); unset($pxf); } else { $data = '<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://paymentexpress.com"> <SOAP-ENV:Body> <ns1:GetTransaction> <ns1:username>InstinctFusion</ns1:username> <ns1:password>inst1234</ns1:password> <ns1:transactionId>' . $transaction_id . '</ns1:transactionId> </ns1:GetTransaction> </SOAP-ENV:Body> </SOAP-ENV:Envelope>'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "https://sec2.paymentexpress.com/pxf/pxf.svc?wsdl"); curl_setopt($ch, CURLOPT_VERBOSE, 0); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_POST, 1); // SSL security curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($ch, CURLOPT_CAINFO, WPSC_GOLD_FILE_PATH . "/merchants/paymentexpress/ThawteServerCA"); // curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: text/xml;charset=\"utf-8\"", "Accept: text/xml", "Cache-Control: no-cache", "Pragma: no-cache", "SOAPAction: \"http://paymentexpress.com/IPxFusion/GetTransaction\"", "Content-length: " . strlen($data))); $response = curl_exec($ch); if (curl_errno($ch)) { wp_die('Curl error: ' . curl_error($ch) . '. Please contact server administrator.'); } curl_close($ch); $xml_parser = xml_parser_create(); if (!xml_parse_into_struct($xml_parser, $response, $vals, $index)) { wp_die("Error while parsing response from PX Fusion. Line " . xml_get_current_line_number($xml_parser) . '. Please contact server administrator.'); } xml_parser_free($xml_parser); $parsed_xml = array(); foreach ($vals as $val) { $parsed_xml[$val['tag']] = $val['value']; } if (!isset($parsed_xml["STATUS"])) { wp_die('Error! There was a problem getting response from DPS, please contact the server administrator.'); } else { $transaction_details['status'] = $parsed_xml["STATUS"]; $transaction_details['transactionId'] = $parsed_xml["TRANSACTIONID"]; } } switch ($transaction_details['status']) { case 0: //'approved'; $this->set_transaction_details($transaction_details['transactionId'], 3); $purchase_log['processed'] = 3; $this->go_to_transaction_results($this->cart_data['session_id']); break; case 1: //declined $this->set_transaction_details($transaction_details['transactionId'], 1); $this->set_error_message(__('Your transaction was declined. Please check your credit card details and try again.', 'wpsc')); $this->return_to_checkout(); break; case 2: //transient error, retry if ($try < 10) { //retry $this->px_process_transaction($transaction_id, $try + 1); } else { $this->set_transaction_details($transaction_details['transactionId'], 2); $purchase_log['processed'] = 2; $this->go_to_transaction_results($this->cart_data['session_id']); } break; case 3: //'invalid data'; if ($try < 5) { //retry $this->px_process_transaction($transaction_id, $try + 1); } else { $this->set_transaction_details($transaction_details['transactionId'], 1); $purchase_log['processed'] = 1; $this->go_to_transaction_results($this->cart_data['session_id']); } break; case 4: //'result cannot be determined at this time, retry'; if ($try < 10) { //retry $this->px_process_transaction($transaction_id, $try + 1); } else { $this->set_transaction_details($transaction_details['transactionId'], 2); $purchase_log['processed'] = 2; $this->go_to_transaction_results($this->cart_data['session_id']); } break; case 5: //failed due timeout or canceled $this->set_transaction_details($transaction_details['transactionId'], 1); $purchase_log['processed'] = 1; $this->go_to_transaction_results($this->cart_data['session_id']); break; case 6: //transaction not found' $this->set_transaction_details($transaction_details['transactionId'], 1); $purchase_log['processed'] = 1; $this->go_to_transaction_results($this->cart_data['session_id']); break; } }
function wpec_pxfusion_return() { if (isset($_GET['sessionid'])) { $PXsessionid = $_GET['sessionid']; } else { wp_die('Session id error'); } if (get_option('permalink_structure') != '') { $separator = "?"; } else { $separator = "&"; } global $wpdb; $query = "SELECT `transactid`,`sessionid` FROM `" . WPSC_TABLE_PURCHASE_LOGS . "` WHERE `authcode` ='" . $PXsessionid . "'"; $results = $wpdb->get_results($query, 'ARRAY_A'); $pxTransactionid = $results[0]['transactid']; $sessionid = $results[0]['sessionid']; $options = get_option('wpec_pxfusion'); $pxf = new PxFusion($options['username'], $options['password']); $response = $pxf->get_transaction($pxTransactionid); $transaction_details = get_object_vars($response->GetTransactionResult); //debug info switch ($transaction_details['status']) { case 0: //'approved'; $success = 'Completed'; break; case 1: //declined $success = 'Failed'; break; case 2: //transient error, retry $success = 'Failed'; break; case 3: //'invalid data'; $success = 'Failed'; break; case 4: //'result cannot be determined at this time, retry'; $success = 'Failed'; break; case 5: //failed due timeout or canceled $success = 'Failed'; break; case 6: //transaction not found' $success = 'Failed'; break; } switch ($success) { case 'Completed': $purchase_log = new WPSC_Purchase_Log($sessionid, 'sessionid'); $purchase_log->set(array('processed' => WPSC_Purchase_Log::ACCEPTED_PAYMENT, 'notes' => 'PX Fusion Status: "' . $transaction_details['responseText'] . '"')); $purchase_log->save(); header("Location: " . get_option('transact_url') . $separator . "sessionid=" . $sessionid); exit; //transaction_results($sessionid,true); break; case 'Failed': // if it fails... $purchase_log = new WPSC_Purchase_Log($sessionid, 'sessionid'); $purchase_log->set(array('processed' => WPSC_Purchase_Log::PAYMENT_DECLINED, 'notes' => 'PX Fusion Status: "' . $transaction_details['responseText'] . '"')); $purchase_log->save(); // redirect to checkout page with an error $checkout_page_url = get_option('shopping_cart_url'); if ($checkout_page_url) { $errors = wpsc_get_customer_meta('checkout_misc_error_messages'); if (!is_array($errors)) { $errors = array(); } $errors[] = '<strong>' . $transaction_details['responseText'] . '</strong>'; wpsc_update_customer_meta('checkout_misc_error_messages', $errors); header('Location: ' . $checkout_page_url); exit; } break; } }