a6e8d4595f8ca3e10f60a5c8eea6b204dfd2ae8f
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the Affero General Public License Version 1, |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the Affero General Public License for more details. |
19 | You should have received a copy of the Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2014
36 class CRM_Core_Payment_IATS
extends CRM_Core_Payment
{
37 # (not used, implicit in the API, might need to convert?)
38 CONST CHARSET
= 'UFT-8';
39 /* check IATS website for additional supported currencies */
40 CONST CURRENCIES
= 'CAD,USD,AUD,GBP,EUR,NZD';
43 * We only need one instance of this object. So we use the singleton
44 * pattern and cache the instance in this variable
49 static private $_singleton = NULL;
54 * @param string $mode the mode of operation: live or test
56 * @param $paymentProcessor
58 * @return \CRM_Core_Payment_IATS
60 function __construct($mode, &$paymentProcessor) {
61 $this->_paymentProcessor
= $paymentProcessor;
62 $this->_processorName
= ts('IATS');
64 // get merchant data from config
65 $config = CRM_Core_Config
::singleton();
67 $this->_profile
['mode'] = $mode;
68 $this->_profile
['webserver'] = parse_url($this->_paymentProcessor
['url_site'], PHP_URL_HOST
);
69 $currencyID = $config->defaultCurrency
;
71 if (!in_array($currencyID, explode(',', self
::CURRENCIES
))) {
72 // Configuration error: default currency must be in CURRENCIES const
73 return self
::error('Invalid configuration:' . $currencyID . ', you must use one of ' . self
::CURRENCIES
. ' with IATS');
79 * @param array $paymentProcessor
83 static function &singleton($mode, &$paymentProcessor) {
84 $processorName = $paymentProcessor['name'];
85 if (self
::$_singleton[$processorName] === NULL) {
86 self
::$_singleton[$processorName] = new CRM_Core_Payment_IATS($mode, $paymentProcessor);
88 return self
::$_singleton[$processorName];
92 * This function collects all the information from a web/api form and invokes
93 * the relevant payment processor specific functions to perform the transaction
95 * @param array $params assoc array of input parameters for this transaction
97 * @return array the result in an nice formatted array (or an error object)
100 function doDirectPayment(&$params) {
102 // foreach($params as $key => $value) {
103 // $result .= "<strong>$key</strong>: $value<br />";
105 // return self::error($result);
106 // make sure i've been called correctly ...
108 if (!$this->_profile
) {
109 return self
::error('Unexpected error, missing profile');
111 if (!in_array($params['currencyID'], explode(',', self
::CURRENCIES
))) {
112 return self
::error('Invalid currency selection, must be one of ' . self
::CURRENCIES
);
114 $isRecur = CRM_Utils_Array
::value('is_recur', $params, FALSE);
115 // AgentCode = $this->_paymentProcessor['signature'];
116 // Password = $this->_paymentProcessor['password' ];
117 // beginning of modified sample code from IATS php api include IATS supplied api library
120 include_once ('Services/IATS/iats_reoccur.php');
121 $iatslink1 = new iatslinkReoccur
;
124 include_once ('Services/IATS/iatslink.php');
125 $iatslink1 = new iatslink
;
128 $iatslink1->setTestMode($this->_profile
['mode'] != 'live');
129 $iatslink1->setWebServer($this->_profile
['webserver']);
131 // return self::error($this->_profile['webserver']);
133 // Put your invoice here
134 $iatslink1->setInvoiceNumber($params['invoiceID']);
136 // $iatslink1->setCardType("VISA");
137 // If CardType is not set, iatslink will find the cardType
138 // CardType not set because IATS uses different names!
139 // $iatslink1->setCardType($params['credit_card_type']);
141 $iatslink1->setCardNumber($params['credit_card_number']);
142 $expiry_string = sprintf('%02d/%02d', $params['month'], ($params['year'] %
100));
143 $iatslink1->setCardExpiry($expiry_string);
144 $amount = sprintf('%01.2f', $params['amount']);
146 $iatslink1->setDollarAmount($amount);
148 //$iatslink1->setDollarAmount(-1.15);
150 $AgentCode = $this->_paymentProcessor
['signature'];
151 $Password = $this->_paymentProcessor
['password'];
152 $iatslink1->setAgentCode($AgentCode);
153 $iatslink1->setPassword($Password);
154 // send IATS my invoiceID to match things up later
155 $iatslink1->setInvoiceNumber($params['invoiceID']);
157 // Set billing fields
158 $iatslink1->setFirstName($params['billing_first_name']);
159 $iatslink1->setLastName($params['billing_last_name']);
160 $iatslink1->setStreetAddress($params['street_address']);
161 $iatslink1->setCity($params['city']);
162 $iatslink1->setState($params['state_province']);
163 $iatslink1->setZipCode($params['postal_code']);
164 // and now go! ... uses curl to post and retrieve values
165 // after various data integrity tests
168 // cvv2 only seems to get set for this!
169 $iatslink1->setCVV2($params['cvv2']);
171 // Allow further manipulation of the arguments via custom hooks,
172 // before initiating processCreditCard()
173 CRM_Utils_Hook
::alterPaymentProcessorParams($this, $params, $iatslink1);
175 $iatslink1->processCreditCard();
176 // extra fields for recurring donations
179 // implicit - test?: 1 == $params['frequency_interval'];
180 $scheduleType = NULL;
181 if ($params['installments']) {
182 $paymentsRecur = $params['installments'] - 1;
184 // handle unspecified installments by setting to 10 years, IATS doesn't allow indefinitely recurring contributions
186 switch ($params['frequency_unit']) {
188 $paymentsRecur = 520;
190 $paymentsRecur = 120;
193 // IATS requires end date, calculated here
195 // to be converted to date format later
197 $date = getdate($startTime);
199 switch ($params['frequency_unit']) {
201 $scheduleType = 'WEEKLY';
202 $scheduleDate = $date['wday'] +
1;
203 $endTime = $startTime +
($paymentsRecur * 7 * 24 * 60 * 60);
207 $scheduleType = 'MONTHLY';
208 $scheduleDate = $date['mday'];
209 $date['mon'] +
= $paymentsRecur;
210 while ($date['mon'] > 12) {
214 $endTime = mktime($date['hours'], $date['minutes'], $date['seconds'], $date['mon'], $date['mday'], $date['year']);
218 die('Invalid frequency unit!');
221 $endDate = date('Y-m-d', $endTime);
222 $startDate = date('Y-m-d', $startTime);
223 $iatslink1->setReoccuringStatus("ON");
224 $iatslink1->setBeginDate($startDate);
225 $iatslink1->setEndDate($endDate);
226 $iatslink1->setScheduleType($scheduleType);
227 $iatslink1->setScheduleDate($scheduleDate);
229 // Allow further manipulation of the arguments via custom hooks,
230 // before initiating the curl process
231 CRM_Utils_Hook
::alterPaymentProcessorParams($this, $params, $iatslink1);
233 // this next line is the reoccc equiv of processCreditCard
234 $iatslink1->createReoccCustomer();
237 if ($iatslink1->getStatus() == 1) {
238 // this just means we got some kind of answer, not necessarily approved
239 $result = $iatslink1->getAuthorizationResult();
240 //return self::error($result);
241 $result = explode(':', $result, 2);
242 $trxn_result = trim($result[0]);
243 $trxn_id = trim($result[1]);
244 if ($trxn_result == 'OK') {
245 $params['trxn_id'] = $trxn_id . ':' . time();
246 $params['gross_amount'] = $amount;
249 // createReoccCustomer() may return other, valid result codes...
250 elseif (preg_match('/A\d+/', $trxn_result)) {
251 $params['trxn_id'] = $trxn_result;
252 $params['gross_amount'] = $amount;
256 return self
::error($trxn_id);
260 return self
::error($iatslink1->getError());
269 function &error($error = NULL) {
270 $e = CRM_Core_Error
::singleton();
271 if (is_object($error)) {
272 $e->push($error->getResponseCode(),
277 elseif ($error && is_numeric($error)) {
280 $this->errorString($error)
283 elseif (is_string($error)) {
290 $e->push(9001, 0, NULL, "Unknown System Error.");
300 function errorString($error_id) {
302 1 => 'Agent Code has not been set up on the authorization system.',
303 2 => 'Unable to process transaction. Verify and re-enter credit card information.',
304 3 => 'Charge card expired.',
305 4 => 'Incorrect expiration date.',
306 5 => 'Invalid transaction. Verify and re-enter credit card information.',
307 6 => 'Transaction not supported by institution.',
308 7 => 'Lost or stolen card.',
309 8 => 'Invalid card status.',
310 9 => 'Restricted card status. Usually on corporate cards restricted to specific sales.',
311 10 => 'Error. Please verify and re-enter credit card information.',
312 11 => 'General decline code, may have different reasons for each card type. Please have your client call customer service.',
313 14 => 'This means that the credit card is over the limit.',
314 15 => 'Decline code, may have different reasons for each card type. Please have your client call customer service.',
315 16 => 'Invalid charge card number. Verify and re-enter credit card information.',
316 17 => 'Unable to authorize transaction. Verify card information with customer and re-enter. Could be invalid name or expiry date.',
317 18 => 'Card not supported by institution.',
318 19 => 'Incorrect CVV2.',
319 22 => 'Bank Timeout. Bank lines may be down or busy. Re-try transaction later.',
320 23 => 'System error. Re-try transaction later.',
321 24 => 'Charge card expired.',
322 25 => 'Capture card. Reported lost or stolen.',
323 27 => 'System error, please re-enter transaction.',
324 29 => 'Rejected by Ticketmaster.',
325 31 => 'Manual reject code ',
326 39 => 'Contact Ticketmaster 1-888-955-5455 ',
327 40 => 'Card not supported by Ticketmaster. Invalid cc number.',
328 41 => 'Invalid Expiry date ',
329 100 => 'Authorization system down. DO NOT REPROCESS.',
331 return ' <strong>' . $errors[(integer) $error_id] . '</strong>';
335 * This function checks to see if we have the right config values
337 * @internal param string $mode the mode we are operating in (live or test)
339 * @return string the error message if any
342 function checkConfig() {
345 if (empty($this->_paymentProcessor
['signature'])) {
346 $error[] = ts('Agent Code is not set in the Administer CiviCRM » System Settings » Payment Processors.');
349 if (empty($this->_paymentProcessor
['password'])) {
350 $error[] = ts('Password is not set in the Administer CiviCRM » System Settings » Payment Processors.');
353 if (!empty($error)) {
354 return implode('<p>', $error);