forked from Project60/org.project60.sepa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsepa_pp_sdd.php
285 lines (247 loc) · 12.1 KB
/
sepa_pp_sdd.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
<?php
/*-------------------------------------------------------+
| Project 60 - SEPA direct debit |
| Copyright (C) 2013-2018 SYSTOPIA |
| Author: B. Endres (endres -at- systopia.de) |
| http://www.systopia.de/ |
+--------------------------------------------------------+
| This program is released as free software under the |
| Affero GPL license. You can redistribute it and/or |
| modify it under the terms of this license which you |
| can read by viewing the included agpl.txt or online |
| at www.gnu.org/licenses/agpl.html. Removal of this |
| copyright header is strictly prohibited without |
| written permission from the original author(s). |
+--------------------------------------------------------*/
/**
* Provides the SEPA payment processor
*
* @package CiviCRM_SEPA
*
*/
/**
* buildForm Hook for payment processor
*/
function sepa_pp_buildForm ( $formName, &$form ) {
if ($formName == "CRM_Admin_Form_PaymentProcessor") { // PAYMENT PROCESSOR CONFIGURATION PAGE
$pp = civicrm_api("PaymentProcessorType", "getsingle", array("id"=>$form->_ppType, "version"=>3));
if ($pp['class_name'] == "Payment_SDD") {
// that's ours!
// get payment processor id
$pp_id = $form->getVar('_id');
// find the associated creditor(s)
$creditor_id = NULL;
$test_creditor_id = NULL;
$pp_creditor = NULL;
$test_pp_creditor = NULL;
if (!empty($pp_id)) {
$creditor_id = CRM_Core_BAO_Setting::getItem('SEPA Direct Debit PP', 'pp'.$pp_id);
$test_creditor_id = CRM_Core_BAO_Setting::getItem('SEPA Direct Debit PP', 'pp_test'.$pp_id);
}
// load settings from creditor
if ($creditor_id) {
$pp_creditor = civicrm_api('SepaCreditor', 'getsingle', array('version'=>3, 'id'=>$creditor_id));
$test_pp_creditor = civicrm_api('SepaCreditor', 'getsingle', array('version'=>3, 'id'=>$test_creditor_id));
// TODO: ERROR HANDLING
}
$creditors = civicrm_api('SepaCreditor', 'get', array('version'=>3));
$creditors = $creditors['values'];
$test_creditors = civicrm_api('SepaCreditor', 'get', array('version'=>3, 'category'=>'TEST'));
$test_creditors = $test_creditors['values'];
// use settings
if ($pp_creditor) {
$form->assign('user_name', $creditor_id);
}
if ($test_pp_creditor) {
$form->assign('test_user_name', $test_creditor_id);
}
$form->assign('creditors', $creditors);
$form->assign('test_creditors', $test_creditors);
// add new elements
CRM_Core_Region::instance('page-body')->add(array(
'template' => 'CRM/Admin/Form/PaymentProcessor/SDD.tpl'
));
}
} elseif ($formName == "CRM_Contribute_Form_Contribution_Main") { // PAYMENT PROCESS MAIN PAGE
$mendForm = CRM_Core_BAO_Setting::getItem('SEPA Direct Debit Preferences', 'pp_improve_frequency');
if ($mendForm) {
// inject improved form logic
CRM_Core_Region::instance('page-body')->add(array(
'template' => 'CRM/Contribute/Form/ContributionMain.sepa.tpl'));
}
} elseif ($formName == "CRM_Contribute_Form_Contribution_Confirm") { // PAYMENT PROCESS CONFIRMATION PAGE
// check if the PP is ours
$pp_id = CRM_Utils_Array::value('payment_processor', $form->_params);
if (empty($pp_id)) {
// there is no payment processor?
return;
} else {
$pp = civicrm_api3('PaymentProcessor', 'getsingle', array('id' => $pp_id));
if (empty($pp['class_name']) || $pp['class_name'] != 'Payment_SDD') {
// this is not our processor
return;
}
}
// this IS our processor -> inject stuff
CRM_Core_Region::instance('page-body')->add(array(
'template' => 'CRM/Contribute/Form/ContributionConfirm.sepa.tpl'));
} elseif ($formName == "CRM_Event_Form_Registration_Confirm") { // EVENT REGISTRATION CONFIRMATION PAGE
// only for our SDD payment processors:
$pp = $form->getTemplate()->get_template_vars('paymentProcessor');
if ($pp['class_name'] != "Payment_SDD") return;
// FIXME: this is a gross hack, please help me if you know
// how to extract bank_bic and bank_iban variables properly...
$form_data = print_r($form,true);
$matches = array();
if (preg_match('/\[bank_identification_number\] => (?P<bank_identification_number>[\w0-9]+)/i', $form_data, $matches)) {
$form->assign("bank_identification_number",$matches[1]);
}
$matches = array();
if (preg_match('/\[bank_account_number\] => (?P<bank_account_number>[\w0-9]+)/i', $form_data, $matches)) {
$form->assign("bank_account_number",$matches[1]);
}
unset($form_data);
CRM_Core_Region::instance('page-body')->add(array(
'template' => 'CRM/Event/Form/RegistrationConfirm.sepa.tpl'));
} elseif ($formName == "CRM_Contribute_Form_Contribution_ThankYou") { // PAYMENT PROCESS THANK YOU PAGE
// check if the PP is ours
$pp_id = CRM_Utils_Array::value('payment_processor', $form->_params);
if (empty($pp_id)) {
// there is no payment processor?
return;
} else {
$pp = civicrm_api3('PaymentProcessor', 'getsingle', array('id' => $pp_id));
if (empty($pp['class_name']) || $pp['class_name'] != 'Payment_SDD') {
// this is not our processor
return;
}
}
// this IS ours
$mandate_reference = $form->getTemplate()->get_template_vars('trxn_id');
if ($mandate_reference) {
$mandate = civicrm_api3('SepaMandate', 'getsingle', array('reference' => $mandate_reference));
$creditor = civicrm_api3('SepaCreditor', 'getsingle', array('id' => $mandate['creditor_id']));
$contribution = civicrm_api3('Contribution', 'getsingle', array('trxn_id' => $mandate_reference));
$rcontribution = array(
'cycle_day' => CRM_Utils_Array::value('cycle_day', $form->_params),
'frequency_interval' => CRM_Utils_Array::value('frequency_interval', $form->_params),
'frequency_unit' => CRM_Utils_Array::value('frequency_unit', $form->_params),
'start_date' => CRM_Utils_Array::value('start_date', $form->_params));
$form->assign('mandate_reference', $mandate_reference);
$form->assign("bank_account_number", $mandate["iban"]);
$form->assign("bank_identification_number", $mandate["bic"]);
$form->assign("collection_day", CRM_Utils_Array::value('cycle_day', $form->_params));
$form->assign("frequency_interval", CRM_Utils_Array::value('frequency_interval', $form->_params));
$form->assign("frequency_unit", CRM_Utils_Array::value('frequency_unit', $form->_params));
$form->assign("creditor_id", $creditor['identifier']);
$form->assign("collection_date", $contribution['receive_date']);
$form->assign("cycle", CRM_Sepa_Logic_Batching::getCycle($rcontribution));
$form->assign("cycle_day", CRM_Sepa_Logic_Batching::getCycleDay($rcontribution, $creditor['id']));
}
CRM_Core_Region::instance('contribution-thankyou-billing-block')->add(array(
'template' => 'CRM/Contribute/Form/ContributionThankYou.sepa.tpl'));
} elseif ($formName == "CRM_Event_Form_Registration_ThankYou") { // EVENT REGISTRATION THANK YOU PAGE
// only for our SDD payment processors:
$pp = $form->getTemplate()->get_template_vars('paymentProcessor');
if ($pp['class_name'] != "Payment_SDD") return;
$mandate_reference = $form->getTemplate()->get_template_vars('trxn_id');
if ($mandate_reference) {
$mandate = civicrm_api3('SepaMandate', 'getsingle', array('reference' => $mandate_reference));
$creditor = civicrm_api3('SepaCreditor', 'getsingle', array('id' => $mandate['creditor_id']));
$contribution = civicrm_api3('Contribution', 'getsingle', array('trxn_id' => $mandate_reference));
$form->assign('mandate_reference', $mandate_reference);
$form->assign("bank_account_number", $mandate["iban"]);
$form->assign("bank_identification_number", $mandate["bic"]);
$form->assign("creditor_id", $creditor['identifier']);
$form->assign("collection_date", $contribution['receive_date']);
}
CRM_Core_Region::instance('page-body')->add(array(
'template' => 'CRM/Event/Form/RegistrationThankYou.sepa.tpl'));
}
}
/**
* postProcess Hook for payment processor
*/
function sepa_pp_postProcess( $formName, &$form ) {
if ("CRM_Admin_Form_PaymentProcessor" == $formName) {
$pp = civicrm_api("PaymentProcessorType", "getsingle", array("id"=>$form->_ppType, "version"=>3));
if ($pp['class_name'] = "Payment_SDD") {
$paymentProcessor = civicrm_api3('PaymentProcessor', 'getsingle',
array('name' => $form->_submitValues['name'], 'is_test' => 0));
$creditor_id = $form->_submitValues['user_name'];
$test_creditor_id = $form->_submitValues['test_user_name'];
$pp_id = $paymentProcessor['id'];
// save settings
// FIXME: we might consider saving this as a JSON object
CRM_Core_BAO_Setting::setItem($creditor_id, 'SEPA Direct Debit PP', 'pp'.$pp_id);
CRM_Core_BAO_Setting::setItem($test_creditor_id, 'SEPA Direct Debit PP', 'pp_test'.$pp_id);
}
} elseif ('CRM_Contribute_Form_Contribution_Confirm' == $formName) {
// post process the contributions created
CRM_Core_Payment_SDD::processPartialMandates();
} elseif ('CRM_Event_Form_Registration_Confirm' == $formName) {
// post process the contributions created
CRM_Core_Payment_SDD::processPartialMandates();
}
}
/**
* Will install the SEPA payment processor
*/
function sepa_pp_install() {
$sdd_pp = civicrm_api('PaymentProcessorType', 'getsingle', array('name'=>'sepa_direct_debit', 'version' => 3));
if (!empty($sdd_pp['is_error'])) {
// doesn't exist yet => create
$payment_processor_data = array(
"version" => 3,
"name" => "SEPA_Direct_Debit",
"title" => ts("SEPA Direct Debit"),
"description" => ts("Payment processor for the 'Single European Payement Area' (SEPA)."),
"is_active" => 1,
"user_name_label" => "SEPA Creditor identifier",
"class_name" => "Payment_SDD",
"url_site_default" => "",
"url_recur_default" => "",
"url_site_test_default" => "",
"url_recur_test_default" => "",
"billing_mode" => "1",
"is_recur" => "1",
"payment_type" => CRM_Core_Payment::PAYMENT_TYPE_DIRECT_DEBIT
);
$result = civicrm_api('PaymentProcessorType', 'create', $payment_processor_data);
if (!empty($result['is_error'])) {
// something went wrong here...
CRM_Core_Error::debug_log_message("org.project60.sepa_dd: payment processor with name 'SEPA_Direct_Debit' could not be created. Error was ".$result['error_message']);
} else {
CRM_Core_Error::debug_log_message("org.project60.sepa_dd: created payment processor with name 'SEPA_Direct_Debit'");
}
} else {
// already exists => enable if not enabled
if (!$sdd_pp['is_active']) {
$result = civicrm_api('PaymentProcessorType', 'create', array('id'=>$sdd_pp['id'], 'is_active'=>1, 'version' => 3));
if (!empty($result['is_error'])) {
// something went wrong here...
CRM_Core_Error::debug_log_message("org.project60.sepa_dd: payment processor with name 'SEPA_Direct_Debit' created.");
}
}
}
}
/**
* Will disable the SEPA payment processor
*/
function sepa_pp_disable() {
// get payment processor...
$sdd_pp = civicrm_api('PaymentProcessorType', 'getsingle', array('name'=>'SEPA_Direct_Debit', 'version' => 3));
if (empty($sdd_pp['is_error'])) {
// ... and disable, if active. We can't delete it, because there might be donation pages linked to it.
if ($sdd_pp['is_active']) {
$result = civicrm_api('PaymentProcessorType', 'create', array('id'=>$sdd_pp['id'], 'is_active'=>0, 'version' => 3));
if (!empty($result['is_error'])) {
// something went wrong here...
CRM_Core_Error::debug_log_message("org.project60.sepa_dd: payment processor with name 'SEPA_Direct_Debit' could not be disabled. Error was ".$result['error_message']);
}
}
} else {
// huh? payment processor does not exist (any more)?
CRM_Core_Error::debug_log_message("org.project60.sepa_dd: payment processor with name 'SEPA_Direct_Debit' has gone missing.");
}
}