-
Notifications
You must be signed in to change notification settings - Fork 5
/
moneris.php
157 lines (146 loc) · 5.74 KB
/
moneris.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
<?php
require_once 'moneris.civix.php';
/**
* Implementation of hook_civicrm_config
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function moneris_civicrm_config(&$config) {
_moneris_civix_civicrm_config($config);
}
/**
* Implementation of hook_civicrm_install
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
*/
function moneris_civicrm_install() {
return _moneris_civix_civicrm_install();
}
/**
* Implementation of hook_civicrm_enable
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
*/
function moneris_civicrm_enable() {
return _moneris_civix_civicrm_enable();
}
/**
* Implementation of 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 moneris_civicrm_managed(&$entities) {
$entities[] = array(
'module' => 'ca.civicrm.moneris',
'name' => 'Moneris',
'entity' => 'PaymentProcessorType',
'params' => array(
'version' => 3,
'name' => 'Moneris',
'title' => 'Moneris Credit Card',
'description' => 'Moneris credit card payment processor.',
'class_name' => 'Payment_Moneris',
'billing_mode' => 'form',
'user_name_label' => 'Store ID',
'password_label' => 'API Token',
'signature_label' => '',
'url_site_default' => 'https://www3.moneris.com/',
'url_recur_default' => 'https://www3.moneris.com/',
'url_site_test_default' => 'https://esqa.moneris.com/',
'url_recur_test_default' => 'https://esqa.moneris.com/',
'is_recur' => 1,
'payment_type' => 1,
),
);
return;
}
function _moneris_civicrm_nscd_fid() {
$codeVer = CRM_Utils_System::version();
return (version_compare($codeVer, '4.4') < 0) ? 'next_sched_contribution' : 'next_sched_contribution_date';
}
/*
* 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 _moneris_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 _moneris_civicrm_is_moneris($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
}
return ('Payment_Moneris' == $result['class_name']) ? 'Payment_Moneris' : '';
}
/*
* hook_civicrm_pre
*
* Handle special cases of creating contributions records (regular and recurring) when using Moneris
*
* 1. CiviCRM assumes all recurring contributions need to be confirmed using the IPN mechanism.
* This is not true for Moneris recurring contributions, because I'm testing with a capture first.
* So when creating a contribution that is part of a recurring series, test for status = 2, and set to status = 1 instead.
* Do this for the initial and recurring contribution record.
* The (subsequent) recurring contributions' status id is set explicitly in the job that creates it, and doesn't need this modification.
*
* 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 moneris_civicrm_pre($op, $objectName, $objectId, &$params) {
// since this function gets called a lot, quickly determine if I care about the record being created
if (('create' == $op) && ('Contribution' == $objectName || 'ContributionRecur' == $objectName) && !empty($params['contribution_status_id'])) {
// watchdog('moneris_civicrm','hook_civicrm_pre for Contribution <pre>@params</pre>',array('@params' => print_r($params));
// figure out the payment processor id, not nice
$payment_processor_id = ('ContributionRecur' == $objectName) ? $params['payment_processor_id'] :
(!empty($params['payment_processor']) ? $params['payment_processor'] :
(!empty($params['contribution_recur_id']) ? _moneris_civicrm_get_payment_processor_id($params['contribution_recur_id']) :
0)
);
if (_moneris_civicrm_is_moneris($payment_processor_id)) {
switch ($objectName) {
case 'Contribution': // cc contribution, test if it's been set to status 2 on a recurring contribution
if ((2 == $params['contribution_status_id']) && !empty($params['contribution_recur_id'])) {
$params['contribution_status_id'] = 1;
}
break;
case 'ContributionRecur':
// calculate the date of the next schedule contribution
$params['contribution_status_id'] = 5;
// $params['trxn_id'] = NULL;
//TODO: next 20th of the month
// $next = strtotime('+'.$params['frequency_interval'].' '.$params['frequency_unit']);
// the next scheduled contribution date field name is civicrm version dependent
// $field_name = _moneris_civicrm_nscd_fid();
// $params[$field_name] = date('YmdHis',$next);
break;
}
}
}
}
// /**
// * Implements hook_civicrm_entityTypes().
// *
// * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_entityTypes
// */
// function moneris_civicrm_entityTypes(&$entityTypes) {
// _moneris_civix_civicrm_entityTypes($entityTypes);
// }