-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwoocommerce-citcon-payments.php
497 lines (454 loc) · 21.3 KB
/
woocommerce-citcon-payments.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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
<?php
/**
* Plugin Name: WooCommerce Citcon Payments
* Plugin URI: https://woocommerce.com/products/woocommerce-citcon-payments/
* Description: Citcon latest complete payments processing solution.
* Version: 0.0.1
* Author: SHOPEO
* Author URI: https://shopeo.cn/
* License: GPL-2.0
* Requires PHP: 7.1
* WC requires at least: 3.9
* WC tested up to: 6.7
* Text Domain: woocommerce-citcon-payments
*
*/
require_once 'vendor/autoload.php';
use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;
use Automattic\WooCommerce\Utilities\FeaturesUtil;
use Shopeo\WoocommerceCitconPayments\CitconPayApi;
use Shopeo\WoocommerceCitconPayments\PaymentChannel;
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly.
}
if (!defined('WOOCOMMERCE_CITCON_PAYMENTS_PLUGIN_FILE')) {
define('WOOCOMMERCE_CITCON_PAYMENTS_PLUGIN_FILE', __FILE__);
}
if (!defined('WOOCOMMERCE_CITCON_PAYMENTS_PLUGIN_BASE')) {
define('WOOCOMMERCE_CITCON_PAYMENTS_PLUGIN_BASE', plugin_basename(WOOCOMMERCE_CITCON_PAYMENTS_PLUGIN_FILE));
}
if (!defined('WOOCOMMERCE_CITCON_PAYMENTS_PATH')) {
define('WOOCOMMERCE_CITCON_PAYMENTS_PATH', plugin_dir_path(WOOCOMMERCE_CITCON_PAYMENTS_PLUGIN_FILE));
}
if (!function_exists('woocommerce_citcon_payments_activate')) {
function woocommerce_citcon_payments_activate()
{
}
}
register_activation_hook(__FILE__, 'woocommerce_citcon_payments_activate');
if (!function_exists('woocommerce_citcon_payments_deactivate')) {
function woocommerce_citcon_payments_deactivate()
{
}
}
register_deactivation_hook(__FILE__, 'woocommerce_citcon_payments_deactivate');
if (!function_exists('woocommerce_citcon_payments_load_textdomain')) {
function woocommerce_citcon_payments_load_textdomain()
{
load_plugin_textdomain('woocommerce-citcon-payments', false, dirname(plugin_basename(__FILE__)) . '/languages');
}
}
add_action('init', 'woocommerce_citcon_payments_load_textdomain');
$payment_channels = [
new PaymentChannel([
'method' => 'card',
'title' => 'Card',
'currency' => ['USD'],
'country' => '',
'enabled' => 'no',
'checked' => 'yes',
'hide_form_title' => 'no',
'icon' => 'assets/images/card.jpg',
'icon_height' => '24',
]),
new PaymentChannel([
'title' => 'Alipay',
'currency' => ['USD', 'CAD'],
'country' => '',
'enabled' => 'no',
'method' => 'alipay',
'checked' => 'no',
'icon' => 'assets/images/alipay-logo.png',
]),
new PaymentChannel([
'method' => 'wechatpay',
'title' => 'WeChat Pay',
'currency' => ['USD', 'CAD'],
'country' => '',
'enabled' => 'no',
'checked' => 'no',
'icon' => 'assets/images/wechatpay-logo.png',
]),
new PaymentChannel([
'method' => 'upop',
'title' => 'Union Pay',
'currency' => ['USD', 'CAD'],
'country' => '',
'enabled' => 'no',
'checked' => 'no',
'icon' => 'assets/images/unionpay2-logo.png',
]),
new PaymentChannel([
'method' => 'paypal',
'title' => 'Paypal',
'currency' => ['USD'],
'country' => '',
'enabled' => 'no',
'checked' => 'no',
'icon' => 'assets/images/paypal-logo.png',
]),
new PaymentChannel([
'method' => 'venmo',
'title' => 'Venmo',
'currency' => ['USD'],
'country' => '',
'enabled' => 'no',
'checked' => 'no',
'icon' => 'assets/images/venmo-logo.png',
'icon_height' => '20',
]),
];
if (!function_exists('get_payment_channels')) {
function get_payment_channels()
{
global $payment_channels;
return $payment_channels;
}
}
if (!function_exists('get_form_payment_channels')) {
function get_form_payment_channels()
{
$list = [];
$payment_channels = get_payment_channels();
if (!empty($payment_channels)) {
foreach ($payment_channels as $channel) {
$list[$channel->method] = $channel->get_form_fields();
}
}
return $list;
}
}
if (!function_exists('get_payment_channel_titles')) {
function get_payment_channel_titles()
{
$list = [];
$payment_channels = get_payment_channels();
if (!empty($payment_channels)) {
foreach ($payment_channels as $channel) {
$list[$channel->method] = $channel->title;
}
}
return $list;
}
}
if (!function_exists('woocommerce_gateway_citcon_init')) {
function woocommerce_gateway_citcon_init()
{
if (!class_exists('WC_Payment_Gateway')) {
return;
}
if (!class_exists('WC_Citcon_Pay_Gateway')) {
class WC_Citcon_Pay_Gateway extends WC_Payment_Gateway
{
private $debug;
private $merchant_key;
public function __construct()
{
$this->id = 'citcon';
$this->icon = plugins_url('/assets/images/citcon-pay-logo.svg', WOOCOMMERCE_CITCON_PAYMENTS_PLUGIN_FILE);
$this->method_title = __('Citcon Payment', 'woocommerce-citcon-payments');
$this->method_description = __('Citcon Payment Gateway', 'woocommerce-citcon-payments');
$this->supports = array(
'products'
);
$this->init_form_fields();
$this->init_settings();
$this->enabled = $this->get_option('enabled');
$this->debug = $this->get_option('debug') == 'yes' ? true : false;
$this->title = $this->get_option('title');
$this->description = $this->get_option('description');
$this->merchant_key = $this->get_option('merchant_key');
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array(
$this,
'process_admin_options'
));
add_action('woocommerce_api_' . $this->id, array($this, 'webhook'));
}
public function init_form_fields()
{
$titles = get_payment_channel_titles();
$this->form_fields = array(
'enabled' => array(
'title' => __('Enabled/Disable', 'woocommerce-citcon-payments'),
'label' => __('Enable Citcon Gateway', 'woocommerce-citcon-payments'),
'type' => 'checkbox',
'description' => '',
'default' => 'no'
),
'debug' => array(
'title' => __('Debug', 'woocommerce-citcon-payments'),
'label' => __('Debug', 'woocommerce-citcon-payments'),
'type' => 'checkbox',
'description' => '',
'default' => 'no'
),
'title' => array(
'title' => __('Title', 'woocommerce-citcon-payments'),
'type' => 'text',
'default' => __('Citcon Payment', 'woocommerce-citcon-payments'),
'desc_tip' => true,
),
'description' => array(
'title' => __('Description', 'woocommerce-citcon-payments'),
'type' => 'textarea',
'default' => __('Please select your payment method', 'woocommerce-citcon-payments'),
'desc_tip' => true,
),
'merchant_key' => array(
'title' => __('Merchant Key', 'woocommerce-citcon-payments'),
'type' => 'text',
'default' => '',
'desc_tip' => true,
),
'default_channel' => array(
'title' => __('Default Payment Method', 'woocommerce-citcon-payments'),
'type' => 'select',
'options' => $titles,
'default' => array_keys($titles)[0],
'description' => __('Select a default payment method', 'woocommerce-citcon-payments')
)
);
$channels = get_form_payment_channels();
foreach ($channels as $key => $value) {
$this->form_fields[$key] = $value;
}
}
public function payment_fields()
{
if ($this->description) {
?>
<p><?php esc_html_e($this->description); ?></p>
<?php
}
?>
<fieldset>
<legend><label><?php esc_html_e('Method of payment', 'woocommerce-citcon-payments'); ?><span
class="required">*</span></label></legend>
<ul class="wc_payment_methods payment_methods methods">
<?php
$plugin_dir = plugin_dir_url(__FILE__);
$currency = get_option('woocommerce_currency');
foreach (get_payment_channels() as $channel) {
$method = $channel->method;
$title = $channel->title;
$icon = $channel->icon;
$icon_height = $channel->icon_height;
if (strcmp($this->settings[$method], 'yes') == 0 && in_array($currency, $channel->currency)) { ?>
<li class="wc_payment_method">
<div style="display: flex; align-items: center;">
<input id="citcon_pay_method_<?php echo $method; ?>"
class="input-radio"
name="payment_channel"
value="<?php echo $method; ?>"
data-order_button_text=""
type="radio" required
<?php
if (strcmp($this->settings['default_channel'], $method) == 0) { ?>
checked="checked"
<?php } ?>
>
<label for="citcon_pay_method_<?php echo $method; ?>">
<img src="<?php echo $plugin_dir . $icon; ?>"
style="height: <?php echo $icon_height; ?>px; margin-left: -2px;"
alt="Citcon Pay"
title="<?php esc_html_e($title); ?>"
/>
<!-- <?php esc_html_e($title); ?> -->
</label>
</div>
</li>
<?php
}
}
?>
</ul>
</fieldset>
<?php
}
public function process_payment($order_id)
{
global $woocommerce;
$order = new WC_Order($order_id);
if (!wp_verify_nonce('', 'woocommerce-process_checkout')
&& !empty($_POST['payment_channel'])
&& sanitize_key($_POST['payment_channel'])) {
$payment_channel = sanitize_key($_POST['payment_channel']);
}
$citconApi = new CitconPayApi($this->merchant_key, $this->debug);
$reference = $order_id;
$amount = intval($order->get_total() * 100);
$currency = $order->get_currency();
$country = $order->get_shipping_country();
$method = $payment_channel;
$ipn_url = home_url() . '/wc-api/' . $this->id . '?id=' . $order->get_id();
$success_url = $this->get_return_url($order);
$fail_url = $success_url;
$goods = [
'shipping' => [
'city' => $order->get_shipping_city(),
'zip' => $order->get_shipping_postcode(),
'country' => $order->get_shipping_country()
]
];
foreach ($order->get_items(['line_item', 'fee', 'shipping']) as $item) {
$total_amount = $item->get_total();
if ($total_amount > 0) {
$goods['data'][] = [
'name' => $item->get_name(),
'quantity' => 1,
'unit_amount' => intval($total_amount * 100),
"product_type" => "physical"
];
}
}
$billing_address = [
"street" => $order->get_billing_address_1(),
"street2" => $order->get_billing_address_2(),
"city" => $order->get_billing_city(),
"state" => $order->get_billing_state(),
"zip" => $order->get_billing_postcode(),
"country" => $order->get_billing_country()
];
$data = $citconApi->charge($reference, $amount, $currency, $country, $billing_address, $method, $ipn_url, $success_url, $fail_url, $goods);
if ($data['status'] === 'success') {
$order->set_transaction_id($data['data']['id']);
$order->save();
$woocommerce->cart->empty_cart();
error_log('Redirect:' . $data['data']['payment']['client'][0]['content']);
foreach ($data['data']['payment']['client'] as $item) {
if ($item['format'] === 'redirect' && $item['method'] === 'GET') {
$redirect_url = $item['content'];
}
}
error_log($redirect_url);
return array(
'result' => 'success',
'redirect' => $redirect_url
);
}
return null;
}
public function webhook()
{
$order = new WC_Order($_GET['id']);
error_log('WebHook:' . $_GET['id']);
$params = file_get_contents("php://input");
error_log($params);
$params = json_decode($params, true);
error_log(print_r($params, true));
$sign_str = "amount={$params['amount']}&amount_captured={$params['amount_captured']}&amount_refunded={$params['amount_refunded']}¤cy={$params['currency']}&fields={$params['fields']}&id={$params['id']}&payment={$params['payment']}&payment_method={$params['payment_method']}&reference={$params['reference']}&status={$params['status']}&time_completed={$params['time_completed']}&time_created={$params['time_created']}&transaction_type={$params['transaction_type']}&secret={$this->merchant_key}";
error_log($sign_str);
$sign = hash('sha256', $sign_str);
error_log($sign);
if ($order->get_id() === intval($params['reference']) && $sign === $params['sign'] && $params['transaction_type'] === 'charge' && $params['status'] === 'succeeded') {
error_log('Payment completed');
$order->payment_complete();
wc_reduce_stock_levels($order->get_id());
$order->add_order_note(
__('Payment completed', 'woocommerce-citcon-payments')
);
}
}
}
}
}
}
add_action('plugins_loaded', 'woocommerce_gateway_citcon_init', 0);
if (!function_exists('woocommerce_add_gateway_citcon_gateway')) {
function woocommerce_add_gateway_citcon_gateway($methods)
{
$methods[] = 'WC_Citcon_Pay_Gateway';
return $methods;
}
}
add_filter('woocommerce_payment_gateways', 'woocommerce_add_gateway_citcon_gateway');
if (!function_exists('citcon_declare_cart_checkout_blocks_compatibility')) {
function citcon_declare_cart_checkout_blocks_compatibility()
{
if (class_exists('\Automattic\WooCommerce\Utilities\FeaturesUtil')) {
FeaturesUtil::declare_compatibility('cart_checkout_blocks', __FILE__, true);
FeaturesUtil::declare_compatibility('custom_order_tables', __FILE__, true);
}
}
}
add_action('before_woocommerce_init', 'citcon_declare_cart_checkout_blocks_compatibility');
if (!function_exists('citcon_register_order_approval_payment_method_type')) {
function citcon_register_order_approval_payment_method_type()
{
if (!class_exists('Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType')) {
return;
}
if (!class_exists('WC_Citcon_Pay_Gateway_Blocks')) {
final class WC_Citcon_Pay_Gateway_Blocks extends AbstractPaymentMethodType
{
private $gateway;
protected $name = 'citcon';
public function initialize()
{
$this->settings = get_option('woocommerce_citcon_settings', []);
$gateways = WC()->payment_gateways->payment_gateways();
$this->gateway = $gateways[$this->name];
}
public function is_active()
{
return $this->gateway->is_available();
}
public function get_payment_method_script_handles()
{
wp_register_script('citcon-blocks-integration', plugin_dir_url(__FILE__) . 'assets/js/checkout.js', [
'wc-blocks-registry',
'wc-settings',
'wp-element',
'wp-html-entities',
'wp-i18n',
], null, true);
if (function_exists('wp_set_script_translations')) {
wp_set_script_translations('citcon-blocks-integration', 'woocommerce-citcon-payments', plugin_dir_path(__FILE__) . 'languages');
}
return ['citcon-blocks-integration'];
}
public function get_payment_method_data()
{
$channels = [];
$plugin_dir = plugin_dir_url(__FILE__);
$currency = get_option('woocommerce_currency');
foreach (get_payment_channels() as $channel) {
$method = $channel->method;
$title = $channel->title;
$icon = $channel->icon;
$icon_height = $channel->icon_height;
if (strcmp($this->settings[$method], 'yes') == 0 && in_array($currency, $channel->currency)) {
$channels[] = [
'method' => $method,
'title' => $title,
'icon' => $plugin_dir . $icon,
'icon_height' => $icon_height,
];
}
}
return [
'title' => $this->get_setting('title'),
'description' => $this->get_setting('description'),
'channels' => json_encode($channels),
'default_channel' => $this->settings['default_channel'],
'supports' => $this->gateway->supports,
];
}
}
}
add_action('woocommerce_blocks_payment_method_type_registration', function (Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry $payment_method_registry) {
$payment_method_registry->register(new WC_Citcon_Pay_Gateway_Blocks);
});
}
}
add_action('woocommerce_blocks_loaded', 'citcon_register_order_approval_payment_method_type');