-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtsys.php
497 lines (461 loc) · 18.8 KB
/
tsys.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
<?php
require_once 'tsys.civix.php';
use CRM_Tsys_ExtensionUtil as E;
/**
* Implements hook_civicrm_links().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_links
*/
function tsys_civicrm_links($op, $objectName, $objectId, &$links, &$mask, &$values) {
// Adds links to process payments by device to Contribution Actions for Pending/Partially Paid Payments
if ($op == 'contribution.selector.row' && $objectName == 'Contribution') {
$deviceSettings = CRM_Core_Payment_Tsys::getDeviceSettings('buttons');
if (!empty($deviceSettings)) {
try {
$contribDetails = civicrm_api3('Contribution', 'getsingle', [
'id' => $objectId,
]);
}
catch (CiviCRM_API3_Exception $e) {
$error = $e->getMessage();
CRM_Core_Error::debug_log_message(E::ts('API Error %1', array(
'domain' => 'com.aghstrategies.tsys',
1 => $error,
)));
}
$statusesToShowDeviceLinks = ['Pending', 'Partially paid'];
if (!empty($contribDetails['contribution_status'])
&& in_array($contribDetails['contribution_status'], $statusesToShowDeviceLinks)) {
foreach ($deviceSettings as $key => $device) {
if (!empty($device['devicename']) && !empty($device['ip'])) {
$links[] = [
'name' => "Record Payment via Device: {$device['devicename']}",
'url' => 'civicrm/tsysdevicepayment',
'qs' => "reset=1&deviceid={$key}&contribid={$objectId}",
'title' => "Record Payment via Device: {$device['devicename']}",
];
}
}
}
}
}
// Adds a refund link to each payment made with a status of completed
// (also known as the payments that can be refunded)
if ($objectName == 'Payment' && $op == 'Payment.edit.action') {
if (!empty($values['contribution_id'])) {
// DO NOT show refund link for payments that have failed or already been refunded.
try {
$contribDetails = civicrm_api3('Contribution', 'getsingle', [
'id' => $values['contribution_id'],
]);
}
catch (CiviCRM_API3_Exception $e) {
$error = $e->getMessage();
CRM_Core_Error::debug_log_message(E::ts('API Error %1', array(
'domain' => 'com.aghstrategies.tsys',
1 => $error,
)));
}
if (!empty($contribDetails['contribution_status'])
&& in_array($contribDetails['contribution_status'], ['Completed', 'Partially Paid', 'Pending refund'])) {
try {
$trxnDetails = civicrm_api3('FinancialTrxn', 'getsingle', [
'return' => "payment_processor_id, status_id, trxn_id",
'is_payment' => 1,
'id' => $values['id'],
]);
}
catch (CiviCRM_API3_Exception $e) {
$error = $e->getMessage();
CRM_Core_Error::debug_log_message(E::ts('API Error %1', array(
'domain' => 'com.aghstrategies.tsys',
1 => $error,
)));
}
$completedStatusId = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');
if (!empty($trxnDetails['status_id']) && $trxnDetails['status_id'] == $completedStatusId) {
$tsysProcessors = CRM_Core_Payment_Tsys::getAllTsysPaymentProcessors();
if ($trxnDetails['payment_processor_id'] && !empty($tsysProcessors[$trxnDetails['payment_processor_id']])) {
$links[] = [
'name' => '<i class="crm-i fa fa-undo" aria-hidden="true"></i>',
'url' => 'civicrm/tsys/refund',
'class' => 'medium-popup',
'qs' => 'reset=1&id=%%id%%&contribution_id=%%contribution_id%%',
'title' => 'Refund or Void Payment',
'bit' => 2,
];
}
}
}
}
}
}
/**
* Implements hook_civicrm_buildForm().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_buildForm
*/
function tsys_civicrm_buildForm($formName, &$form) {
// add link to make a payment via a Device:
// When viewing or editing a contribution or a payment
if ($formName == 'CRM_Contribute_Form_ContributionView'
|| $formName == 'CRM_Contribute_Form_Contribution'
|| $formName == 'CRM_Contribute_Form_AdditionalPayment') {
$contributionId = CRM_Utils_Request::retrieve('id', 'Integer');
if ($contributionId == NULL && isset($form->_id)) {
$contributionId = $form->_id;
}
if ($contributionId > 0) {
$deviceSettings = CRM_Core_Payment_Tsys::getDeviceSettings('buttons');
if (!empty($deviceSettings)) {
$deviceButtons = [];
foreach ($deviceSettings as $key => $device) {
if (!empty($device['id'])) {
$deviceButtons[] = [
'url' => CRM_Utils_System::url('civicrm/tsysdevicepayment', "reset=1&deviceid={$device['id']}&contribid={$form->_id}"),
'label' => $device['devicename'],
];
}
}
$form->assign('swipedevices', $deviceButtons);
$templatePath = realpath(dirname(__FILE__) . "/templates");
CRM_Core_Region::instance('form-body')->add([
'template' => "{$templatePath}/CRM/Contribute/Form/swipedevices.tpl",
]);
CRM_Core_Resources::singleton()->addScriptFile('com.aghstrategies.tsys', 'js/moveButton.js');
}
}
}
if ($formName == 'CRM_Contribute_Form_AdditionalPayment') {
// This adds a warning to the "New Refund" form letting the user know that
// submitting this form will not result in a refund to the user only a record
// in CiviCRM. The new refund form can be found when you register for an event
// using a price set and then change the selected price to a LOWER price. This
// will make the contributions status "Pending Refund" and trigger a "Record
// Refund" button to appear. Clicking the record refund button will take you
// to the "New Refund" Form.
// TODO either make it so submitting this form does result in a refund in the processor
// or filter this message to only show up for contributions that uses a Genius processor
if ($form->getVar('_paymentType') == 'refund') {
CRM_Core_Session::setStatus(E::ts('Submitting this refund form will
NOT result in a refund of money to the user. A refund will be recorded in CiviCRM. If this
was a payment made thru a Genius processor either: refund the payment using the
credit card action button OR submit this form and then login to Genius to process
the refund.'), '', 'no-popup');
}
}
// Load stripe.js on all civi forms per stripe requirements
if (!isset(\Civi::$statics[E::LONG_NAME]['tsysJSLoaded'])) {
\Civi::resources()->addScriptUrl('https://ecommerce.merchantware.net/v1/CayanCheckoutPlus.js');
\Civi::$statics[E::LONG_NAME]['tsysJSLoaded'] = TRUE;
}
// If on a form with a Tsys Payment Processor
if (!empty($form->_paymentProcessor['api.payment_processor_type.getsingle']['name'])
&& $form->_paymentProcessor['api.payment_processor_type.getsingle']['name'] == 'TSYS') {
// Add data-cayan attributes to credit card fields so CayanCheckoutPlus script can find them:
$form->updateElementAttr('credit_card_number', array('data-cayan' => 'cardnumber'));
$form->updateElementAttr('cvv2', array('data-cayan' => 'cvv'));
// AGH #20367 If street address and zip code fields are on the form add
// data-cayan attributes for them too so that data gets sent to the processor
if (isset($form->_paymentFields)) {
foreach ($form->_paymentFields as $field => $value) {
if (substr($field, 0, 22) == 'billing_street_address') {
$form->updateElementAttr($field, array('data-cayan' => 'streetaddress'));
}
if (substr($field, 0, 19) == 'billing_postal_code') {
$form->updateElementAttr($field, array('data-cayan' => 'zipcode'));
}
}
}
// Don't use \Civi::resources()->addScriptFile etc as they often don't
// work on AJAX loaded forms (eg. participant backend registration)
\Civi::resources()->addVars('tsys', [
'allApiKeys' => CRM_Core_Payment_Tsys::getAllTsysPaymentProcessors(),
'pp' => CRM_Utils_Array::value('id', $form->_paymentProcessor),
]);
CRM_Core_Region::instance('billing-block')->add([
'scriptUrl' => \Civi::resources()->getUrl(E::LONG_NAME, "js/civicrm_tsys.js"),
]);
}
}
/**
* Implements hook_civicrm_validateForm().
*
* Prevent server validation of cc fields:
*/
function tsys_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) {
// ensure total amount is a positive number
if ($formName == 'CRM_Tsys_Form_Device') {
$total = CRM_Utils_Array::value('total_amount', $fields );
if ($total < 0) {
$errors['total_amount'] = E::ts( 'Total Amount must be a positive number' );
}
}
// Device Settings Form Validation
if ($formName == 'CRM_Tsys_Form_Settings_Device') {
// Valid IP address
if (!filter_var($fields['ip'], FILTER_VALIDATE_IP)) {
$errors['ip'] = E::ts('Please enter a valid IP address');
}
// Verify that device settings are unique when creating and updating - terminal id, ip and devicename should be unique
$deviceSettings = CRM_Core_Payment_Tsys::getDeviceSettings('all');
// If updating ignore the existing record
if (!empty($form->_submitValues['id'])) {
unset($deviceSettings[$form->_submitValues['id']]);
}
$uniqueFields = [
'terminalid' => 'Terminal ID',
'ip' => 'IP Address',
'devicename' => "Device Name",
];
foreach ($deviceSettings as $deviceId => $device) {
foreach ($uniqueFields as $fieldName => $label) {
if ($device[$fieldName] == $fields[$fieldName]) {
$errors[$fieldName] = E::ts('%1 must be unique.', [1 => $label]);
}
}
}
if ($form->_action && $form->_action == CRM_Core_Action::ADD) {
if (!empty($fields['terminalid']) && !empty($deviceSettings[$fields['terminalid']])) {
$errors['terminalid'] = E::ts('Terminal ID must be unique.');
}
}
// Terminal ID validation
// Must be a number
if (!is_numeric($fields['terminalid'])) {
$errors['terminalid'] = E::ts('This Terminal ID must be a number.');
}
// Must be between 1 and 16 characters
if (strlen($fields['terminalid']) > 16) {
$errors['terminalid'] = E::ts('This Terminal ID must be between 1 and 16 characters long.');
}
}
// This is copied from stripe: https://lab.civicrm.org/extensions/stripe/blob/master/stripe.php#L125
// Ensures credit card number does not get sent to server in edge case
if (empty($form->_paymentProcessor['payment_processor_type'])) {
return;
}
// If Tsys is active here.
if ($form->_paymentProcessor['class_name'] == 'Payment_Tsys') {
if (isset($form->_elementIndex['payment_token'])) {
if ($form->elementExists('credit_card_number')) {
$cc_field = $form->getElement('credit_card_number');
$form->removeElement('credit_card_number', TRUE);
$form->addElement($cc_field);
}
if ($form->elementExists('cvv2')) {
$cvv2_field = $form->getElement('cvv2');
$form->removeElement('cvv2', TRUE);
$form->addElement($cvv2_field);
}
}
}
else {
return;
}
}
/**
* Implementation of hook_civicrm_check().
*/
function tsys_civicrm_check(&$messages) {
// TODO add a system check to see if the root certificate is installed
// https://docs.tsysmerchant.com/knowledge-base/faqs/how-do-i-install-the-genius-root-certificate
// First get the Processors on this site
try {
$tsysProcesors = civicrm_api3('PaymentProcessor', 'get', [
'sequential' => 1,
'payment_processor_type_id' => "TSYS",
'is_test' => 0,
]);
}
catch (CiviCRM_API3_Exception $e) {
$error = $e->getMessage();
CRM_Core_Error::debug_log_message(E::ts('API Error %1', array(
'domain' => 'com.aghstrategies.tsys',
1 => $error,
)));
}
// If one or more payment processors are set up
if (!empty($tsysProcesors['values'])) {
$processors = [];
foreach ($tsysProcesors['values'] as $key => $processorDets) {
$processors[] = $processorDets['id'];
// Check for if Credentials are Good
$tsysCreds = CRM_Core_Payment_Tsys::getPaymentProcessorSettings($processorDets['id']);
$response = CRM_Tsys_Soap::composeReportByDate($tsysCreds);
if (isset($response->Body->CurrentBatchSummaryResponse->CurrentBatchSummaryResult->TransactionSummary4->ErrorMessage)
&& !empty($response->Body->CurrentBatchSummaryResponse->CurrentBatchSummaryResult->TransactionSummary4->ErrorMessage)) {
$messages[] = new CRM_Utils_Check_Message(
'failed_genius_creds',
E::ts('The Genius Credentials for Payment Processor ID %1 are incorrect please update them <a href=%2>here</a>.', [
'domain' => 'com.aghstrategies.tsys',
1 => $processorDets['id'],
2 => CRM_Utils_System::url('civicrm/admin/paymentProcessor', "action=update&id={$processorDets['id']}&reset=1"),
]),
E::ts('Invalid Genius Credentials for Payment Processor ID: %1', [
'domain' => 'com.aghstrategies.tsys',
1 => $processorDets['id'],
]),
\Psr\Log\LogLevel::WARNING,
'fa-user-times'
);
}
}
if (!empty($processors)) {
// This adds a System Status message if their are Recurring Contributions that are not processing as expected.
try {
$failedContributions = civicrm_api3('ContributionRecur', 'get', [
'sequential' => 1,
'contribution_status_id' => ['IN' => ["Failed", "Pending"]],
'payment_processor_id' => ['IN' => $processors],
]);
}
catch (CiviCRM_API3_Exception $e) {
$error = $e->getMessage();
CRM_Core_Error::debug_log_message(E::ts('API Error %1', array(
'domain' => 'com.aghstrategies.tsys',
1 => $error,
)));
}
if (!empty($failedContributions['values']) && $failedContributions['count'] > 0) {
$recurContributionToLookInto = [];
foreach ($failedContributions['values'] as $key => $value) {
// check that there is atleast one successful transaction for the recurring contribution
try {
$payments = civicrm_api3('Contribution', 'get', [
'contribution_recur_id' => $value['id'],
'contribution_status_id' => "Completed",
]);
}
catch (CiviCRM_API3_Exception $e) {
$error = $e->getMessage();
CRM_Core_Error::debug_log_message(ts('API Error %1', array(
'domain' => 'com.aghstrategies.tsys',
1 => $error,
)));
}
if (!empty($payments['count']) && $payments['count'] > 0) {
$recurContributionToLookInto[] = $value['id'];
}
}
if (!empty($recurContributionToLookInto)) {
$recurContributionToLookInto = implode(', ', $recurContributionToLookInto);
$warningLevel = \Psr\Log\LogLevel::NOTICE;
if ($failedContributions['count'] > 3) {
$warningLevel = \Psr\Log\LogLevel::WARNING;
}
if ($failedContributions['count'] > 5) {
$warningLevel = \Psr\Log\LogLevel::ERROR;
}
$tsParams = array(
1 => $failedContributions['count'],
2 => $recurContributionToLookInto,
);
$details = E::ts('%1 Recurring Contribution(s) not successfully processed including the following recurring contribution(s): %2. <br></br> For more information run a "Recurring Contributions" report and filter for "Contribution Status" of "Pending"', $tsParams);
$messages[] = new CRM_Utils_Check_Message(
'failed_recurring_contributions_found',
$details,
E::ts('Uncompleted Recurring Genius Contributions Found', array('domain' => 'com.aghstrategies.tsys')),
$warningLevel,
'fa-user-times'
);
}
}
}
}
}
/**
* Implements hook_civicrm_managed().
*
* Generate a list of entities to create/deactivate/delete when this module
* is installed, disabled, uninstalled.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_managed
*/
function tsys_civicrm_managed(&$entities) {
// Creates the payment processor entity for the Tsys Payment Processor
$entities[] = array(
'module' => 'com.aghstrategies.tsys',
'name' => 'TSYS',
'entity' => 'PaymentProcessorType',
'params' => array(
'version' => 3,
'name' => 'TSYS',
'title' => 'TSYS',
'description' => 'TSYS Payment Processor',
'class_name' => 'Payment_Tsys',
'billing_mode' => 'form',
'user_name_label' => 'Merchant Name',
'password_label' => 'Web API Key',
'signature_label' => 'Merchant Site Key',
'subject_label' => 'Merchant Site ID',
'url_site_default' => 'https://cayan.accessaccountdetails.com/',
'url_recur_default' => 'https://cayan.accessaccountdetails.com/',
'url_site_test_default' => 'https://cayan.accessaccountdetails.com/',
'url_recur_test_default' => 'https://cayan.accessaccountdetails.com/',
'is_recur' => 1,
'payment_type' => 1,
),
'metadata' => array(
'suppress_submit_button' => 1,
'payment_fields' => ['payment_token'],
),
);
return;
}
/**
* Implements hook_civicrm_navigationMenu().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_navigationMenu
*
*/
function tsys_civicrm_navigationMenu(&$menu) {
_tsys_civix_insert_navigation_menu($menu, 'Administer', [
'label' => E::ts('Genius Settings'),
'name' => 'tsys-settings',
'url' => 'civicrm/tsyssettings',
'permission' => 'administer payment processors',
// 'operator' => 'OR',
'separator' => 1,
]);
_tsys_civix_insert_navigation_menu($menu, 'Contributions', [
'label' => E::ts('Submit Credit Card Contribution Via Device'),
'name' => 'tsys-device',
'url' => 'civicrm/tsysdevice',
'permission' => 'administer payment processors',
// 'operator' => 'OR',
'separator' => 1,
]);
_tsys_civix_navigationMenu($menu);
}
/**
* Implements hook_civicrm_install().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
*/
function tsys_civicrm_install() {
_tsys_civix_civicrm_install();
}
/**
* Implements hook_civicrm_enable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
*/
function tsys_civicrm_enable() {
_tsys_civix_civicrm_enable();
}
/**
* Implements hook_civicrm_config().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function tsys_civicrm_config(&$config) {
_tsys_civix_civicrm_config($config);
}
// --- Functions below this ship commented out. Uncomment as required. ---
/**
* Implements hook_civicrm_preProcess().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_preProcess
*
// */