-
Notifications
You must be signed in to change notification settings - Fork 0
/
braintree_payment.module
244 lines (222 loc) · 7.19 KB
/
braintree_payment.module
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
<?php
/**
* @file
* Defines Credit Card Controller Information and libraries.
*/
use Drupal\braintree_payment\ApplePayController;
use Drupal\braintree_payment\CreditCardController;
use Drupal\braintree_payment\GooglePayController;
/**
* Implemnts hook_theme().
*/
function braintree_payment_theme() {
$hooks['braintree_payment_hosted_field'] = [
'render element' => 'element',
'template' => 'braintree-payment-hosted-field',
'file' => 'braintree_payment.theme.inc',
];
return $hooks;
}
/**
* Implements hook_payment_method_controller_info().
*/
function braintree_payment_payment_method_controller_info() {
$info['braintree_payment_credit_card'] = CreditCardController::class;
$info['braintree_payment_google_pay'] = GooglePayController::class;
$info['braintree_payment_apple_pay'] = ApplePayController::class;
return $info;
}
/**
* Implements hook_libraries_info().
*
* For braintree php wrapper library.
*/
function braintree_payment_libraries_info() {
$libraries['braintree-php'] = array(
'name' => 'Braintree',
'vendor url' => 'https://www.braintreepayments.com/',
'download url' => 'https://developers.braintreepayments.com/start/hello-server/php',
'version arguments' => array(
'file' => 'CHANGELOG.md',
'pattern' => '/([0-9\.]+)/',
'lines' => 1,
),
'xautoload' => function ($adapter) {
$adapter->composerJson('composer.json');
},
);
return $libraries;
}
/**
* Implements hook_menu().
*/
function braintree_payment_menu() {
$items = [];
// Provide the Domain Association File for Apple Pay.
$apple_pay_methods = array_filter(entity_load('payment_method'), function ($m) {
return $m->controller instanceof ApplePayController && $m->enabled;
});
if (!empty($apple_pay_methods)) {
$items['.well-known/apple-developer-merchantid-domain-association'] = [
'title' => 'Apple Developer MerchantID Domain Association',
'access callback' => TRUE,
'page callback' => 'braintree_payment_apple_pay_domain_association_file',
];
}
return $items;
}
/**
* Menu callback returning the Braintree Domain Association File for Apple Pay.
*
* The file is a copy of
* https://assets.braintreegateway.com/web/static/apple-pay/apple-developer-merchantid-domain-association.
*/
function braintree_payment_apple_pay_domain_association_file() {
drupal_add_http_header('Content-type', 'application/octet-stream');
$path = drupal_get_path('module', 'braintree_payment');
$path .= '/static/apple-developer-merchantid-domain-association';
readfile($path);
drupal_page_footer();
exit;
}
/**
* Implements hook_entity_load().
*/
function braintree_payment_entity_load(array $entities, $entity_type) {
if ($entity_type == 'payment') {
$query = db_select('braintree_payment', 't')
->fields('t')
->condition('pid', array_keys($entities));
$result = $query->execute();
while ($data = $result->fetchAssoc()) {
$payment = $entities[$data['pid']];
$payment->braintree = [
'braintree_id' => $data['braintree_id'],
'type' => $data['type'],
'plan_id' => $data['plan_id'],
];
}
}
}
/**
* Implements hook_payment_insert().
*/
function braintree_payment_payment_insert(Payment $payment) {
if (_braintree_payment_controlled_payment_method($payment->method)) {
$data = !empty($payment->braintree) ? $payment->braintree : [];
$data += [
'braintree_id' => '',
'type' => '',
'plan_id' => '',
];
$data['pid'] = $payment->pid;
db_insert('braintree_payment')->fields($data)->execute();
}
}
/**
* Implements hook_payment_update().
*/
function braintree_payment_payment_update(Payment $payment) {
if (_braintree_payment_controlled_payment_method($payment->method)) {
$data = !empty($payment->braintree) ? $payment->braintree : [];
$data += [
'braintree_id' => '',
'type' => '',
'plan_id' => '',
];
db_update('braintree_payment')
->fields($data)
->condition('pid', $payment->pid)
->execute();
}
}
/**
* Implements hook_payment_delete().
*/
function braintree_payment_payment_delete(Payment $payment) {
db_delete('braintree_payment')->condition('pid', $payment->pid)->execute();
}
/**
* Implements hook_campaignion_logcrm_payment_event_data_alter().
*/
function braintree_payment_campaignion_logcrm_payment_event_data_alter(array &$data, Payment $payment) {
if (_braintree_payment_controlled_payment_method($payment->method)) {
$data['braintree_id'] = NULL;
if (!empty($payment->braintree['braintree_id'])) {
$data['braintree_id'] = $payment->braintree['braintree_id'];
}
}
}
/**
* Implements hook_payment_method_insert().
*/
function braintree_payment_payment_method_insert(PaymentMethod $method) {
if ($method->controller instanceof ApplePayController) {
variable_set('menu_rebuild_needed', TRUE);
}
}
/**
* Implements hook_payment_method_update().
*/
function braintree_payment_payment_method_update(PaymentMethod $method) {
if ($method->controller instanceof ApplePayController) {
variable_set('menu_rebuild_needed', TRUE);
}
}
/**
* Implements hook_payment_method_delete().
*/
function braintree_payment_payment_method_delete(PaymentMethod $method) {
if ($method->controller instanceof ApplePayController) {
variable_set('menu_rebuild_needed', TRUE);
}
}
/**
* Element validate callback: Comma separated keys.
*/
function _braintree_payment_validate_comma_separated_keys($element, &$form_state, $form) {
$keys = array_map('trim', explode(',', $element['#value']));
form_set_value($element, $keys, $form_state);
}
/**
* Element process function for hosted fields.
*/
function braintree_payment_hosted_fields_process($element) {
$element['#input'] = FALSE;
array_unshift($element['#theme_wrappers'], 'braintree_payment_hosted_field');
return $element;
}
/**
* Helper function to check if a payment method uses a Braintree controller.
*/
function _braintree_payment_controlled_payment_method(\PaymentMethod $method) {
$info = braintree_payment_payment_method_controller_info();
return isset($info[$method->controller->name]);
}
/**
* Implements hook_d7csp_hosts().
*/
function braintree_payment_d7csp_hosts() {
// Basic directives.
$hosts['script-src'][] = 'js.braintreegateway.com';
$hosts['script-src'][] = 'assets.braintreegateway.com';
$hosts['img-src'][] = 'assets.braintreegateway.com';
$hosts['img-src'][] = 'data:';
$hosts['child-src'][] = 'assets.braintreegateway.com';
$hosts['frame-src'][] = 'assets.braintreegateway.com';
$hosts['connect-src'][] = 'api.braintreegateway.com';
$hosts['connect-src'][] = 'api.sandbox.braintreegateway.com';
$hosts['connect-src'][] = 'client-analytics.braintreegateway.com';
$hosts['connect-src'][] = '*.braintree-api.com';
// Needed for 3D Secure (braintree.threeDSecure).
$hosts['script-src'][] = 'songbirdstag.cardinalcommerce.com';
$hosts['script-src'][] = 'https://includestest.ccdc02.com';
$hosts['frame-src'][] = '*';
$hosts['connect-src'][] = '*.cardinalcommerce.com';
$hosts['form-action'][] = '*';
// Needed for Google Pay.
$hosts['script-src'][] = 'pay.google.com';
$hosts['connect-src'][] = 'pay.google.com';
$hosts['connect-src'][] = 'https://google.com/pay';
return $hosts;
}