Guest User

system/library

a guest
Sep 30th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.49 KB | None | 0 0
  1. <?php
  2.  
  3. class AuthorizeNetCim {
  4.   const URL_LIVE  = 'https://api.authorize.net/xml/v1/request.api';
  5.   const URL_TEST  = 'https://apitest.authorize.net/xml/v1/request.api';
  6.  
  7.   private $url;
  8.   private $test_request;
  9.   private $debug;
  10.  
  11.   private $loginname;
  12.   private $transactionkey;
  13.  
  14.   public $customer_profile_id;
  15.   public $customer_recurring_id;
  16.   public $firstname;
  17.   public $lastname;
  18.   public $email;
  19.   public $phone;
  20.   public $company;
  21.   public $address;
  22.   public $city;
  23.   public $state;
  24.   public $zip;
  25.   public $country;
  26.   public $shipping_firstname;
  27.   public $shipping_lastname;
  28.   public $shipping_company;
  29.   public $shipping_address;
  30.   public $shipping_city;
  31.   public $shipping_state;
  32.   public $shipping_zip;
  33.   public $shipping_country;
  34.   public $customer_ip;
  35.  
  36.   function __construct($registry, $logname, $transkey, $test_serv = false, $test_req = false, $deb = false) {
  37.     $this->log = $registry->get('log');
  38.  
  39.     $this->loginname      = $logname;
  40.     $this->transactionkey = $transkey;
  41.  
  42.     if ($test_serv) {
  43.       $this->url = self::URL_TEST;
  44.     } else {
  45.       $this->url = self::URL_LIVE;
  46.     }
  47.  
  48.     $this->test_request = $test_req;
  49.     $this->debug        = $deb;
  50.   }
  51.  
  52.   private function sendRequest($data) {
  53.     array_splice($data, 1, 0, array(
  54.       '<merchantAuthentication>',
  55.       '<name>' . $this->loginname . '</name>',
  56.       '<transactionKey>' . $this->transactionkey . '</transactionKey>',
  57.       '</merchantAuthentication>'
  58.     ));
  59.  
  60.     $xml = '<?xml version="1.0" encoding="utf-8"?>' . implode('', $data);
  61.  
  62.     if ($this->debug) {
  63.       $this->log->write('AUTHNET CIM URL: ' . $this->url);
  64.       $this->log->write('AUTHNET CIM REQUEST: ' . $xml);
  65.     }
  66.  
  67.     $curl = curl_init($this->url);
  68.  
  69.     curl_setopt($curl, CURLOPT_PORT, 443);
  70.     curl_setopt($curl, CURLOPT_HEADER, 0);
  71.     curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
  72.     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  73.     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  74.     curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
  75.     curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
  76.     curl_setopt($curl, CURLOPT_POST, 1);
  77.     curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
  78.     curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  79.     curl_setopt($curl, CURLOPT_POSTFIELDS, $xml);
  80.  
  81.     $response = curl_exec($curl);
  82.  
  83.     if ($this->debug) {
  84.       $this->log->write('AUTHNET CIM RESPONSE: ' . $response);
  85.     }
  86.  
  87.     if (curl_error($curl)) {
  88.       $this->log->write('AUTHNET CIM CURL ERROR: ' . curl_errno($curl) . '::' . curl_error($curl));
  89.     } else if (!$response) {
  90.       $this->log->write('AUTHNET CIM CURL ERROR: Empty Gateway Response');
  91.     }
  92.  
  93.     curl_close($curl);
  94.  
  95.     $response_data = simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOWARNING);
  96.  
  97.     return $response_data;
  98.   }
  99.  
  100.   public function createCustomerProfile($merchant_customer_id = 0, $description = '') {
  101.     $data[] = '<createCustomerProfileRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">';
  102.     $data[] = '<profile>';
  103.  
  104.     if ($merchant_customer_id) {
  105.       $data[] = '<merchantCustomerId>' . $merchant_customer_id . '</merchantCustomerId>';
  106.     }
  107.  
  108.     if ($description) {
  109.       $data[] = '<description>' . $description . '</description>';
  110.     }
  111.  
  112.     $data[] = '<email>' . $this->email . '</email>';
  113.     $data[] = '</profile>';
  114.     $data[] = '</createCustomerProfileRequest>';
  115.  
  116.     $result = $this->sendRequest($data);
  117.  
  118.     if ($result && $result->messages->resultCode == 'Ok') {
  119.       $this->customer_profile_id = $result->customerProfileId;
  120.     }
  121.  
  122.     return $result;
  123.   }
  124.  
  125.   public function createCustomerPaymentProfile($card_number, $card_exp_month, $card_exp_year) {
  126.     $data = array(
  127.       '<createCustomerPaymentProfileRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">',
  128.       '<customerProfileId>' . $this->customer_profile_id . '</customerProfileId>',
  129.       '<paymentProfile>',
  130.       '<billTo>',
  131.       '<firstName>' . $this->firstname . '</firstName>',
  132.       '<lastName>' . $this->lastname . '</lastName>',
  133.       '<address>' . $this->address . '</address>',
  134.       '<city>' . $this->city . '</city>',
  135.       '<state>' . $this->state . '</state>',
  136.       '<zip>' . $this->zip . '</zip>',
  137.       '<country>' . $this->country . '</country>',
  138.       '<phoneNumber>' . $this->phone . '</phoneNumber>',
  139.       '</billTo>',
  140.       '<payment>',
  141.       '<creditCard>',
  142.       '<cardNumber>' . $card_number . '</cardNumber>',
  143.       '<expirationDate>' . $card_exp_year . '-' . $card_exp_month . '</expirationDate>',
  144.       '</creditCard>',
  145.       '</payment>',
  146.       '</paymentProfile>'
  147.     );
  148.  
  149.     if ($this->test_request) {
  150.       $data[] = '<validationMode>testMode</validationMode>';
  151.     }
  152.  
  153.     $data[] = '</createCustomerPaymentProfileRequest>';
  154.  
  155.     $result = $this->sendRequest($data);
  156.  
  157.     return $result;
  158.   }
  159.  
  160.   public function createCustomerShippingAddress() {
  161.     $data = array(
  162.       '<createCustomerShippingAddressRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">',
  163.       '<customerProfileId>' . $this->customer_profile_id . '</customerProfileId>',
  164.       '<address>',
  165.       '<firstName>' . $this->shipping_firstname . '</firstName>',
  166.       '<lastName>' . $this->shipping_lastname . '</lastName>',
  167.       '<address>' . $this->shipping_address . '</address>',
  168.       '<city>' . $this->shipping_city . '</city>',
  169.       '<state>' . $this->shipping_state . '</state>',
  170.       '<zip>' . $this->shipping_zip . '</zip>',
  171.       '<country>' . $this->shipping_country . '</country>',
  172.       '</address>',
  173.       '</createCustomerShippingAddressRequest>'
  174.     );
  175.  
  176.     $result = $this->sendRequest($data);
  177.  
  178.     return $result;
  179.   }
  180.  
  181.   public function createCustomerProfileTransaction($order_id, $transaction_type, $customer_payment_profile_id, $customer_shipping_address_id = 0, $amount = 0.00, $card_code = '', $recurring = false) {
  182.     $data = array();
  183.  
  184.     $data[] = '<createCustomerProfileTransactionRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">';
  185.  
  186.     /*if ($order_id) {
  187.       $data[] = '<refId>' . $order_id . '</refId>';
  188.     }*/
  189.  
  190.     $data[] = '<transaction>';
  191.  
  192.     if ($transaction_type == 'capture') {
  193.       $data[] = '<profileTransAuthCapture>';
  194.     } else {
  195.       $data[] = '<profileTransAuthOnly>';
  196.     }
  197.  
  198.     $data[] = '<amount>' . $amount . '</amount>';
  199.  
  200.     $data[] = '<customerProfileId>' . $this->customer_profile_id . '</customerProfileId>';
  201.     $data[] = '<customerPaymentProfileId>' . $customer_payment_profile_id . '</customerPaymentProfileId>';
  202.  
  203.     if ($customer_shipping_address_id) {
  204.       $data[] = '<customerShippingAddressId>' . $customer_shipping_address_id . '</customerShippingAddressId>';
  205.     }
  206.  
  207.     if ($order_id) {
  208.       $data[] = '<order>';
  209.       $data[] = '<invoiceNumber>' . $order_id . '</invoiceNumber>';
  210.       $data[] = '</order>';
  211.     }
  212.  
  213.     if ($recurring) {
  214.       $data[] = '<recurringBilling>true</recurringBilling>';
  215.     }
  216.  
  217.     if ($card_code) {
  218.       $data[] = '<cardCode>' . $card_code . '</cardCode>';
  219.     }
  220.  
  221.     if ($transaction_type == 'capture') {
  222.       $data[] = '</profileTransAuthCapture>';
  223.     } else {
  224.       $data[] = '</profileTransAuthOnly>';
  225.     }
  226.  
  227.     $data[] = '</transaction>';
  228.     $data[] = '<extraOptions><![CDATA[';
  229.     $data[] = 'x_customer_ip=' . $this->customer_ip;
  230.     $data[] = '&x_duplicate_window=5';
  231.  
  232.     if ($this->test_request) {
  233.       $data[] = '&x_test_request=true';
  234.     }
  235.  
  236.     $data[] = ']]></extraOptions>';
  237.     $data[] = '</createCustomerProfileTransactionRequest>';
  238.  
  239.     $response = $this->sendRequest($data);
  240.  
  241.     $response_data = array(
  242.       'data'    => array(),
  243.       'success' => false,
  244.       'error'   => ''
  245.     );
  246.  
  247.     if ($response) {
  248.       $response_info = array();
  249.  
  250.       if ($response->messages->resultCode == 'Ok') {
  251.         $i = 0;
  252.  
  253.         $results = explode(',', $response->directResponse);
  254.  
  255.         foreach ($results as $result) {
  256.           $response_info[$i] = trim($result, '"');
  257.  
  258.           $i++;
  259.         }
  260.       }
  261.  
  262.       if ($response_info) {
  263.         $response_data['data'] = $response_info;
  264.  
  265.         if ($response_info[0] == '1') {
  266.           $response_data['success'] = true;
  267.         } else {
  268.           $response_data['error'] = $response_info[3];
  269.         }
  270.       } else {
  271.         $response_data['error'] = (string)$response->messages->message->text;
  272.       }
  273.     } else {
  274.       $response_data['error'] = 'There was an error processing your request. Please try again, if the problem persists contact us.';
  275.     }
  276.  
  277.     return $response_data;
  278.   }
  279.  
  280.   public function getCustomerProfile() {
  281.     if ($this->customer_profile_id) {
  282.       $data[] = '<getCustomerProfileRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">';
  283.       $data[] = '<customerProfileId>' . $this->customer_profile_id . '</customerProfileId>';
  284.       $data[] = '</getCustomerProfileRequest>';
  285.  
  286.       $results = $this->sendRequest($data);
  287.  
  288.       return $results;
  289.     }
  290.   }
  291.  
  292.   public function getCustomerPaymentProfile($customer_payment_profile_id) {
  293.     if ($this->customer_profile_id) {
  294.       $data[] = '<getCustomerPaymentProfileRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">';
  295.       $data[] = '<customerProfileId>' . $this->customer_profile_id . '</customerProfileId>';
  296.       $data[] = '<customerPaymentProfileId>' . $customer_payment_profile_id . '</customerPaymentProfileId>';
  297.       $data[] = '</getCustomerPaymentProfileRequest>';
  298.  
  299.       $results = $this->sendRequest($data);
  300.  
  301.       return $results;
  302.     }
  303.   }
  304.  
  305.   public function getCustomerShippingAddress($customer_shipping_address_id) {
  306.     if ($this->customer_profile_id) {
  307.       $data[] = '<getCustomerShippingAddressRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">';
  308.       $data[] = '<customerProfileId>' . $this->customer_profile_id . '</customerProfileId>';
  309.       $data[] = '<customerShippingAddressId>' . $customer_shipping_address_id . '</customerShippingAddressId>';
  310.       $data[] = '</getCustomerShippingAddressRequest>';
  311.  
  312.       $results = $this->sendRequest($data);
  313.  
  314.       return $results;
  315.     }
  316.   }
  317.  
  318.   public function updateCustomerProfile($merchant_customer_id = '', $description = '') {
  319.     if ($this->customer_profile_id) {
  320.       $data[] = '<updateCustomerProfileRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">';
  321.       $data[] = '<profile>';
  322.  
  323.       if ($merchant_customer_id) {
  324.         $data[] = '<merchantCustomerId>' . $merchant_customer_id . '</merchantCustomerId>';
  325.       }
  326.  
  327.       if ($description) {
  328.         $data[] = '<description>' . $description . '</description>';
  329.       }
  330.  
  331.       $data[] = '<email>' . $this->email . '</email>';
  332.       $data[] = '<customerProfileId>' . $this->customer_profile_id . '</customerProfileId>';
  333.       $data[] = '</profile>';
  334.       $data[] = '</updateCustomerProfileRequest>';
  335.  
  336.       $results = $this->sendRequest($data);
  337.  
  338.       return $results;
  339.     }
  340.   }
  341.  
  342.   public function updateCustomerPaymentProfile($customer_payment_profile_id, $card_number, $card_exp_month, $card_exp_year) {
  343.     $data = array(
  344.       '<updateCustomerPaymentProfileRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">',
  345.       '<customerProfileId>' . $this->customer_profile_id . '</customerProfileId>',
  346.       '<paymentProfile>',
  347.       '<billTo>',
  348.       '<firstName>' . $this->firstname . '</firstName>',
  349.       '<lastName>' . $this->lastname . '</lastName>',
  350.       '<address>' . $this->address . '</address>',
  351.       '<city>' . $this->city . '</city>',
  352.       '<state>' . $this->state . '</state>',
  353.       '<zip>' . $this->zip . '</zip>',
  354.       '<country>' . $this->country . '</country>',
  355.       '<phoneNumber>' . $this->phone . '</phoneNumber>',
  356.       '</billTo>',
  357.       '<payment>',
  358.       '<creditCard>',
  359.       '<cardNumber>' . $card_number . '</cardNumber>',
  360.       '<expirationDate>' . $card_exp_year . '-' . $card_exp_month . '</expirationDate>',
  361.       '</creditCard>',
  362.       '</payment>',
  363.       '<customerPaymentProfileId>' . $customer_payment_profile_id . '</customerPaymentProfileId>',
  364.       '</paymentProfile>'
  365.     );
  366.  
  367.     if ($this->test_request) {
  368.       $data[] = '<validationMode>testMode</validationMode>';
  369.     }
  370.  
  371.     $data[] = '</updateCustomerPaymentProfileRequest>';
  372.  
  373.     $result = $this->sendRequest($data);
  374.  
  375.     return $result;
  376.   }
  377.  
  378.   public function updateCustomerShippingAddress($customer_shipping_address_id) {
  379.     $data = array(
  380.       '<updateCustomerShippingAddressRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">',
  381.       '<customerProfileId>' . $this->customer_profile_id . '</customerProfileId>',
  382.       '<address>',
  383.       '<firstName>' . $this->shipping_firstname . '</firstName>',
  384.       '<lastName>' . $this->shipping_lastname . '</lastName>',
  385.       '<address>' . $this->shipping_address . '</address>',
  386.       '<city>' . $this->shipping_city . '</city>',
  387.       '<state>' . $this->shipping_state . '</state>',
  388.       '<zip>' . $this->shipping_zip . '</zip>',
  389.       '<country>' . $this->shipping_country . '</country>',
  390.       '<customerAddressId>' . $customer_shipping_address_id . '</customerAddressId>',
  391.       '</address>',
  392.       '</updateCustomerShippingAddressRequest>'
  393.     );
  394.  
  395.     $result = $this->sendRequest($data);
  396.  
  397.     return $result;
  398.   }
  399.  
  400.   public function deleteCustomerProfile($customer_profile_id) {
  401.     $data = array(
  402.       '<deleteCustomerProfileRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd">',
  403.       '<customerProfileId>' . $customer_profile_id . '</customerProfileId>',
  404.       '</deleteCustomerProfileRequest>'
  405.     );
  406.  
  407.     $result = $this->sendRequest($data);
  408.  
  409.     return $result;
  410.   }
  411. }
  412.  
  413. ?>
Advertisement
Add Comment
Please, Sign In to add comment