1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PayumTW\Ecpay; |
4
|
|
|
|
5
|
|
|
use ECPay_DeviceType; |
6
|
|
|
use ECPay_InvoiceState; |
7
|
|
|
use PayumTW\Ecpay\Sdk\AllInOne; |
8
|
|
|
use Http\Message\MessageFactory; |
9
|
|
|
use Payum\Core\HttpClientInterface; |
10
|
|
|
|
11
|
|
|
class EcpayApi extends Api |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* $client. |
15
|
|
|
* |
16
|
|
|
* @var \Payum\Core\HttpClientInterface |
17
|
|
|
*/ |
18
|
|
|
protected $client; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* MessageFactory. |
22
|
|
|
* |
23
|
|
|
* @var \Http\Message\MessageFactory |
24
|
|
|
*/ |
25
|
|
|
protected $messageFactory; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* $options. |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $options = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* $sdk. |
36
|
|
|
* |
37
|
|
|
* @var \PayumTW\Ecpay\Sdk\AllInOne |
38
|
|
|
*/ |
39
|
|
|
protected $sdk; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param array $options |
43
|
|
|
* @param \Payum\Core\HttpClientInterface $client |
44
|
|
|
* @param \Http\Message\MessageFactory $messageFactory |
45
|
|
|
* @param \PayumTW\Ecpay\Sdk\AllInOne $sdk |
46
|
|
|
* |
47
|
|
|
* @throws \Payum\Core\Exception\InvalidArgumentException if an option is invalid |
48
|
|
|
*/ |
49
|
5 |
View Code Duplication |
public function __construct(array $options, HttpClientInterface $client, MessageFactory $messageFactory, AllInOne $sdk = null) |
|
|
|
|
50
|
|
|
{ |
51
|
5 |
|
$this->options = $options; |
52
|
5 |
|
$this->client = $client; |
53
|
5 |
|
$this->messageFactory = $messageFactory; |
54
|
5 |
|
$this->sdk = $sdk ?: new AllInOne(); |
55
|
5 |
|
$this->sdk->HashKey = $this->options['HashKey']; |
56
|
5 |
|
$this->sdk->HashIV = $this->options['HashIV']; |
57
|
5 |
|
$this->sdk->MerchantID = $this->options['MerchantID']; |
58
|
5 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* getApiEndpoint. |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
4 |
|
public function getApiEndpoint($name = 'AioCheckOut') |
66
|
|
|
{ |
67
|
|
|
$map = [ |
68
|
4 |
|
'AioCheckOut' => 'https://payment.ecpay.com.tw/Cashier/AioCheckOut/V2 ', |
69
|
4 |
|
'QueryTradeInfo' => 'https://payment.ecpay.com.tw/Cashier/QueryTradeInfo/V2', |
70
|
4 |
|
'QueryPeriodCreditCardTradeInfo' => 'https://payment.ecpay.com.tw/Cashier/QueryCreditCardPeriodInfo', |
71
|
4 |
|
'DoAction' => 'https://payment.ecpay.com.tw/CreditDetail/DoAction', |
72
|
4 |
|
'AioChargeback' => 'https://payment.ecpay.com.tw/Cashier/AioChargeback', |
73
|
4 |
|
]; |
74
|
|
|
|
75
|
4 |
|
if ($this->options['sandbox'] === true) { |
76
|
|
|
$map = [ |
77
|
|
|
'AioCheckOut' => 'https://payment-stage.ecpay.com.tw/Cashier/AioCheckOut/V2', |
78
|
|
|
'QueryTradeInfo' => 'https://payment-stage.ecpay.com.tw/Cashier/QueryTradeInfo/V2', |
79
|
|
|
'QueryPeriodCreditCardTradeInfo' => 'https://payment-stage.ecpay.com.tw/Cashier/QueryCreditCardPeriodInfo', |
80
|
|
|
'DoAction' => null, |
81
|
|
|
'AioChargeback' => 'https://payment-stage.ecpay.com.tw/Cashier/AioChargeback', |
82
|
|
|
]; |
83
|
|
|
} |
84
|
|
|
|
85
|
4 |
|
return $map[$name]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* createTransaction. |
90
|
|
|
* |
91
|
|
|
* @param array $params |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
1 |
|
public function createTransaction(array $params) |
95
|
|
|
{ |
96
|
1 |
|
$this->sdk->ServiceURL = $this->getApiEndpoint('AioCheckOut'); |
97
|
1 |
|
$this->sdk->Send['MerchantTradeDate'] = date('Y/m/d H:i:s'); |
98
|
1 |
|
$this->sdk->Send['DeviceSource'] = $this->isMobile() ? ECPay_DeviceType::Mobile : ECPay_DeviceType::PC; |
99
|
1 |
|
$this->sdk->Send = array_replace( |
|
|
|
|
100
|
1 |
|
$this->sdk->Send, |
101
|
1 |
|
array_intersect_key($params, $this->sdk->Send) |
102
|
1 |
|
); |
103
|
|
|
|
104
|
|
|
/* |
|
|
|
|
105
|
|
|
* 電子發票參數 |
106
|
|
|
* $sdk->Send['InvoiceMark'] = ECPay_InvoiceState::Yes; |
107
|
|
|
* $sdk->SendExtend['RelateNumber'] = $MerchantTradeNo; |
108
|
|
|
* $sdk->SendExtend['CustomerEmail'] = '[email protected]'; |
109
|
|
|
* $sdk->SendExtend['CustomerPhone'] = '0911222333'; |
110
|
|
|
* $sdk->SendExtend['TaxType'] = TaxType::Dutiable; |
111
|
|
|
* $sdk->SendExtend['CustomerAddr'] = '台北市南港區三重路19-2號5樓D棟'; |
112
|
|
|
* $sdk->SendExtend['InvoiceItems'] = array(); |
113
|
|
|
* 將商品加入電子發票商品列表陣列 |
114
|
|
|
* foreach ($sdk->Send['Items'] as $info) { |
115
|
|
|
* array_push($sdk->SendExtend['InvoiceItems'],array('Name' => $info['Name'],'Count' => |
116
|
|
|
* $info['Quantity'],'Word' => '個','Price' => $info['Price'],'TaxType' => TaxType::Dutiable)); |
117
|
|
|
* } |
118
|
|
|
* $sdk->SendExtend['InvoiceRemark'] = '測試發票備註'; |
119
|
|
|
* $sdk->SendExtend['DelayDay'] = '0'; |
120
|
|
|
* $sdk->SendExtend['InvType'] = InvType::General; |
121
|
|
|
*/ |
122
|
|
|
|
123
|
1 |
|
return $this->sdk->formToArray( |
124
|
1 |
|
$this->sdk->CheckOutString() |
125
|
1 |
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* cancelTransaction. |
130
|
|
|
* |
131
|
|
|
* @param array $params |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
1 |
View Code Duplication |
public function cancelTransaction($params) |
|
|
|
|
135
|
|
|
{ |
136
|
1 |
|
$this->sdk->ServiceURL = $this->getApiEndpoint('DoAction'); |
137
|
1 |
|
$this->sdk->Action = array_replace( |
|
|
|
|
138
|
1 |
|
$this->sdk->Action, |
139
|
1 |
|
array_intersect_key($params, $this->sdk->Action) |
140
|
1 |
|
); |
141
|
|
|
|
142
|
1 |
|
return $this->sdk->DoAction(); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* refundTransaction. |
147
|
|
|
* |
148
|
|
|
* @param array $params |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
1 |
View Code Duplication |
public function refundTransaction($params) |
|
|
|
|
152
|
|
|
{ |
153
|
1 |
|
$this->sdk->ServiceURL = $this->getApiEndpoint('AioChargeback'); |
154
|
1 |
|
$this->sdk->ChargeBack = array_replace( |
|
|
|
|
155
|
1 |
|
$this->sdk->ChargeBack, |
156
|
1 |
|
array_intersect_key($params, $this->sdk->ChargeBack) |
157
|
1 |
|
); |
158
|
|
|
|
159
|
1 |
|
return $this->sdk->AioChargeback(); |
|
|
|
|
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* getTransactionData. |
164
|
|
|
* |
165
|
|
|
* @param mixed $params |
166
|
|
|
* @return array |
167
|
|
|
*/ |
168
|
1 |
|
public function getTransactionData($params) |
169
|
|
|
{ |
170
|
1 |
|
$this->sdk->ServiceURL = $this->getApiEndpoint('QueryTradeInfo'); |
171
|
1 |
|
$this->sdk->Query['MerchantTradeNo'] = $params['MerchantTradeNo']; |
172
|
1 |
|
$details = $this->sdk->QueryTradeInfo(); |
173
|
1 |
|
$details['RtnCode'] = $details['TradeStatus'] === '1' ? '1' : '2'; |
174
|
|
|
|
175
|
1 |
|
return $details; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.