-
Notifications
You must be signed in to change notification settings - Fork 5
/
bitpay.php
226 lines (203 loc) · 6.56 KB
/
bitpay.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
<?php
require_once 'bitpay.civix.php';
$autoload = __DIR__ . '/vendor/autoload.php';
if (file_exists($autoload)) {
require_once $autoload;
}
use CRM_Bitpay_ExtensionUtil as E;
/**
* Implements hook_civicrm_config().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
*/
function bitpay_civicrm_config(&$config) {
_bitpay_civix_civicrm_config($config);
}
/**
* Implements hook_civicrm_install().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
*/
function bitpay_civicrm_install() {
_bitpay_civicrm_cleanupOldExtension();
_bitpay_civix_civicrm_install();
}
/**
* Implements hook_civicrm_postInstall().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_postInstall
*/
function bitpay_civicrm_postInstall() {
CRM_Core_Payment_Bitpay::createPaymentInstrument(['name' => 'Bitcoin']);
_bitpay_civix_civicrm_postInstall();
}
/**
* Implements hook_civicrm_uninstall().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_uninstall
*/
function bitpay_civicrm_uninstall() {
_bitpay_civix_civicrm_uninstall();
}
/**
* Implements hook_civicrm_enable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
*/
function bitpay_civicrm_enable() {
_bitpay_civix_civicrm_enable();
}
/**
* Implements hook_civicrm_disable().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_disable
*/
function bitpay_civicrm_disable() {
_bitpay_civix_civicrm_disable();
}
/**
* Implements hook_civicrm_upgrade().
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_upgrade
*/
function bitpay_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
return _bitpay_civix_civicrm_upgrade($op, $queue);
}
/**
* Implements hook_civicrm_entityTypes().
*
* Declare entity types provided by this module.
*
* @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_entityTypes
*/
function bitpay_civicrm_entityTypes(&$entityTypes) {
_bitpay_civix_civicrm_entityTypes($entityTypes);
}
/**
* 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 bitpay_civicrm_buildForm($formName, &$form) {
if (!isset($form->_paymentProcessor)) {
return;
}
$paymentProcessor = $form->_paymentProcessor;
if (empty($paymentProcessor['class_name']) || ($paymentProcessor['class_name'] !== 'Payment_Bitpay')) {
return;
}
switch ($formName) {
case 'CRM_Event_Form_Registration_Confirm':
case 'CRM_Contribute_Form_Contribution_Confirm':
// Confirm Contribution (check details and confirm)
CRM_Core_Region::instance('contribution-confirm-billing-block')
->update('default', ['disabled' => TRUE]);
CRM_Core_Region::instance('contribution-confirm-billing-block')
->add(['template' => 'Bitpaycontribution-confirm-billing-block.tpl']);
break;
case 'CRM_Event_Form_Registration_ThankYou':
$billingBlockRegion = 'event-thankyou-billing-block';
case 'CRM_Contribute_Form_Contribution_ThankYou':
if (!isset($billingBlockRegion)) {
$billingBlockRegion = 'contribution-thankyou-billing-block';
}
// Contribution /Event Thankyou form
// Add the bitpay invoice handling
$contributionParams = [
'contact_id' => $form->_contactID,
'total_amount' => $form->_amount,
'contribution_test' => '',
'options' => ['limit' => 1, 'sort' => ['id DESC']],
];
$trxnId = $form->trxnId ?? $form->_trxnId ?? NULL;
if (empty($trxnId)) {
$contribution = civicrm_api3('Contribution', 'get', $contributionParams);
$trxnId = CRM_Utils_Array::first($contribution['values'])['trxn_id'];
if (empty($trxnId)) {
\Civi::log('bitpay')->error('bitpay buildForm: Could not find trxnId');
}
}
$form->assign('bitpayTrxnId', $trxnId);
$form->assign('bitpayTestMode', $paymentProcessor['is_test']);
CRM_Core_Region::instance($billingBlockRegion)
->update('default', ['disabled' => TRUE]);
CRM_Core_Region::instance($billingBlockRegion)
->add(['template' => 'Bitpaycontribution-thankyou-billing-block.tpl']);
break;
}
}
/**
* Cleanup the old uk.co.circleinteractive.payment.bitcoin extension
*/
function _bitpay_civicrm_cleanupOldExtension() {
// Remove old scheduled job
try {
$jobId = civicrm_api3('job', 'getvalue', [
'api_entity' => 'job',
'api_action' => 'update_bitpay_invoices',
'return' => 'id',
]);
civicrm_api3('job', 'delete', [
'id' => $jobId,
]);
$successes[] = 'Deleted scheduled job update_bitpay_invoices';
} catch (CiviCRM_API3_Exception $e) {
$errors[] = 'Unable to delete scheduled job: ' . $e->getMessage();
}
// Uninstall existing bitcoin
try {
$bitcoinId = civicrm_api3('PaymentProcessorType', 'getvalue', [
'class_name' => 'Payment_BitcoinD',
'return' => 'id'
]);
civicrm_api3('PaymentProcessorType', 'create', [
'id' => $bitcoinId,
'class_name' => 'Payment_BitcoinD_Old',
]);
} catch (CiviCRM_API3_Exception $e) {
$errors[] = 'Could not get PaymentProcessorType for Payment_BitcoinD - it is not installed';
}
// Uninstall existing bitpay
try {
$bitPayOldId = civicrm_api3('PaymentProcessorType', 'getvalue', [
'class_name' => 'Payment_BitPay_Old',
'return' => 'id'
]);
if (!empty($bitPayOldId)) {
return;
}
} catch (CiviCRM_API3_Exception $e) {
// That's fine, we haven't already upgraded.
}
try {
$bitPayId = civicrm_api3('PaymentProcessorType', 'getvalue', [
'class_name' => 'Payment_BitPay',
'return' => 'id'
]);
civicrm_api3('PaymentProcessorType', 'create', [
'id' => $bitPayId,
'class_name' => 'Payment_BitPay_Old',
]);
} catch (CiviCRM_API3_Exception $e) {
$errors[] = 'Could not get PaymentProcessorType for Payment_BitPay - it is not installed';
}
// Clear caches
// Access config object.
$config = CRM_Core_Config::singleton();
// Clear database cache.
CRM_Core_Config::clearDBCache();
// Cleanup the "templates_c" directory.
$config->cleanup( 1, TRUE );
// Cleanup the session object.
$session = CRM_Core_Session::singleton();
$session->reset( 1 );
}
/**
* Implements hook_civicrm_check().
*/
function bitpay_civicrm_check(&$messages) {
$checks = new CRM_Bitpay_Check($messages);
$messages = $checks->checkRequirements();
}