here is the class code to go along with the above code.. if you really need it:
class AuthnetCIMException extends Exception {} class AuthnetCIM { const USE_PRODUCTION_SERVER = 0; const USE_DEVELOPMENT_SERVER = 1; const EXCEPTION_CURL = 10; private $params = array(); private $items = array(); private $success = false; private $error = true; private $login; private $transkey; private $xml; private $ch; private $response; private $url; private $resultCode; private $code; private $text; private $profileId; private $validation; private $paymentProfileId; private $results; public function __construct($login, $transkey, $test = self::USE_PRODUCTION_SERVER) { $this->login = trim($login); $this->transkey = trim($transkey); if (empty($this->login) || empty($this->transkey)) { trigger_error('You have not configured your ' . __CLASS__ . '() login credentials properly.', E_USER_ERROR); } $this->test = (bool) $test; $subdomain = ($this->test) ? 'apitest' : 'api'; $this->url = 'https://' . $subdomain . '.authorize.net/xml/v1/request.api'; $this->params['customerType'] = 'individual'; $this->params['validationMode'] = 'liveMode'; $this->params['taxExempt'] = 'false'; $this->params['recurringBilling'] = 'false'; } public function __destruct() { if (isset($this->ch)) { curl_close($this->ch); } } public function __toString() { if (!$this->params) { return (string) $this; } $output = '<table summary="Authnet Results" id="authnet">' . "\n"; $output .= '<tr>' . "\n\t\t" . '<th colspan="2"><b>Outgoing Parameters</b></th>' . "\n" . '</tr>' . "\n"; foreach ($this->params as $key => $value) { $output .= "\t" . '<tr>' . "\n\t\t" . '<td><b>' . $key . '</b></td>'; $output .= '<td>' . $value . '</td>' . "\n" . '</tr>' . "\n"; } $output .= '</table>' . "\n"; if (!empty($this->xml)) { $output .= 'XML: '; $output .= htmlentities($this->xml); } return $output; } private function process() { $this->ch = curl_init(); curl_setopt($this->ch, CURLOPT_URL, $this->url); curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($this->ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml")); curl_setopt($this->ch, CURLOPT_HEADER, 0); curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->xml); curl_setopt($this->ch, CURLOPT_POST, 1); curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0); $this->response = curl_exec($this->ch); if($this->response) { $this->parseResults(); if ($this->resultCode === 'Ok') { $this->success = true; $this->error = false; } else { $this->success = false; $this->error = true; } curl_close($this->ch); unset($this->ch); } else { throw new AuthnetCIMException('Connection error: ' . curl_error($this->ch) . ' (' . curl_errno($this->ch) . ')', self::EXCEPTION_CURL); } }
$cim = new AuthnetCIM('api-id', 'api-key', AuthnetCIM::USE_DEVELOPMENT_SERVER);
// Process the transaction $cim->setParameter('amount', $amount); $cim->setParameter('customerProfileId', $customerProfileId); $cim->setParameter('customerPaymentProfileId', $customerPaymentProfileId); //$cim->setParameter('customerShippingAddressId', $shipping_profile_id); $cim->setParameter('cardCode', $cvv); $cim->setLineItem('1', 'xxxx', 'xxxxxx', '1', '9.95'); $cim->createCustomerProfileTransaction(); //test if transaction went through if ($cim->isSuccessful()) { //should we store this approval code??---------------------- $approval_code = $cim->getAuthCode();
}
public function createCustomerProfileTransaction($type = 'profileTransAuthCapture') { $types = array('profileTransAuthCapture', 'profileTransCaptureOnly','profileTransAuthOnly'); if (!in_array($type, $types)) { trigger_error('createCustomerProfileTransaction() parameter must be"profileTransAuthCapture", "profileTransCaptureOnly", "profileTransAuthOnly", or empty', E_USER_ERROR); } $this->xml = '<?xml version="1.0" encoding="utf-8"?> <createCustomerProfileTransactionRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd"> <merchantAuthentication> <name>' . $this->login . '</name> <transactionKey>' . $this->transkey . '</transactionKey> </merchantAuthentication> <refId>'. $this->params['refId'] .'</refId> <transaction> <' . $type . '> <amount>'. $this->params['amount'] .'</amount>'; if (isset($this->params['taxAmount'])) { $this->xml .= ' <tax> <amount>'. $this->params['taxAmount'].'</amount> <name>'. $this->params['taxName'] .'</name> <description>'.$this->params['taxDescription'].'</description> </tax>'; } if (isset($this->params['shipAmount'])) { $this->xml .= ' <shipping> <amount>'. $this->params['shipAmount'].'</amount> <name>'. $this->params['shipName'] .'</name> <description>'.$this->params['shipDescription'].'< /description> </shipping>'; } if (isset($this->params['dutyAmount'])) { $this->xml .= ' <duty> <amount>'. $this->params['dutyAmount'].'</amount> <name>'. $this->params['dutyName'] .'</name> <description>'.$this->params['dutyDescription'].'< /description> </duty>'; } $this->xml .= ' <lineItems>' . $this->getLineItems() . '</lineItems> <customerProfileId>'.$this->params['customerProfil eId'].'</customerProfileId> <customerPaymentProfileId>'.$this->params['custome rPaymentProfileId'].'</customerPaymentProfileId> <customerShippingAddressId>'.$this->params['custom erShippingAddressId'].'</customerShippingAddressId >'; if (isset($this->params['orderInvoiceNumber'])) { $this->xml .= ' <order> <invoiceNumber>'.$this->params['invoiceNumber'].'< /orderInvoiceNumber> <description>'.$this->params['description'].'</ord erDescription> <purchaseOrderNumber>'.$this->params['purchaseOrde rNumber'].'</orderPurchaseOrderNumber> </order>'; } $this->xml .= ' <taxExempt>'. $this->params['taxExempt'].'</taxExempt> <recurringBilling>'.$this->params['recurringBillin g'].'</recurringBilling> <cardCode>'. $this->params['cardCode'].'</cardCode>'; if (isset($this->params['orderInvoiceNumber'])) { $this->xml .= ' <approvalCode>'. $this->params['approvalCode'].'</approvalCode>'; } $this->xml .= ' </' . $type . '> </transaction> </createCustomerProfileTransactionRequest>'; $this->process(); }
No comments:
Post a Comment