-
Notifications
You must be signed in to change notification settings - Fork 38
/
iats.php
743 lines (700 loc) · 27.4 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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
<?php
/**
* @file Copyright iATS Payments (c) 2014
* Author: Alan Dixon.
*
* This file is a part of CiviCRM published extension.
*
* This extension is free software; you can copy, modify, and distribute it
* under the terms of the GNU Affero General Public License
* Version 3, 19 November 2007.
*
* It is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License with this program; if not, see http://www.gnu.org/licenses/
*/
//opcache_reset();
require_once 'iats.civix.php';
use CRM_Iats_ExtensionUtil as E;
/* First American requires a "category" for ACH transaction requests */
define('FAPS_DEFAULT_ACH_CATEGORY_TEXT', 'CiviCRM ACH');
/**
* Implements hook_civicrm_config().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function iats_civicrm_config(&$config) {
_iats_civix_civicrm_config($config);
}
/**
* Implements hook_civicrm_install().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
* TODO: don't require iATS if we're not installing the old processors.
*/
function iats_civicrm_install() {
if (!class_exists('SoapClient')) {
$session = CRM_Core_Session::singleton();
$session->setStatus(ts('The PHP SOAP extension is not installed on this server, but is required for this extension'), ts('iATS Payments Installation'), 'error');
}
_iats_civix_civicrm_install();
}
/**
* Implements hook_civicrm_enable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
*/
function iats_civicrm_enable() {
if (!class_exists('SoapClient')) {
$session = CRM_Core_Session::singleton();
$session->setStatus(ts('The PHP SOAP extension is not installed on this server, but is required for this extension'), ts('iATS Payments Installation'), 'error');
}
_iats_civix_civicrm_enable();
}
// --- Functions below this ship commented out. Uncomment as required. ---
/**
* Implements hook_civicrm_preProcess().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_preProcess
*
// */
/**
* Implements hook_civicrm_navigationMenu().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_navigationMenu
*
function iats_civicrm_navigationMenu(&$menu) {
_iats_civix_insert_navigation_menu($menu, 'Mailings', array(
'label' => E::ts('New subliminal message'),
'name' => 'mailing_subliminal_message',
'url' => 'civicrm/mailing/subliminal',
'permission' => 'access CiviMail',
'operator' => 'OR',
'separator' => 0,
));
_iats_civix_navigationMenu($menu);
} // */
/**
* Implements hook_civicrm_check().
*/
function iats_civicrm_check(&$messages) {
if (!class_exists('SoapClient')) {
$messages[] = new CRM_Utils_Check_Message(
'iats_soap',
ts('The SOAP extension for PHP %1 is not installed on this server, but is required for this extension.', array(1 => phpversion())),
ts('iATS Payments Installation'),
\Psr\Log\LogLevel::CRITICAL,
'fa-flag'
);
}
}
/**
* Utility function to get domain info.
*
* Get values from the civicrm_domain table, or a domain setting.
* May be called multiple times, so be efficient.
*/
function _iats_civicrm_domain_info($key) {
static $domain, $settings;
if (empty($domain)) {
$domain = civicrm_api3('Domain', 'getsingle', array('current_domain' => TRUE));
}
if (!isset($settings)) {
$settings = array();
}
switch ($key) {
case 'version':
return explode('.', $domain['version']);
default:
if (isset($domain[$key])) {
return $domain[$key];
}
elseif (isset($settings[$key])) {
return $settings[$key];
}
else {
try{
$setting = civicrm_api3('Setting', 'getvalue', array('name' => $key));
if (is_string($setting)) {
$settings[$key] = $setting;
return $setting;
}
}
catch (CiviCRM_API3_Exception $e) {
// ignore errors
}
// This remaining code is now very legacy, from earlier Civi versions and should soon be retired.
if (!empty($domain['config_backend'])) {
$config_backend = unserialize($domain['config_backend']);
if (!empty($config_backend[$key])) {
$settings[$key] = $config_backend[$key];
return $config_backend[$key];
}
}
}
// Uncomment one or more of these lines to find out what it was we were looking for and didn't find.
// CRM_Core_Error::debug_var('domain', $domain);
// CRM_Core_Error::debug_var($key, $settings);
// CRM_Core_Error::debug_var($key, $setting);
}
}
/* START utility functions to allow this extension to work with different civicrm version */
// removed, 1.7 release
/* END functions to allow this extension to work with different civicrm version */
/**
* Utility to get the next available menu key.
*/
function _iats_getMenuKeyMax($menuArray) {
$max = array(max(array_keys($menuArray)));
foreach ($menuArray as $v) {
if (!empty($v['child'])) {
$max[] = _iats_getMenuKeyMax($v['child']);
}
}
return max($max);
}
/**
*
*/
function iats_civicrm_navigationMenu(&$navMenu) {
$pages = array(
'admin_page' => array(
'label' => 'iATS Payments Admin',
'name' => 'iATS Payments Admin',
'url' => 'civicrm/iATSAdmin',
'parent' => array('Contributions'),
'permission' => 'access CiviContribute,administer CiviCRM',
'operator' => 'AND',
'separator' => NULL,
'active' => 1,
),
'settings_page' => array(
'label' => 'iATS Payments Settings',
'name' => 'iATS Payments Settings',
'url' => 'civicrm/admin/setting/iats',
'parent' => array('Administer', 'CiviContribute'),
'permission' => 'access CiviContribute,administer CiviCRM',
'operator' => 'AND',
'separator' => NULL,
'active' => 1,
),
);
foreach ($pages as $item) {
// Check that our item doesn't already exist.
$menu_item_search = array('url' => $item['url']);
$menu_items = array();
CRM_Core_BAO_Navigation::retrieve($menu_item_search, $menu_items);
if (empty($menu_items)) {
$path = implode('/', $item['parent']);
unset($item['parent']);
_iats_civix_insert_navigation_menu($navMenu, $path, $item);
}
}
}
/**
* 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)) {
// CRM_Core_Error::debug_var('overridden formName',$formName);
$fname($form);
}
// Else echo $fname;.
}
/**
* Modifications to a (public/frontend) contribution financial forms for iATS
* procesors.
* 1. enable public selection of future recurring contribution start date, but only if the form allows recurring!
*
* We're only handling financial payment class forms here. Note that we can no
* longer test for whether the page has/is recurring or not.
* Special note - if a page offers recurring contributions and if
* future recurring start dates are enabled with restrictions on which days,
* then any one-time contribution will be forced to use those rules as well.
*/
function iats_civicrm_buildForm_CRM_Financial_Form_Payment(&$form) {
// If form loads with no default processor skip this
if (empty($form->_paymentProcessor['id'])) {
return;
}
// Skip this if it's not an iATS-type of processor
$type = _iats_civicrm_is_iats($form->_paymentProcessor['id']);
if (empty($type)) {
return;
}
// Skip if this form doesn't allow recurring
if (empty($form->_values['is_recur'])) {
return;
}
// If future public start dates are enabled on a recurring-enabled page ...
// Uses javascript to hide/reset unless they have recurring contributions checked.
// 2023-11-08 updated how the payment processor object is found temporarily
// update to use getter method in the future.
$settings = CRM_Iats_Utils::getSettings();
if (!empty($settings['enable_public_future_recurring_start'])
// && $form->getPaymentProcessorObject()->supports('FutureRecurStartDate')
&& $form->_paymentProcessor['object']->supports('FutureRecurStartDate')
) {
$allow_days = empty($settings['days']) ? array('-1') : $settings['days'];
$start_dates = CRM_Iats_Transaction::get_future_monthly_start_dates(time(), $allow_days);
$form->addElement('select', 'receive_date', ts('Date of first contribution'), $start_dates);
CRM_Core_Region::instance('billing-block')->add(array(
'template' => 'CRM/Iats/BillingBlockRecurringExtra.tpl',
));
$recurStartJs = CRM_Core_Resources::singleton()->getUrl('com.iatspayments.civicrm', 'js/recur_start.js');
$script = 'var recurStartJs = "' . $recurStartJs . '";';
$script .= 'CRM.$(function ($) { $.getScript(recurStartJs); });';
CRM_Core_Region::instance('billing-block')->add(array(
'script' => $script,
));
}
}
/**
* The main civicrm contribution form is the public one, there are some
* edge cases where we need to do the same as the Financial form above.
*/
function iats_civicrm_buildForm_CRM_Contribute_Form_Contribution_Main(&$form) {
return iats_civicrm_buildForm_CRM_Financial_Form_Payment($form);
}
/**
* Implements hook_civicrm_buildForm for the CRM_Admin_Form_Generic form
*
* @var CRM_Admin_Form_Generic $form
*/
function iats_civicrm_buildForm_CRM_Admin_Form_Generic($form) {
if ($form->getSettingPageFIlter() === 'iats') {
$form->addRule('iats_email_recurring_failure_report', ts('Email address is not a valid format.'), 'email');
$form->addRule('iats_bcc_email_recurring_failure_report', ts('Email address is not a valid format.'), 'email');
$form->addRule('iats_recurring_failure_threshhold', ts('Threshhold must be a positive integer.'), 'integer');
}
}
/**
*
*/
function iats_civicrm_pageRun(&$page) {
$fname = 'iats_civicrm_pageRun_' . $page->getVar('_name');
if (function_exists($fname)) {
$fname($page);
}
}
/**
* Modify the recurring contribution (subscription) page.
* Display extra information about recurring contributions using Legacy iATS, and
* link to iATS CustomerLink display and editing pages.
*/
function iats_civicrm_pageRun_CRM_Contribute_Page_ContributionRecur($page) {
// Get the corresponding (most recently created) iATS customer code record
// we'll also get the expiry date and last four digits (at least, our best information about that).
$extra = array();
$crid = CRM_Utils_Request::retrieve('id', 'Integer', $page, FALSE);
try {
$recur = civicrm_api3('ContributionRecur', 'getsingle', array('id' => $crid));
}
catch (CiviCRM_API3_Exception $e) {
return;
}
$type = _iats_civicrm_is_iats($recur['payment_processor_id']);
if ((0 !== strpos($type,'iATSService')) || empty($recur['payment_token_id'])) {
return;
}
try {
$payment_token = civicrm_api3('PaymentToken', 'getsingle', [
'id' => $recur['payment_token_id'],
]);
$customer_code = $payment_token['token'];
$extra['iATS Customer Code'] = $customer_code;
$customerLinkView = CRM_Utils_System::url('civicrm/contact/view/iatscustomerlink',
'reset=1&cid=' . $recur['contact_id'] . '&customerCode=' . $customer_code . '&paymentProcessorId=' . $recur['payment_processor_id'] . '&is_test=' . $recur['is_test']);
$extra['customerLink'] = "<a href='$customerLinkView'>View</a>";
if ($type == 'iATSService' || $type == 'iATSServiceSWIPE') {
$customerLinkEdit = CRM_Utils_System::url('civicrm/contact/edit/iatscustomerlink',
'reset=1&cid=' . $recur['contact_id'] . '&customerCode=' . $customer_code . '&paymentProcessorId=' . $recur['payment_processor_id'] . '&is_test=' . $recur['is_test']);
$extra['customerLink'] .= " | <a href='$customerLinkEdit'>Edit</a>";
$processLink = CRM_Utils_System::url('civicrm/contact/iatsprocesslink',
'reset=1&cid=' . $recur['contact_id'] . '&customerCode=' . $customer_code . '&paymentProcessorId=' . $recur['payment_processor_id'] . '&crid=' . $crid . '&is_test=' . $recur['is_test']);
$extra['customerLink'] .= " | <a href='$processLink'>Process</a>";
$expiry = $payment_token['expiry_date'];
$extra['expiry'] = empty($expiry) ? 'N/A' : date('Y-m', strtotime($expiry));
}
}
catch (CiviCRM_API3_Exception $e) {
return;
}
if (!count($extra)) {
return;
}
$template = CRM_Core_Smarty::singleton();
foreach ($extra as $key => $value) {
$template->assign($key, $value);
}
CRM_Core_Region::instance('page-body')->add(array(
'template' => 'CRM/Iats/ContributionRecur.tpl',
));
CRM_Core_Resources::singleton()->addScriptFile('com.iatspayments.civicrm', 'js/subscription_view.js');
}
/**
* Hook_civicrm_merge
* Deal with contact merges - our custom iats customer code table contains contact id's as a check, it might need to be updated.
*/
function iats_civicrm_merge($type, &$data, $mainId = NULL, $otherId = NULL, $tables = NULL) {
if ('cidRefs' == $type) {
$data['civicrm_iats_verify'] = array('cid');
}
}
/**
* Hook_civicrm_pre.
*
* Handle special cases of creating contribution (regular and recurring) records when using IATS Payments.
*
* 1. CiviCRM assumes all recurring contributions need to be confirmed using the IPN mechanism. This is not true for iATS recurring contributions.
* So when creating a contribution that is part of a recurring series, test for status = 2, and set to status = 1 instead, unless we're using the fixed day feature
* Do this only for the initial contribution record.
* The (subsequent) recurring contributions' status id is set explicitly in the job that creates it, this modification breaks that process.
*
* 2. For ACH/EFT, we also have the opposite problem - all contributions will need to verified by iATS and only later set to status success or
* failed via the acheft verify job. We also want to modify the payment instrument from CC to ACH/EFT
*
* TODO: update this code with constants for the various id values of 1 and 2.
* TODO: CiviCRM should have nicer ways to handle this.
*/
function iats_civicrm_pre($op, $objectName, $objectId, &$params) {
// If I've set fixed monthly recurring dates, force any iats recurring contribution schedule records to comply.
if (('ContributionRecur' == $objectName) && ('create' == $op || 'edit' == $op) && !empty($params['payment_processor_id'])) {
if ($type = _iats_civicrm_is_iats($params['payment_processor_id'])) {
if (!empty($params['next_sched_contribution_date'])) {
$settings = CRM_Iats_Utils::getSettings();
$allow_days = empty($settings['days']) ? array('-1') : $settings['days'];
// Force one of the fixed days, and set the cycle_day at the same time.
if (0 < max($allow_days)) {
$init_time = ('create' == $op) ? time() : strtotime($params['next_sched_contribution_date']);
$from_time = CRM_Iats_Transaction::contributionrecur_next($init_time, $allow_days);
$params['next_sched_contribution_date'] = date('YmdHis', $from_time);
// Day of month without leading 0.
$params['cycle_day'] = date('j', $from_time);
}
}
// Fix a civi bug while I'm here.
if (empty($params['installments'])) {
$params['installments'] = '0';
}
}
}
}
function iats_get_setting($key = NULL) {
static $settings;
if (empty($settings)) {
$settings = CRM_Iats_Utils::getSettings();
}
return empty($key) ? $settings : (isset($settings[$key]) ? $settings[$key] : '');
}
/**
* 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(
'id' => $contribution_recur_id,
);
try {
$result = civicrm_api3('ContributionRecur', 'getsingle', $params);
}
catch (CiviCRM_API3_Exception $e) {
return FALSE;
}
if (empty($result['payment_processor_id'])) {
return FALSE;
// TODO: log error.
}
return $result['payment_processor_id'];
}
/**
* Utility function to see if a payment processor id is using one of the iATS payment processors.
*
* This function relies on our naming convention for the iats payment processor classes, staring with the string Payment_iATSService.
*/
function _iats_civicrm_is_iats($payment_processor_id) {
if (empty($payment_processor_id)) {
return FALSE;
}
$params = array(
'id' => $payment_processor_id,
);
try {
$result = civicrm_api3('PaymentProcessor', 'getsingle', $params);
}
catch (CiviCRM_API3_Exception $e) {
return FALSE;
// TODO: log error?.
}
if (empty($result['class_name'])) {
return FALSE;
// TODO: log error?.
}
// type is class name with Payment_ stripped from the front
$type = substr($result['class_name'], 8);
$is_iats = (0 === strpos($type, 'iATSService')) || (0 === strpos($type, 'Faps'));
return ($is_iats ? $type : FALSE);
}
/**
* Internal utility function: return the id's of any iATS processors matching various conditions.
*
* class: the payment object class name to match (prefixed w/ 'Payment_')
* processors: an array of payment processors indexed by id to filter by
* params: an array of additional params to pass to the api call.
*/
function _iats_filter_payment_processors($class, $processors = array(), $params = array()) {
$list = array();
$params['class_name'] = ['LIKE' => 'Payment_' . $class];
// On the chance that there are a lot of payment processors and the caller
// hasn't specified a limit, assume they want them all.
if (empty($params['options']['limit'])) {
$params['options']['limit'] = 0;
}
// Set the domain id if not passed in.
if (!array_key_exists('domain_id', $params)) {
$params['domain_id'] = CRM_Core_Config::domainID();
}
$params['sequential'] = FALSE; // return list indexed by processor id
$result = civicrm_api3('PaymentProcessor', 'get', $params);
if (0 == $result['is_error'] && count($result['values']) > 0) {
$list = (0 < count($processors)) ? array_intersect_key($result['values'], $processors) : $result['values'];
}
return $list;
}
/**
* Internal utility function: return a list of the payment processors attached
* to a contribution form of variable class
* */
function _iats_get_form_payment_processors($form) {
$form_class = get_class($form);
if ($form_class == 'CRM_Financial_Form_Payment') {
// We're on CRM_Financial_Form_Payment, we've got just one payment processor
$id = $form->_paymentProcessor['id'];
return array($id => $form->_paymentProcessor);
}
else {
// Handle the legacy: event and contribution page forms
if (empty($form->_paymentProcessors)) {
if (empty($form->_paymentProcessorIDs)) {
return;
}
else {
return array_fill_keys($form->_paymentProcessorIDs, 1);
}
}
else {
return $form->_paymentProcessors;
}
}
}
/**
*
*/
function iats_getCurrency($form) {
// getting the currency depends on the form class
$form_class = get_class($form);
$currency = '';
switch($form_class) {
case 'CRM_Contribute_Form_Contribution':
case 'CRM_Contribute_Form_Contribution_Main':
case 'CRM_Member_Form_Membership':
$currency = $form->_values['currency'];
break;
case 'CRM_Financial_Form_Payment':
// This is the new ajax-loaded payment form.
$currency = $form->getCurrency();
break;
case 'CRM_Event_Form_Participant':
case 'CRM_Event_Form_Registration_Register':
$currency = $form->_values['event']['currency'];
break;
}
if (empty($currency)) {
// This may occur in edge cases, so don't break, though the form won't be rendered correctly.
// See comment on civicrm core commit f61437d
CRM_Core_Error::debug_var($form_class, $form);
}
return $currency;
}
/**
* Provide helpful links to backend-ish payment pages for ACH/EFT, since the backend credit card pages don't work/apply
* Could do the same for swipe?
*/
function iats_civicrm_buildForm_CRM_Contribute_Form_Search(&$form) {
// Ignore invocations that aren't for a specific contact, e.g. the civicontribute dashboard.
if (empty($form->_defaultValues['contact_id'])) {
return;
}
$contactID = $form->_defaultValues['contact_id'];
$acheft = _iats_filter_payment_processors('iATSServiceACHEFT', array(), array('is_active' => 1, 'is_test' => 0));
$acheft_backoffice_links = array();
// For each ACH/EFT payment processor, try to provide a different mechanism for 'backoffice' type contributions
// note: only offer payment pages that provide iATS ACH/EFT exclusively.
foreach (array_keys($acheft) as $pp_id) {
$params = array('is_active' => 1, 'payment_processor' => $pp_id);
$result = civicrm_api3('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=' . $contactID . '&id=' . $page['id']);
$acheft_backoffice_links[] = array('url' => $url, 'title' => $page['title']);
}
}
}
if (count($acheft_backoffice_links)) {
CRM_Core_Resources::singleton()->addVars('iatspayments', array('backofficeLinks' => $acheft_backoffice_links));
CRM_Core_Resources::singleton()->addScriptFile('com.iatspayments.civicrm', 'js/contribute_form_search.js');
}
}
/**
* If this recurring contribution sequence is using an iATS payment processor,
* modify the recurring contribution cancelation form to exclude the confusing message about sending the request to the backend.
*/
function iats_civicrm_buildForm_CRM_Contribute_Form_CancelSubscription(&$form) {
$crid = CRM_Utils_Request::retrieve('crid', 'Integer', $form, FALSE);
try {
$recur = civicrm_api3('ContributionRecur', 'getsingle', array('id' => $crid));
}
catch (CiviCRM_API3_Exception $e) {
return;
}
if (_iats_civicrm_is_iats($recur['payment_processor_id'])) {
if ($form->elementExists('send_cancel_request')) {
$form->removeElement('send_cancel_request');
}
}
}
/**
* Add some functionality to the update subscription form for recurring contributions.
*/
function iats_civicrm_buildForm_CRM_Contribute_Form_UpdateSubscription(&$form) {
/* For 4.7, we implement getEditableRecurringScheduleFields but still need this for additional niceness */
// Only do this if the user is allowed to edit contributions. A more stringent permission might be smart.
if (!CRM_Core_Permission::check('edit contributions')) {
return;
}
// Only mangle this form for recurring contributions using iATS
$payment_processor_type = empty($form->_paymentProcessor) ? substr(get_class($form->_paymentProcessorObj),9) : $form->_paymentProcessor['class_name'];
if ( (0 !== strpos($payment_processor_type, 'Payment_iATSService'))
&& (0 !== strpos($payment_processor_type, 'Payment_Faps')) ){
return;
}
$settings = CRM_Iats_Utils::getSettings();
// don't do this if the site administrator has disabled it.
if (!empty($settings['no_edit_extra'])) {
return;
}
$allow_days = empty($settings['days']) ? array('-1') : $settings['days'];
if (0 < max($allow_days)) {
$userAlert = ts('Your next scheduled contribution date will automatically be updated to the next allowable day of the month: %1',
array(1 => implode(', ', $allow_days)));
CRM_Core_Session::setStatus($userAlert, ts('Warning'), 'alert');
}
$crid = CRM_Utils_Request::retrieve('crid', 'Integer', $form, FALSE);
/* get the recurring contribution record and the contact record, or quit */
try {
$recur = civicrm_api3('ContributionRecur', 'getsingle', array('id' => $crid));
}
catch (CiviCRM_API3_Exception $e) {
return;
}
try {
$contact = civicrm_api3('Contact', 'getsingle', array('id' => $recur['contact_id']));
}
catch (CiviCRM_API3_Exception $e) {
return;
}
try {
$pp = civicrm_api3('PaymentProcessor', 'getsingle', array('id' => $recur['payment_processor_id']));
}
catch (CiviCRM_API3_Exception $e) {
$pp = array();
}
// Turn off default notification checkbox, because that's a better default.
$defaults = array('is_notify' => 0);
$edit_fields = array(
'contribution_status_id' => 'Status',
'next_sched_contribution_date' => 'Next Scheduled Contribution',
'start_date' => 'Start Date',
'is_email_receipt' => 'Email receipt for each Contribution in this Recurring Series',
);
$dupe_fields = array();
// To be a good citizen, I check if core or another extension hasn't already added these fields
// and don't add them again if they have.
foreach (array_keys($edit_fields) as $fid) {
if ($form->elementExists($fid)) {
unset($edit_fields[$fid]);
$dupe_fields[] = str_replace('_','-',$fid);
}
elseif (isset($recur[$fid])) {
$defaults[$fid] = $recur[$fid];
}
}
// Use this in my js to identify which fields need to be removed from the tpl I inject below
CRM_Core_Resources::singleton()->addVars('iatspayments', array('dupeSubscriptionFields' => $dupe_fields));
foreach ($edit_fields as $fid => $label) {
switch($fid) {
case 'contribution_status_id':
$contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
$form->addElement('select', 'contribution_status_id', ts('Status'), $contributionStatus);
break;
case 'is_email_receipt':
$receiptStatus = array('0' => 'No', '1' => 'Yes');
$form->addElement('select', $fid, ts($label), $receiptStatus);
break;
default:
$form->addDateTime($fid, ts($label));
break;
}
}
$form->setDefaults($defaults);
// Now add some more fields for display only
/* Add in the contact's name */
$form->addElement('static', 'contact', $contact['display_name']);
// get my pp, if available.
$pp_label = empty($pp['name']) ? $form->_paymentProcessor['name'] : $pp['name'];
$form->addElement('static', 'payment_processor', $pp_label);
$label = CRM_Contribute_Pseudoconstant::financialType($recur['financial_type_id']);
$form->addElement('static', 'financial_type', $label);
$labels = CRM_Contribute_Pseudoconstant::paymentInstrument();
$label = $labels[$recur['payment_instrument_id']];
$form->addElement('static', 'payment_instrument', $label);
$form->addElement('static', 'failure_count', $recur['failure_count']);
CRM_Core_Region::instance('page-body')->add(array(
'template' => 'CRM/Iats/Subscription.tpl',
));
CRM_Core_Resources::singleton()->addScriptFile('com.iatspayments.civicrm', 'js/subscription.js');
}
function iats_civicrm_buildForm_CRM_Contribute_Form_UpdateBilling(&$form) {
// add hidden form field for the contribution recur ID taken from URL
// if not specified directly, look it up via a membership ID
$crid = CRM_Utils_Array::value('crid', $_GET);
if (!$crid) {
$mid = CRM_Utils_Array::value('mid', $_GET);
if ($mid) {
try {
$crid = civicrm_api3('Membership', 'getvalue', array(
'id' => $mid,
'return' => 'contribution_recur_id',
));
}
catch (CiviCRM_API3_Exception $e) {
$crid = 0;
}
}
}
if ($crid) {
$form->addElement('hidden', 'crid', $crid);
}
}
function _iats_payment_status_complete() {
return [
'payment_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'),
'payment_status' => 'Completed',
];
}