-
Notifications
You must be signed in to change notification settings - Fork 0
/
enterbrain.module
169 lines (155 loc) · 4.75 KB
/
enterbrain.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
<?php
/**
* @file
* The main module file containing hook implementations and the cron job.
*/
use \Drupal\enterbrain\Api;
use \Drupal\manual_direct_debit\AccountDataController;
require_once 'integrations/manual_direct_debit.php';
require_once 'integrations/payone_payment.php';
/**
* Implements hook_menu().
*/
function enterbrain_menu() {
$items['admin/config/services/enterbrain'] = [
'title' => 'Enterbrain settings',
'description' => 'API access and default project.',
'page callback' => 'drupal_get_form',
'page arguments' => ['enterbrain_config_form'],
'file' => 'enterbrain.admin.inc',
'access arguments' => ['administer enterbrain settings'],
'type' => MENU_NORMAL_ITEM,
];
$items['enterbrain/check-iban/%'] = [
'title' => 'Submit an IBAN for checking',
'description' => 'The IBAN is checked against the enterbrain API and if valid returns the accompanying BIC.',
'page callback' => 'enterbrain_check_iban',
'page arguments' => [2],
'access callback' => 'enterbrain_form_api_access',
'delivery callback' => 'little_helpers_deliver_json',
'type' => MENU_CALLBACK,
];
return $items;
}
/**
* Implements hook_payment_status_change().
*
* This callback triggers the calls to NeuerFoerderer2.
*/
function enterbrain_payment_status_change(Payment $payment, PaymentStatusItem $previous_status_item) {
if (!$payment->pid) {
return;
}
if (!payment_status_is_or_has_ancestor($payment->getStatus()->status, PAYMENT_STATUS_SUCCESS)) {
return;
}
if (payment_status_is_or_has_ancestor($previous_status_item->status, PAYMENT_STATUS_SUCCESS)) {
return;
}
$queue = \DrupalQueue::get('enterbrain_payment', TRUE);
$queue->createItem($payment->pid);
}
/**
* Implements hook_cronapi().
*
* Execute our send queues.
*/
function enterbrain_cronapi() {
$items['enterbrain_send_payments'] = array(
'title' => t('EnterBrain payment data.'),
'tags' => array('enterbrain'),
);
return $items;
}
/**
* Cron-callback: Send payment events.
*
* Much of this is copied from @see drupal_cron_run() .
*/
function enterbrain_send_payments() {
$api = Api::fromConfig();
$callback = 'enterbrain_send_payment';
$end = time() + variable_get('enterbrain_api_send_time', 30);
$queue = DrupalQueue::get('enterbrain_payment');
while (time() < $end && ($item = $queue->claimItem())) {
try {
$pid = $item->data;
$payments = entity_load('payment', [$pid], [], TRUE);
$payment = reset($payments);
$api->sendPayment($payment);
$queue->deleteItem($item);
}
catch (Exception $e) {
// In case of exception log it and leave the item in the queue
// to be processed again later.
watchdog_exception('enterbrain', $e);
}
}
}
/**
* Access callback: Allow up to 60 requests per hour per IP.
*
* @return bool
* TRUE if access is granted otherwise false.
*/
function enterbrain_form_api_access() {
$event = 'enterbrain_form_api';
if (flood_is_allowed('enterbrain_form_api', 60, 3600)) {
flood_register_event('enterbrain_form_api', 3600);
return TRUE;
}
return FALSE;
}
/**
* Page callback: Checks an IBAN against the enterbrain API.
*
* @param string $iban
* The string to check.
*
* @return array
* API response includes the following keys:
* - valid (TRUE|FALSE): Whether this is a valid key.
* - bic (only if available): The BIC code for this IBAN.
* - error (if not valid): A reason for why this IBAN is invalid.
*
* @see enterbrain_menu()
*/
function enterbrain_check_iban($iban) {
try {
$api = Api::fromConfig();
return $api->checkIBAN($iban);
}
catch (\SoapFault $e) {
watchdog_exception('enterbrain', $e);
return 502;
}
}
/**
* Implements hook_payment_forms_payment_form_alter().
*/
function enterbrain_payment_forms_payment_form_alter(&$element, \Payment $payment) {
if ($payment->method->controller instanceof AccountDataController) {
$element['#attached']['js'][] = drupal_get_path('module', 'enterbrain') . '/enterbrain.js';
$element['#attached']['libraries_load'][] = array('iban');
$element['ibanbic']['iban']['#attributes']['data-enterbrain-field'][] = 'iban';
$element['ibanbic']['bic']['#attributes']['data-enterbrain-field'][] = 'bic';
// Account holder isn’t used with enterbrain.
$element['holder']['#access'] = FALSE;
$element['holder']['#soft_required'] = FALSE;
}
}
/**
* Implements hook_libraries_info().
*/
function enterbrain_libraries_info() {
$libraries['iban'] = array(
'name' => 'IBAN validation',
'vendor url' => 'https://github.com/arhs/iban.js',
'download url' => 'https://github.com/arhs/iban.js',
'version callback' => 'libraries_get_package_json_version',
'files' => array(
'js' => array('iban.js'),
),
);
return $libraries;
}