-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-wc-gateway-revenuemonster.php
401 lines (355 loc) · 12.5 KB
/
class-wc-gateway-revenuemonster.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
<?php
/**
* Plugin Name: WooCommerce RevenueMonster Payment Gateway
* Description: Accept all major Malaysia e-wallet, such as TnG eWallet, Boost, Maybank QRPay & credit cards. Fast, seamless, and flexible.
* Author: RevenueMonster
* Author URI: https://revenuemonster.my/
* Version: 1.0.7
* WC requires at least: 2.6
* WC tested up to: 4.0.1
*
* @package WC_Gateway_RevenueMonster
*/
defined('ABSPATH') || die('Missing global variable ABSPATH');
add_filter('cron_schedules', 'add_cron_minute_interval');
/**
* Function add_cron_minute_interval
*
* @param array $schedules wp schedules.
*/
function add_cron_minute_interval($schedules)
{
if (!isset($schedules['minute'])) {
$schedules['minute'] = array(
'interval' => 60,
'display' => __('Every Minutes', 'woocommerce-gateway-revenuemonster'),
);
}
return $schedules;
}
if (!wp_next_scheduled('pending_orders_requery')) {
wp_schedule_event(time(), 'minute', 'pending_orders_requery');
}
add_action('pending_orders_requery', array(WC_Gateway_RevenueMonster::class, 'pending_orders_requery_cron'));
add_filter('woocommerce_payment_gateways', 'wc_revenuemonster_add_to_gateways');
/**
* Function wc_revenuemonster_add_to_gateways
*
* @param array $gateways gateways.
*/
function wc_revenuemonster_add_to_gateways($gateways)
{
$gateways[] = 'WC_Gateway_RevenueMonster';
return $gateways;
}
add_action('plugins_loaded', 'wc_gateway_revenuemonster_init', 11);
/**
* Function wc_gateway_revenuemonster_init
*
* @throws \Exception Invalid payment status.
*/
function wc_gateway_revenuemonster_init()
{
require_once dirname(__FILE__) . '/includes/class-revenuemonster.php';
if (!function_exists('is_plugin_active')) {
require_once ABSPATH . '/wp-admin/includes/plugin.php';
}
if (!is_plugin_active('woocommerce/woocommerce.php')) {
add_action(
'admin_notices',
function () {
/* translators: 1. URL link. */
echo '<div class="error"><p><strong>' . sprintf(esc_html__('Revenue Monster Payment Gateway requires WooCommerce to be installed and active. You can download %s here.', 'woocommerce-gateway-revenuemonster'), '<a href="https://woocommerce.com/" target="_blank">WooCommerce</a>') . '</strong></p></div>';
}
);
return;
}
/**
* Class WC_Gateway_RevenueMonster
*/
class WC_Gateway_RevenueMonster extends WC_Payment_Gateway
{
/**
* Whether or not logging is enabled
*
* @var bool
*/
public static $log_enabled = false;
/**
* Logger instance
*
* @var WC_Logger
*/
public static $log = false;
/**
* Construct
*/
public function __construct()
{
$this->id = 'revenuemonster'; // payment gateway plugin ID.
$this->icon = $this->get_option('logo'); // URL of the icon that will be displayed on checkout page near your gateway name.
$this->has_fields = true; // in case you need a custom credit card form.
$this->method_title = 'RevenueMonster Checkout';
$this->method_description = 'Pay via RevenueMonster Payment Gateway'; // will be displayed on the options page.
$this->supports = array(
'products',
);
$this->init_form_fields();
$this->init_settings();
// data will show on the checkout page (wordpress/index.php/checkout/).
$this->title = $this->get_option('title');
$this->description = $this->get_option('description');
$this->enabled = $this->get_option('enabled');
$this->payment_methods = $this->get_option('payment_methods');
if (is_admin()) {
// This action hook saves the settings.
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
}
// Register a webhook.
add_action('woocommerce_api_wc_gateway_revenuemonster', array($this, 'webhook'));
}
/**
* Logging method.
*
* @param string $message Log message.
* @param string $level Optional. Default 'info'. Possible values:
* emergency|alert|critical|error|warning|notice|info|debug.
*/
public static function log($message, $level = 'info')
{
if (self::$log_enabled) {
if (empty(self::$log)) {
self::$log = wc_get_logger();
}
self::$log->log($level, $message, array('source' => 'revenuemonster'));
}
}
/**
* Function cronjob
*/
public static function pending_orders_requery_cron()
{
$pending_orders = wc_get_orders(
array(
'limit' => -1,
// 'date_created' => '>' . ( time() - 2*24*60*60 ),
'status' => array('on-hold'),
)
);
$settings = get_option('woocommerce_revenuemonster_settings');
$sdk = RevenueMonster::get_instance(
array(
'client_id' => $settings['client_id'],
'client_secret' => $settings['client_secret'],
'private_key' => $settings['private_key'],
'public_key' => $settings['public_key'],
'version' => 'stable',
'is_sandbox' => filter_var($settings['sandbox'], FILTER_VALIDATE_BOOLEAN),
)
);
foreach ($pending_orders as $order) {
if ($order->get_payment_method() === 'revenuemonster') {
$oid = $order->get_transaction_id();
try {
$response = $sdk->query_order($oid);
if ($response && strtoupper($response->status) === 'SUCCESS') {
$obj = json_decode(json_encode($response), true);
$order->payment_complete($obj['transactionId']);
$order->set_payment_method($response->method);
$order->save();
} elseif ($response && strtoupper($response->status) === 'FAILED') {
$order->update_status('failed', __('Payment failed', 'woocommerce-gateway-revenuemonster'));
$order->save();
}
} catch (Exception $e) {
if (strtoupper($e->getMessage()) === 'TRANSACTION_NOT_FOUND' && (filter_var($settings['auto_cancel'], FILTER_VALIDATE_BOOLEAN))) {
$order_id = explode('-', $oid);
if (count($order_id) === 2) {
if ((time() - intval($order_id[1])) > 60 * 30) {
$order->update_status('failed', __('Payment failed', 'woocommerce-gateway-revenuemonster'));
$order->save();
}
}
}
}
}
}
}
/**
* Function woocommerce settings form fields
*/
public function init_form_fields()
{
$this->form_fields = apply_filters(
'wc_gateway_revenuemonster_form_fields',
array(
'enabled' => array(
'title' => __('Enable/Disable', 'woocommerce-gateway-revenuemonster'),
'type' => 'checkbox',
'label' => __('Enable this payment gateway', 'woocommerce-gateway-revenuemonster'),
'default' => 'yes',
),
'sandbox' => array(
'title' => __('Sandbox', 'woocommerce-gateway-revenuemonster'),
'type' => 'checkbox',
'default' => 'no',
),
'auto_cancel' => array(
'title' => __('Auto cancel', 'woocommerce-gateway-revenuemonster'),
'type' => 'checkbox',
'label' => __('Cancel on hold order if transaction not found', 'woocommerce-gateway-revenuemonster'),
'default' => 'no',
),
'title' => array(
'title' => __('Title', 'woocommerce-gateway-revenuemonster'),
'type' => 'text',
'description' => __('This controls the title for the payment method the customer sees during checkout.', 'woocommerce-gateway-revenuemonster'),
'desc_tip' => true,
),
'description' => array(
'title' => __('Description', 'woocommerce-gateway-revenuemonster'),
'type' => 'text',
'description' => __('Payment method description that the customer will see on your checkout.', 'woocommerce-gateway-revenuemonster'),
'desc_tip' => true,
),
'logo' => array(
'title' => __('Logo', 'woocommerce-gateway-revenuemonster'),
'type' => 'text',
'description' => __('Logo URL', 'woocommerce-gateway-revenuemonster'),
),
'store_id' => array(
'title' => __('Store ID', 'woocommerce-gateway-revenuemonster'),
'type' => 'text',
'description' => __('Store ID setup at Revenuemonster', 'woocommerce-gateway-revenuemonster'),
'desc_tip' => true,
),
'client_id' => array(
'title' => __('Client ID', 'woocommerce-gateway-revenuemonster'),
'type' => 'text',
'description' => __('Client ID setup at Revenuemonster', 'woocommerce-gateway-revenuemonster'),
'desc_tip' => true,
),
'client_secret' => array(
'title' => __('Client Secret', 'woocommerce-gateway-revenuemonster'),
'type' => 'password',
'description' => __('Client Secret setup at Revenuemonster', 'woocommerce-gateway-revenuemonster'),
'desc_tip' => true,
),
'private_key' => array(
'title' => __('Client Private Key', 'woocommerce-gateway-revenuemonster'),
'type' => 'textarea',
'description' => __('Instructions that will be added to the thank you page and emails.', 'woocommerce-gateway-revenuemonster'),
'default' => '',
'desc_tip' => true,
),
'public_key' => array(
'title' => __('Server Public Key', 'woocommerce-gateway-revenuemonster'),
'type' => 'textarea',
'description' => __('Instructions that will be added to the thank you page and emails.', 'woocommerce-gateway-revenuemonster'),
'default' => '',
'desc_tip' => true,
),
)
);
}
/**
* Function get_sdk
*/
public function get_sdk()
{
return RevenueMonster::get_instance(
array(
'client_id' => $this->get_option('client_id'),
'client_secret' => $this->get_option('client_secret'),
'private_key' => $this->get_option('private_key'),
'public_key' => $this->get_option('public_key'),
'version' => 'stable',
'is_sandbox' => filter_var($this->get_option('sandbox'), FILTER_VALIDATE_BOOLEAN),
)
);
}
/**
* Function woocommerce payment logic
*
* @param string $order_id Order ID.
*/
public function process_payment($order_id)
{
global $woocommerce;
$order = new WC_Order($order_id);
$oid = $order_id . '-' . time();
$order->set_transaction_id($oid);
$order->save();
$sdk = $this->get_sdk();
$url = WC()->api_request_url('WC_Gateway_RevenueMonster');
$payload = array(
'order' => array(
'id' => strval($oid),
'title' => 'WooCommerce Order ' . strval($order_id),
'detail' => strval($order_id),
'additionalData' => '',
'amount' => (int) round(floatval($order->get_total()) * 100),
'currencyType' => 'MYR',
),
'method' => array(),
// 'method' => $this->get_option( 'payment_methods' ),
'type' => 'WEB_PAYMENT',
'storeId' => strval($this->get_option('store_id')),
'redirectUrl' => escape_url($url),
'notifyUrl' => escape_url($url),
'layoutVersion' => 'v4',
);
$response = $sdk->create_order($payload);
$order->update_status('on-hold', __('Awaiting payment', 'woocommerce-gateway-revenuemonster'));
// Reduce stock levels.
$order->reduce_order_stock();
// Remove cart.
$woocommerce->cart->empty_cart();
return array(
'result' => 'success',
'redirect' => $response->url,
);
}
/**
* Function webhook
*
* @throws \Exception Invalid webhook response.
*/
public function webhook()
{
if (empty($_GET['orderId'])) {
wp_die('RevenueMonster payment failed', 'RevenueMonster Payment', array('response' => 500));
return;
}
$oid = sanitize_key(wp_unslash($_GET['orderId']));
$order_id = explode('-', $oid);
if (count($order_id) !== 2) {
wp_die('RevenueMonster payment failed', 'RevenueMonster Payment', array('response' => 500));
return;
}
$order = new WC_Order($order_id[0]);
$sdk = $this->get_sdk();
try {
$response = $sdk->query_order($oid);
$order->set_payment_method($response->method);
$order->save();
switch (strtoupper($response->status)) {
case 'SUCCESS':
$obj = json_decode(json_encode($response), true);
$order->payment_complete($obj['transactionId']);
WC()->cart->empty_cart();
wp_redirect($order->get_checkout_order_received_url());
break;
default:
throw new Exception('invalid payment status');
}
} catch (Exception $e) {
// https://docs.woocommerce.com/document/managing-orders/.
// mark as failed.
$order->update_status('failed', __('Payment failed', 'woocommerce-gateway-revenuemonster'));
$order->save();
wp_die('RevenueMonster payment failed', 'RevenueMonster Payment', array('response' => 500));
}
}
}
}