Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class AuthorizeNetCim {
- const URL_LIVE = 'https://api.authorize.net/xml/v1/request.api';
- const URL_TEST = 'https://apitest.authorize.net/xml/v1/request.api';
- private $url;
- private $test_request;
- private $debug;
- private $loginname;
- private $transactionkey;
- public $customer_profile_id;
- public $customer_recurring_id;
- public $firstname;
- public $lastname;
- public $email;
- public $phone;
- public $company;
- public $address;
- public $city;
- public $state;
- public $zip;
- public $country;
- public $shipping_firstname;
- public $shipping_lastname;
- public $shipping_company;
- public $shipping_address;
- public $shipping_city;
- public $shipping_state;
- public $shipping_zip;
- public $shipping_country;
- public $customer_ip;
- function __construct($registry, $logname, $transkey, $test_serv = false, $test_req = false, $deb = false) {
- $this->log = $registry->get('log');
- $this->loginname = $logname;
- $this->transactionkey = $transkey;
- if ($test_serv) {
- $this->url = self::URL_TEST;
- } else {
- $this->url = self::URL_LIVE;
- }
- $this->test_request = $test_req;
- $this->debug = $deb;
- }
- private function sendRequest($data) {
- array_splice($data, 1, 0, array(
- '<merchantAuthentication>',
- '<name>' . $this->loginname . '</name>',
- '<transactionKey>' . $this->transactionkey . '</transactionKey>',
- '</merchantAuthentication>'
- ));
- $xml = '<?xml version="1.0" encoding="utf-8"?>' . implode('', $data);
- if ($this->debug) {
- $this->log->write('AUTHNET CIM URL: ' . $this->url);
- $this->log->write('AUTHNET CIM REQUEST: ' . $xml);
- }
- $curl = curl_init($this->url);
- curl_setopt($curl, CURLOPT_PORT, 443);
- curl_setopt($curl, CURLOPT_HEADER, 0);
- curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
- 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_POST, 1);
- curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
- curl_setopt($curl, CURLOPT_TIMEOUT, 10);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
- $response = curl_exec($curl);
- if ($this->debug) {
- $this->log->write('AUTHNET CIM RESPONSE: ' . $response);
- }
- if (curl_error($curl)) {
- $this->log->write('AUTHNET CIM CURL ERROR: ' . curl_errno($curl) . '::' . curl_error($curl));
- } else if (!$response) {
- $this->log->write('AUTHNET CIM CURL ERROR: Empty Gateway Response');
- }
- curl_close($curl);
- $response_data = simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOWARNING);
- return $response_data;
- }
- public function createCustomerProfile($merchant_customer_id = 0, $description = '') {
- $data[] = '<createCustomerProfileRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">';
- $data[] = '<profile>';
- if ($merchant_customer_id) {
- $data[] = '<merchantCustomerId>' . $merchant_customer_id . '</merchantCustomerId>';
- }
- if ($description) {
- $data[] = '<description>' . $description . '</description>';
- }
- $data[] = '<email>' . $this->email . '</email>';
- $data[] = '</profile>';
- $data[] = '</createCustomerProfileRequest>';
- $result = $this->sendRequest($data);
- if ($result && $result->messages->resultCode == 'Ok') {
- $this->customer_profile_id = $result->customerProfileId;
- }
- return $result;
- }
- public function createCustomerPaymentProfile($card_number, $card_exp_month, $card_exp_year) {
- $data = array(
- '<createCustomerPaymentProfileRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">',
- '<customerProfileId>' . $this->customer_profile_id . '</customerProfileId>',
- '<paymentProfile>',
- '<billTo>',
- '<firstName>' . $this->firstname . '</firstName>',
- '<lastName>' . $this->lastname . '</lastName>',
- '<address>' . $this->address . '</address>',
- '<city>' . $this->city . '</city>',
- '<state>' . $this->state . '</state>',
- '<zip>' . $this->zip . '</zip>',
- '<country>' . $this->country . '</country>',
- '<phoneNumber>' . $this->phone . '</phoneNumber>',
- '</billTo>',
- '<payment>',
- '<creditCard>',
- '<cardNumber>' . $card_number . '</cardNumber>',
- '<expirationDate>' . $card_exp_year . '-' . $card_exp_month . '</expirationDate>',
- '</creditCard>',
- '</payment>',
- '</paymentProfile>'
- );
- if ($this->test_request) {
- $data[] = '<validationMode>testMode</validationMode>';
- }
- $data[] = '</createCustomerPaymentProfileRequest>';
- $result = $this->sendRequest($data);
- return $result;
- }
- public function createCustomerShippingAddress() {
- $data = array(
- '<createCustomerShippingAddressRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">',
- '<customerProfileId>' . $this->customer_profile_id . '</customerProfileId>',
- '<address>',
- '<firstName>' . $this->shipping_firstname . '</firstName>',
- '<lastName>' . $this->shipping_lastname . '</lastName>',
- '<address>' . $this->shipping_address . '</address>',
- '<city>' . $this->shipping_city . '</city>',
- '<state>' . $this->shipping_state . '</state>',
- '<zip>' . $this->shipping_zip . '</zip>',
- '<country>' . $this->shipping_country . '</country>',
- '</address>',
- '</createCustomerShippingAddressRequest>'
- );
- $result = $this->sendRequest($data);
- return $result;
- }
- public function createCustomerProfileTransaction($order_id, $transaction_type, $customer_payment_profile_id, $customer_shipping_address_id = 0, $amount = 0.00, $card_code = '', $recurring = false) {
- $data = array();
- $data[] = '<createCustomerProfileTransactionRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">';
- /*if ($order_id) {
- $data[] = '<refId>' . $order_id . '</refId>';
- }*/
- $data[] = '<transaction>';
- if ($transaction_type == 'capture') {
- $data[] = '<profileTransAuthCapture>';
- } else {
- $data[] = '<profileTransAuthOnly>';
- }
- $data[] = '<amount>' . $amount . '</amount>';
- $data[] = '<customerProfileId>' . $this->customer_profile_id . '</customerProfileId>';
- $data[] = '<customerPaymentProfileId>' . $customer_payment_profile_id . '</customerPaymentProfileId>';
- if ($customer_shipping_address_id) {
- $data[] = '<customerShippingAddressId>' . $customer_shipping_address_id . '</customerShippingAddressId>';
- }
- if ($order_id) {
- $data[] = '<order>';
- $data[] = '<invoiceNumber>' . $order_id . '</invoiceNumber>';
- $data[] = '</order>';
- }
- if ($recurring) {
- $data[] = '<recurringBilling>true</recurringBilling>';
- }
- if ($card_code) {
- $data[] = '<cardCode>' . $card_code . '</cardCode>';
- }
- if ($transaction_type == 'capture') {
- $data[] = '</profileTransAuthCapture>';
- } else {
- $data[] = '</profileTransAuthOnly>';
- }
- $data[] = '</transaction>';
- $data[] = '<extraOptions><![CDATA[';
- $data[] = 'x_customer_ip=' . $this->customer_ip;
- $data[] = '&x_duplicate_window=5';
- if ($this->test_request) {
- $data[] = '&x_test_request=true';
- }
- $data[] = ']]></extraOptions>';
- $data[] = '</createCustomerProfileTransactionRequest>';
- $response = $this->sendRequest($data);
- $response_data = array(
- 'data' => array(),
- 'success' => false,
- 'error' => ''
- );
- if ($response) {
- $response_info = array();
- if ($response->messages->resultCode == 'Ok') {
- $i = 0;
- $results = explode(',', $response->directResponse);
- foreach ($results as $result) {
- $response_info[$i] = trim($result, '"');
- $i++;
- }
- }
- if ($response_info) {
- $response_data['data'] = $response_info;
- if ($response_info[0] == '1') {
- $response_data['success'] = true;
- } else {
- $response_data['error'] = $response_info[3];
- }
- } else {
- $response_data['error'] = (string)$response->messages->message->text;
- }
- } else {
- $response_data['error'] = 'There was an error processing your request. Please try again, if the problem persists contact us.';
- }
- return $response_data;
- }
- public function getCustomerProfile() {
- if ($this->customer_profile_id) {
- $data[] = '<getCustomerProfileRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">';
- $data[] = '<customerProfileId>' . $this->customer_profile_id . '</customerProfileId>';
- $data[] = '</getCustomerProfileRequest>';
- $results = $this->sendRequest($data);
- return $results;
- }
- }
- public function getCustomerPaymentProfile($customer_payment_profile_id) {
- if ($this->customer_profile_id) {
- $data[] = '<getCustomerPaymentProfileRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">';
- $data[] = '<customerProfileId>' . $this->customer_profile_id . '</customerProfileId>';
- $data[] = '<customerPaymentProfileId>' . $customer_payment_profile_id . '</customerPaymentProfileId>';
- $data[] = '</getCustomerPaymentProfileRequest>';
- $results = $this->sendRequest($data);
- return $results;
- }
- }
- public function getCustomerShippingAddress($customer_shipping_address_id) {
- if ($this->customer_profile_id) {
- $data[] = '<getCustomerShippingAddressRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">';
- $data[] = '<customerProfileId>' . $this->customer_profile_id . '</customerProfileId>';
- $data[] = '<customerShippingAddressId>' . $customer_shipping_address_id . '</customerShippingAddressId>';
- $data[] = '</getCustomerShippingAddressRequest>';
- $results = $this->sendRequest($data);
- return $results;
- }
- }
- public function updateCustomerProfile($merchant_customer_id = '', $description = '') {
- if ($this->customer_profile_id) {
- $data[] = '<updateCustomerProfileRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">';
- $data[] = '<profile>';
- if ($merchant_customer_id) {
- $data[] = '<merchantCustomerId>' . $merchant_customer_id . '</merchantCustomerId>';
- }
- if ($description) {
- $data[] = '<description>' . $description . '</description>';
- }
- $data[] = '<email>' . $this->email . '</email>';
- $data[] = '<customerProfileId>' . $this->customer_profile_id . '</customerProfileId>';
- $data[] = '</profile>';
- $data[] = '</updateCustomerProfileRequest>';
- $results = $this->sendRequest($data);
- return $results;
- }
- }
- public function updateCustomerPaymentProfile($customer_payment_profile_id, $card_number, $card_exp_month, $card_exp_year) {
- $data = array(
- '<updateCustomerPaymentProfileRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">',
- '<customerProfileId>' . $this->customer_profile_id . '</customerProfileId>',
- '<paymentProfile>',
- '<billTo>',
- '<firstName>' . $this->firstname . '</firstName>',
- '<lastName>' . $this->lastname . '</lastName>',
- '<address>' . $this->address . '</address>',
- '<city>' . $this->city . '</city>',
- '<state>' . $this->state . '</state>',
- '<zip>' . $this->zip . '</zip>',
- '<country>' . $this->country . '</country>',
- '<phoneNumber>' . $this->phone . '</phoneNumber>',
- '</billTo>',
- '<payment>',
- '<creditCard>',
- '<cardNumber>' . $card_number . '</cardNumber>',
- '<expirationDate>' . $card_exp_year . '-' . $card_exp_month . '</expirationDate>',
- '</creditCard>',
- '</payment>',
- '<customerPaymentProfileId>' . $customer_payment_profile_id . '</customerPaymentProfileId>',
- '</paymentProfile>'
- );
- if ($this->test_request) {
- $data[] = '<validationMode>testMode</validationMode>';
- }
- $data[] = '</updateCustomerPaymentProfileRequest>';
- $result = $this->sendRequest($data);
- return $result;
- }
- public function updateCustomerShippingAddress($customer_shipping_address_id) {
- $data = array(
- '<updateCustomerShippingAddressRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">',
- '<customerProfileId>' . $this->customer_profile_id . '</customerProfileId>',
- '<address>',
- '<firstName>' . $this->shipping_firstname . '</firstName>',
- '<lastName>' . $this->shipping_lastname . '</lastName>',
- '<address>' . $this->shipping_address . '</address>',
- '<city>' . $this->shipping_city . '</city>',
- '<state>' . $this->shipping_state . '</state>',
- '<zip>' . $this->shipping_zip . '</zip>',
- '<country>' . $this->shipping_country . '</country>',
- '<customerAddressId>' . $customer_shipping_address_id . '</customerAddressId>',
- '</address>',
- '</updateCustomerShippingAddressRequest>'
- );
- $result = $this->sendRequest($data);
- return $result;
- }
- public function deleteCustomerProfile($customer_profile_id) {
- $data = array(
- '<deleteCustomerProfileRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">',
- '<customerProfileId>' . $customer_profile_id . '</customerProfileId>',
- '</deleteCustomerProfileRequest>'
- );
- $result = $this->sendRequest($data);
- return $result;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment