/** * Parse the raw data response from gateway * * @param string $body */ private function parse($body) { parse_str($body, $response_array); if ($response_array['ACK'] == self::FAILURE) { $error_message = "Error code (" . $response_array['L_ERRORCODE0'] . ")\n " . $response_array['L_SHORTMESSAGE0'] . ".\n Reason: " . $response_array['L_LONGMESSAGE0']; Merchant_Logger::error_log($error_message); } return $response_array; }
public static function save_request($string) { if (null === self::$path) { self::$path = dirname(__FILE__) . '/../../../log/'; } if (!is_writable(self::$path . 'request.xml') or !file_exists(self::$path . 'request.xml')) { return; } $fp = fopen(self::$path . 'request.xml', 'w'); fwrite($fp, $string); fclose($fp); }
public function request($method, $body, $options = array()) { $timeout = isset($options['timeout']) ? $options['timeout'] : '0'; $user_agent = isset($options['user_agent']) ? $options['user_agent'] : null; $headers = isset($options['headers']) ? $options['headers'] : array(); $server = parse_url($this->endpoint); if (!isset($server['port'])) { $server['port'] = $server['scheme'] == 'https' ? 443 : 80; } if (!isset($server['path'])) { $server['path'] = '/'; } if (isset($server['user']) && isset($server['pass'])) { $headers[] = 'Authorization: Basic ' . base64_encode($server['user'] . ':' . $server['pass']); } $transaction_url = $server['scheme'] . '://' . $server['host'] . $server['path'] . (isset($server['query']) ? '?' . $server['query'] : ''); Merchant_Logger::save_request($body); if (function_exists('curl_init')) { $curl = curl_init($transaction_url); curl_setopt($curl, CURLOPT_PORT, $server['port']); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_FORBID_REUSE, 1); curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout); if (isset($user_agent)) { curl_setopt($curl, CURLOPT_USERAGENT, $user_agent); } if ($method == 'post') { curl_setopt($curl, CURLOPT_POST, 1); } curl_setopt($curl, CURLOPT_POSTFIELDS, $body); $response = curl_exec($curl); curl_close($curl); Merchant_Logger::log($response); Merchant_Logger::save_response($response); return $response; } else { throw new Exception('curl is not installed!'); } }
/** * * @param string $token * @param string $payer_id * * @return Merchant_Billing_Response */ public function get_details_for($token, $payer_id) { $this->payer_id = urldecode($payer_id); $this->token = urldecode($token); $params = array('METHOD' => 'GetExpressCheckoutDetails', 'TOKEN' => $token); $this->post = array_merge($this->post, $params); Merchant_Logger::log("Commit Paypal Method: GetExpressCheckoutDetails"); return $this->commit($this->urlize($this->post)); }
public static function save_request($string) { if (self::$request_file === FALSE) { return; } if (null === self::$path) { self::$path = dirname(__FILE__) . '/../../../log/'; } if (self::$request_file === NULL) { self::$request_file = 'response.xml'; } if (substr(self::$request_file, 1, 1) == '=') { self::$request_file = $self::$path . '/' . self::$request_file; } if (null === self::$path) { self::$path = dirname(__FILE__) . '/../../../log/'; } if (!is_writable(self::$request_file) or !file_exists(self::$request_file)) { return; } $fp = fopen(self::$request_file, 'w'); fwrite($fp, $string); fclose($fp); }
/** * Send an HTTP or HTTPS request. * * * @param string $method Type of HTTP request ('post' for a POST, anything else for a GET) * @param string $body Body of request to send * @param array $options Options for this request, including: * <ul> * <li>timeout - Timeout in seconds * <li>user_agent - User-agent header to send * <li>headers - Array of additional headers to send. * Each header should be a string with the header name, * followed by a colon, followed by the header value. See * CURLOPT_HTTPHEADER for details. * <li>allow_unsafe_ssl - Set to a true value to allow SSL transactions * even if the certificate fails. * </ul> * @throws Merchant_Billing_Exception If the request fails at the network or HTTP layer */ public function request($method, $body, $options = array()) { $timeout = isset($options['timeout']) ? $options['timeout'] : '0'; $user_agent = isset($options['user_agent']) ? $options['user_agent'] : null; $headers = isset($options['headers']) ? $options['headers'] : array(); $server = parse_url($this->endpoint); if (!isset($server['port'])) { $server['port'] = $server['scheme'] == 'https' ? 443 : 80; } if (!isset($server['path'])) { $server['path'] = '/'; } if (isset($server['user']) && isset($server['pass'])) { $headers[] = 'Authorization: Basic ' . base64_encode($server['user'] . ':' . $server['pass']); } $transaction_url = $server['scheme'] . '://' . $server['host'] . $server['path'] . (isset($server['query']) ? '?' . $server['query'] : ''); Merchant_Logger::save_request($body); if (function_exists('curl_init')) { $curl = curl_init($transaction_url); curl_setopt($curl, CURLOPT_PORT, $server['port']); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, empty($options['allow_unsafe_ssl']) ? 0 : 1); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_FORBID_REUSE, 1); curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout); // First Data requires the SSLCERT pointing to the pem file if (isset($options['pem'])) { curl_setopt($curl, CURLOPT_SSLCERT, $options['pem']); } if (isset($user_agent)) { curl_setopt($curl, CURLOPT_USERAGENT, $user_agent); } if ($method == 'post') { curl_setopt($curl, CURLOPT_POST, 1); } curl_setopt($curl, CURLOPT_POSTFIELDS, $body); $response = curl_exec($curl); // Check for outright failure if ($response === FALSE) { $ex = new Merchant_Billing_Exception(curl_error($curl), curl_errno($curl)); curl_close($curl); throw $ex; } // We got a response, so let's log it Merchant_Logger::log("Merchant response: {$response}"); Merchant_Logger::save_response($response); // Now check for an HTTP error $curlInfo = curl_getinfo($curl); if ($curlInfo['http_code'] < 200 && $curlInfo['http_code'] >= 300) { $ex = new Merchant_Billing_Exception("HTTP Status #" . $this->m_curlinfo['http_code'] . "\n" . ($this->m_doc ? "\n{$this->m_doc}" : "") . "CurlInfo:\n" . print_r($this->m_curlinfo, TRUE)); curl_close($curl); throw $ex; } curl_close($curl); // OK, the response was OK at the HTTP level at least! Pass it up a layer. return $response; } else { throw new Merchant_Billing_Exception('curl is not installed!'); } }
private function commit($money, Merchant_Billing_CreditCard $creditcard = NULL, $options = array()) { $url = $this->is_test() ? self::TEST_URL : self::LIVE_URL; // Log request, but mask real user information if ($creditcard === NULL) { $log_card = NULL; } else { $log_card = clone $creditcard; $log_card->number = $this->mask_cardnum($log_card->number); if ($log_card->verification_value) { $log_card->verification_value = $this->mask_cvv($log_card->verification_value); } } Merchant_Logger::log("Sending POST to {$url}:\n" . $this->post_data($money, $log_card, $options)); // Make the request $data = $this->ssl_post($url, $this->post_data($money, $creditcard, $options)); $response = $this->parse($data); // Make sure the response is valid and doesn't contain an error if (empty($response['approved'])) { throw new Merchant_Billing_Exception("Error parsing merchant response: No status information"); } if ($response['approved'] == 'ERROR') { throw new Merchant_Billing_Exception("Merchant error: " . (isset($response['errmsg']) ? $response['errmsg'] : 'Unknown error')); } if ($response['approved'] != 'APPROVED' && $response['approved'] != 'DECLINED') { throw new Merchant_Billing_Exception("Merchant error: Unknown status '{$response['approved']}'"); } return new Merchant_Billing_Response($this->success_from($response), $this->message_from($response), $response, array('test' => $this->is_test(), 'authorization' => isset($response['orderid']) && isset($response['transrefnumber']) ? $this->pack_authorization_values($response['orderid'], $response['transrefnumber']) : NULL, 'avs_result' => isset($response['avsresult']) ? array('code' => $response['avsresult']) : NULL, 'cvv_result' => isset($response['cardidresult']) ? $response['cardidresult'] : NULL)); }
<?php require_once '../../lib/merchant.php'; require_once '../login.php'; Merchant_Billing_Base::mode('test'); try { $gateway = new Merchant_Billing_PiraeusPaycenter(array('acquire_id' => acquire_id, 'merchant_id' => merchant_id, 'pos_id' => pos_id, 'user' => user, 'password' => e_password, 'channel_type' => channel_type)); $cc = new Merchant_Billing_CreditCard(array("first_name" => "Test", "last_name" => "User", "number" => "4111111111111111", "month" => "01", "year" => "2011", "verification_value" => "123")); $options = array('order_id' => $gateway->generate_unique_id()); $response = $gateway->purchase('1', $cc, $options); Merchant_Logger::print_ar($response); if ($response->success()) { echo 'Success Authorize<br />'; echo $response->message() . "<br />"; } else { echo $response->message(); } } catch (Exception $e) { echo $e->getMessage(); }
<?php require_once '../lib/merchant.php'; try { $options = array('amount' => '50.00', 'currency' => 'EUR', 'service' => 'Eurobank'); $intergration = Merchant_Billing_Integration::payment_service_for('1000', '*****@*****.**', $options); $intergration->billing_address(array('country' => 'Greece'))->currency('EUR')->customer(array('first_name' => 'John', 'last_name' => 'Doe')); Merchant_Logger::print_ar($intergration); } catch (Exception $exc) { echo $exc->getMessage(); }
/** * * @param string $action * @param number $money * @param array $parameters * * @return Merchant_Billing_Response */ private function commit($action, $money, $parameters = array()) { $url = $this->is_test() ? self::LIVE_URL : self::TEST_URL; if ($action != 'VOID') { $parameters['amount'] = $this->amount($money); } /* Request a test response */ if ($this->is_test()) { $parameters['test_request'] = 'TRUE'; } // Log request, but mask real user information $log_post = $this->post; if (isset($log_post['card_num'])) { $log_post['card_num'] = $this->mask_cardnum($log_post['card_num']); } if (isset($log_post['card_code'])) { $log_post['card_code'] = $this->mask_cvv($log_post['card_code']); } Merchant_Logger::log("Sending POST to {$url}:\n" . $this->post_data($action, $parameters, $log_post)); $data = $this->ssl_post($url, $this->post_data($action, $parameters, $this->post)); $response = $this->parse($data); // Check the response code, and throw an exception if necessary if (empty($response['response_code'])) { throw new Merchant_Billing_Exception("Error parsing merchant response: No status information"); } switch ($response['response_code']) { case self::ERROR: switch ($response['response_reason_code']) { case self::RESPONSE_REASON_CARD_INVALID: case self::RESPONSE_REASON_CARD_EXPIRATION_INVALID: case self::RESPONSE_REASON_CARD_EXPIRED: case self::RESPONSE_REASON_ABA_INVALID: case self::RESPONSE_REASON_ACCOUNT_INVALID: case self::RESPONSE_REASON_DUPLICATE: case self::RESPONSE_REASON_AUTHCODE_REQUIRED: // These should be treated like a decline break; default: throw new Merchant_Billing_Exception("Merchant error: {$response['response_reason_text']} (code {$response['response_code']}/{$response['response_reason_code']})"); } break; case self::APPROVED: case self::DECLINED: case self::FRAUD_REVIEW: // These are OK break; default: throw new Merchant_Billing_Exception("Merchant error: Unknown status '{$response['response_code']}'"); } $message = $this->message_from($response); $test_mode = $this->is_test(); return new Merchant_Billing_Response($this->success_from($response), $message, $response, array('test' => $test_mode, 'authorization' => $response['transaction_id'], 'fraud_review' => $this->fraud_review_from($response), 'avs_result' => array('code' => $response['avs_result_code']), 'cvv_result' => $response['card_code'])); }
<?php require_once '../../lib/merchant.php'; require_once '../login.php'; Merchant_Billing_Base::mode('test'); try { $gateway = new Merchant_Billing_Eurobank(array('login' => EUROBANK_LOGIN, 'password' => EUROBANK_PASS)); $cc = new Merchant_Billing_CreditCard(array("first_name" => "Test", "last_name" => "User", "number" => "41111111111111", "month" => "12", "year" => "2012", "verification_value" => "123")); Merchant_Logger::print_ar($gateway->authorize('1', $cc, array('customer_email' => '*****@*****.**'))); } catch (Exception $e) { echo $e->getMessage(); }