4 * Licensed to CiviCRM under the Academic Free License version 3.0.
6 * Written and contributed by Ideal Solution, LLC (http://www.idealso.com)
13 * @author Marshal Newrock <marshal@idealso.com>
17 * When looking up response codes in the Authorize.Net API, they
18 * begin at one, so always delete one from the "Position in Response"
20 class CRM_Core_Payment_AuthorizeNet
extends CRM_Core_Payment
{
21 CONST CHARSET
= 'iso-8859-1';
22 CONST AUTH_APPROVED
= 1;
23 CONST AUTH_DECLINED
= 2;
25 CONST TIMEZONE
= 'America/Denver';
27 protected $_mode = NULL;
29 protected $_params = array();
32 * We only need one instance of this object. So we use the singleton
33 * pattern and cache the instance in this variable
38 static private $_singleton = NULL;
43 * @param string $mode the mode of operation: live or test
45 * @param $paymentProcessor
47 * @return \CRM_Core_Payment_AuthorizeNet
49 function __construct($mode, &$paymentProcessor) {
51 $this->_paymentProcessor
= $paymentProcessor;
52 $this->_processorName
= ts('Authorize.net');
54 $config = CRM_Core_Config
::singleton();
55 $this->_setParam('apiLogin', $paymentProcessor['user_name']);
56 $this->_setParam('paymentKey', $paymentProcessor['password']);
57 $this->_setParam('paymentType', 'AIM');
58 $this->_setParam('md5Hash', $paymentProcessor['signature']);
60 $this->_setParam('emailCustomer', 'TRUE');
61 $this->_setParam('timestamp', time());
63 $this->_setParam('sequence', rand(1, 1000));
67 * Singleton function used to manage this object
69 * @param string $mode the mode of operation: live or test
70 * @param object $paymentProcessor the details of the payment processor being invoked
71 * @param CRM_Core_Form $paymentForm reference to the form object if available
72 * @param boolean $force should we force a reload of this payment object
78 static function &singleton($mode, &$paymentProcessor, &$paymentForm = NULL, $force = FALSE) {
79 $processorName = $paymentProcessor['name'];
80 if (!isset(self
::$_singleton[$processorName]) || self
::$_singleton[$processorName] === NULL) {
81 self
::$_singleton[$processorName] = new CRM_Core_Payment_AuthorizeNet($mode, $paymentProcessor);
83 return self
::$_singleton[$processorName];
87 * Should the first payment date be configurable when setting up back office recurring payments
88 * In the case of Authorize.net this is an option
91 protected function supportsFutureRecurStartDate() {
96 * Submit a payment using Advanced Integration Method
98 * @param array $params assoc array of input parameters for this transaction
100 * @return array the result in a nice formatted array (or an error object)
103 function doDirectPayment(&$params) {
104 if (!defined('CURLOPT_SSLCERT')) {
105 return self
::error(9001, 'Authorize.Net requires curl with SSL support');
109 * recurpayment function does not compile an array & then proces it -
110 * - the tpl does the transformation so adding call to hook here
111 * & giving it a change to act on the params array
113 $newParams = $params;
114 if (!empty($params['is_recur']) && !empty($params['contributionRecurID'])) {
115 CRM_Utils_Hook
::alterPaymentProcessorParams($this,
120 foreach ($newParams as $field => $value) {
121 $this->_setParam($field, $value);
124 if (!empty($params['is_recur']) && !empty($params['contributionRecurID'])) {
125 $result = $this->doRecurPayment();
126 if (is_a($result, 'CRM_Core_Error')) {
132 $postFields = array();
133 $authorizeNetFields = $this->_getAuthorizeNetFields();
135 // Set up our call for hook_civicrm_paymentProcessor,
136 // since we now have our parameters as assigned for the AIM back end.
137 CRM_Utils_Hook
::alterPaymentProcessorParams($this,
142 foreach ($authorizeNetFields as $field => $value) {
143 // CRM-7419, since double quote is used as enclosure while doing csv parsing
144 $value = ($field == 'x_description') ?
str_replace('"', "'", $value) : $value;
145 $postFields[] = $field . '=' . urlencode($value);
148 // Authorize.Net will not refuse duplicates, so we should check if the user already submitted this transaction
149 if ($this->_checkDupe($authorizeNetFields['x_invoice_num'])) {
150 return self
::error(9004, 'It appears that this transaction is a duplicate. Have you already submitted the form once? If so there may have been a connection problem. Check your email for a receipt from Authorize.net. If you do not receive a receipt within 2 hours you can try your transaction again. If you continue to have problems please contact the site administrator.');
153 $submit = curl_init($this->_paymentProcessor
['url_site']);
156 return self
::error(9002, 'Could not initiate connection to payment gateway');
159 curl_setopt($submit, CURLOPT_POST
, TRUE);
160 curl_setopt($submit, CURLOPT_RETURNTRANSFER
, TRUE);
161 curl_setopt($submit, CURLOPT_POSTFIELDS
, implode('&', $postFields));
162 curl_setopt($submit, CURLOPT_SSL_VERIFYPEER
, CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'verifySSL'));
164 $response = curl_exec($submit);
167 return self
::error(curl_errno($submit), curl_error($submit));
172 $response_fields = $this->explode_csv($response);
174 // check gateway MD5 response
175 if (!$this->checkMD5($response_fields[37], $response_fields[6], $response_fields[9])) {
176 return self
::error(9003, 'MD5 Verification failed');
179 // check for application errors
181 // AVS, CVV2, CAVV, and other verification results
182 if ($response_fields[0] != self
::AUTH_APPROVED
) {
183 $errormsg = $response_fields[2] . ' ' . $response_fields[3];
184 return self
::error($response_fields[1], $errormsg);
189 // test mode always returns trxn_id = 0
190 // also live mode in CiviCRM with test mode set in
191 // Authorize.Net return $response_fields[6] = 0
192 // hence treat that also as test mode transaction
194 if (($this->_mode
== 'test') ||
$response_fields[6] == 0) {
195 $query = "SELECT MAX(trxn_id) FROM civicrm_contribution WHERE trxn_id RLIKE 'test[0-9]+'";
197 $trxn_id = strval(CRM_Core_DAO
::singleValueQuery($query, $p));
198 $trxn_id = str_replace('test', '', $trxn_id);
199 $trxn_id = intval($trxn_id) +
1;
200 $params['trxn_id'] = sprintf('test%08d', $trxn_id);
203 $params['trxn_id'] = $response_fields[6];
205 $params['gross_amount'] = $response_fields[9];
206 // TODO: include authorization code?
212 * Submit an Automated Recurring Billing subscription
216 function doRecurPayment() {
217 $template = CRM_Core_Smarty
::singleton();
219 $intervalLength = $this->_getParam('frequency_interval');
220 $intervalUnit = $this->_getParam('frequency_unit');
221 if ($intervalUnit == 'week') {
222 $intervalLength *= 7;
223 $intervalUnit = 'days';
225 elseif ($intervalUnit == 'year') {
226 $intervalLength *= 12;
227 $intervalUnit = 'months';
229 elseif ($intervalUnit == 'day') {
230 $intervalUnit = 'days';
232 elseif ($intervalUnit == 'month') {
233 $intervalUnit = 'months';
236 // interval cannot be less than 7 days or more than 1 year
237 if ($intervalUnit == 'days') {
238 if ($intervalLength < 7) {
239 return self
::error(9001, 'Payment interval must be at least one week');
241 elseif ($intervalLength > 365) {
242 return self
::error(9001, 'Payment interval may not be longer than one year');
245 elseif ($intervalUnit == 'months') {
246 if ($intervalLength < 1) {
247 return self
::error(9001, 'Payment interval must be at least one week');
249 elseif ($intervalLength > 12) {
250 return self
::error(9001, 'Payment interval may not be longer than one year');
254 $template->assign('intervalLength', $intervalLength);
255 $template->assign('intervalUnit', $intervalUnit);
257 $template->assign('apiLogin', $this->_getParam('apiLogin'));
258 $template->assign('paymentKey', $this->_getParam('paymentKey'));
259 $template->assign('refId', substr($this->_getParam('invoiceID'), 0, 20));
261 //for recurring, carry first contribution id
262 $template->assign('invoiceNumber', $this->_getParam('contributionID'));
263 $firstPaymentDate = $this->_getParam('receive_date');
264 if (!empty($firstPaymentDate)) {
265 //allow for post dated payment if set in form
266 $startDate = date_create($firstPaymentDate);
269 $startDate = date_create();
271 /* Format start date in Mountain Time to avoid Authorize.net error E00017
272 * we do this only if the day we are setting our start time to is LESS than the current
273 * day in mountaintime (ie. the server time of the A-net server). A.net won't accept a date
274 * earlier than the current date on it's server so if we are in PST we might need to use mountain
275 * time to bring our date forward. But if we are submitting something future dated we want
276 * the date we entered to be respected
278 $minDate = date_create('now', new DateTimeZone(self
::TIMEZONE
));
279 if(strtotime($startDate->format('Y-m-d')) < strtotime($minDate->format('Y-m-d'))){
280 $startDate->setTimezone(new DateTimeZone(self
::TIMEZONE
));
283 $template->assign( 'startDate', $startDate->format('Y-m-d') );
285 $installments = $this->_getParam('installments');
287 // for open ended subscription totalOccurrences has to be 9999
288 $installments = empty($installments) ?
9999 : $installments;
289 $template->assign('totalOccurrences', $installments);
291 $template->assign('amount', $this->_getParam('amount'));
293 $template->assign('cardNumber', $this->_getParam('credit_card_number'));
294 $exp_month = str_pad($this->_getParam('month'), 2, '0', STR_PAD_LEFT
);
295 $exp_year = $this->_getParam('year');
296 $template->assign('expirationDate', $exp_year . '-' . $exp_month);
298 // name rather than description is used in the tpl - see http://www.authorize.net/support/ARB_guide.pdf
299 $template->assign('name', $this->_getParam('description', TRUE));
301 $template->assign('email', $this->_getParam('email'));
302 $template->assign('contactID', $this->_getParam('contactID'));
303 $template->assign('billingFirstName', $this->_getParam('billing_first_name'));
304 $template->assign('billingLastName', $this->_getParam('billing_last_name'));
305 $template->assign('billingAddress', $this->_getParam('street_address', TRUE));
306 $template->assign('billingCity', $this->_getParam('city', TRUE));
307 $template->assign('billingState', $this->_getParam('state_province'));
308 $template->assign('billingZip', $this->_getParam('postal_code', TRUE));
309 $template->assign('billingCountry', $this->_getParam('country'));
311 $arbXML = $template->fetch('CRM/Contribute/Form/Contribution/AuthorizeNetARB.tpl');
312 // submit to authorize.net
314 $submit = curl_init($this->_paymentProcessor
['url_recur']);
316 return self
::error(9002, 'Could not initiate connection to payment gateway');
318 curl_setopt($submit, CURLOPT_RETURNTRANSFER
, 1);
319 curl_setopt($submit, CURLOPT_HTTPHEADER
, array("Content-Type: text/xml"));
320 curl_setopt($submit, CURLOPT_HEADER
, 1);
321 curl_setopt($submit, CURLOPT_POSTFIELDS
, $arbXML);
322 curl_setopt($submit, CURLOPT_POST
, 1);
323 curl_setopt($submit, CURLOPT_SSL_VERIFYPEER
, CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'verifySSL'));
325 $response = curl_exec($submit);
328 return self
::error(curl_errno($submit), curl_error($submit));
332 $responseFields = $this->_ParseArbReturn($response);
334 if ($responseFields['resultCode'] == 'Error') {
335 return self
::error($responseFields['code'], $responseFields['text']);
338 // update recur processor_id with subscriptionId
339 CRM_Core_DAO
::setFieldValue('CRM_Contribute_DAO_ContributionRecur', $this->_getParam('contributionRecurID'),
340 'processor_id', $responseFields['subscriptionId']
342 //only impact of assigning this here is is can be used to cancel the subscription in an automated test
343 // if it isn't cancelled a duplicate transaction error occurs
344 if (!empty($responseFields['subscriptionId'])) {
345 $this->_setParam('subscriptionId', $responseFields['subscriptionId']);
352 function _getAuthorizeNetFields() {
353 $amount = $this->_getParam('total_amount');//Total amount is from the form contribution field
354 if(empty($amount)){//CRM-9894 would this ever be the case??
355 $amount = $this->_getParam('amount');
358 $fields['x_login'] = $this->_getParam('apiLogin');
359 $fields['x_tran_key'] = $this->_getParam('paymentKey');
360 $fields['x_email_customer'] = $this->_getParam('emailCustomer');
361 $fields['x_first_name'] = $this->_getParam('billing_first_name');
362 $fields['x_last_name'] = $this->_getParam('billing_last_name');
363 $fields['x_address'] = $this->_getParam('street_address');
364 $fields['x_city'] = $this->_getParam('city');
365 $fields['x_state'] = $this->_getParam('state_province');
366 $fields['x_zip'] = $this->_getParam('postal_code');
367 $fields['x_country'] = $this->_getParam('country');
368 $fields['x_customer_ip'] = $this->_getParam('ip_address');
369 $fields['x_email'] = $this->_getParam('email');
370 $fields['x_invoice_num'] = substr($this->_getParam('invoiceID'), 0, 20);
371 $fields['x_amount'] = $amount;
372 $fields['x_currency_code'] = $this->_getParam('currencyID');
373 $fields['x_description'] = $this->_getParam('description');
374 $fields['x_cust_id'] = $this->_getParam('contactID');
375 if ($this->_getParam('paymentType') == 'AIM') {
376 $fields['x_relay_response'] = 'FALSE';
377 // request response in CSV format
378 $fields['x_delim_data'] = 'TRUE';
379 $fields['x_delim_char'] = ',';
380 $fields['x_encap_char'] = '"';
382 $fields['x_card_num'] = $this->_getParam('credit_card_number');
383 $fields['x_card_code'] = $this->_getParam('cvv2');
384 $exp_month = str_pad($this->_getParam('month'), 2, '0', STR_PAD_LEFT
);
385 $exp_year = $this->_getParam('year');
386 $fields['x_exp_date'] = "$exp_month/$exp_year";
389 if ($this->_mode
!= 'live') {
390 $fields['x_test_request'] = 'TRUE';
397 * Checks to see if invoice_id already exists in db
399 * @param int $invoiceId The ID to check
401 * @return bool True if ID exists, else false
403 function _checkDupe($invoiceId) {
404 $contribution = new CRM_Contribute_DAO_Contribution();
405 $contribution->invoice_id
= $invoiceId;
406 return $contribution->find();
413 * @param string $data
415 * @return string the HMAC_MD5 encoding string
417 function hmac($key, $data) {
418 if (function_exists('mhash')) {
419 // Use PHP mhash extension
420 return (bin2hex(mhash(MHASH_MD5
, $data, $key)));
423 // RFC 2104 HMAC implementation for php.
424 // Creates an md5 HMAC.
425 // Eliminates the need to install mhash to compute a HMAC
426 // Hacked by Lance Rushing
427 // byte length for md5
429 if (strlen($key) > $b) {
430 $key = pack("H*", md5($key));
432 $key = str_pad($key, $b, chr(0x00));
433 $ipad = str_pad('', $b, chr(0x36));
434 $opad = str_pad('', $b, chr(0x5c));
435 $k_ipad = $key ^
$ipad;
436 $k_opad = $key ^
$opad;
437 return md5($k_opad . pack("H*", md5($k_ipad . $data)));
442 * Check the gateway MD5 response to make sure that this is a proper
445 * @param string $responseMD5 MD5 hash generated by the gateway
446 * @param string $transaction_id Transaction id generated by the gateway
447 * @param string $amount Purchase amount
453 function checkMD5($responseMD5, $transaction_id, $amount, $ipn = FALSE) {
454 // cannot check if no MD5 hash
455 $md5Hash = $this->_getParam('md5Hash');
456 if (empty($md5Hash)) {
459 $loginid = $this->_getParam('apiLogin');
460 $hashString = $ipn ?
($md5Hash . $transaction_id . $amount) : ($md5Hash . $loginid . $transaction_id . $amount);
461 $result = strtoupper(md5($hashString));
463 if ($result == $responseMD5) {
472 * Calculate and return the transaction fingerprint
474 * @return string fingerprint
476 function CalculateFP() {
477 $x_tran_key = $this->_getParam('paymentKey');
478 $loginid = $this->_getParam('apiLogin');
479 $sequence = $this->_getParam('sequence');
480 $timestamp = $this->_getParam('timestamp');
481 $amount = $this->_getParam('amount');
482 $currency = $this->_getParam('currencyID');
483 $transaction = "$loginid^$sequence^$timestamp^$amount^$currency";
484 return $this->hmac($x_tran_key, $transaction);
488 * Split a CSV file. Requires , as delimiter and " as enclosure.
489 * Based off notes from http://php.net/fgetcsv
491 * @param string $data a single CSV line
493 * @return array CSV fields
495 function explode_csv($data) {
497 //make it easier to parse fields with quotes in them
498 $data = str_replace('""', "''", $data);
501 while ($data != '') {
503 if ($data[0] == '"') {
504 // handle quoted fields
505 preg_match('/^"(([^"]|\\")*?)",?(.*)$/', $data, $matches);
507 $fields[] = str_replace("''", '"', $matches[1]);
511 preg_match('/^([^,]*),?(.*)$/', $data, $matches);
513 $fields[] = $matches[1];
521 * Extract variables from returned XML
523 * Function is from Authorize.Net sample code, and used
524 * to prevent the requirement of XML functions.
526 * @param string $content XML reply from Authorize.Net
528 * @return array refId, resultCode, code, text, subscriptionId
530 function _parseArbReturn($content) {
531 $refId = $this->_substring_between($content, '<refId>', '</refId>');
532 $resultCode = $this->_substring_between($content, '<resultCode>', '</resultCode>');
533 $code = $this->_substring_between($content, '<code>', '</code>');
534 $text = $this->_substring_between($content, '<text>', '</text>');
535 $subscriptionId = $this->_substring_between($content, '<subscriptionId>', '</subscriptionId>');
538 'resultCode' => $resultCode,
541 'subscriptionId' => $subscriptionId,
546 * Helper function for _parseArbReturn
548 * Function is from Authorize.Net sample code, and used to avoid using
551 function _substring_between(&$haystack, $start, $end) {
552 if (strpos($haystack, $start) === FALSE ||
strpos($haystack, $end) === FALSE) {
556 $start_position = strpos($haystack, $start) +
strlen($start);
557 $end_position = strpos($haystack, $end);
558 return substr($haystack, $start_position, $end_position - $start_position);
563 * Get the value of a field if set
565 * @param string $field the field
567 * @param bool $xmlSafe
568 * @return mixed value of the field, or empty string if the field is
571 function _getParam($field, $xmlSafe = FALSE) {
572 $value = CRM_Utils_Array
::value($field, $this->_params
, '');
574 $value = str_replace(array( '&', '"', "'", '<', '>' ), '', $value);
580 * @param null $errorCode
581 * @param null $errorMessage
585 function &error($errorCode = NULL, $errorMessage = NULL) {
586 $e = CRM_Core_Error
::singleton();
588 $e->push($errorCode, 0, array( ), $errorMessage);
591 $e->push(9001, 0, array( ), 'Unknown System Error.');
597 * Set a field to the specified value. Value must be a scalar (int,
598 * float, string, or boolean)
600 * @param string $field
601 * @param mixed $value
603 * @return bool false if value is not a scalar, true if successful
605 function _setParam($field, $value) {
606 if (!is_scalar($value)) {
610 $this->_params
[$field] = $value;
615 * This function checks to see if we have the right config values
617 * @return string the error message if any
620 function checkConfig() {
622 if (empty($this->_paymentProcessor
['user_name'])) {
623 $error[] = ts('APILogin is not set for this payment processor');
626 if (empty($this->_paymentProcessor
['password'])) {
627 $error[] = ts('Key is not set for this payment processor');
630 if (!empty($error)) {
631 return implode('<p>', $error);
641 function accountLoginURL() {
642 return ($this->_mode
== 'test') ?
'https://test.authorize.net' : 'https://authorize.net';
646 * @param string $message
647 * @param array $params
649 * @return bool|object
651 function cancelSubscription(&$message = '', $params = array()) {
652 $template = CRM_Core_Smarty
::singleton();
654 $template->assign('subscriptionType', 'cancel');
656 $template->assign('apiLogin', $this->_getParam('apiLogin'));
657 $template->assign('paymentKey', $this->_getParam('paymentKey'));
658 $template->assign('subscriptionId', CRM_Utils_Array
::value('subscriptionId', $params));
660 $arbXML = $template->fetch('CRM/Contribute/Form/Contribution/AuthorizeNetARB.tpl');
662 // submit to authorize.net
663 $submit = curl_init($this->_paymentProcessor
['url_recur']);
665 return self
::error(9002, 'Could not initiate connection to payment gateway');
668 curl_setopt($submit, CURLOPT_RETURNTRANSFER
, 1);
669 curl_setopt($submit, CURLOPT_HTTPHEADER
, array("Content-Type: text/xml"));
670 curl_setopt($submit, CURLOPT_HEADER
, 1);
671 curl_setopt($submit, CURLOPT_POSTFIELDS
, $arbXML);
672 curl_setopt($submit, CURLOPT_POST
, 1);
673 curl_setopt($submit, CURLOPT_SSL_VERIFYPEER
, CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'verifySSL'));
675 $response = curl_exec($submit);
678 return self
::error(curl_errno($submit), curl_error($submit));
683 $responseFields = $this->_ParseArbReturn($response);
684 $message = "{$responseFields['code']}: {$responseFields['text']}";
686 if ($responseFields['resultCode'] == 'Error') {
687 return self
::error($responseFields['code'], $responseFields['text']);
693 * @param string $message
694 * @param array $params
696 * @return bool|object
698 function updateSubscriptionBillingInfo(&$message = '', $params = array()) {
699 $template = CRM_Core_Smarty
::singleton();
700 $template->assign('subscriptionType', 'updateBilling');
702 $template->assign('apiLogin', $this->_getParam('apiLogin'));
703 $template->assign('paymentKey', $this->_getParam('paymentKey'));
704 $template->assign('subscriptionId', $params['subscriptionId']);
706 $template->assign('cardNumber', $params['credit_card_number']);
707 $exp_month = str_pad($params['month'], 2, '0', STR_PAD_LEFT
);
708 $exp_year = $params['year'];
709 $template->assign('expirationDate', $exp_year . '-' . $exp_month);
711 $template->assign('billingFirstName', $params['first_name']);
712 $template->assign('billingLastName', $params['last_name']);
713 $template->assign('billingAddress', $params['street_address']);
714 $template->assign('billingCity', $params['city']);
715 $template->assign('billingState', $params['state_province']);
716 $template->assign('billingZip', $params['postal_code']);
717 $template->assign('billingCountry', $params['country']);
719 $arbXML = $template->fetch('CRM/Contribute/Form/Contribution/AuthorizeNetARB.tpl');
721 // submit to authorize.net
722 $submit = curl_init($this->_paymentProcessor
['url_recur']);
724 return self
::error(9002, 'Could not initiate connection to payment gateway');
727 curl_setopt($submit, CURLOPT_RETURNTRANSFER
, 1);
728 curl_setopt($submit, CURLOPT_HTTPHEADER
, array("Content-Type: text/xml"));
729 curl_setopt($submit, CURLOPT_HEADER
, 1);
730 curl_setopt($submit, CURLOPT_POSTFIELDS
, $arbXML);
731 curl_setopt($submit, CURLOPT_POST
, 1);
732 curl_setopt($submit, CURLOPT_SSL_VERIFYPEER
, CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'verifySSL'));
734 $response = curl_exec($submit);
737 return self
::error(curl_errno($submit), curl_error($submit));
742 $responseFields = $this->_ParseArbReturn($response);
743 $message = "{$responseFields['code']}: {$responseFields['text']}";
745 if ($responseFields['resultCode'] == 'Error') {
746 return self
::error($responseFields['code'], $responseFields['text']);
752 * @param string $message
753 * @param array $params
755 * @return bool|object
757 function changeSubscriptionAmount(&$message = '', $params = array()) {
758 $template = CRM_Core_Smarty
::singleton();
760 $template->assign('subscriptionType', 'update');
762 $template->assign('apiLogin', $this->_getParam('apiLogin'));
763 $template->assign('paymentKey', $this->_getParam('paymentKey'));
765 $template->assign('subscriptionId', $params['subscriptionId']);
767 // for open ended subscription totalOccurrences has to be 9999
768 $installments = empty($params['installments']) ?
9999 : $params['installments'];
769 $template->assign('totalOccurrences', $installments);
771 $template->assign('amount', $params['amount']);
773 $arbXML = $template->fetch('CRM/Contribute/Form/Contribution/AuthorizeNetARB.tpl');
775 // submit to authorize.net
776 $submit = curl_init($this->_paymentProcessor
['url_recur']);
778 return self
::error(9002, 'Could not initiate connection to payment gateway');
781 curl_setopt($submit, CURLOPT_RETURNTRANSFER
, 1);
782 curl_setopt($submit, CURLOPT_HTTPHEADER
, array("Content-Type: text/xml"));
783 curl_setopt($submit, CURLOPT_HEADER
, 1);
784 curl_setopt($submit, CURLOPT_POSTFIELDS
, $arbXML);
785 curl_setopt($submit, CURLOPT_POST
, 1);
786 curl_setopt($submit, CURLOPT_SSL_VERIFYPEER
, CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
, 'verifySSL'));
788 $response = curl_exec($submit);
791 return self
::error(curl_errno($submit), curl_error($submit));
796 $responseFields = $this->_ParseArbReturn($response);
797 $message = "{$responseFields['code']}: {$responseFields['text']}";
799 if ($responseFields['resultCode'] == 'Error') {
800 return self
::error($responseFields['code'], $responseFields['text']);