-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiats.php
376 lines (351 loc) · 13.6 KB
/
iats.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
define('IATS_CIVICRM_NSCD_FID',_iats_civicrm_nscd_fid());
require_once 'iats.civix.php';
/**
* Implementation of hook_civicrm_config
*/
function iats_civicrm_config(&$config) {
_iats_civix_civicrm_config($config);
}
/**
* Implementation of hook_civicrm_xmlMenu
*
* @param $files array(string)
*/
function iats_civicrm_xmlMenu(&$files) {
_iats_civix_civicrm_xmlMenu($files);
}
/**
* Implementation of hook_civicrm_install
*/
function iats_civicrm_install() {
return _iats_civix_civicrm_install();
}
/**
* Implementation of hook_civicrm_uninstall
*/
function iats_civicrm_uninstall() {
return _iats_civix_civicrm_uninstall();
}
/**
* Implementation of hook_civicrm_enable
*/
function iats_civicrm_enable() {
return _iats_civix_civicrm_enable();
}
/**
* Implementation of hook_civicrm_disable
*/
function iats_civicrm_disable() {
return _iats_civix_civicrm_disable();
}
/**
* Implementation of hook_civicrm_upgrade
*
* @param $op string, the type of operation being performed; 'check' or 'enqueue'
* @param $queue CRM_Queue_Queue, (for 'enqueue') the modifiable list of pending up upgrade tasks
*
* @return mixed based on op. for 'check', returns array(boolean) (TRUE if upgrades are pending)
* for 'enqueue', returns void
*/
function iats_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
return _iats_civix_civicrm_upgrade($op, $queue);
}
/**
* Implementation of hook_civicrm_managed
*
* Generate a list of entities to create/deactivate/delete when this module
* is installed, disabled, uninstalled.
*/
function iats_civicrm_managed(&$entities) {
$entities[] = array(
'module' => 'ca.civicrm.iats',
'name' => 'iATS Payments',
'entity' => 'PaymentProcessorType',
'params' => array(
'version' => 3,
'name' => 'iATS Payments Credit Card',
'title' => 'iATS Payments Credit Card',
'description' => 'iATS credit card payment processor using the web services interface.',
'class_name' => 'Payment_iATSService',
'billing_mode' => 'form',
'user_name_label' => 'Agent Code',
'password_label' => 'Password',
'url_site_default' => 'https://www.iatspayments.com/NetGate/ProcessLink.asmx?WSDL',
'url_recur_default' => 'https://www.iatspayments.com/NetGate/ProcessLink.asmx?WSDL',
'url_site_test_default' => 'https://www.iatspayments.com/NetGate/ProcessLink.asmx?WSDL',
'url_recur_test_default' => 'https://www.iatspayments.com/NetGate/ProcessLink.asmx?WSDL',
'is_recur' => 1,
'payment_type' => 1,
),
);
$entities[] = array(
'module' => 'ca.civicrm.iats',
'name' => 'iATS Payments ACH/EFT',
'entity' => 'PaymentProcessorType',
'params' => array(
'version' => 3,
'name' => 'iATS Payments ACH/EFT',
'title' => 'iATS Payments ACH/EFT',
'description' => 'iATS ACH/EFT payment processor using the web services interface.',
'class_name' => 'Payment_iATSServiceACHEFT',
'billing_mode' => 'form',
'user_name_label' => 'Agent Code',
'password_label' => 'Password',
'url_site_default' => 'https://www.iatspayments.com/NetGate/ProcessLink.asmx?WSDL',
'url_recur_default' => 'https://www.iatspayments.com/NetGate/ProcessLink.asmx?WSDL',
'url_site_test_default' => 'https://www.iatspayments.com/NetGate/ProcessLink.asmx?WSDL',
'url_recur_test_default' => 'https://www.iatspayments.com/NetGate/ProcessLink.asmx?WSDL',
'is_recur' => 1,
'payment_type' => 2,
),
);
return _iats_civix_civicrm_managed($entities);
}
function iats_civicrm_navigationMenu(&$params) {
// get the maximum key of $params
$maxKey = 1 + (max(array_keys($params)));
foreach($params as $key => $value) {
if ('Contributions' == $value['attributes']['name']) {
$params[$key]['child'][$maxKey] = array (
'attributes' => array (
'label' => 'iATS Payments Admin',
'name' => 'iATS Payments Admin',
'url' => 'civicrm/iATSAdmin',
'permission' => 'access CiviContribute,administer CiviCRM',
'operator' => 'AND',
'separator' => null,
'parentID' => 28,
'navID' => $maxKey,
'active' => 1
)
);
$maxKey++; // just in case ...
}
}
}
/*
* hook_civicrm_buildForm
* Do a Drupal 7 style thing so we can write smaller functions
*/
function iats_civicrm_buildForm($formName, &$form) {
$fname = 'iats_civicrm_buildForm_'.$formName;
if (function_exists($fname)) {
$fname($form);
}
}
/*
* hook_civicrm_pre
*
* CiviCRM assumes all recurring contributions need to be reverified
* using the IPN mechanism.
* After saving any contribution, test for status = 2 and using IATS Payments
* and set to status = 1 instead.
* Applies only to the initial contribution and the recurring contribution record.
* The recurring contribution status id is set explicitly in the job that creates it, and doesn't need this modification.
*/
function iats_civicrm_pre($op, $objectName, $objectId, &$params) {
if ('create' == $op) {
if (('Contribution' == $objectName)
&& !empty($params['contribution_status_id'])
&& !empty($params['contribution_recur_id'])) {
if (2 == $params['contribution_status_id']) {
// watchdog('iats_civicrm','hook_civicrm_pre for Contribution recur @id',array('@id' => $params['contribution_recur_id']));
if ($payment_processor_id = _iats_civicrm_get_payment_processor_id($params['contribution_recur_id'])) {
// watchdog('iats_civicrm','hook_civicrm_pre for PP id @id',array('@id' => $payment_processor_id));
if ($type = _iats_civicrm_is_iats($payment_processor_id)) {
// watchdog('iats_civicrm','Convert to status of 1');
switch ($type) {
case 'iATSService': // cc
$params['contribution_status_id'] = 1;
break;
case 'iATSServiceACHEFT': // cc
$params['payment_instrument_id'] = 2;
// watchdog('iats_civicrm_regular','<pre>'.print_r($params,TRUE).'</pre>');
// $params['contribution_status_id'] = 1;
break;
}
}
}
}
}
elseif ('ContributionRecur' == $objectName) {
// watchdog('iats_civicrm','hook_civicrm_pre for ContributionRecur params @id',array('@id' => print_r($params, TRUE)));
if (2 == $params['contribution_status_id']
&& !empty($params['payment_processor_id'])
) {
if ($type = _iats_civicrm_is_iats($params['payment_processor_id'])) {
// watchdog('iats_civicrm','Convert to status of 1');
switch ($type) {
case 'iATSService': // cc
// we have already taken the first payment, so calculate the next one
$params['contribution_status_id'] = 1;
$next = strtotime('+'.$params['frequency_interval'].' '.$params['frequency_unit']);
$params[IATS_CIVICRM_NSCD_FID] = date('YmdHis',$next);
break;
case 'iATSServiceACHEFT': //
// watchdog('iats_civicrm_recur','<pre>'.print_r($params,TRUE).'</pre>');
$params['payment_instrument_id'] = 2;
$params['contribution_status_id'] = 1;
$next = strtotime('+'.$params['frequency_interval'].' '.$params['frequency_unit']);
$params[IATS_CIVICRM_NSCD_FID] = date('YmdHis',$next);
break;
}
}
}
}
// watchdog('iats_civicrm','ignoring hook_civicrm_pre for objectName @id',array('@id' => $objectName));
}
}
/*
* The contribution itself doesn't tell you which payment processor it came from
* So we have to dig back via the contribution_recur_id that it is associated with.
*/
function _iats_civicrm_get_payment_processor_id($contribution_recur_id) {
$params = array(
'version' => 3,
'sequential' => 1,
'id' => $contribution_recur_id,
);
$result = civicrm_api('ContributionRecur', 'getsingle', $params);
if (empty($result['payment_processor_id'])) {
return FALSE;
// TODO: log error
}
return $result['payment_processor_id'];
}
function _iats_civicrm_is_iats($payment_processor_id) {
$params = array(
'version' => 3,
'sequential' => 1,
'id' => $payment_processor_id,
);
$result = civicrm_api('PaymentProcessor', 'getsingle', $params);
if (empty($result['class_name'])) {
return FALSE;
// TODO: log error
}
$type = substr($result['class_name'],0,19);
$subtype = substr($result['class_name'],19);
return ('Payment_iATSService' == $type) ? 'iATSService'.$subtype : FALSE;
}
/* internal utility function: return the id's of any iats ach/eft processors */
function iats_civicrm_acheft_processors($processors) {
$acheft = array();
foreach($processors as $id => $paymentProcessor) {
$params = array('version' => 3, 'sequential' => 1, 'id' => $id);
$result = civicrm_api('PaymentProcessor', 'getsingle', $params);
if (!empty($result['class_name']) && ('Payment_iATSServiceACHEFT' == $result['class_name'])) {
$acheft[$id] = TRUE;
break;
}
}
return $acheft;
}
/* ACH/EFT modifications from the default direct debit form */
function iats_civicrm_buildForm_CRM_Contribute_Form_Contribution_Main(&$form) {
if (empty($form->_paymentProcessors)) {
return;
}
$acheft = iats_civicrm_acheft_processors($form->_paymentProcessors);
// I only need to mangle forms that allow ACH/EFT
if (0 == count($acheft)) {
return;
}
if (isset($form->_elementIndex['is_recur'])) {
$form->getElement('is_recur')->setValue(1); // force recurring contrib option
$form->getElement('is_recur')->freeze();
}
elseif (empty($form->_values['is_recur'])) {
CRM_Core_Session::setStatus(ts('You must configure iATS ACH/EFT for recurring contributions.'), ts('Invalid form setting!'), 'alert');
}
/* In addition, I need to mangle the ajax-bit of the form if I've just selected an ach/eft option
* I need to include an extra field for iATS
* TODO: make this form nicer by include a sample check with instructions for getting the account number
*/
if (!empty($acheft[$form->_paymentProcessor['id']])){
$element = $form->getElement('account_holder');
$element->setLabel(ts('Name of Account Holder'));
$element = $form->getElement('bank_identification_number');
$element->setLabel(ts('Bank number + branch transit number'));
//$element = $form->getElement('bank_name');
//$element->setLabel(ts('Bank name'));
$form->addElement('select', 'bank_account_type', ts('Account type'), array('CHECKING' => 'Checking', 'SAVING' => 'Saving'));
$form->addRule('bank_account_type', ts('%1 is a required field.', array(1 => ts('Account type'))), 'required');
CRM_Core_Region::instance('billing-block')->add(array(
'template' => 'CRM/iATS/BillingBlockDirectDebitExtra.tpl'
));
// watchdog('iats_acheft',kprint_r($form,TRUE));
}
// TODO: add legal requirement notice and perhaps checkbox acceptance for electronic acceptance of ACH/EFT ? Country dependent!
}
/* Fix the backend contribution form.
* 1. take out my ACH/EFT processors
* 2. if I took them out, provide helpful links to backend-ish payment pages
* This should get fixed in core.
*/
function iats_civicrm_buildForm_CRM_Contribute_Form_Contribution(&$form) {
if (empty($form->_processors)) {
return;
}
$acheft = iats_civicrm_acheft_processors($form->_processors);
// I only need to mangle forms that allow ACH/EFT
if (0 == count($acheft)) {
return;
}
$acheft_backoffice_links = array();
// yes, there's a more efficient/clever way to find the right element
foreach($form->_elements as $form_id => $element) {
if ($element->_attributes['name'] == 'payment_processor_id') {
$pp_form_id = $form_id;
break;
}
}
foreach(array_keys($acheft) as $pp_id) {
unset($form->_processors[$pp_id]);
if (!empty($form->_recurPaymentProcessors[$pp_id])) {
unset($form->_recurPaymentProcessors[$pp_id]);
}
$element = $form->_elements[$pp_form_id];
if (is_array($element->_options)) {
foreach($element->_options as $option_id => $option) {
if ($option['attr']['value'] == $pp_id) {
unset($element->_options[$option_id]);
}
}
}
// and now try to provide a different mechanism for 'backoffice' type contributions using this ACH/EFT payment processor
$params = array('version' => 3, 'sequential' => 1, 'payment_processor' => $pp_id);
$result = civicrm_api('ContributionPage', 'get', $params);
if (0 == $result['is_error'] && count($result['values']) > 0) {
foreach($result['values'] as $page) {
$url = CRM_Utils_System::url('civicrm/contribute/transact','reset=1&cid='.$form->_contactID.'&id='.$page['id']);
$acheft_backoffice_links[] = '<a href="'.$url.'">'.$page['title'].'</a>';
}
}
}
if (count($acheft_backoffice_links)) {
// a hackish way to inject these links into the form, they are display nicely using some javascript
$form->addElement('hidden','acheft_backoffice_links',json_encode($acheft_backoffice_links));
}
}
function _iats_civicrm_domain_info($key) {
static $domain;
if (empty($domain)) {
$domain = civicrm_api('Domain', 'getsingle', array('version' => 3));
}
switch($key) {
case 'version':
return explode('.',$domain['version']);
default:
if (!empty($domain[$key])) {
return $key;
}
$config_backend = unserialize($domain['config_backend']);
return $config_backend[$key];
}
}
function _iats_civicrm_nscd_fid() {
$version = _iats_civicrm_domain_info('version');
return (($version[0] <= 4) && ($version[1] <= 3)) ? 'next_sched_contribution' : 'next_sched_contribution_date';
}