forked from Karbovanets/karbo-woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkrbwc-cron.php
339 lines (275 loc) · 15.9 KB
/
krbwc-cron.php
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
<?php
/*
Karbo for WooCommerce
https://github.com/Karbovanets/karbo-woocommerce/
*/
// Include everything
define('KRBWC_MUST_LOAD_WP', '1');
include (dirname(__FILE__) . '/krbwc-include-all.php');
// Cpanel-scheduled cron job call
if (@$_REQUEST['hardcron']=='1')
KRBWC_cron_job_worker (true);
//===========================================================================
// '$hardcron' == true if job is ran by Cpanel's cron job.
function KRBWC_cron_job_worker ($hardcron=false)
{
global $wpdb;
$krbwc_settings = KRBWC__get_settings ();
// status = "unused", "assigned", "used"
$krb_payments_table_name = $wpdb->prefix . 'krbwc_krb_payments';
$funds_received_value_expires_in_secs = $krbwc_settings['funds_received_value_expires_in_mins'] * 60;
$assigned_address_expires_in_secs = $krbwc_settings['assigned_address_expires_in_mins'] * 60;
$confirmations_required = $krbwc_settings['confs_num'];
$clean_address = NULL;
$current_time = time();
// Search for completed orders (addresses that received full payments for their orders) ...
$query =
"SELECT * FROM `$krb_payments_table_name`
WHERE
(
(`status`='assigned' AND (('$current_time' - `assigned_at`) < '$assigned_address_expires_in_secs'))
OR
(`status`='revalidate')
)
ORDER BY `received_funds_checked_at` ASC;"; // Check the ones that haven't been checked for the longest of time
$rows_for_balance_check = $wpdb->get_results ($query, ARRAY_A);
if (is_array($rows_for_balance_check))
$count_rows_for_balance_check = count($rows_for_balance_check);
else
$count_rows_for_balance_check = 0;
if (is_array($rows_for_balance_check))
{
$ran_cycles = 0;
foreach ($rows_for_balance_check as $row_for_balance_check)
{
$ran_cycles++; // To limit number of cycles per soft cron job.
// Prepare 'address_meta' for use.
$address_meta = KRBWC_unserialize_address_meta(@$row_for_balance_check['address_meta']);
$address_request_array = array();
//$address_request_array['dcontext1'] = strlen(@$row_for_balance_check['address_meta']) . ":" . strlen($address_meta); // Arr test, delete it.
$address_request_array['address_meta'] = $address_meta;
// Retrieve current balance at address considering required confirmations number and api_timemout value.
$address_request_array['krb_payment_id'] = $row_for_balance_check['krb_payment_id'];
$address_request_array['krb_address'] = $row_for_balance_check['krb_address'];
$address_request_array['block_index'] = $row_for_balance_check['index_in_wallet'];
$address_request_array['required_confirmations'] = $confirmations_required;
$address_request_array['api_timeout'] = $krbwc_settings['blockchain_api_timeout_secs'];
$balance_info_array = KRBWC__getreceivedbyaddress_info($address_request_array, $krbwc_settings);
$last_order_info = @$address_request_array['address_meta']['orders'][0];
$row_id = $row_for_balance_check['id'];
if ($balance_info_array['result'] == 'success')
{
/*
$balance_info_array = array (
'result' => 'success',
'message' => "",
'host_reply_raw' => "",
'balance' => $funds_received,
);
*/
// Refresh 'received_funds_checked_at' field
$current_time = time();
$query =
"UPDATE `$krb_payments_table_name`
SET
`total_received_funds` = '{$balance_info_array['balance']}',
`received_funds_checked_at`='$current_time'
WHERE `id`='$row_id';";
$ret_code = $wpdb->query ($query);
if ($balance_info_array['balance'] > 0)
{
KRBWC__log_event (__FILE__, __LINE__, "Cron job: NOTE: Detected non-zero balance at address: '{$row_for_balance_check['krb_address']}, Payment ID = {$row_for_balance_check['krb_payment_id']}, order ID = '{$last_order_info['order_id']}'. Detected balance ='{$balance_info_array['balance']}'.");
if ($balance_info_array['balance'] < $last_order_info['order_total'])
{
KRBWC__log_event (__FILE__, __LINE__, "Cron job: NOTE: balance at address: '{$row_for_balance_check['krb_address']}, Payment ID = {$row_for_balance_check['krb_payment_id']}' (KRB '{$balance_info_array['balance']}') is not yet sufficient to complete it's order (order ID = '{$last_order_info['order_id']}'). Total required: '{$last_order_info['order_total']}'. Will wait for more funds to arrive...");
}
}
if ($balance_info_array['balance'] >= $last_order_info['order_total'])
{
// Process full payment event
/*
$address_meta =
array (
'orders' =>
array (
// All orders placed on this address in reverse chronological order
array (
'order_id' => $order_id,
'order_total' => $order_total_in_krb,
'order_datetime' => date('Y-m-d H:i:s T'),
'requested_by_ip' => @$_SERVER['REMOTE_ADDR'],
),
array (
...
),
),
'other_meta_info' => array (...)
);
*/
// Last order was fully paid! Complete it...
KRBWC__log_event (__FILE__, __LINE__, "Cron job: NOTE: Full payment for order ID '{$last_order_info['order_id']}' detected at address: '{$row_for_balance_check['krb_address']}, Payment ID = {$row_for_balance_check['krb_payment_id']}' (KRB '{$balance_info_array['balance']}'). Total was required for this order: '{$last_order_info['order_total']}'. Processing order ...");
// Update order' meta info
$address_meta['orders'][0]['paid'] = true;
// Process and complete the order within WooCommerce (send confirmation emails, etc...)
KRBWC__process_payment_completed_for_order ($last_order_info['order_id'], $balance_info_array['balance']);
// Update address' record
$address_meta_serialized = KRBWC_serialize_address_meta ($address_meta);
// Update DB - mark address as 'used'.
//
$current_time = time();
// Note: `total_received_funds` and `received_funds_checked_at` are already updated above.
//
$query =
"UPDATE `$krb_payments_table_name`
SET
`status`='used',
`address_meta`='$address_meta_serialized'
WHERE `id`='$row_id';";
$ret_code = $wpdb->query ($query);
KRBWC__log_event (__FILE__, __LINE__, "Cron job: SUCCESS: Order ID '{$last_order_info['order_id']}' successfully completed.");
}
}
else
{
KRBWC__log_event (__FILE__, __LINE__, "Cron job: Warning: Cannot retrieve balance for address: '{$row_for_balance_check['krb_address']} Payment ID: {$row_for_balance_check['krb_payment_id']}: " . $balance_info_array['message']);
}
//..//
}
}
// Process all late payments here and calculate the new exchange rate.
// ...
// // SELECT the 5 most recent assigned but expired orders to check for late payments
// $query =
// "SELECT * FROM `$krb_payments_table_name`
// WHERE
// (
// (`status`='assigned' AND (('$current_time' - `assigned_at`) >= '$assigned_address_expires_in_secs'))
// )
// ORDER BY `received_funds_checked_at` DESC LIMIT 5;"; // Check the ones that haven't been checked for the longest of time
// $rows_for_balance_check = $wpdb->get_results ($query, ARRAY_A);
// if (is_array($rows_for_balance_check))
// {
// $ran_cycles = 0;
// foreach ($rows_for_balance_check as $row_for_balance_check)
// {
// $ran_cycles++; // To limit number of cycles per soft cron job.
// // Prepare 'address_meta' for use.
// $address_meta = KRBWC_unserialize_address_meta(@$row_for_balance_check['address_meta']);
// $address_request_array = array();
// //$address_request_array['dcontext1'] = strlen(@$row_for_balance_check['address_meta']) . ":" . strlen($address_meta); // Arr test, delete it.
// $address_request_array['address_meta'] = $address_meta;
// // Retrieve current balance at address considering required confirmations number and api_timemout value.
// $address_request_array['krb_payment_id'] = $row_for_balance_check['krb_payment_id'];
// $address_request_array['krb_address'] = $row_for_balance_check['krb_address'];
// $address_request_array['block_index'] = $row_for_balance_check['index_in_wallet'];
// $address_request_array['required_confirmations'] = $confirmations_required;
// $address_request_array['api_timeout'] = $krbwc_settings['blockchain_api_timeout_secs'];
// $balance_info_array = KRBWC__getreceivedbyaddress_info($address_request_array, $krbwc_settings);
// $last_order_info = @$address_request_array['address_meta']['orders'][0];
// $row_id = $row_for_balance_check['id'];
// if ($balance_info_array['result'] == 'success')
// {
// /*
// $balance_info_array = array (
// 'result' => 'success',
// 'message' => "",
// 'host_reply_raw' => "",
// 'balance' => $funds_received,
// );
// */
// // Refresh 'received_funds_checked_at' field
// $current_time = time();
// $query =
// "UPDATE `$krb_payments_table_name`
// SET
// `total_received_funds` = '{$balance_info_array['balance']}',
// `received_funds_checked_at`='$current_time'
// WHERE `id`='$row_id';";
// $ret_code = $wpdb->query ($query);
// if ($balance_info_array['balance'] > 0)
// {
// KRBWC__log_event (__FILE__, __LINE__, "Cron job: NOTE: Detected LATE PAYMENT non-zero balance at address: '{$row_for_balance_check['krb_address']}, Payment ID = {$row_for_balance_check['krb_payment_id']}, order ID = '{$last_order_info['order_id']}'. Detected balance ='{$balance_info_array['balance']}'.");
// $exchange_rate = KRBWC__get_exchange_rate_per_Karbo (get_woocommerce_currency(), 'getfirst');
// /// $exchange_rate = KRBWC__get_exchange_rate_per_Karbo (get_woocommerce_currency(), $this->exchange_rate_retrieval_method, $this->exchange_rate_type);
// if (!$exchange_rate)
// {
// $msg = 'ERROR: Cannot determine Karbo exchange rate. Possible issues: store server does not allow outgoing connections, exchange rate servers are blocking incoming connections or down. ' .
// 'You may avoid that by setting store currency directly to Karbo(KRB)';
// KRBWC__log_event (__FILE__, __LINE__, $msg);
// exit ('<h2 style="color:red;">' . $msg . '</h2>');
// }
// // Instantiate order object.
// $order = new WC_Order($last_order_info['order_id']);
// $order_total_in_krb = ($order->get_total() / $exchange_rate);
// if (get_woocommerce_currency() != 'KRB')
// // Apply exchange rate multiplier only for stores with non-Karbo default currency.
// $order_total_in_krb = $order_total_in_krb;
// $order_total_in_krb = sprintf ("%.8f", $order_total_in_krb);
// $difference = $order_total_in_krb - $last_order_info['order_total'];
// $diffincur = $difference * $exchange_rate;
// $order->add_order_note( __('Late Payment ', 'woocommerce') .
// __('Balance: ', 'woocommerce') . $balance_info_array['balance'] . ' ' .
// __('Order Total: ', 'woocommerce') . $last_order_info['order_total'] . ' ' .
// __('New Order Total: ', 'woocommerce') . $order_total_in_krb . ' '.
// __('Difference: ', 'woocommerce') . $order_total_in_krb . ' (' . $diffincur . ' '. get_woocommerce_currency() .')'
// );
// if ($balance_info_array['balance'] < $last_order_info['order_total'])
// {
// KRBWC__log_event (__FILE__, __LINE__, "Cron job: NOTE: LATE PAYMENT balance at address: '{$row_for_balance_check['krb_address']}, Payment ID = {$row_for_balance_check['krb_payment_id']}' (KRB '{$balance_info_array['balance']}') is not yet sufficient to complete it's order (order ID = '{$last_order_info['order_id']}'). Total required: '{$last_order_info['order_total']}'. Will wait for more funds to arrive...");
// }
// }
// // Note: to be perfectly safe against late-paid orders, we need to:
// // Scan '$address_meta['orders']' for first UNPAID order that is exactly matching amount at address.
// if ($balance_info_array['balance'] >= $last_order_info['order_total'])
// {
// // Process full payment event
// /*
// $address_meta =
// array (
// 'orders' =>
// array (
// // All orders placed on this address in reverse chronological order
// array (
// 'order_id' => $order_id,
// 'order_total' => $order_total_in_krb,
// 'order_datetime' => date('Y-m-d H:i:s T'),
// 'requested_by_ip' => @$_SERVER['REMOTE_ADDR'],
// ),
// array (
// ...
// ),
// ),
// 'other_meta_info' => array (...)
// );
// */
// // Last order was fully paid! Complete it...
// KRBWC__log_event (__FILE__, __LINE__, "Cron job: NOTE: Full LATE PAYMENT for order ID '{$last_order_info['order_id']}' detected at address: '{$row_for_balance_check['krb_address']}, Payment ID = {$row_for_balance_check['krb_payment_id']}' (KRB '{$balance_info_array['balance']}'). Total was required for this order: '{$last_order_info['order_total']}'. Processing order ...");
// // Update order' meta info
// $address_meta['orders'][0]['paid'] = true;
// // Process and complete the order within WooCommerce (send confirmation emails, etc...)
// KRBWC__process_payment_completed_for_order ($last_order_info['order_id'], $balance_info_array['balance']);
// // Update address' record
// $address_meta_serialized = KRBWC_serialize_address_meta ($address_meta);
// // Update DB - mark address as 'used'.
// //
// $current_time = time();
// // Note: `total_received_funds` and `received_funds_checked_at` are already updated above.
// //
// $query =
// "UPDATE `$krb_payments_table_name`
// SET
// `status`='used',
// `address_meta`='$address_meta_serialized'
// WHERE `id`='$row_id';";
// $ret_code = $wpdb->query ($query);
// KRBWC__log_event (__FILE__, __LINE__, "Cron job: SUCCESS LATE PAYMENT: Order ID '{$last_order_info['order_id']}' successfully completed.");
// }
// }
// else
// {
// KRBWC__log_event (__FILE__, __LINE__, "Cron job: Warning: Cannot retrieve balance for address: '{$row_for_balance_check['krb_address']} Payment ID: {$row_for_balance_check['krb_payment_id']}: " . $balance_info_array['message']);
// }
// }
// }
}
//===========================================================================