-
Notifications
You must be signed in to change notification settings - Fork 1
/
square.php
253 lines (231 loc) · 9.13 KB
/
square.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
<?php
require_once 'square.civix.php';
$autoload = __DIR__ . '/vendor/autoload.php';
if (file_exists($autoload)) {
require_once $autoload;
}
// phpcs:disable
use CRM_Square_ExtensionUtil as E;
// phpcs:enable
/**
* Implements hook_civicrm_config().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_config/
*/
function square_civicrm_config(&$config): void {
_square_civix_civicrm_config($config);
}
/**
* Implements hook_civicrm_install().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_install
*/
function square_civicrm_install(): void {
_square_civix_civicrm_install();
}
/**
* Implements hook_civicrm_enable().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable
*/
function square_civicrm_enable(): void {
_square_civix_civicrm_enable();
}
/**
* Implements hook_civicrm_check().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_check
*/
function square_civicrm_check(&$messages) {
CRM_Core_Payment_SquarePP::checkConfig2($messages);
//CRM_Square_Webhook::check($messages);
}
/**
* Implements hook_civicrm_managed().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_managed
*/
function square_civicrm_managed(&$entities): void {
Civi::log()->debug('square.php::civicrm_managed hook');
foreach ($entities as $entity) {
if($entity["module"] == 'me.sxs.square') {
Civi::log()->debug('square.php::civicrm_managed entity' . ' ' . print_r($entity, true));
}
}
}
/**
* Implements hook_civicrm_postinstall().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_postinstall
*/
function square_civicrm_postinstall(): void {
Civi::log()->debug('square.php::civicrm_postinstall hook');
// Check if a financial account "Square Account" exist.
// If not, create it.
$financial_accounts = \Civi\Api4\FinancialAccount::save(FALSE)
->addRecord([
'name' => 'Square Account',
'contact_id' => 1,
'financial_account_type_id' => 1,
'account_type_code' => 'BANK',
'description' => 'Square payment processor merchant account',
'is_reserved'=> false,
'is_active'=> true,
'is_default'=> false ])
->setMatch(['name'])
->execute();
foreach ($financial_accounts as $financial_account) {
Civi::log()->debug('square.php::post_install hook financial account' . ' ' . print_r($financial_account, true));
// Check if a payment instrument name "square terminal" exist.
// If not, create it.
$optionGroups = \Civi\Api4\OptionGroup::get(TRUE)
->addSelect('id')
->addWhere('name', '=', 'payment_instrument')
->execute();
foreach ($optionGroups as $optionGroup) {
Civi::log()->debug('square.php::post_install hook optionGroup' . ' ' . print_r($optionGroup, true));
Civi::log()->debug('square.php::post_install hook optionGroup[id]' . ' ' . print_r($optionGroup["id"], true));
$payment_instruments = \Civi\Api4\OptionValue::save(TRUE)
->addRecord([
'option_group_id' => $optionGroup["id"],
'label' => 'Square terminal',
'name' => 'Square terminal',
'grouping' => NULL,
'filter' => 0,
'is_default' => FALSE,
'description' => 'Payment made with onsite Square terminal',
'is_optgroup' => FALSE,
'is_reserved' => TRUE,
'is_active' => TRUE ])
->setMatch(['option_group_id', 'name'])
->execute();
foreach ($payment_instruments as $payment_instrument) {
Civi::log()->debug('square.php::post_install hook payment instrument' . ' ' . print_r($payment_instrument, true));
// do something
}
}
}
// Check if the payment processor type "Square Terminal" exist.
// If not, create it.
$paymentProcessorTypes = \Civi\Api4\PaymentProcessorType::save(TRUE)
->addRecord([
'name'=> 'Square Terminal',
'title'=> 'Square for Terminal Integration',
'description' => E::ts('Square payment processor for onsite terminal integration'),
'is_active'=> true,
'is_default'=> false,
'user_name_label'=> 'Access Token',
'signature_label'=> 'Webhook Signature Key',
'class_name'=> 'Payment_SquarePP',
'url_site_default' => 'https://unused.org',
'url_api_default'=> 'https://connect.squareup.com',
'url_site_test_default' => 'https://unused.org',
'url_api_test_default'=> 'https://connect.squareupsandbox.com',
'billing_mode'=> 4,
'is_recur'=> 0,
'payment_type' => 1,
'payment_instrument_id'=> $payment_instrument["id"] ])
->setMatch(['name'])
->execute();
foreach ($paymentProcessorTypes as $paymentProcessorType) {
Civi::log()->debug('square.php::post_install hook payment processor type' . ' ' . print_r($paymentProcessorType, true));
// do something
}
}
/**
* Implements hook_civicrm_postCommit().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_postCommit
*
* hook_civicrm_postCommit($op, $objectName, $objectId, &$objectRef)
*/
function square_civicrm_postCommit(string $op, string $objectName, int $objectId, &$objectRef) {
Civi::log()->debug('square.php::postCommit hook op ' . print_r($op, true));
Civi::log()->debug('square.php::postCommit hook objectName ' . print_r($objectName, true));
Civi::log()->debug('square.php::postCommit hook objectId ' . print_r($objectId, true));
if ($objectName == 'PaymentProcessor' && $op == 'create') {
if ($objectRef->class_name == 'Payment_SquarePP') {
if(!$objectRef->is_test) {
Civi::log()->debug('square.php::postCommit hook objectRef ' . print_r($objectRef, true));
Civi::log()->debug('square.php::postCommit hook objectRef class_name ' . print_r($objectRef->class_name, true));
// Test for webhook presence
$paymentProcessors = get_object_vars($objectRef);
CRM_Square_Webhook::myUpdateWebhookSubscription($paymentProcessors);
// Test for syncing Square Id with contact
}
}
}
}
/**
* Implements hook_civicrm_postIPNProcess().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_postIPNProcess
*/
function square_civicrm_postIPNProcess(&$IPNData) {
Civi::log()->debug('square.php::postIPNProcess hook' . ' ' . print_r($IPNData, true));
if (!empty($IPNData['custom'])) {
$customParams = json_decode($IPNData['custom'], TRUE);
if (!empty($customParams['gaid'])) {
Civi::log()->debug('square.php::postIPNProcess hook json decode' . ' ' . print_r($customParams["gaid"], true));
// do something
}
}
}
/**
* Implements hook_civicrm_postProcess().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_postIPNProcess
*
* @param string $formName
* @param CRM_Core_Form $form
*/
//function square_civicrm_postProcess($formName, $form) {
// Civi::log()->debug('square.php::postProcess hook formName' . ' ' . print_r($formName, true));
// Civi::log()->debug('square.php::postProcess hook form' . ' ' . print_r($form, true));
//}
/**
* Implements hook_civicrm_alterContent().
*
* @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_alterContent
*/
function square_civicrm_alterContent(&$content, $context, &$tplName, &$object) {
// Civi::log()->debug('square.php::civicrm_alterContent Hook templateName' . ' ' . print_r($tplName, true));
// Civi::log()->debug('squere.php::civicrm_alterContent Hook context' . ' ' . print_r($context, true));
if ($context == "form") {
if ($tplName == "CRM/Event/Form/Registration/Confirm.tpl") {
$closuremethod = Closure::bind(function($object) {
return $object->_params;
}, null, get_class($object));
$lParams = $closuremethod($object);
Civi::log()->debug('square.php::civicrm_alterContent Hook lParams.0.InvoiceID' . ' ' . print_r( $lParams[0]["invoiceID"], true));
//Civi::log()->debug('square.php::civicrm_alterContent Hook lParams.0.InvoiceID' . ' ' . print_r( $accessProtected ($object, "invoiceID"), true));
$payInvoiceID = ts("Invoice ID") . ": " . $lParams[0]["invoiceID"];
$payContactID = ts("Client ID") . ": " . $lParams[0]["contact_id"];
$payAmount = ts("Total Amount") . ": " . $lParams[0]["amount"];
$payInstruction = ts("Please proceed to Square terminal and use these values to complete the transaction.");
// Find the div element with id "continue_message-section"
$pattern = '/<\s*div\s*class\s*=\s*".*\Qcontinue_message-section\E.*"\s*>\X*?<\s*\/div\s*>/';
$replacement = '$0<br/><div>' . $payInstruction . '<br/>' . $payContactID . '<br/>' . $payInvoiceID . '</div>';
// Replace div element with pay_instruction
$content = preg_replace ($pattern, $replacement, $content, 1);
}
else {
if($tplName == "CRM/Financial/Form/Payment.tpl") {
Civi::log()->debug('squere.php::civicrm_alterContent Hook $content' . ' ' . print_r($content, true));
// todo support other forms
}
}
}
}
/**
* Implements Closure::bind to access protected properties.
*
*/
{
$accessProtected = function & ($obj, $prop) {
$value = & Closure::bind(function & () use ($prop) {
return $this->$prop;
}, $obj, $obj)->__invoke();
return $value;
};
}