-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweenx-wc-gateway.php
409 lines (347 loc) · 16 KB
/
weenx-wc-gateway.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
<?php
/**
* Weenx WooCommerce gateway plugin
*
* this plugin will add weenx specific gateway support
* for wooCommerce plugin
*
* @class WC_Gateway_Weenx
* @extends WC_Payment_Gateway
* @version 1.0.0
* @package WooCommerce/Classes/Payment
* @author Zoha Banam
*/
if (!in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) return;
add_action('plugins_loaded', 'wc_weenx_gateway_init', 11);
function wc_weenx_gateway_init()
{
// load soap wrapper
require_once(WEENX_WC_GATEWAY_PLUGIN_DIR . "soap_wrappers.php");
class Weenx_Gift_Pay_Gateway extends WC_Payment_Gateway
{
protected $wrapper = null;
protected $sender_id;
protected $plain_id;
protected $encrypt_id;
public function __construct()
{
$this->id = 'Weenx_Gift_Pay_Gateway';
$this->has_fields = false;
$this->method_title = "Weenx Voucher";
$this->method_description = "Weenx Voucher Gateway";
$this->icon = apply_filters('Weenx_Gift_Pay_Gateway_logo', WP_PLUGIN_URL . '/' . plugin_basename(__DIR__) . '/assets/logo.png');
$this->init_form_fields();
$this->init_settings();
$this->title = $this->settings['title'];
$this->description = $this->settings['description'];
$this->sender_id = $this->settings['sender_id'];
$this->plain_id = $this->settings['plain_id'];
$this->encrypt_id = $this->settings['encrypt_id'];
$this->gateway_url = $this->settings['gateway_url'];
$this->injectWrapper();
if (version_compare(WOOCOMMERCE_VERSION, '2.0.0', '>=')) {
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
} else {
add_action('woocommerce_update_options_payment_gateways', array($this, 'process_admin_options'));
}
add_action('woocommerce_api_weenx_gift_pay_gateway', array($this, 'process_callback_return'));
}
protected function injectWrapper()
{
// soap wrapper
$this->wrapper = new Weenx_GiftPay_Soap_Wrapper($this->sender_id, $this->plain_id, $this->encrypt_id);
}
public function init_form_fields()
{
$this->form_fields = apply_filters('Weenx_Gift_Pay_Gateway_config', array(
'enabled' => array(
'title' => __('Enable/Disable', 'woocommerce'),
'type' => 'checkbox',
'label' => __('Enable Weenx Voucher Gateway Payment', 'woocommerce'),
'default' => 'yes'
),
'title' => array(
'title' => __('Title', 'woocommerce'),
'type' => 'text',
'description' => __('This controls the title which the user sees during checkout.', 'woocommerce'),
'default' => __('Weenx Voucher', 'woocommerce'),
'desc_tip' => true,
),
'description' => array(
'title' => __('Description', 'woocommerce'),
'type' => 'textarea',
'default' => ''
),
'sender_id' => array(
'title' => __('Sender Id', 'woocommerce'),
'description' => __('sender id of gateway that you get from weenx gift pay', 'woocommerce'),
'type' => 'text',
'desc_tip' => true,
),
'plain_id' => array(
'title' => __('Plain Id', 'woocommerce'),
'description' => __('plain id of gateway that you get from weenx git pay', 'woocommerce'),
'type' => 'text',
'desc_tip' => true,
),
'encrypt_id' => array(
'title' => __('Encrypt Id', 'woocommerce'),
'description' => __('encrypt id of gateway that you get from weenx git pay', 'woocommerce'),
'type' => 'text',
'desc_tip' => true,
),
'gateway_url' => array(
'title' => __('Gateway Url Endpoint', 'woocommerce'),
'description' => __('endpoint for api calls', 'woocommerce'),
'default' => WEENX_WC_GATEWAY_GIFT_PAY_DEFAULT_URL,
'type' => 'url',
'desc_tip' => true,
)
));
}
function process_payment($order_id)
{
global $woocommerce;
$order = new WC_Order($order_id);
$woocommerce->session->order_id_weenx_gift_pay = $order_id;
$callback = WC()->api_request_url('Weenx_Gift_Pay_Gateway');
$currencyId = $this->getCurrencyId($order->get_currency()) ?: 3;
$language = $currencyId == 0 ? 'farsi' : 'english';
$result = $this->wrapper->payment($order_id, $order->get_total(), $callback, $language, $currencyId);
if (!$result->Status || $result->Status !== 1) {
throw new Exception($result->Message);
}
$order->update_status('on-hold', __('Awaiting weenx git pay payment', 'woocommerce'));
return array(
'result' => 'success',
'redirect' => $result->URL
);
}
protected function getCurrencyId($currency = 'USD')
{
return [
'IRT' => 1,
'EUR' => 2,
'USD' => 3,
][$currency];
}
function process_callback_return()
{
global $woocommerce;
// get result token
$resultToken = $_GET['result'] ?: $_POST['result'];
if (!$resultToken) {
wc_add_notice(__('No token has specified', 'woocommerce'), 'error');
wp_redirect($woocommerce->cart->get_checkout_url());
exit;
}
// get order id
if (isset($_GET['wc_order'])) {
$order_id = $_GET['wc_order'];
} else {
$order_id = $woocommerce->session->order_id_weenx_gift_pay;
unset($woocommerce->session->order_id_weenx_gift_pay);
}
if ($order_id) {
try {
$order = new WC_Order($order_id);
$result = $this->wrapper->verify($resultToken);
// compare result fields with order fields
if ($result->Status !== 1) {
throw new Exception($result->Message);
}
if ($order_id != $result->ReservationNumber) {
throw new Exception("Order id is not equal to reservation number");
}
if ($order->get_total() != $result->Amount) {
throw new Exception("Amount of products is not equal to paid amount");
}
$orderCurrency = $this->getCurrencyId($order->get_currency()) ?: 3;
if ($orderCurrency != $result->CurrencyId) {
throw new Exception("Currencies are not equal");
}
update_post_meta($order_id, '_transaction_id', $result->ReferenceNumber);
$order->payment_complete($result->ReferenceNumber);
$woocommerce->cart->empty_cart();
wc_add_notice("Payment Was Successful, Reference Id : " . $result->ReferenceNumber, 'success');
wp_redirect(add_query_arg('wc_status', 'success', $this->get_return_url($order)));
} catch (Exception $e) {
wc_add_notice(__($e->getMessage(), 'woocommerce'), 'error');
wp_redirect($woocommerce->cart->get_checkout_url());
exit;
}
} else {
wc_add_notice(__('Order id not exists', 'woocommerce'), 'error');
wp_redirect($woocommerce->cart->get_checkout_url());
exit;
}
}
}
class Weenx_Go_Pay_Gateway extends WC_Payment_Gateway
{
protected $wrapper = null;
protected $sender_id;
protected $plain_id;
protected $encrypt_id;
public function __construct()
{
$this->id = 'Weenx_Go_Pay_Gateway';
$this->has_fields = false;
$this->method_title = "Weenx";
$this->method_description = "Weenx Gateway";
$this->icon = apply_filters('Weenx_Go_Pay_Gateway_logo', WP_PLUGIN_URL . '/' . plugin_basename(__DIR__) . '/assets/logo.png');
$this->init_form_fields();
$this->init_settings();
$this->title = $this->settings['title'];
$this->description = $this->settings['description'];
$this->sender_id = $this->settings['sender_id'];
$this->plain_id = $this->settings['plain_id'];
$this->encrypt_id = $this->settings['encrypt_id'];
$this->gateway_url = $this->settings['gateway_url'];
$this->injectWrapper();
if (version_compare(WOOCOMMERCE_VERSION, '2.0.0', '>=')) {
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
} else {
add_action('woocommerce_update_options_payment_gateways', array($this, 'process_admin_options'));
}
add_action('woocommerce_api_weenx_go_pay_gateway', array($this, 'process_callback_return'));
}
protected function injectWrapper()
{
// soap wrapper
$this->wrapper = new Weenx_GoPay_Soap_Wrapper($this->sender_id, $this->plain_id, $this->encrypt_id);
}
public function init_form_fields()
{
$this->form_fields = apply_filters('Weenx_Go_Pay_Gateway_config', array(
'enabled' => array(
'title' => __('Enable/Disable', 'woocommerce'),
'type' => 'checkbox',
'label' => __('Enable Weenx Gateway Payment', 'woocommerce'),
'default' => 'yes'
),
'title' => array(
'title' => __('Title', 'woocommerce'),
'type' => 'text',
'description' => __('This controls the title which the user sees during checkout.', 'woocommerce'),
'default' => __('Weenx', 'woocommerce'),
'desc_tip' => true,
),
'description' => array(
'title' => __('Description', 'woocommerce'),
'type' => 'textarea',
'default' => ''
),
'sender_id' => array(
'title' => __('Sender Id', 'woocommerce'),
'description' => __('sender id of gateway that you get from weenx go pay', 'woocommerce'),
'type' => 'text',
'desc_tip' => true,
),
'plain_id' => array(
'title' => __('Plain Id', 'woocommerce'),
'description' => __('plain id of gateway that you get from weenx git pay', 'woocommerce'),
'type' => 'text',
'desc_tip' => true,
),
'encrypt_id' => array(
'title' => __('Encrypt Id', 'woocommerce'),
'description' => __('encrypt id of gateway that you get from weenx git pay', 'woocommerce'),
'type' => 'text',
'desc_tip' => true,
),
'gateway_url' => array(
'title' => __('Gateway Url Endpoint', 'woocommerce'),
'description' => __('endpoint for api calls', 'woocommerce'),
'default' => WEENX_WC_GATEWAY_GO_PAY_DEFAULT_URL,
'type' => 'url',
'desc_tip' => true,
)
));
}
function process_payment($order_id)
{
global $woocommerce;
$order = new WC_Order($order_id);
$woocommerce->session->order_id_weenx_go_pay = $order_id;
$callback = WC()->api_request_url('Weenx_Go_Pay_Gateway');
$currencyId = $this->getCurrencyId($order->get_currency()) ?: 3;
$language = $currencyId == 0 ? 'farsi' : 'english';
$result = $this->wrapper->payment($order_id, $order->get_total(), $callback, $language, $currencyId);
if (!$result->Status || $result->Status !== 1) {
throw new Exception($result->Message);
}
$order->update_status('on-hold', __('Awaiting weenx git pay payment', 'woocommerce'));
return array(
'result' => 'success',
'redirect' => $result->URL
);
}
protected function getCurrencyId($currency = 'USD')
{
return [
'IRT' => 1,
'EUR' => 2,
'USD' => 3,
][$currency];
}
function process_callback_return()
{
global $woocommerce;
// get result token
$resultToken = $_GET['result'] ?: $_POST['result'];
if (!$resultToken) {
wc_add_notice(__('No token has specified', 'woocommerce'), 'error');
wp_redirect($woocommerce->cart->get_checkout_url());
exit;
}
// get order id
if (isset($_GET['wc_order'])) {
$order_id = $_GET['wc_order'];
} else {
$order_id = $woocommerce->session->order_id_weenx_go_pay;
unset($woocommerce->session->order_id_weenx_go_pay);
}
if ($order_id) {
try {
$order = new WC_Order($order_id);
$result = $this->wrapper->verify($resultToken);
// compare result fields with order fields
if ($result->Status !== 4) {
throw new Exception($result->Message);
}
if ($order_id != $result->ReservationNumber) {
throw new Exception("Order id is not equal to reservation number");
}
if ($order->get_total() != $result->Amount) {
throw new Exception("Amount of products is not equal to paid amount");
}
$orderCurrency = $this->getCurrencyId($order->get_currency()) ?: 3;
if ($orderCurrency != $result->CurrencyId) {
throw new Exception("Currencies are not equal");
}
update_post_meta($order_id, '_transaction_id', $result->ReferenceNumber);
$order->payment_complete($result->ReferenceNumber);
$woocommerce->cart->empty_cart();
wc_add_notice("Payment Was Successful, Reference Id : " . $result->ReferenceNumber, 'success');
wp_redirect(add_query_arg('wc_status', 'success', $this->get_return_url($order)));
} catch (Exception $e) {
wc_add_notice(__($e->getMessage(), 'woocommerce'), 'error');
wp_redirect($woocommerce->cart->get_checkout_url());
exit;
}
} else {
wc_add_notice(__('Order id not exists', 'woocommerce'), 'error');
wp_redirect($woocommerce->cart->get_checkout_url());
exit;
}
}
}
}
function add_weenx_gateway_class($methods)
{
$methods[] = 'Weenx_Gift_Pay_Gateway';
$methods[] = 'Weenx_Go_Pay_Gateway';
return $methods;
}
add_filter('woocommerce_payment_gateways', 'add_weenx_gateway_class');