-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathsagepay.php
More file actions
executable file
·251 lines (194 loc) · 8.54 KB
/
sagepay.php
File metadata and controls
executable file
·251 lines (194 loc) · 8.54 KB
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
<?php
/**
* Sagepay payment processor for CiviCRM
* @author andyw@circle, 01/07/2016
*/
use Civi\Payment\System;
// Enable logging
define('CIVICRM_SAGEPAY_LOGGING', true);
// Number of failures after which to cancel a recurring payment
define('CIVICRM_SAGEPAY_RECURRING_MAX_FAILURES', 10);
/**
* Job api callback for running recurring payments via a Scheduled Task
* @param array $params currently unused
*/
function civicrm_api3_job_process_sagepay_recurring_payments($params) {
foreach (CRM_Core_Payment_Sagepay::getIDs() as $ppID)
System::singleton()->getById($ppID)->handlePaymentCron();
}
/**
* Implementation of hook_civicrm_config
*/
function sagepay_civicrm_config(&$config) {
# initialize include path
set_include_path(__DIR__ . '/custom/php/' . PATH_SEPARATOR . get_include_path());
# initialize template path
$templates = &CRM_Core_Smarty::singleton()->template_dir;
if (!is_array($templates))
$templates = [$templates];
array_unshift($templates, __DIR__ . '/custom/templates');
}
/**
* Implementation of hook_civicrm_enable
* Perform upgrade tasks here (if applicable) - because version 4.0 changes extension type from 'payment' to
* 'module', it didn't seem possible to do this via hook_civicrm_upgrade
*/
function sagepay_civicrm_enable() {
# if an upgrade from < 4.0 is needed
if (CRM_Core_DAO::singleValueQuery("
SELECT 1 FROM civicrm_payment_processor_type WHERE class_name = 'uk.co.circleinteractive.payment.sagepay'
")) {
# perform upgrade tasks
foreach ([
"Update payment processor type record for Sagepay processor." =>
"UPDATE civicrm_payment_processor_type
SET class_name='Payment_Sagepay',
url_site_test_default='https://test.sagepay.com/gateway/service/vspserver-register.vsp',
url_recur_test_default='https://test.sagepay.com/gateway/service/repeat.vsp'
WHERE class_name='uk.co.circleinteractive.payment.sagepay'",
"Update payment processor record for Sagepay processor (live)." =>
"UPDATE civicrm_payment_processor
SET class_name='Payment_Sagepay'
WHERE class_name='uk.co.circleinteractive.payment.sagepay'
AND !is_test",
"Update payment processor record for Sagepay processor (test)." =>
"UPDATE civicrm_payment_processor
SET class_name='Payment_Sagepay',
url_site='https://test.sagepay.com/gateway/service/vspserver-register.vsp',
url_recur='https://test.sagepay.com/gateway/service/repeat.vsp'
WHERE class_name='uk.co.circleinteractive.payment.sagepay'
AND is_test",
"Changing type of Sagepay extension from 'payment' to 'module'." =>
"UPDATE civicrm_extension
SET `type`='module'
WHERE full_name='uk.co.circleinteractive.payment.sagepay'",
"Changing api action for scheduled job." =>
"UPDATE civicrm_job
SET api_action='process_sagepay_recurring_payments', parameters = NULL
WHERE api_action='run_payment_cron'"
] as $task => $query) {
CRM_Core_Error::debug_log_message($task);
CRM_Core_DAO::executeQuery($query);
}
}
}
function sagepay_civicrm_disable() {
$config = null;
sagepay_civicrm_config($config);
}
/**
* Implementation of hook_civicrm_install
*/
function sagepay_civicrm_install() {
CRM_Core_DAO::executeQuery("
CREATE TABLE IF NOT EXISTS `civicrm_sagepay` (
`id` int(10) unsigned NOT NULL auto_increment,
`created` datetime NOT NULL,
`data_type` varchar(16) NOT NULL,
`entity_type` varchar(32) NOT NULL,
`entity_id` int(10) unsigned NOT NULL,
`data` longtext NOT NULL,
PRIMARY KEY (`id`),
KEY `entity_id` (`entity_id`),
KEY `data_type` (`data_type`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;
");
civicrm_api3('PaymentProcessorType', 'create', [
'name' => 'Sagepay',
'title' => 'Sagepay',
'description' => 'Sagepay payment processor (using the Server protocol)',
'class_name' => 'Payment_Sagepay',
'billing_mode' => 4,
'user_name_label' => 'Vendor ID',
'url_site_default' => 'https://live.sagepay.com/gateway/service/vspserver-register.vsp',
'url_recur_default' => 'https://live.sagepay.com/gateway/service/repeat.vsp',
'url_site_test_default' => 'https://test.sagepay.com/gateway/service/vspserver-register.vsp',
'url_recur_test_default' => 'https://test.sagepay.com/gateway/service/repeat.vsp',
'is_recur' => 1,
'payment_type' => 1,
'is_active' => 1
]);
}
/**
* Implementations of hook_civicrm_uninstall
*/
function sagepay_civicrm_uninstall() {
civicrm_api3('PaymentProcessorType', 'delete', [
'id' => civicrm_api3('PaymentProcessorType', 'getvalue', [
'class_name' => 'Payment_Sagepay',
'return' => 'id'
])
]);
}
/**
* Implementation of hook_civicrm_managed
*/
function sagepay_civicrm_managed(&$entities) {
// this doesn't work unfortunately, but does when you pass the exact same params
// to civicrm_api3() - so just doing that in the install hook for now.
// would be great if this stuff actually worked though!
/*
$entities[] = [
'module' => 'uk.co.circleinteractive.payment.sagepay',
'name' => 'Sagepay',
'entity' => 'PaymentProcessorType',
'params' => [
'version' => 3,
'name' => 'Sagepay',
'title' => 'Sagepay',
'description' => 'Sagepay payment processor (using the Server protocol)',
'class_name' => 'Payment_Sagepay',
'billing_mode' => 'form',
'user_name_label' => 'Vendor ID',
'url_site_default' => 'https://live.sagepay.com/gateway/service/vspserver-register.vsp',
'url_recur_default' => 'https://live.sagepay.com/gateway/service/repeat.vsp',
'url_site_test_default' => 'https://test.sagepay.com/gateway/service/vspserver-register.vsp',
'url_recur_test_default' => 'https://test.sagepay.com/gateway/service/repeat.vsp',
'is_recur' => 1,
'payment_type' => 1,
'is_active' => 1
]
];
*/
$entities[] = [
'module' => 'uk.co.circleinteractive.payment.sagepay',
'name' => 'Sagepay',
'entity' => 'Job',
'update' => 'never',
'params' => [
'version' => 3,
'name' => ts('Process Sagepay Recurring Payments'),
'description' => ts('Processes any Sagepay recurring payments that are due'),
'run_frequency' => 'Always',
'api_entity' => 'job',
'api_action' => 'process_sagepay_recurring_payments',
'is_active' => 1
]
];
}
/**
* Helper function for logging
*/
function sagepay_log($message) {
if (defined('CIVICRM_SAGEPAY_LOGGING') and CIVICRM_SAGEPAY_LOGGING) {
CRM_Core_Error::debug_log_message($message);
if (function_exists('watchdog'))
watchdog('civicrm_sagepay', $message, [], WATCHDOG_INFO);
}
}
/**
* Class uk_co_circleinteractive_payment_sagepay
* This is here to prevent 500 errors before the upgrade is run
*/
class uk_co_circleinteractive_payment_sagepay extends CRM_Core_Payment {
public static function &singleton($mode = 'test', &$paymentProcessor, &$paymentForm = null, $force = false) {
$processorName = $paymentProcessor['name'];
if (is_null(self::$_singleton[$processorName]))
self::$_singleton[$processorName] = new uk_co_circleinteractive_payment_sagepay($mode, $paymentProcessor);
return self::$_singleton[$processorName];
}
public function __construct($mode, &$paymentProcessor) {}
public function checkConfig() {}
public function doDirectPayment(&$params) {}
public function doTransferCheckout(&$params, $component = 'contribute') {}
}