-
Notifications
You must be signed in to change notification settings - Fork 0
/
commerce_turtlecoin.module
103 lines (89 loc) · 3.08 KB
/
commerce_turtlecoin.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
<?php
/**
* @file
* Includes various helper for the TurtleCoin payment gateway.
*/
use Drupal\Core\Queue\SuspendQueueException;
/**
* Implements hook_cron().
*
* @see https://drupal.stackexchange.com/questions/206838/documentation-or-tutorial-on-using-batch-or-queue-services-api-programmatically
*/
function commerce_turtlecoin_cron() {
$queue_factory = \Drupal::service('queue');
$queue_manager = \Drupal::service('plugin.manager.queue_worker');
// Get the queue implementation for import_content_from_xml queue.
$queue = $queue_factory->get('turtlecoin_payment_process_worker');
// Get the queue worker.
$queue_worker = $queue_manager->createInstance('turtlecoin_payment_process_worker');
// Get the number of items.
$number_of_queue = $queue->numberOfItems();
\Drupal::logger('commerce_turtlecoin')->notice('Transactions in queue to process: @number', [
'@number' => $number_of_queue,
]);
$items_to_release = [];
// Repeat $number_of_queue times.
for ($i = 0; $i < $number_of_queue; $i++) {
// Get a queued item, $lease_time is set to 60 seconds.
if ($item = $queue->claimItem(60)) {
try {
// Process it.
$transaction_result = $queue_worker->processItem($item->data);
// If everything was correct, delete the processed item from the queue.
if ($transaction_result === 'completed' || $transaction_result === 'voided') {
if ($item->data->mode === 'debug') {
\Drupal::logger('commerce_turtlecoin')->notice('Transaction @payment_id is @state.', [
'@payment_id' => $item->data->paymentId,
'@state' => $transaction_result,
]);
}
$queue->deleteItem($item);
}
else {
if ($item->data->mode === 'debug') {
\Drupal::logger('commerce_turtlecoin')->notice('Transaction @payment_id not yet payed.', [
'@payment_id' => $item->data->paymentId,
]);
}
// Add items for releasing later.
$items_to_release[] = $item;
}
}
catch (SuspendQueueException $e) {
// If there was an Exception thrown because of an error
// Releases the item that the worker could not process.
// Another worker can come and process it.
$queue->releaseItem($item);
break;
}
}
}
// Finally, release the not yet finished transactions.
foreach ($items_to_release as $item) {
$queue->releaseItem($item);
}
}
/**
* Implements hook_theme().
*/
function commerce_turtlecoin_theme($existing, $type, $theme, $path) {
return [
'turtlecoin_turtle_pay_payment_instructions' => [
'variables' => [
'payment_amount' => NULL,
'payment_amount_atomic' => NULL,
'turtle_address' => NULL,
'validity_time_blocks' => NULL,
'validity_time' => NULL,
],
],
'turtlecoin_turtlecoin_payment_instructions' => [
'variables' => [
'payment_amount' => NULL,
'turtle_address' => NULL,
'validity_time_blocks' => NULL,
'validity_time' => NULL,
],
],
];
}