-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaaa_cybersource.module
362 lines (319 loc) · 10.5 KB
/
aaa_cybersource.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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<?php
/**
* @file
* Primary module hooks for aaa_cybersource module.
*/
use \Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\aaa_cybersource\Entity\Payment;
use Drupal\webform\WebformSubmissionInterface;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Utility\WebformMailHelper;
// Cron functions.
include('inc/aaa_cybersource.cron.inc');
/**
* Implements hook_theme().
*/
function aaa_cybersource_theme() {
return [
'payment' => [
'render element' => 'elements',
],
];
}
/**
* Prepares variables for payment templates.
*
* Default template: payment.html.twig.
*
* @param array $variables
* An associative array containing:
* - elements: An associative array containing the payment information and any
* fields attached to the entity.
* - attributes: HTML attributes for the containing element.
*/
function template_preprocess_payment(array &$variables) {
foreach (Element::children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
$variables['payment'] = $variables['elements']['#payment'];
}
/**
* Implements hook_entity_type_insert().
*
* Link webform_submission to the payment entity.
*/
function aaa_cybersource_webform_submission_insert(WebformSubmissionInterface $entity) {
if (!is_null($entity->getData()['payment_entity'])) {
$payment = Payment::load($entity->getData()['payment_entity']);
$payment->set('submission', $entity->id());
$payment->save();
}
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
function aaa_cybersource_theme_suggestions_webform_confirmation(array $variables) {
if ($variables['webform']->get('category') === 'Cybersource') {
return ['webform_confirmation__cybersource__receipt'];
}
}
/**
* Implements hook_entity_insert().
*
* Copy the Webform alias updater and add a '/receipt' route.
*/
function aaa_cybersource_webform_insert($entity) {
if ($entity->get('category') === 'Cybersource') {
_add_receipt_alias($entity);
}
}
/**
* Implements hook_entity_update().
*
* Copy the Webform alias updater and add a '/receipt' route.
*/
function aaa_cybersource_webform_update($entity) {
if ($entity->get('category') === 'Cybersource') {
_add_receipt_alias($entity);
}
}
/**
* Implements_webform_submission_form_alter().
*/
function aaa_cybersource_webform_submission_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
$webform = Webform::load($form['#webform_id']);
$handlers = $webform->get('handlers');
// Only donation forms.
if (isset($handlers['donation_webform_handler'])) {
$config = \Drupal::service('config.factory')->get('aaa_cybersource.settings');
$environment = $config->get('global')['environment'];
$uuid = $webform->get('uuid');
if ($config->get($uuid . '_environment') !== TRUE) {
$environment = $config->get($uuid . '_environment');
}
if ($environment === 'development') {
$form['#attached']['library'][] = 'aaa_cybersource/flex-dev';
}
else {
$form['#attached']['library'][] = 'aaa_cybersource/flex';
}
$form['#attached']['drupalSettings']['aaa_cybersource']['webform'] = $form['#webform_id'];
$form['#attached']['drupalSettings']['aaa_cybersource']['logging'] = (bool) $config->get('global')['logging'] ?? FALSE;
}
}
/**
* Implements hook_mail().
*
* Copied from the Webform module, including using some of the utility functions.
*/
function aaa_cybersource_mail($key, &$message, $params) {
// Never send emails when using devel generate to create
// 1000's of submissions.
if (\Drupal::moduleHandler()->moduleExists('devel_generate')) {
/** @var \Drupal\devel_generate\DevelGeneratePluginManager $devel_generate */
$devel_generate = \Drupal::service('plugin.manager.develgenerate');
$definition = $devel_generate->getDefinition('webform_submission', FALSE);
if ($definition) {
$class = $definition['class'];
if ($class::isGeneratingSubmissions()) {
$message['send'] = FALSE;
}
}
}
// Set default parameters.
$params += [
'from_mail' => '',
'from_name' => '',
'cc_mail' => '',
'bcc_mail' => '',
'reply_to' => '',
'return_path' => '',
'sender_mail' => '',
'sender_name' => '',
];
$message['subject'] = $params['subject'];
$message['body'][] = $params['body'];
// Set the header 'From'.
// Using the 'from_mail' so that the webform's email from value is used
// instead of site's email address.
// @see: \Drupal\Core\Mail\MailManager::mail.
if (!empty($params['from_mail'])) {
// 'From name' is only used when the 'From mail' contains a single
// email address.
$from = (!empty($params['from_name']) && strpos($params['from_mail'], ',') === FALSE)
? WebformMailHelper::formatAddress($params['from_mail'], $params['from_name'])
: $params['from_mail'];
$message['from'] = $message['headers']['From'] = $from;
}
// Set header 'Cc'.
if (!empty($params['cc_mail'])) {
$message['headers']['Cc'] = $params['cc_mail'];
}
// Set header 'Bcc'.
if (!empty($params['bcc_mail'])) {
$message['headers']['Bcc'] = $params['bcc_mail'];
}
// Set header 'Reply-to'.
$reply_to = $params['reply_to'] ?: '';
if (empty($reply_to) && !empty($params['from_mail'])) {
$reply_to = $message['from'];
}
if ($reply_to) {
$message['reply-to'] = $message['headers']['Reply-to'] = $reply_to;
}
// Set header 'Return-Path' which only supports a single email address and the
// 'from_mail' may contain multiple comma delimited email addresses.
$return_path = $params['return_path'] ?: $params['from_mail'] ?: '';
if ($return_path) {
$return_path = explode(',', $return_path);
$message['headers']['Sender'] = $message['headers']['Return-Path'] = $return_path[0];
}
// Set header 'Sender'.
$sender_mail = $params['sender_mail'] ?: '';
$sender_name = $params['sender_name'] ?: $params['from_name'] ?: '';
if ($sender_mail) {
$message['headers']['Sender'] = WebformMailHelper::formatAddress($sender_mail, $sender_name);
}
}
/**
* Adds an alias URL for the receipt page of cybersource forms.
*
* @param Webform $entity
*/
function _add_receipt_alias(Webform $entity) {
$page_submit_path = $entity->getSetting('page_submit_path');
$default_page_base_path = \Drupal::config('webform.settings')->get('settings.default_page_base_path');
if (empty($page_submit_path) && empty($default_page_base_path)) {
return;
}
$path_base_alias = ($page_submit_path ?: $default_page_base_path . '/' . str_replace('_', '-', $entity->id()));
$path_suffix = '/receipt';
$path_source = '/webform/' . $entity->id() . $path_suffix;
$path_alias = $path_base_alias . $path_suffix;
$langcode = 'en';
// $this->updatePath($path_source, $path_alias, 'en');
$path_alias_storage = \Drupal::entityTypeManager()->getStorage('path_alias');
// Check if the path alias is already setup.
$path_aliases = $path_alias_storage->loadByProperties(['path' => $path_source, 'langcode' => $langcode]);
if ($path_aliases) {
/** @var \Drupal\path_alias\PathAliasInterface $path_alias */
$path_alias_current = reset($path_aliases);
if ($path_alias_current->getAlias() === $path_alias) {
return;
}
}
else {
$path_alias_current = $path_alias_storage->create([
'path' => $path_source,
'langcode' => $langcode,
]);
}
$path_alias_current->setAlias($path_alias);
$path_alias_current->save();
}
/**
* Implements hook_webform_element_alter().
*/
function aaa_cybersource_webform_element_alter(array &$element, \Drupal\Core\Form\FormStateInterface $form_state, array $context) {
if ($element['#type'] === 'webform_table' && $element['#webform_key'] === 'gala') {
// Attach helpful JS library.
$element['#attached']['library'][] = 'aaa_cybersource/gala-table';
}
elseif (
$element['#type'] === 'select' &&
$element['#title'] === 'Country/Region'
) {
// Move United States to the top.
$options = $element['#options'];
$united_states = $options['US'];
unset($options['US']);
$options = ['US' => $united_states] + $options;
$element['#options'] = $options;
}
}
/**
* Create the next recurring time. +1 month.
*
* @param int $timestamp
* Current timestamp.
*
* @return string
* the next recurring date time string for database storage.
*/
function aaa_cybersource_get_next_recurring_payment_date(int $timestamp) {
$nextRecurringTime = strtotime('+1 month', $timestamp);
$format = 'Y-m-d\TH:i:s';
$nextRecurringDateTimeFormat = date($format, $nextRecurringTime);
return $nextRecurringDateTimeFormat;
}
/**
* Takes CyberSource code and returns the human-string card type.
*
* @param string $code
* @return string
*/
function aaa_cybersource_card_type_number_to_string(string $code) {
$codes = [
'001' => 'Visa',
'002' => 'Mastercard',
'003' => 'American Express',
'004' => 'Discover',
'005' => 'Diners Club',
'006' => 'Carte Blanche',
'007' => 'JCB',
'014' => 'Enroute',
'021' => 'JAL',
'024' => 'Maestro',
'031' => 'Delta',
'033' => 'Visa Electron',
'034' => 'Dankort',
'036' => 'Cartes Bancaires',
'037' => 'Carta Si',
'039' => 'Encoded account number',
'040' => 'UATP',
'042' => 'Maestro',
'050' => 'Hipercard',
'051' => 'Aura',
'054' => 'Elo',
'062' => 'China UnionPay',
];
return $codes[$code];
}
/**
* Format amounts which may contain symbols like commas.
*
* @param integer|float|string $form_amount
* @return string $new_amount
*/
function aaa_cybersource_format_amount($form_amount) {
$new_amount = (string) $form_amount;
$form_amount_cents = '00';
if (is_null($form_amount) || empty($form_amount)) {
return 0;
}
$form_amount_array = explode('.', (string) $form_amount);
// Malformed currency amount: multiple decimals.
if (count($form_amount_array) > 2) {
return 0;
}
else if (count($form_amount_array) === 2) {
$form_amount_cents = $form_amount_array[1];
// Malformed cents: not equal to 2.
if ($form_amount_cents != '0' && strlen($form_amount_cents) !== 2) {
return 0;
}
else if ($form_amount_cents === '0') {
$form_amount_cents = '00';
}
}
$form_amount_dollars = $form_amount_array[0];
if (empty($form_amount_dollars)) {
return 0;
}
// strip any commas
preg_match_all('/\d+/', $form_amount_dollars, $form_amount_dollars_array);
$amount_dollars = implode('', $form_amount_dollars_array[0]);
$new_amount = $amount_dollars . '.' . $form_amount_cents;
return (string) $new_amount;
}