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