-
Notifications
You must be signed in to change notification settings - Fork 25
/
class-omise-payment.php
504 lines (442 loc) · 12 KB
/
class-omise-payment.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
<?php
defined( 'ABSPATH' ) or die( 'No direct script access allowed.' );
if ( ! class_exists( 'WC_Payment_Gateway' ) ) {
return;
}
if ( class_exists( 'Omise_Payment' ) ) {
return;
}
abstract class Omise_Payment extends WC_Payment_Gateway {
/** Omise charge id post meta key. */
const CHARGE_ID = 'omise_charge_id';
/**
* @var string Omise charge statuses
*/
const STATUS_SUCCESSFUL = 'successful';
const STATUS_FAILED = 'failed';
const STATUS_PENDING = 'pending';
/**
* @see woocommerce/includes/abstracts/abstract-wc-settings-api.php
*
* @var string
*/
public $id = 'omise';
/**
* @since 3.4
*
* @var \Omise_Backend
*/
protected $backend;
/**
* @see omise/includes/class-omise-setting.php
*
* @var Omise_Setting
*/
protected $omise_settings;
/**
* Payment setting values.
*
* @var array
*/
public $payment_settings = array();
/**
* A list of countries the payment method can be operated with.
*
* @var array
*/
public $restricted_countries = array();
/**
* @var array
*/
private $currency_subunits = array(
'EUR' => 100,
'GBP' => 100,
'JPY' => 1,
'SGD' => 100,
'THB' => 100,
'USD' => 100
);
/**
* @var Omise_Order|null
*/
protected $order;
public function __construct() {
$this->omise_settings = Omise()->settings();
$this->payment_settings = $this->omise_settings->get_settings();
add_action( 'wp_enqueue_scripts', array( $this, 'omise_checkout_assets' ) );
}
/**
* Register all required javascripts
*/
public function omise_checkout_assets() {
if ( is_checkout() ) {
wp_enqueue_style( 'omise', plugins_url( '../../assets/css/omise-css.css', __FILE__ ), array(), OMISE_WOOCOMMERCE_PLUGIN_VERSION );
do_action( 'omise_checkout_assets' );
}
}
/**
* @param string|WC_Order $order
*
* @return Omise_Order|null
*/
public function load_order( $order ) {
if ( $order instanceof WC_Order ) {
$this->order = $order;
} else {
$this->order = wc_get_order( $order );
}
if ( ! $this->order ) {
$this->order = null;
}
return $this->order;
}
/**
* @return Omise_Order|null
*/
public function order() {
return $this->order;
}
/**
* Whether Sandbox (test) mode is enabled or not.
*
* @return bool
*/
public function is_test() {
return $this->omise_settings->is_test();
}
/**
* Return Omise public key.
*
* @return string
*/
protected function public_key() {
return $this->omise_settings->public_key();
}
/**
* Return Omise secret key.
*
* @return string
*/
protected function secret_key() {
return $this->omise_settings->secret_key();
}
/**
* @param string $currency
*
* @return bool
*/
protected function is_currency_support( $currency ) {
if ( isset( $this->currency_subunits[ strtoupper( $currency ) ] ) ) {
return true;
}
return false;
}
/**
* @param string $country_code
*
* @return bool
*/
public function is_country_support( $country_code ) {
array_map( 'strtoupper', $this->restricted_countries );
if ( in_array( strtoupper( $country_code ), $this->restricted_countries ) ) {
return true;
}
return false;
}
/**
* @since 3.4
*
* @see WC_Payment_Gateway::process_payment( $order_id )
* @see woocommerce/includes/abstracts/abstract-wc-payment-gateway.php
*
* @param int $order_id
*
* @return array
*/
public function process_payment( $order_id ) {
if ( ! $order = $this->load_order( $order_id ) ) {
return $this->payment_failed_no_order( $order_id );
}
$order->add_order_note( sprintf( __( 'Omise: Processing a payment with %s', 'omise' ), $this->method_title ) );
try {
$charge = $this->charge( $order_id, $order );
} catch ( Exception $e ) {
return $this->payment_failed( $e->getMessage() );
}
$order->add_order_note( sprintf( __( 'Omise: Charge (ID: %s) has been created', 'omise' ), $charge['id'] ) );
$this->set_order_transaction_id( $charge['id'] );
return $this->result( $order_id, $order, $charge );
}
/**
* @since 3.4
*
* @see Omise_Payment::process_payment( $order_id )
*
* @param int $order_id
* @param WC_Order $order
*
* @return OmiseCharge|OmiseException
*/
abstract public function charge( $order_id, $order );
/**
* @since 3.4
*
* @see Omise_Payment::process_payment( $order_id )
*
* @param int $order_id
* @param WC_Order $order
* @param OmiseCharge $charge
*
* @return array|Exception
*/
abstract public function result( $order_id, $order, $charge );
/**
* Process refund.
*
* @param int $order_id
* @param float $amount
* @param string $reason
*
* @return boolean True|False based on success, or a WP_Error object.
*
* @see WC_Payment_Gateway::process_refund( $order_id, $amount = null, $reason = '' )
*/
public function process_refund( $order_id, $amount = null, $reason = '' ) {
if ( ! $order = wc_get_order( $order_id ) ) {
$message = __(
'Refund failed. Cannot retrieve an order with the given ID: %s. Please try again or do a manual refund.',
'omise'
);
return new WP_Error( 'error', sprintf( wp_kses( $message, array( 'br' => array() ) ), $order_id ) );
}
try {
$charge = OmiseCharge::retrieve( $order->get_transaction_id() );
$refund = $charge->refunds()->create( array(
'amount' => Omise_Money::to_subunit( $amount, $order->get_order_currency() )
) );
if ( $refund['voided'] ) {
$message = sprintf(
wp_kses(
__( 'Omise: Voided an amount of %1$s %2$s.<br/>Refund id is %3$s', 'omise' ),
array( 'br' => array() )
),
$amount,
$order->get_order_currency(),
$refund['id']
);
} else {
$message = sprintf(
wp_kses(
__( 'Omise: Refunded an amount of %1$s %2$s.<br/>Refund id is %3$s', 'omise' ),
array( 'br' => array() )
),
$amount,
$order->get_order_currency(),
$refund['id']
);
}
$order->add_order_note( $message );
return true;
} catch (Exception $e) {
return new WP_Error( 'error', __( 'Refund failed.' ) . ' ' . $e->getMessage() );
}
}
/**
* Retrieve a charge by a given charge id (that attach to an order).
* Find some diff, then merge it back to WooCommerce system.
*
* @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 sync_payment( $order ) {
$this->load_order( $order );
try {
$charge = OmiseCharge::retrieve( $this->get_charge_id_from_order() );
if ( ! $this->order()->get_transaction_id() ) {
/** backward compatible with WooCommerce v2.x series **/
if ( version_compare( WC()->version, '3.0.0', '>=' ) ) {
$this->order()->set_transaction_id( $charge['id'] );
$this->order()->save();
} else {
update_post_meta( $this->order()->id, '_transaction_id', $charge['id'] );
}
}
if ( 'failed' === $charge['status'] ) {
$this->order()->add_order_note(
sprintf(
wp_kses(
__( 'Omise: Payment failed.<br/>%s (code: %s) (manual sync).', 'omise' ),
array( 'br' => array() )
),
$charge['failure_message'],
$charge['failure_code']
)
);
$this->order()->update_status( 'failed' );
return;
}
if ( 'pending' === $charge['status'] ) {
$this->order()->add_order_note(
wp_kses(
__( 'Omise: Payment is still in progress.<br/>You might wait for a moment before click sync the status again or contact Omise support team at support@omise.co if you have any questions (manual sync).', 'omise' ),
array( 'br' => array() )
)
);
return;
}
if ( 'successful' === $charge['status'] ) {
$this->order()->add_order_note(
sprintf(
wp_kses(
__( 'Omise: Payment successful.<br/>An amount %1$s %2$s has been paid (manual sync).', 'omise' ),
array( 'br' => array() )
),
$this->order()->get_total(),
$this->order()->get_order_currency()
)
);
if ( ! $this->order()->is_paid() ) {
$this->order()->payment_complete();
}
return;
}
throw new Exception(
__( 'Cannot read the payment status. Please try sync again or contact Omise support team at support@omise.co if you have any questions.', 'omise' )
);
} catch ( Exception $e ) {
$order->add_order_note(
sprintf(
wp_kses(
__( 'Omise: Sync failed (manual sync).<br/>%s (manual sync).', 'omise' ),
array( 'br' => array() )
),
$e->getMessage()
)
);
}
}
/**
* Set an order transaction id
*
* @param string $transaction_id Omise charge id.
*/
protected function set_order_transaction_id( $transaction_id ) {
/** backward compatible with WooCommerce v2.x series **/
if ( version_compare( WC()->version, '3.0.0', '>=' ) ) {
$this->order()->set_transaction_id( $transaction_id );
$this->order()->save();
} else {
update_post_meta( $this->order()->id, '_transaction_id', $transaction_id );
}
}
/**
* @param int|mixed $order_id
*/
protected function payment_failed_no_order( $order_id ) {
wc_add_notice(
sprintf(
wp_kses(
__( 'We have been unable to process your payment.<br/>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
),
'error'
);
}
/**
* @param string $message
*/
protected function payment_failed( $message ) {
wc_add_notice(
sprintf(
wp_kses(
__( 'It seems we\'ve been unable to process your payment properly:<br/>%s', 'omise' ),
array( 'br' => array() )
),
$message
),
'error'
);
if ( $this->order() ) {
$this->order()->add_order_note(
sprintf( __( 'Omise: Payment failed, %s', 'omise' ), $message )
);
}
}
/**
* Retrieve an attached charge id.
*
* @deprecated 3.4 We can simply retrieve Omise charge id via WC_Order::get_transaction_id().
* Unfortunately, we may need to leave this code
* as it is for backward compatibility reason.
*
* @return string
*/
public function get_charge_id_from_order() {
if ( $charge_id = $this->order()->get_transaction_id() ) {
return $charge_id;
}
/**
* @deprecated 3.4
* The following code are for backward compatible only.
*/
// Backward compatible for Omise v3.0 - v3.3
$order_id = version_compare( WC()->version, '3.0.0', '>=' ) ? $this->order()->get_id() : $this->order()->id;
$charge_id = get_post_meta( $order_id, self::CHARGE_ID, true );
// Backward compatible for Omise v1.2.3
if ( empty( $charge_id ) ) {
$charge_id = $this->deprecated_get_charge_id_from_post();
}
return $charge_id;
}
/**
* Attach a charge id into an order.
*
* @deprecated 3.4 Now using Omise_Payment::set_order_transaction_id().
* However, keeping this method here just in case
* if this method has been implemented in some other of 3rd-party plugins.
*
* @param string $charge_id Omise charge id.
*/
public function attach_charge_id_to_order( $charge_id ) {
$this->set_order_transaction_id( $charge_id );
}
/**
* Retrieve a charge id from a post.
*
* @deprecated 3.0 No longer assign a new charge id with new post.
*
* @return string
*/
protected function deprecated_get_charge_id_from_post() {
/** backward compatible with WooCommerce v2.x series **/
$order_id = version_compare( WC()->version, '3.0.0', '>=' ) ? $this->order()->get_id() : $this->order()->id;
$posts = get_posts(
array(
'post_type' => 'omise_charge_items',
'meta_query' => array(
array(
'key' => '_wc_order_id',
'value' => $order_id,
'compare' => '='
)
)
)
);
if ( empty( $posts ) ) {
return '';
}
$post = $posts[0];
$value = get_post_custom_values( '_omise_charge_id', $post->ID );
if ( ! is_null( $value ) && ! empty( $value ) ) {
return $value[0];
}
}
}