-
Notifications
You must be signed in to change notification settings - Fork 25
/
class-omise-payment-creditcard.php
562 lines (488 loc) · 18.8 KB
/
class-omise-payment-creditcard.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
<?php
defined( 'ABSPATH' ) or die( 'No direct script access allowed.' );
class Omise_Payment_Creditcard extends Omise_Payment {
const PAYMENT_ACTION_AUTHORIZE = 'manual_capture';
const PAYMENT_ACTION_AUTHORIZE_CAPTURE = 'auto_capture';
public function __construct() {
parent::__construct();
$this->id = 'omise';
$this->has_fields = true;
$this->method_title = __( 'Omise Credit / Debit Card', 'omise' );
$this->method_description = wp_kses(
__( 'Accept payment through <strong>Credit / Debit Card</strong> via Omise payment gateway.', 'omise' ),
array(
'strong' => array()
)
);
$this->supports = array( 'products', 'refunds' );
$this->init_form_fields();
$this->init_settings();
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->payment_action = $this->get_option( 'payment_action' );
$this->restricted_countries = array( 'TH', 'JP', 'SG' );
add_action( 'woocommerce_api_' . $this->id . '_callback', array( $this, 'callback' ) );
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'omise_scripts' ) );
add_action( 'woocommerce_order_action_' . $this->id . '_charge_capture', array( $this, 'process_capture' ) );
add_action( 'woocommerce_order_action_' . $this->id . '_sync_payment', array( $this, 'sync_payment' ) );
/** @deprecated 3.0 */
add_action( 'woocommerce_api_wc_gateway_' . $this->id, array( $this, 'callback' ) );
}
/**
* @see WC_Settings_API::init_form_fields()
* @see woocommerce/includes/abstracts/abstract-wc-settings-api.php
*/
function init_form_fields() {
$this->form_fields = array_merge(
array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'omise' ),
'type' => 'checkbox',
'label' => __( 'Enable Omise Credit / Debit Card Payment', 'omise' ),
'default' => 'no'
),
'title' => array(
'title' => __( 'Title', 'omise' ),
'type' => 'text',
'description' => __( 'This controls the title which the user sees during checkout.', 'omise' ),
'default' => __( 'Credit / Debit Card', 'omise' )
),
'description' => array(
'title' => __( 'Description', 'omise' ),
'type' => 'textarea',
'description' => __( 'This controls the description which the user sees during checkout.', 'omise' )
),
),
array(
'advanced' => array(
'title' => __( 'Advance Settings', 'omise' ),
'type' => 'title'
),
'payment_action' => array(
'title' => __( 'Payment action', 'omise' ),
'type' => 'select',
'description' => __( 'Capture automatically during the checkout process or manually after order has been placed', 'omise' ),
'default' => self::PAYMENT_ACTION_AUTHORIZE_CAPTURE,
'class' => 'wc-enhanced-select',
'options' => array(
self::PAYMENT_ACTION_AUTHORIZE_CAPTURE => __( 'Auto Capture', 'omise' ),
self::PAYMENT_ACTION_AUTHORIZE => __( 'Manual Capture', 'omise' )
),
'desc_tip' => true
),
'accept_visa' => array(
'title' => __( 'Supported card icons', 'omise' ),
'type' => 'checkbox',
'label' => Omise_Card_Image::get_visa_image(),
'css' => Omise_Card_Image::get_css(),
'default' => Omise_Card_Image::get_visa_default_display()
),
'accept_mastercard' => array(
'type' => 'checkbox',
'label' => Omise_Card_Image::get_mastercard_image(),
'css' => Omise_Card_Image::get_css(),
'default' => Omise_Card_Image::get_mastercard_default_display()
),
'accept_jcb' => array(
'type' => 'checkbox',
'label' => Omise_Card_Image::get_jcb_image(),
'css' => Omise_Card_Image::get_css(),
'default' => Omise_Card_Image::get_jcb_default_display()
),
'accept_diners' => array(
'type' => 'checkbox',
'label' => Omise_Card_Image::get_diners_image(),
'css' => Omise_Card_Image::get_css(),
'default' => Omise_Card_Image::get_diners_default_display()
),
'accept_amex' => array(
'type' => 'checkbox',
'label' => Omise_Card_Image::get_amex_image(),
'css' => Omise_Card_Image::get_css(),
'default' => Omise_Card_Image::get_amex_default_display(),
'description' => wp_kses(
__( 'This only controls the icons displayed on the checkout page.<br />It is not related to card processing on Omise payment gateway.', 'omise' ),
array( 'br' => array() )
)
)
)
);
}
/**
* @see WC_Payment_Gateway::payment_fields()
* @see woocommerce/includes/abstracts/abstract-wc-payment-gateway.php
*/
public function payment_fields() {
parent::payment_fields();
if ( is_user_logged_in() ) {
$viewData['user_logged_in'] = true;
$current_user = wp_get_current_user();
$omise_customer_id = $this->is_test() ? $current_user->test_omise_customer_id : $current_user->live_omise_customer_id;
if ( ! empty( $omise_customer_id ) ) {
try {
$customer = OmiseCustomer::retrieve( $omise_customer_id );
$viewData['existingCards'] = $customer->cards( array( 'order' => 'reverse_chronological' ) );
} catch (Exception $e) {
// nothing
}
}
} else {
$viewData['user_logged_in'] = false;
}
Omise_Util::render_view( 'templates/payment/form.php', $viewData );
}
/**
* @inheritdoc
*/
public function charge( $order_id, $order ) {
$token = isset( $_POST['omise_token'] ) ? wc_clean( $_POST['omise_token'] ) : '';
$card_id = isset( $_POST['card_id'] ) ? wc_clean( $_POST['card_id'] ) : '';
if ( empty( $token ) && empty( $card_id ) ) {
throw new Exception( __( 'Please select an existing card or enter new card information.', 'omise' ) );
}
$user = $order->get_user();
$omise_customer_id = $this->is_test() ? $user->test_omise_customer_id : $user->live_omise_customer_id;
// Saving card.
if ( isset( $_POST['omise_save_customer_card'] ) && empty( $card_id ) ) {
if ( empty( $token ) ) {
throw new Exception( __( 'Unable to process the card. Please make sure that the information is correct, or contact our support team if you have any questions.', 'omise' ) );
}
if ( ! empty( $omise_customer_id ) ) {
try {
// attach a new card to customer
$customer = OmiseCustomer::retrieve( $omise_customer_id );
$customer->update( array(
'card' => $token
) );
$cards = $customer->cards( array(
'limit' => 1,
'order' => 'reverse_chronological'
) );
$card_id = $cards['data'][0]['id'];
} catch (Exception $e) {
throw new Exception( $e->getMessage() );
}
} else {
$description = "WooCommerce customer " . $user->ID;
$customer_data = array(
"description" => $description,
"card" => $token
);
$omise_customer = OmiseCustomer::create( $customer_data );
if ( $omise_customer['object'] == "error" ) {
throw new Exception( $omise_customer['message'] );
}
$omise_customer_id = $omise_customer['id'];
if ( $this->is_test() ) {
update_user_meta( $user->ID, 'test_omise_customer_id', $omise_customer_id );
} else {
update_user_meta( $user->ID, 'live_omise_customer_id', $omise_customer_id );
}
if ( 0 == sizeof( $omise_customer['cards']['data'] ) ) {
throw new Exception(
sprintf(
wp_kses(
__( 'Please note that you\'ve done nothing wrong - this is likely an issue with our store.<br/><br/>Feel free to try submitting your order again, or report this problem to our support team (Your temporary order id is \'%s\')', 'omise' ),
array(
'br' => array()
)
),
$order_id
)
);
}
$cards = $omise_customer->cards( array( 'order' => 'reverse_chronological' ) );
$card_id = $cards['data'][0]['id']; //use the latest card
}
}
$success = false;
$return_uri = add_query_arg(
array(
'wc-api' => 'omise_callback',
'order_id' => $order_id
),
home_url()
);
$data = array(
'amount' => Omise_Money::to_subunit( $order->get_total(), $order->get_order_currency() ),
'currency' => $order->get_order_currency(),
'description' => apply_filters( 'omise_charge_params_description', 'WooCommerce Order id ' . $order_id, $order ),
'return_uri' => $return_uri
);
if ( ! empty( $omise_customer_id ) && ! empty( $card_id ) ) {
$data['customer'] = $omise_customer_id;
$data['card'] = $card_id;
} else {
$data['card'] = $token;
}
// Set capture status (otherwise, use API's default behaviour)
if ( self::PAYMENT_ACTION_AUTHORIZE_CAPTURE === $this->payment_action ) {
$data['capture'] = true;
} else if ( self::PAYMENT_ACTION_AUTHORIZE === $this->payment_action ) {
$data['capture'] = false;
}
$metadata = apply_filters( 'omise_charge_params_metadata', array(), $order );
$data['metadata'] = array_merge( $metadata, array(
/** override order_id as a reference for webhook handlers **/
/** backward compatible with WooCommerce v2.x series **/
'order_id' => version_compare( WC()->version, '3.0.0', '>=' ) ? $order->get_id() : $order->id
) );
return OmiseCharge::create( $data );
}
/**
* @inheritdoc
*/
public function result( $order_id, $order, $charge ) {
if ( Omise_Charge::is_failed( $charge ) ) {
return $this->payment_failed( Omise_Charge::get_error_message( $charge ) );
}
// If 3-D Secure feature is enabled, redirecting user out to a 3rd-party credit card authorization page.
if ( self::STATUS_PENDING === $charge['status'] && ! $charge['authorized'] && ! $charge['paid'] && ! empty( $charge['authorize_uri'] ) ) {
$order->add_order_note(
sprintf(
__( 'Omise: Processing a 3-D Secure payment, redirecting buyer to %s', 'omise' ),
esc_url( $charge['authorize_uri'] )
)
);
return array(
'result' => 'success',
'redirect' => $charge['authorize_uri'],
);
}
switch ( $this->payment_action ) {
case self::PAYMENT_ACTION_AUTHORIZE:
$success = Omise_Charge::is_authorized( $charge );
if ( $success ) {
$order->add_order_note(
sprintf(
wp_kses(
__( 'Omise: Payment processing.<br/>An amount of %1$s %2$s has been authorized', 'omise' ),
array( 'br' => array() )
),
$order->get_total(),
$order->get_order_currency()
)
);
}
break;
case self::PAYMENT_ACTION_AUTHORIZE_CAPTURE:
$success = Omise_Charge::is_paid( $charge );
if ( $success ) {
$order->payment_complete();
$order->add_order_note(
sprintf(
wp_kses(
__( 'Omise: Payment successful.<br/>An amount of %1$s %2$s has been paid', 'omise' ),
array( 'br' => array() )
),
$order->get_total(),
$order->get_order_currency()
)
);
}
break;
default:
// Default behaviour is, check if it paid first.
$success = Omise_Charge::is_paid( $charge );
// Then, check is authorized after if the first condition is false.
if ( ! $success )
$success = Omise_Charge::is_authorized( $charge );
break;
}
if ( ! $success ) {
return $this->payment_failed( __( 'Note that your payment may have already been processed. Please contact our support team if you have any questions.', 'omise' ) );
}
// Remove cart
WC()->cart->empty_cart();
return array (
'result' => 'success',
'redirect' => $this->get_return_url( $order )
);
}
/**
* Capture an authorized charge.
*
* @param WC_Order $order WooCommerce's order object
*
* @return void
*
* @see WC_Meta_Box_Order_Actions::save( $post_id, $post )
* @see woocommerce/includes/admin/meta-boxes/class-wc-meta-box-order-actions.php
*/
public function process_capture( $order ) {
$this->load_order( $order );
try {
$charge = OmiseCharge::retrieve( $this->get_charge_id_from_order() );
$charge->capture();
if ( ! OmisePluginHelperCharge::isPaid( $charge ) ) {
throw new Exception( $charge['failure_message'] );
}
$this->order()->add_order_note(
sprintf(
wp_kses(
__( 'Omise: Payment successful (manual capture).<br/>An amount of %1$s %2$s has been paid', 'omise' ),
array( 'br' => array() )
),
$this->order()->get_total(),
$this->order()->get_order_currency()
)
);
$this->order()->payment_complete();
} catch ( Exception $e ) {
$this->order()->add_order_note(
sprintf(
wp_kses(
__( 'Omise: Payment failed (manual capture).<br/>%s', 'omise' ),
array( 'br' => array() )
),
$e->getMessage()
)
);
}
}
public function callback() {
if ( ! isset( $_GET['order_id'] ) || ! $order = $this->load_order( $_GET['order_id'] ) ) {
wc_add_notice(
wp_kses(
__( 'We cannot validate your payment result:<br/>Note that your payment might already has been processed. Please contact our support team if you have any questions.', 'omise' ),
array( 'br' => array() )
),
'error'
);
header( 'Location: ' . WC()->cart->get_checkout_url() );
die();
}
$order->add_order_note( __( 'Omise: Validating the payment result..', 'omise' ) );
try {
$charge = OmiseCharge::retrieve( $this->get_charge_id_from_order() );
switch ( $this->payment_action ) {
case self::PAYMENT_ACTION_AUTHORIZE:
$success = Omise_Charge::is_authorized( $charge );
if ( $success ) {
$order->add_order_note(
sprintf(
wp_kses(
__( 'Omise: Payment processing.<br/>An amount %1$s %2$s has been authorized', 'omise' ),
array( 'br' => array() )
),
$order->get_total(),
$order->get_order_currency()
)
);
}
break;
case self::PAYMENT_ACTION_AUTHORIZE_CAPTURE:
$success = Omise_Charge::is_paid( $charge );
if ( $success ) {
$order->add_order_note(
sprintf(
wp_kses(
__( 'Omise: Payment successful.<br/>An amount %1$s %2$s has been paid', 'omise' ),
array( 'br' => array() )
),
$order->get_total(),
$order->get_order_currency()
)
);
$order->payment_complete();
}
break;
default:
// Default behaviour is, check if it paid first.
$success = Omise_Charge::is_paid( $charge );
// Then, check is authorized after if the first condition is false.
if ( ! $success )
$success = Omise_Charge::is_authorized( $charge );
break;
}
if ( ! $success ) {
throw new Exception( Omise_Charge::get_error_message( $charge ) );
}
// Remove cart
WC()->cart->empty_cart();
header( 'Location: ' . $this->get_return_url( $order ) );
die();
} catch ( Exception $e ) {
wc_add_notice(
sprintf(
wp_kses(
__( 'Seems we cannot process your payment properly:<br/>%s', 'omise' ),
array( 'br' => array() )
),
$e->getMessage()
),
'error'
);
$order->add_order_note(
sprintf(
wp_kses(
__( 'Omise: Payment failed.<br/>%s', 'omise' ),
array( 'br' => array() )
),
$e->getMessage()
)
);
header( 'Location: ' . WC()->cart->get_checkout_url() );
die();
}
wp_die( 'Access denied', 'Access Denied', array( 'response' => 401 ) );
die();
}
/**
* Get icons of all supported card types
*
* @see WC_Payment_Gateway::get_icon()
*/
public function get_icon() {
$icon = '';
// TODO: Refactor 'Omise_Card_Image' class that we don't need to pass
// these options to check outside this class.
$card_icons['accept_amex'] = $this->get_option( 'accept_amex' );
$card_icons['accept_diners'] = $this->get_option( 'accept_diners' );
$card_icons['accept_jcb'] = $this->get_option( 'accept_jcb' );
$card_icons['accept_mastercard'] = $this->get_option( 'accept_mastercard' );
$card_icons['accept_visa'] = $this->get_option( 'accept_visa' );
if ( Omise_Card_Image::is_visa_enabled( $card_icons ) ) {
$icon .= Omise_Card_Image::get_visa_image();
}
if ( Omise_Card_Image::is_mastercard_enabled( $card_icons ) ) {
$icon .= Omise_Card_Image::get_mastercard_image();
}
if ( Omise_Card_Image::is_jcb_enabled( $card_icons ) ) {
$icon .= Omise_Card_Image::get_jcb_image();
}
if ( Omise_Card_Image::is_diners_enabled( $card_icons ) ) {
$icon .= Omise_Card_Image::get_diners_image();
}
if ( Omise_Card_Image::is_amex_enabled( $card_icons ) ) {
$icon .= Omise_Card_Image::get_amex_image();
}
return empty( $icon ) ? '' : apply_filters( 'woocommerce_gateway_icon', $icon, $this->id );
}
/**
* Register all required javascripts
*/
public function omise_scripts() {
if ( is_checkout() && $this->is_available() ) {
wp_enqueue_script( 'omise-js', 'https://cdn.omise.co/omise.js', array( 'jquery' ), OMISE_WOOCOMMERCE_PLUGIN_VERSION, true );
wp_enqueue_script( 'omise-payment-form-handler', plugins_url( '../../assets/javascripts/omise-payment-form-handler.js', __FILE__ ), array( 'omise-js' ), OMISE_WOOCOMMERCE_PLUGIN_VERSION, true );
$omise_params = array(
'key' => $this->public_key(),
'required_card_name' => __( 'Cardholder\'s name is a required field', 'omise' ),
'required_card_number' => __( 'Card number is a required field', 'omise' ),
'required_card_expiration_month' => __( 'Card expiry month is a required field', 'omise' ),
'required_card_expiration_year' => __( 'Card expiry year is a required field', 'omise' ),
'required_card_security_code' => __( 'Card security code is a required field', 'omise' ),
'invalid_card' => __( 'Invalid card.', 'omise' ),
'no_card_selected' => __( 'Please select a card or enter a new one.', 'omise' ),
'cannot_create_token' => __( 'Unable to proceed to the payment.', 'omise' ),
'cannot_connect_api' => __( 'Currently, the payment provider server is undergoing maintenance.', 'omise' ),
'retry_checkout' => __( 'Please place your order again in a couple of seconds.', 'omise' ),
'cannot_load_omisejs' => __( 'Cannot connect to the payment provider.', 'omise' ),
'check_internet_connection' => __( 'Please make sure that your internet connection is stable.', 'omise' ),
);
wp_localize_script( 'omise-payment-form-handler', 'omise_params', $omise_params );
}
}
}