-
Notifications
You must be signed in to change notification settings - Fork 2
/
intend.php
461 lines (386 loc) · 14.9 KB
/
intend.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
<?php
/*
* Plugin Name: Intend Payment Plugin
* Plugin URI: https://github.com/akbarali1/intend-pay-wordpress-woocommerce
* Description: Intend Checkout Plugin for WooCommerce
* Version: 0.7
* Author: Akbarali
* Author URI: https://github.com/akbarali1
* Text Domain: intend
* Telegram: @akbar_aka
*/
// Prevent direct access
if (!defined('ABSPATH')) {
exit;
}
require('core/intend.php');
add_action('plugins_loaded', 'woocommerce_intend', 0);
function callbackIntend()
{
$all_request = json_decode(file_get_contents('php://input'), true);
$api = new WC_INTEND();
$order_id = $all_request['order_id'];
$api_key = $all_request['api_key'];
$status = $all_request['status'];
$message = $all_request['message'];
$order = wc_get_order($order_id);
if ($api_key !== $api->api_key) {
return wp_send_json_error('Invalid API Key');
}
if (!isset($order)) {
return wp_send_json_error('Invalid Order');
}
$status_all = [
0 => 'wc-pending',
1 => 'wc-on-hold',
2 => 'wc-processing',
3 => 'wc-failed',
];
if (isset($status_all[$status]) && $status_all[$status] === 'wc-processing') {
$order->payment_complete();
$order->add_order_note($message ?? 'Intend payment successful');
$order->update_status($status_all[$status]);
$order->save();
return wp_send_json_success('Payment successful');
}
$order->add_order_note($message ?? 'Intend payment failed');
$order->update_status('failed');
$order->save();
return wp_send_json_error('Payment failed');
}
function webhookRedirect()
{
$order_id = $_GET['order_id'];
$order = wc_get_order($order_id);
if (!isset($order)) {
return wp_send_json_error('Invalid Order');
}
if ($order->get_status() === 'processing') {
wp_redirect($order->get_checkout_order_received_url());
die();
}
wp_redirect($order->get_cancel_order_url());
die();
}
add_action('rest_api_init', function () {
register_rest_route('intend-pay/v1', 'check-order', [
'methods' => 'GET',
'callback' => 'webhookRedirect',
]);
});
add_action('rest_api_init', function () {
register_rest_route('intend-pay/v1', 'callback', [
'methods' => 'POST',
'callback' => 'callbackIntend',
]);
});
function woocommerce_intend()
{
load_plugin_textdomain('intend', false, dirname(plugin_basename(__FILE__)).'/lang/');
// Do nothing, if WooCommerce is not available
if (!class_exists('WC_Payment_Gateway')) {
return;
}
// Do not re-declare class
if (class_exists('WC_INTEND')) {
return;
}
class WC_INTEND extends WC_Payment_Gateway
{
public $api_key;
// protected $checkout_url;
// protected $return_url;
public function __construct()
{
$plugin_dir = plugin_dir_url(__FILE__);
$this->id = 'intend';
$this->title = 'Intend';
$this->description = __("Intend orqali to'lash", 'intend');
$this->icon = apply_filters('woocommerce_intend_icon', ''.$plugin_dir.'intend.png');
$this->has_fields = false;
$this->init_form_fields();
$this->init_settings();
// Populate options from the saved settings
$this->api_key = $this->get_option('api_key');
// $this->checkout_url = $this->get_option('checkout_url');
// $this->return_url = $this->get_option('return_url');
add_action('woocommerce_receipt_'.$this->id, [$this, 'receipt_page']);
add_action('woocommerce_update_options_payment_gateways_'.$this->id, [$this, 'process_admin_options']);
add_action('woocommerce_api_wc_'.$this->id, [$this, 'callback']);
}
function showMessage($content)
{
return '<h1>'.$this->msg['title'].'</h1><div class="box '.$this->msg['class'].'-box">'.$this->msg['message'].'</div>';
}
function showTitle($title)
{
return false;
}
public function admin_options()
{
?>
<h3><?php
_e('Intend', 'intend'); ?></h3>
<p><?php
_e('Configure checkout settings', 'intend'); ?></p>
<!-- <p>-->
<!-- <strong>--><?php
//_e('Your Web Cash Endpoint URL to handle requests is:', 'intend');
?><!--</strong>-->
<!-- <em>--><?//= site_url('/?wc-api=wc_intend');
?><!--</em>-->
<!-- </p>-->
<table class="form-table">
<?php
$this->generate_settings_html(); ?>
</table>
<?php
}
public function init_form_fields()
{
$this->form_fields = [
'enabled' => [
'title' => __('Enable/Disable', 'intend'),
'type' => 'checkbox',
'label' => __('Enabled', 'intend'),
'default' => 'yes',
],
'api_key' => [
'title' => __('API KEY', 'intend'),
'type' => 'text',
'description' => __('Intend.uz tomonidan berilgan API KEYni kiriting.', 'intend'),
'default' => '',
],
];
}
public function generate_form($order_id)
{
// get order by id
$order = wc_get_order($order_id);
// Get and Loop Over Order Items
// convert an amount to the coins (Intend accepts only coins)
$sum = $order->get_total() * 100;
// format the amount
$sum = number_format($sum, 0, '.', '');
$lang_codes = ['ru_RU' => 'ru', 'en_US' => 'en', 'uz_UZ' => 'uz'];
$lang = $lang_codes[get_locale()] ?? 'en';
$label_pay = __('Pay', 'intend');
$label_cancel = __('Cancel payment and return back', 'intend');
$callbackUrl = site_url().'/wp-json/intend-pay/v1/check-order/?order_id='.$order_id;
$html_form = '';
$i = 0;
$apiKey = trim($this->api_key);
foreach ($order->get_items() as $item_id => $item) {
$i++;
$html_form .= '<input type="hidden" name="products['.$i.'][id]" value="'.$item->get_product_id().'">';
$html_form .= '<input type="hidden" name="products['.$i.'][name]" value="'.$item->get_name().'">';
$html_form .= '<input type="hidden" name="products['.$i.'][price]" value="'.((int) ($item->get_total() / $item->get_quantity())).'">';
$html_form .= '<input type="hidden" name="products['.$i.'][quantity]" value="'.$item->get_quantity().'">';
$html_form .= '<input type="hidden" name="products['.$i.'][sku]" value="sku_'.$item->get_product_id().'">';
$html_form .= '<input type="hidden" name="products['.$i.'][weight]" value="0">';
}
$form = <<<FORM
<form action="https://pay.intend.uz" method="POST" id="intend_form">
<input type="hidden" name="duration" value="12">
<input type="hidden" name="plugin_version" value="0.7">
<input type="hidden" name="order_id" value="$order_id">
<input type="hidden" name="api_key" value="{$apiKey}">
<input type="hidden" name="redirect_url" value="{$callbackUrl}">
{$html_form}
<hr />
<input type="submit" class="button alt" id="submit_intend_form" value="$label_pay">
<a class="button cancel" href="{$order->get_cancel_order_url()}">$label_cancel</a>
</form>
FORM;
return $form;
}
public function process_payment($order_id)
{
$order = new WC_Order($order_id);
return [
'result' => 'success',
'redirect' => add_query_arg(
'order_pay',
$order->get_id(),
add_query_arg('key', $order->get_order_key(), $order->get_checkout_payment_url(true))
),
];
}
public function receipt_page($order_id)
{
echo '<p>'.__('Thank you for your order, press "Pay" button to continue.', 'intend').'</p>';
echo $this->generate_form($order_id);
}
/**
* Endpoint method. This method handles requests from Paycom.
*/
public function callback()
{
// Parse payload
$payload = json_decode(file_get_contents('php://input'), true);
if (json_last_error() !== JSON_ERROR_NONE) { // handle Parse error
$this->respond($this->error_invalid_json());
}
// Authorize client
$headers = getallheaders();
$v = html_entity_decode($this->api_key);
$encoded_credentials = base64_encode("Paycom:".$v);
//$encoded_credentials = base64_encode("Paycom:{$this->merchant_key}");
if (!$headers || // there is no headers
!isset($headers['Authorization']) || // there is no Authorization
!preg_match('/^\s*Basic\s+(\S+)\s*$/i', $headers['Authorization'], $matches) || // invalid Authorization value
$matches[1] != $encoded_credentials // invalid credentials
) {
$this->respond($this->error_authorization($payload));
}
// Execute appropriate method
$response = method_exists($this, $payload['method'])
? $this->{$payload['method']}($payload)
: $this->error_unknown_method($payload);
// Respond with result
$this->respond($response);
}
/**
* Responds and terminates request processing.
* @param array $response specified response
*/
private function respond($response)
{
if (!headers_sent()) {
header('Content-Type: application/json; charset=UTF-8');
}
echo json_encode($response);
die();
}
/**
* Gets order instance by id.
* @param array $payload request payload
* @return WC_Order found order by id
*/
private function get_order(array $payload)
{
try {
return new WC_Order($payload['params']['account']['id']);
} catch (Exception $ex) {
$this->respond($this->error_order_id($payload));
}
}
/**
* Gets order instance by transaction id.
* @param array $payload request payload
* @return WC_Order found order by id
*/
private function get_order_by_transaction($payload)
{
global $wpdb;
try {
$prepared_sql = $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_value = '%s' AND meta_key = '_intend_transaction_id'", $payload['params']['id']);
$order_id = $wpdb->get_var($prepared_sql);
return new WC_Order($order_id);
} catch (Exception $ex) {
$this->respond($this->error_transaction($payload));
}
}
/**
* Converts amount to coins.
* @param float $amount amount value.
* @return int Amount representation in coins.
*/
private function amount_to_coin($amount)
{
return 100 * number_format($amount, 2, '.', '');
}
/**
* Gets current timestamp in milliseconds.
* @return float current timestamp in ms.
*/
private function current_timestamp()
{
return round(microtime(true) * 1000);
}
/**
* Get order's create time.
* @param WC_Order $order order
* @return float create time as timestamp
*/
private function get_create_time(WC_Order $order)
{
return (double) get_post_meta($order->get_id(), '_intend_create_time', true);
}
/**
* Get order's perform time.
* @param WC_Order $order order
* @return float perform time as timestamp
*/
private function get_perform_time(WC_Order $order)
{
return (double) get_post_meta($order->get_id(), '_intend_perform_time', true);
}
/**
* Get order's cancel time.
* @param WC_Order $order order
* @return float cancel time as timestamp
*/
private function get_cancel_time(WC_Order $order)
{
return (double) get_post_meta($order->get_id(), '_intend_cancel_time', true);
}
/**
* Get order's transaction id
* @param WC_Order $order order
* @return string saved transaction id
*/
private function get_transaction_id(WC_Order $order)
{
return (string) get_post_meta($order->get_id(), '_intend_transaction_id', true);
}
private function get_cencel_reason(WC_Order $order)
{
$b_v = (int) get_post_meta($order->get_id(), '_cancel_reason', true);
if ($b_v) {
return $b_v;
}
return null;
}
}
function add_intend_gateway($methods)
{
$methods[] = 'WC_INTEND';
return $methods;
}
add_filter('woocommerce_payment_gateways', 'add_intend_gateway');
}
/////////////// success page
add_filter('query_vars', 'intend_success_query_vars');
function intend_success_query_vars($query_vars)
{
$query_vars[] = 'intend_success';
$query_vars[] = 'id';
return $query_vars;
}
add_action('parse_request', 'intend_success_parse_request');
function intend_success_parse_request(&$wp)
{
if (array_key_exists('intend_success', $wp->query_vars)) {
$order = new WC_Order($wp->query_vars['id']);
$a = new WC_INTEND();
add_action('the_title', [$a, 'showTitle']);
add_action('the_content', [$a, 'showMessage']);
if ($wp->query_vars['intend_success'] == 1) {
if ($order->get_status() === "pending") {
wp_redirect($order->get_cancel_order_url());
} else {
$a->msg['title'] = __('Intend successfully paid', 'intend');
$a->msg['message'] = __('Thank you for your purchase!', 'intend');
$a->msg['class'] = 'woocommerce_message woocommerce_message_info';
WC()->cart->empty_cart();
}
} else {
$a->msg['title'] = __('Intend not paid', 'intend');
$a->msg['message'] = __('An error occurred during payment. Try again or contact your administrator.', 'intend');
$a->msg['class'] = 'woocommerce_message woocommerce_message_info';
}
}
return;
}
?>