-
Notifications
You must be signed in to change notification settings - Fork 3
/
btcpay.php
328 lines (280 loc) · 9.88 KB
/
btcpay.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
<?php
/**
* Package: BTCPay (CiviCRM Extension)
* Copyright (C) 2020, Kofi Oghenerukevwe <[email protected]>
* Licensed under the GNU Affero Public License 3.0
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, version 3 of the license.
*
* This program 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
* along with this program. If not, see <https://www.gnu.org/licenses/>.
**/
require_once 'btcpay.civix.php';
require_once __DIR__ . '/vendor/autoload.php';
// phpcs:disable
use CRM_Btcpay_ExtensionUtil as E;
// phpcs:enable
/**
* Implements hook_civicrm_config().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_config/
*/
function btcpay_civicrm_config(&$config) {
_btcpay_civix_civicrm_config($config);
}
/**
* Implements hook_civicrm_xmlMenu().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_xmlMenu
*/
function btcpay_civicrm_xmlMenu(&$files) {
_btcpay_civix_civicrm_xmlMenu($files);
}
/**
* Implements hook_civicrm_install().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_install
*/
function btcpay_civicrm_install() {
_btcpay_civix_civicrm_install();
}
/**
* Implements hook_civicrm_postInstall().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_postInstall
*/
function btcpay_civicrm_postInstall() {
CRM_Core_Payment_Btcpay::createPaymentInstrument(['name' => 'Bitcoin']);
_btcpay_civix_civicrm_postInstall();
}
/**
* Implements hook_civicrm_uninstall().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_uninstall
*/
function btcpay_civicrm_uninstall() {
_btcpay_civix_civicrm_uninstall();
}
/**
* Implements hook_civicrm_enable().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable
*/
function btcpay_civicrm_enable() {
_btcpay_civix_civicrm_enable();
}
/**
* Implements hook_civicrm_disable().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_disable
*/
function btcpay_civicrm_disable() {
_btcpay_civix_civicrm_disable();
}
/**
* Implements hook_civicrm_upgrade().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_upgrade
*/
function btcpay_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
return _btcpay_civix_civicrm_upgrade($op, $queue);
}
/**
* Implements hook_civicrm_managed().
*
* Generate a list of entities to create/deactivate/delete when this module
* is installed, disabled, uninstalled.
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_managed
*/
function btcpay_civicrm_managed(&$entities) {
_btcpay_civix_civicrm_managed($entities);
}
/**
* Implements hook_civicrm_caseTypes().
*
* Generate a list of case-types.
*
* Note: This hook only runs in CiviCRM 4.4+.
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_caseTypes
*/
function btcpay_civicrm_caseTypes(&$caseTypes) {
_btcpay_civix_civicrm_caseTypes($caseTypes);
}
/**
* Implements hook_civicrm_angularModules().
*
* Generate a list of Angular modules.
*
* Note: This hook only runs in CiviCRM 4.5+. It may
* use features only available in v4.6+.
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_angularModules
*/
function btcpay_civicrm_angularModules(&$angularModules) {
_btcpay_civix_civicrm_angularModules($angularModules);
}
/**
* Implements hook_civicrm_alterSettingsFolders().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_alterSettingsFolders
*/
function btcpay_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
_btcpay_civix_civicrm_alterSettingsFolders($metaDataFolders);
}
/**
* Implements hook_civicrm_entityTypes().
*
* Declare entity types provided by this module.
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_entityTypes
*/
function btcpay_civicrm_entityTypes(&$entityTypes) {
_btcpay_civix_civicrm_entityTypes($entityTypes);
}
/**
* Implements hook_civicrm_thems().
*/
function btcpay_civicrm_themes(&$themes) {
_btcpay_civix_civicrm_themes($themes);
}
/**
* Implements hook_civicrm_check().
*/
function btcpay_civicrm_check(&$messages) {
CRM_Btcpay_Utils_Check_Requirements::check($messages);
}
/**
* Add {payment_library}.js to forms, for payment processor handling
* hook_civicrm_alterContent is not called for all forms (eg.
* CRM_Contribute_Form_Contribution on backend)
*
* @param string $formName
* @param CRM_Core_Form $form
*/
function btcpay_civicrm_buildForm($formName, &$form) {
if (!isset($form->_paymentProcessor)) {
return;
}
$paymentProcessor = $form->_paymentProcessor;
if (empty($paymentProcessor['class_name']) || ($paymentProcessor['class_name'] !== 'Payment_Btcpay')) {
return;
}
switch ($formName) {
case 'CRM_Event_Form_Registration_Confirm':
Civi::log()
->debug("====================================MODIFYING EVENT CONFIRMATION FORM");
case 'CRM_Contribute_Form_Contribution_Confirm':
Civi::log()
->debug("====================================MODIFYING CONTRIBUTION CONFIRMATION FORM");
$form->assign('btcpayServerUrl', $paymentProcessor["url_site"]);
Civi::resources()->add(
[
'template' => 'Btcpaycontribution-confirm-billing-block.tpl',
'region' => 'form-top',
]
);
break;
case 'CRM_Event_Form_Registration_ThankYou':
Civi::log()
->debug("====================================MODIFYING EVENT THANK YOU FORM");
case 'CRM_Contribute_Form_Contribution_ThankYou':
Civi::log()
->debug("====================================MODIFYING CONTRIBUTION THANK YOU FORM");
if (!isset($billingBlockRegion)) {
$billingBlockRegion = 'contribution-thankyou-billing-block';
}
// Contribution Thankyou form
// Add the Btcpay invoice handling
$trxnId = isset($form->_trxnId) ? $form->_trxnId : NULL;
if (empty($trxnId) && $formName == "CRM_Contribute_Form_Contribution_ThankYou") {
$contributionParams = [
'contact_id' => $form->_contactID,
'total_amount' => $form->_amount,
'contribution_test' => '',
'options' => ['limit' => 1, 'sort' => ['id DESC']],
];
$contribution = civicrm_api3('Contribution', 'get', $contributionParams);
$trxnId = CRM_Utils_Array::first($contribution['values'])['trxn_id'];
}
$client = new CRM_Btcpay_Client($form->_paymentProcessor);
$paymentInfo = $client->getClient()->getInvoicePaymentInfo($trxnId);
$form->assign('btcpayTrxnId', $trxnId);
$form->assign('btcpayServerUrl', $paymentProcessor["url_site"]);
$form->assign('btcpayPrice', $paymentInfo['price']);
$form->assign('btcpayCurrency', $paymentInfo['currency']);
$form->assign('btcpayCryptoInfo', $paymentInfo['cryptoInfo']);
$form->assign('btcpayPaymentUrl', $paymentInfo["url"]);
Civi::resources()->add(
[
'template' => 'Btcpaycontribution-thankyou-billing-block.tpl',
'region' => 'form-top',
]
);
break;
}
}
/**
* Implements hook_civicrm_postProcess().
*/
function btcpay_civicrm_postProcess($formName, &$form) {
switch ($formName) {
case 'CRM_Event_Form_Registration_Confirm':
Civi::log()
->debug("====================================UPDATING PARTICIPANT AND CONTRIBUTION STATUS FOR EVENT\n");
// update the Contribution and Participants' status for the event to Pending
$contributionId = CRM_Utils_Array::value("contributionId", $form->_values);
$participantParams = CRM_Utils_Array::value("participant", $form->_values);
$mainParticipant = $participantParams;
$participants = [];
if (isset($participantParams["participant_registered_by_id"])) {
// get the main participant who did the registering
$mainParticipant = civicrm_api3("Participant", "getsingle", [
"id" => $participantParams["participant_registered_by_id"],
]);
// get participants that were registered by the main participant
$result = civicrm_api3('Participant', 'get', [
'sequential' => 1,
'registered_by_id' => $mainParticipant["id"],
]);
$participants = $result["values"];
}
$participants[] = $mainParticipant;
// update all the participants' status to pending - incomplete transaction
foreach ($participants as $participant) {
$participantParams = [
"id" => $participant["id"],
"status_id" => 6,
];
civicrm_api3('Participant', 'create', $participantParams);
}
// delete the former contribution and create a new pending one
// the api won't allow me directly update the status of the contribution, this is the only way to do it.
$contributionParams = [
"id" => $contributionId,
];
$contribution = civicrm_api3('Contribution', 'getsingle', $contributionParams);
civicrm_api3("Contribution", "delete", $contributionParams);
Civi::log()
->debug("\n" . "CONTRIBUTION ARRAY" . "\n\n" . print_r($contribution, TRUE));
$newContribution = $contribution;
$newContribution["contribution_status_id"] = 2;
unset($newContribution["id"]);
unset($newContribution["contribution_id"]);
unset($newContribution["contribution_status"]);
unset($newContribution["amount_level"]);
$newContribution = civicrm_api3("Contribution", "create", $newContribution);
$result = civicrm_api3('ParticipantPayment', 'create', [
'participant_id' => $mainParticipant["id"],
'contribution_id' => $newContribution["id"],
]);
}
}