-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclass-hpd-linepay-client.php
69 lines (55 loc) · 2.19 KB
/
class-hpd-linepay-client.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
<?php
if ( !defined( 'ABSPATH' ) ) { exit; }
class HPD_LinePay_Client {
public function __construct( $channel_id, $channel_secret, $sandbox_mode = false ) {
$this->channel_id = $channel_id;
$this->channel_secret = $channel_secret;
$this->sandbox_mode = $sandbox_mode;
}
protected function get_api_root_url() {
return $this->sandbox_mode ? HPD_LINEPAY_SANDBOX_API_ROOT : HPD_LINEPAY_API_ROOT;
}
public function reserve( $product_name, $product_image_url, $amount, $currency, $confirm_url, $cancel_url, $order_id, $lang_cd = '', $other_args = array() ) {
$endpoint = '/payments/request';
$postdata = array(
'productName' => $product_name,
'productImageUrl' => $product_image_url,
'amount' => $amount,
'currency' => $currency,
'confirmUrl' => $confirm_url,
'cancelUrl' => $cancel_url,
'orderId' => $order_id,
'langCd' => $lang_cd,
);
return $this->send_request( $endpoint, $postdata );
}
public function confirm( $transaction_id, $amount, $currency ) {
$endpoint = "/payments/$transaction_id/confirm";
$postdata = array(
'amount' => $amount,
'currency' => $currency,
);
return $this->send_request( $endpoint, $postdata );
}
public function refund( $transaction_id, $refund_amount ) {
$endpoint = "/payments/$transaction_id/refund";
$postdata = array(
'refundAmount' => $refund_amount,
);
return $this->send_request( $endpoint, $postdata );
}
protected function send_request( $endpoint, $postdata ) {
$r = wp_remote_post( $this->get_api_root_url() . $endpoint, array(
'headers' => array(
'Content-type' => 'application/json; charset=UTF-8',
'X-LINE-ChannelId' => $this->channel_id,
'X-LINE-ChannelSecret' => $this->channel_secret,
),
'httpversion' => '1.1',
'body' => json_encode( $postdata ),
'timeout' => 30,
) );
$body = wp_remote_retrieve_body( $r );
return json_decode( $body );
}
}