Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
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 GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU 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 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @author Alan Dixon | |
06b69b18 | 32 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
33 | * $Id$ |
34 | * | |
35 | */ | |
36 | class CRM_Core_Payment_Moneris extends CRM_Core_Payment { | |
37 | # (not used, implicit in the API, might need to convert?) | |
38 | CONST CHARSET = 'UFT-8'; | |
39 | ||
40 | /** | |
41 | * We only need one instance of this object. So we use the singleton | |
42 | * pattern and cache the instance in this variable | |
43 | * | |
44 | * @var object | |
45 | * @static | |
46 | */ | |
47 | static private $_singleton = NULL; | |
48 | ||
49 | /** | |
50 | * Constructor | |
51 | * | |
52 | * @param string $mode the mode of operation: live or test | |
53 | * | |
54 | * @return void | |
55 | */ | |
56 | function __construct($mode, &$paymentProcessor) { | |
57 | $this->_mode = $mode; | |
58 | $this->_paymentProcessor = $paymentProcessor; | |
59 | $this->_processorName = ts('Moneris'); | |
60 | ||
61 | // require moneris supplied api library | |
62 | if ((include_once 'Services/mpgClasses.php') === FALSE) { | |
63 | CRM_Core_Error::fatal(ts('Please download and put the Moneris mpgClasses.php file in packages/Services directory to enable Moneris Support.')); | |
64 | } | |
65 | ||
66 | // get merchant data from config | |
67 | $config = CRM_Core_Config::singleton(); | |
68 | // live or test | |
69 | $this->_profile['mode'] = $mode; | |
70 | $this->_profile['storeid'] = $this->_paymentProcessor['signature']; | |
71 | $this->_profile['apitoken'] = $this->_paymentProcessor['password']; | |
72 | $currencyID = $config->defaultCurrency; | |
73 | if ('CAD' != $currencyID) { | |
74 | return self::error('Invalid configuration:' . $currencyID . ', you must use currency $CAD with Moneris'); | |
75 | // Configuration error: default currency must be CAD | |
76 | } | |
77 | } | |
78 | ||
79 | /** | |
80 | * singleton function used to manage this object | |
81 | * | |
82 | * @param string $mode the mode of operation: live or test | |
83 | * | |
84 | * @return object | |
85 | * @static | |
86 | * | |
87 | */ | |
88 | static function &singleton($mode, &$paymentProcessor) { | |
89 | $processorName = $paymentProcessor['name']; | |
90 | if (self::$_singleton[$processorName] === NULL) { | |
91 | self::$_singleton[$processorName] = new CRM_Core_Payment_Moneris($mode, $paymentProcessor); | |
92 | } | |
93 | return self::$_singleton[$processorName]; | |
94 | } | |
95 | ||
96 | function doDirectPayment(&$params) { | |
97 | //make sure i've been called correctly ... | |
98 | if (!$this->_profile) { | |
99 | return self::error('Unexpected error, missing profile'); | |
100 | } | |
101 | if ($params['currencyID'] != 'CAD') { | |
102 | return self::error('Invalid currency selection, must be $CAD'); | |
103 | } | |
104 | /* unused params: cvv not yet implemented, payment action ingored (should test for 'Sale' value?) | |
105 | [cvv2] => 000 | |
106 | [ip_address] => 192.168.0.103 | |
107 | [payment_action] => Sale | |
108 | [contact_type] => Individual | |
109 | [geo_coord_id] => 1 */ | |
110 | ||
111 | //this code based on Moneris example code # | |
112 | //create an mpgCustInfo object | |
113 | $mpgCustInfo = new mpgCustInfo(); | |
114 | //call set methods of the mpgCustinfo object | |
115 | $mpgCustInfo->setEmail($params['email']); | |
116 | //get text representations of province/country to send to moneris for billing info | |
117 | ||
118 | $billing = array( | |
119 | 'first_name' => $params['first_name'], | |
120 | 'last_name' => $params['last_name'], | |
121 | 'address' => $params['street_address'], | |
122 | 'city' => $params['city'], | |
123 | 'province' => $params['state_province'], | |
124 | 'postal_code' => $params['postal_code'], | |
125 | 'country' => $params['country'], | |
126 | ); | |
127 | $mpgCustInfo->setBilling($billing); | |
128 | // set orderid as invoiceID to help match things up with Moneris later | |
129 | $my_orderid = $params['invoiceID']; | |
130 | $expiry_string = sprintf('%04d%02d', $params['year'], $params['month']); | |
131 | ||
132 | $txnArray = array( | |
133 | 'type' => 'purchase', | |
134 | 'order_id' => $my_orderid, | |
135 | 'amount' => sprintf('%01.2f', $params['amount']), | |
136 | 'pan' => $params['credit_card_number'], | |
137 | 'expdate' => substr($expiry_string, 2, 4), | |
138 | 'crypt_type' => '7', | |
139 | 'cust_id' => $params['contactID'], | |
140 | ); | |
141 | ||
142 | // Allow further manipulation of params via custom hooks | |
143 | CRM_Utils_Hook::alterPaymentProcessorParams($this, $params, $txnArray); | |
144 | ||
145 | //create a transaction object passing the hash created above | |
146 | $mpgTxn = new mpgTransaction($txnArray); | |
147 | ||
148 | //use the setCustInfo method of mpgTransaction object to | |
149 | //set the customer info (level 3 data) for this transaction | |
150 | $mpgTxn->setCustInfo($mpgCustInfo); | |
151 | // add a recurring payment if requested | |
152 | if ($params['is_recur'] && $params['installments'] > 1) { | |
153 | //Recur Variables | |
154 | $recurUnit = $params['frequency_unit']; | |
155 | $recurInterval = $params['frequency_interval']; | |
156 | $next = time(); | |
157 | $day = 60 * 60 * 24; | |
158 | switch ($recurUnit) { | |
159 | case 'day': | |
160 | $next += $recurInterval * $day; | |
161 | break; | |
162 | ||
163 | case 'week': | |
164 | $next += $recurInterval * $day * 7; | |
165 | break; | |
166 | ||
167 | case 'month': | |
168 | $date = getdate(); | |
169 | $date['mon'] += $recurInterval; | |
170 | while ($date['mon'] > 12) { | |
171 | $date['mon'] -= 12; | |
172 | $date['year'] += 1; | |
173 | } | |
174 | $next = mktime($date['hours'], $date['minutes'], $date['seconds'], $date['mon'], $date['mday'], $date['year']); | |
175 | break; | |
176 | ||
177 | case 'year': | |
178 | $date = getdate(); | |
179 | $date['year'] += 1; | |
180 | $next = mktime($date['hours'], $date['minutes'], $date['seconds'], $date['mon'], $date['mday'], $date['year']); | |
181 | break; | |
182 | ||
183 | default: | |
184 | die('Unexpected error!'); | |
185 | } | |
186 | // next payment in moneris required format | |
187 | $startDate = date("Y/m/d", $next); | |
188 | $numRecurs = $params['installments'] - 1; | |
189 | //$startNow = 'true'; -- setting start now to false will mean the main transaction doesn't happen! | |
190 | $recurAmount = sprintf('%01.2f', $params['amount']); | |
191 | //Create an array with the recur variables | |
192 | // (day | week | month) | |
193 | $recurArray = array( | |
194 | 'recur_unit' => $recurUnit, | |
195 | // yyyy/mm/dd | |
196 | 'start_date' => $startDate, | |
197 | 'num_recurs' => $numRecurs, | |
198 | 'start_now' => 'true', | |
199 | 'period' => $recurInterval, | |
200 | 'recur_amount' => $recurAmount, | |
201 | ); | |
202 | $mpgRecur = new mpgRecur($recurArray); | |
203 | // set the Recur Object to mpgRecur | |
204 | $mpgTxn->setRecur($mpgRecur); | |
205 | } | |
206 | //create a mpgRequest object passing the transaction object | |
207 | $mpgRequest = new mpgRequest($mpgTxn); | |
208 | ||
209 | // create mpgHttpsPost object which does an https post ## | |
210 | // [extra parameter added to library by AD] | |
211 | $isProduction = ($this->_profile['mode'] == 'live'); | |
212 | $mpgHttpPost = new mpgHttpsPost($this->_profile['storeid'], $this->_profile['apitoken'], $mpgRequest, $isProduction); | |
213 | // get an mpgResponse object | |
214 | $mpgResponse = $mpgHttpPost->getMpgResponse(); | |
215 | $params['trxn_result_code'] = $mpgResponse->getResponseCode(); | |
216 | if (self::isError($mpgResponse)) { | |
217 | if ($params['trxn_result_code']) { | |
218 | return self::error($mpgResponse); | |
219 | } | |
220 | else { | |
221 | return self::error('No reply from server - check your settings &/or try again'); | |
222 | } | |
223 | } | |
224 | /* Check for application errors */ | |
225 | ||
226 | $result = self::checkResult($mpgResponse); | |
227 | if (is_a($result, 'CRM_Core_Error')) { | |
228 | return $result; | |
229 | } | |
230 | ||
231 | /* Success */ | |
232 | ||
233 | $params['trxn_result_code'] = (integer) $mpgResponse->getResponseCode(); | |
234 | // todo: above assignment seems to be ignored, not getting stored in the civicrm_financial_trxn table | |
235 | $params['trxn_id'] = $mpgResponse->getTxnNumber(); | |
236 | $params['gross_amount'] = $mpgResponse->getTransAmount(); | |
237 | return $params; | |
238 | } | |
239 | ||
240 | function isError(&$response) { | |
241 | $responseCode = $response->getResponseCode(); | |
242 | if (is_null($responseCode)) { | |
243 | return TRUE; | |
244 | } | |
245 | if ('null' == $responseCode) { | |
246 | return TRUE; | |
247 | } | |
248 | if (($responseCode >= 0) && ($responseCode < 50)) { | |
249 | return FALSE; | |
250 | } | |
251 | return TRUE; | |
252 | } | |
253 | ||
254 | // ignore for now, more elaborate error handling later. | |
255 | function &checkResult(&$response) { | |
256 | return $response; | |
257 | ||
258 | $errors = $response->getErrors(); | |
259 | if (empty($errors)) { | |
260 | return $result; | |
261 | } | |
262 | ||
263 | $e = CRM_Core_Error::singleton(); | |
264 | if (is_a($errors, 'ErrorType')) { | |
265 | $e->push($errors->getErrorCode(), | |
266 | 0, NULL, | |
267 | $errors->getShortMessage() . ' ' . $errors->getLongMessage() | |
268 | ); | |
269 | } | |
270 | else { | |
271 | foreach ($errors as $error) { | |
272 | $e->push($error->getErrorCode(), | |
273 | 0, NULL, | |
274 | $error->getShortMessage() . ' ' . $error->getLongMessage() | |
275 | ); | |
276 | } | |
277 | } | |
278 | return $e; | |
279 | } | |
280 | ||
281 | function &error($error = NULL) { | |
282 | $e = CRM_Core_Error::singleton(); | |
283 | if (is_object($error)) { | |
284 | $e->push($error->getResponseCode(), | |
285 | 0, NULL, | |
286 | $error->getMessage() | |
287 | ); | |
288 | } | |
289 | elseif (is_string($error)) { | |
290 | $e->push(9002, | |
291 | 0, NULL, | |
292 | $error | |
293 | ); | |
294 | } | |
295 | else { | |
296 | $e->push(9001, 0, NULL, "Unknown System Error."); | |
297 | } | |
298 | return $e; | |
299 | } | |
300 | ||
301 | /** | |
302 | * This function checks to see if we have the right config values | |
303 | * | |
304 | * @return string the error message if any | |
305 | * @public | |
306 | */ | |
307 | function checkConfig() { | |
308 | $error = array(); | |
309 | ||
310 | if (empty($this->_paymentProcessor['signature'])) { | |
311 | $error[] = ts('Store ID is not set in the Administer CiviCRM » System Settings » Payment Processors.'); | |
312 | } | |
313 | ||
314 | if (empty($this->_paymentProcessor['password'])) { | |
315 | $error[] = ts('Password is not set in the Administer CiviCRM » System Settings » Payment Processors.'); | |
316 | } | |
317 | ||
318 | if (!empty($error)) { | |
319 | return implode('<p>', $error); | |
320 | } | |
321 | else { | |
322 | return NULL; | |
323 | } | |
324 | } | |
325 | } | |
326 |