1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Builds placeholder replacement tokens for payment-related data.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements hook_token_info().
|
10
|
*/
|
11
|
function commerce_payment_token_info() {
|
12
|
$type = array(
|
13
|
'name' => t('Payment transactions', array(), array('context' => 'a drupal commerce payment transaction')),
|
14
|
'description' => t('Tokens related to payment transactions.'),
|
15
|
'needs-data' => 'commerce-payment-transaction',
|
16
|
);
|
17
|
|
18
|
// Tokens for payments.
|
19
|
$transaction = array();
|
20
|
|
21
|
$transaction['transaction-id'] = array(
|
22
|
'name' => t('Transaction ID'),
|
23
|
'description' => t('The primary identifier for a payment transaction.'),
|
24
|
);
|
25
|
$transaction['revision-id'] = array(
|
26
|
'name' => t('Revision ID'),
|
27
|
'description' => t('The unique ID for the latest revision of a payment transaction.'),
|
28
|
);
|
29
|
$transaction['payment-method'] = array(
|
30
|
'name' => t('Payment method'),
|
31
|
'description' => t('The payment method method_id for the payment transaction.'),
|
32
|
);
|
33
|
$transaction['payment-method-title'] = array(
|
34
|
'name' => t('Payment method title'),
|
35
|
'description' => t('The administrative title of the payment method for the payment transaction.'),
|
36
|
);
|
37
|
$transaction['payment-method-short-title'] = array(
|
38
|
'name' => t('Payment method short title'),
|
39
|
'description' => t('The customer oriented short title of the payment method for the payment transaction.'),
|
40
|
);
|
41
|
$transaction['remote-id'] = array(
|
42
|
'name' => t('Remote ID'),
|
43
|
'description' => t('The remote identifier for the payment transaction.'),
|
44
|
);
|
45
|
$transaction['message'] = array(
|
46
|
'name' => t('Message'),
|
47
|
'description' => t('The human-readable message associated to the payment transaction.'),
|
48
|
);
|
49
|
$transaction['amount-raw'] = array(
|
50
|
'name' => t('Raw amount'),
|
51
|
'description' => t('The raw amount of the payment transaction.'),
|
52
|
);
|
53
|
$transaction['amount-formatted'] = array(
|
54
|
'name' => t('Formatted amount'),
|
55
|
'description' => t('The formatted amount of the payment transaction.'),
|
56
|
);
|
57
|
$transaction['currency-code'] = array(
|
58
|
'name' => t('Currency code'),
|
59
|
'description' => t('The currency code for the payment.'),
|
60
|
);
|
61
|
$transaction['currency-symbol'] = array(
|
62
|
'name' => t('Currency symbol'),
|
63
|
'description' => t('The currency symbol for the payment.'),
|
64
|
);
|
65
|
$transaction['status'] = array(
|
66
|
'name' => t('Status'),
|
67
|
'description' => t('The status of this transaction (pending, success, or failure).'),
|
68
|
);
|
69
|
$transaction['remote-status'] = array(
|
70
|
'name' => t('Remote status'),
|
71
|
'description' => t('The status of the transaction at the payment provider.'),
|
72
|
);
|
73
|
|
74
|
// Chained tokens for payment transactions.
|
75
|
$transaction['order'] = array(
|
76
|
'name' => t('Order'),
|
77
|
'description' => t('The order related to the payment transaction.'),
|
78
|
'type' => 'commerce-order',
|
79
|
);
|
80
|
$transaction['creator'] = array(
|
81
|
'name' => t('Creator'),
|
82
|
'description' => t('The creator of the payment transaction.'),
|
83
|
'type' => 'user',
|
84
|
);
|
85
|
$transaction['created'] = array(
|
86
|
'name' => t('Date created'),
|
87
|
'description' => t('The date the payment transaction was created.'),
|
88
|
'type' => 'date',
|
89
|
);
|
90
|
$transaction['changed'] = array(
|
91
|
'name' => t('Date changed'),
|
92
|
'description' => t('The date the payment transaction was last updated.'),
|
93
|
'type' => 'date',
|
94
|
);
|
95
|
return array(
|
96
|
'types' => array('commerce-payment-transaction' => $type),
|
97
|
'tokens' => array('commerce-payment-transaction' => $transaction),
|
98
|
);
|
99
|
}
|
100
|
|
101
|
/**
|
102
|
* Implements hook_token_info_alter().
|
103
|
*/
|
104
|
function commerce_payment_token_info_alter(&$data) {
|
105
|
if (module_exists('commerce_checkout')) {
|
106
|
$data['tokens']['commerce-order']['payment-method'] = array(
|
107
|
'name' => t('Payment method'),
|
108
|
'description' => t('The method_id of the payment method selected by the customer during checkout.'),
|
109
|
);
|
110
|
$data['tokens']['commerce-order']['payment-method-title'] = array(
|
111
|
'name' => t('Payment method title'),
|
112
|
'description' => t('The administrative title of the payment method selected by the customer during checkout.'),
|
113
|
);
|
114
|
$data['tokens']['commerce-order']['payment-method-short-title'] = array(
|
115
|
'name' => t('Payment method short title'),
|
116
|
'description' => t('The customer oriented short title of the payment method selected by the customer during checkout.'),
|
117
|
);
|
118
|
|
119
|
}
|
120
|
}
|
121
|
|
122
|
/**
|
123
|
* Implements hook_tokens().
|
124
|
*/
|
125
|
function commerce_payment_tokens($type, $tokens, array $data = array(), array $options = array()) {
|
126
|
$url_options = array('absolute' => TRUE);
|
127
|
|
128
|
if (isset($options['language'])) {
|
129
|
$url_options['language'] = $options['language'];
|
130
|
$language_code = $options['language']->language;
|
131
|
}
|
132
|
else {
|
133
|
$language_code = NULL;
|
134
|
}
|
135
|
|
136
|
$sanitize = !empty($options['sanitize']);
|
137
|
|
138
|
$replacements = array();
|
139
|
|
140
|
if ($type == 'commerce-payment-transaction' && !empty($data['commerce-payment-transaction'])) {
|
141
|
$transaction = $data['commerce-payment-transaction'];
|
142
|
|
143
|
foreach ($tokens as $name => $original) {
|
144
|
switch ($name) {
|
145
|
case 'transaction-id':
|
146
|
$replacements[$original] = $transaction->transaction_id;
|
147
|
break;
|
148
|
|
149
|
case 'revision-id':
|
150
|
$replacements[$original] = $transaction->revision_id;
|
151
|
break;
|
152
|
|
153
|
case 'payment-method':
|
154
|
$replacements[$original] = $sanitize ? check_plain($transaction->payment_method) : $transaction->payment_method;
|
155
|
break;
|
156
|
|
157
|
case 'payment-method-title':
|
158
|
$title = commerce_payment_method_get_title('title', $transaction->payment_method);
|
159
|
$replacements[$original] = $sanitize ? check_plain($title) : $title;
|
160
|
break;
|
161
|
|
162
|
case 'payment-method-short-title':
|
163
|
$title = commerce_payment_method_get_title('short_title', $transaction->payment_method);
|
164
|
$replacements[$original] = $sanitize ? check_plain($title) : $title;
|
165
|
break;
|
166
|
|
167
|
case 'remote-id':
|
168
|
$replacements[$original] = $sanitize ? check_plain($transaction->remote_id) : $transaction->remote_id;
|
169
|
break;
|
170
|
|
171
|
case 'message':
|
172
|
$replacements[$original] = t($transaction->message, is_array($transaction->message_variables) ? $transaction->message_variables : array());
|
173
|
break;
|
174
|
|
175
|
case 'amount-raw':
|
176
|
$replacements[$original] = $sanitize ? check_plain($transaction->amount) : $transaction->amount;
|
177
|
break;
|
178
|
|
179
|
case 'amount-formatted':
|
180
|
$replacements[$original] = commerce_currency_format($transaction->amount, $transaction->currency_code);
|
181
|
break;
|
182
|
|
183
|
case 'currency-code':
|
184
|
$replacements[$original] = $sanitize ? check_plain($transaction->currency_code) : $transaction->currency_code;
|
185
|
break;
|
186
|
|
187
|
case 'currency-symbol':
|
188
|
$replacements[$original] = commerce_currency_get_symbol($transaction->currency_code);
|
189
|
break;
|
190
|
|
191
|
case 'status':
|
192
|
$transaction_status = commerce_payment_transaction_status_load($transaction->status);
|
193
|
$replacements[$original] = $sanitize ? check_plain($transaction_status['title']) : $transaction_status['title'];
|
194
|
break;
|
195
|
|
196
|
case 'remote-status':
|
197
|
$replacements[$original] = $sanitize ? check_plain($transaction->remote_status) : $transaction->remote_status;
|
198
|
break;
|
199
|
|
200
|
// Default values for the chained tokens handled below.
|
201
|
case 'order':
|
202
|
if ($transaction->order_id) {
|
203
|
$order = commerce_order_load($transaction->order_id);
|
204
|
$replacements[$original] = $sanitize ? check_plain($order->order_number) : $order->order_number;
|
205
|
}
|
206
|
break;
|
207
|
|
208
|
case 'creator':
|
209
|
if ($transaction->uid == 0) {
|
210
|
$name = variable_get('anonymous', t('Anonymous'));
|
211
|
}
|
212
|
else {
|
213
|
$creator = user_load($transaction->uid);
|
214
|
$name = $creator->name;
|
215
|
}
|
216
|
$replacements[$original] = $sanitize ? filter_xss($name) : $name;
|
217
|
break;
|
218
|
|
219
|
case 'created':
|
220
|
$replacements[$original] = format_date($transaction->created, 'medium', '', NULL, $language_code);
|
221
|
break;
|
222
|
|
223
|
case 'changed':
|
224
|
$replacements[$original] = format_date($transaction->changed, 'medium', '', NULL, $language_code);
|
225
|
break;
|
226
|
}
|
227
|
}
|
228
|
|
229
|
if ($order_tokens = token_find_with_prefix($tokens, 'order')) {
|
230
|
$order = commerce_order_load($transaction->order_id);
|
231
|
$replacements += token_generate('commerce-order', $order_tokens, array('commerce-order' => $order), $options);
|
232
|
}
|
233
|
|
234
|
if ($creator_tokens = token_find_with_prefix($tokens, 'creator')) {
|
235
|
$creator = user_load($transaction->uid);
|
236
|
$replacements += token_generate('user', $creator_tokens, array('user' => $creator), $options);
|
237
|
}
|
238
|
|
239
|
foreach (array('created', 'changed') as $date) {
|
240
|
if ($created_tokens = token_find_with_prefix($tokens, $date)) {
|
241
|
$replacements += token_generate('date', $created_tokens, array('date' => $transaction->{$date}), $options);
|
242
|
}
|
243
|
}
|
244
|
}
|
245
|
|
246
|
if ($type == 'commerce-order' && !empty($data['commerce-order'])) {
|
247
|
$order = $data['commerce-order'];
|
248
|
|
249
|
if (!empty($order->data['payment_method'])) {
|
250
|
$payment_method = commerce_payment_method_instance_load($order->data['payment_method']);
|
251
|
|
252
|
if (!empty($payment_method) && !empty($payment_method['method_id'])) {
|
253
|
$method_id = $payment_method['method_id'];
|
254
|
|
255
|
foreach ($tokens as $name => $original) {
|
256
|
switch ($name) {
|
257
|
case 'payment-method':
|
258
|
$replacements[$original] = $sanitize ? check_plain($method_id) : $method_id;
|
259
|
break;
|
260
|
|
261
|
case 'payment-method-title':
|
262
|
$title = commerce_payment_method_get_title('title', $method_id);
|
263
|
$replacements[$original] = $sanitize ? check_plain($title) : $title;
|
264
|
break;
|
265
|
|
266
|
case 'payment-method-short-title':
|
267
|
$title = commerce_payment_method_get_title('short_title', $method_id);
|
268
|
$replacements[$original] = $sanitize ? check_plain($title) : $title;
|
269
|
break;
|
270
|
}
|
271
|
}
|
272
|
}
|
273
|
else {
|
274
|
foreach ($tokens as $name => $original) {
|
275
|
switch ($name) {
|
276
|
case 'payment-method':
|
277
|
case 'payment-method-title':
|
278
|
case 'payment-method-short-title':
|
279
|
$replacements[$original] = t('Unknown');
|
280
|
break;
|
281
|
}
|
282
|
}
|
283
|
}
|
284
|
}
|
285
|
else {
|
286
|
foreach ($tokens as $name => $original) {
|
287
|
switch ($name) {
|
288
|
case 'payment-method':
|
289
|
case 'payment-method-title':
|
290
|
case 'payment-method-short-title':
|
291
|
$replacements[$original] = t('Unknown');
|
292
|
break;
|
293
|
}
|
294
|
}
|
295
|
}
|
296
|
}
|
297
|
|
298
|
return $replacements;
|
299
|
}
|