Skip to content

Commit df6a7da

Browse files
committed
purchase
1 parent 7ff80ea commit df6a7da

File tree

11 files changed

+431
-131
lines changed

11 files changed

+431
-131
lines changed

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
"phpseclib/phpseclib": "^3.0"
2020
},
2121
"require-dev": {
22-
"roave/security-advisories": "dev-latest",
2322
"omnipay/tests": "^3.0|^4.0",
2423
"squizlabs/php_codesniffer": "^3"
2524
},

src/Gateway.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44

55
use Omnipay\Common\AbstractGateway;
66
use Omnipay\YiPAY\Message\AuthorizeRequest;
7+
use Omnipay\YiPAY\Message\PurchaseRequest;
8+
use Omnipay\YiPAY\Traits\HasYiPAY;
79

810
/**
911
* YiPAY Gateway
1012
*/
1113
class Gateway extends AbstractGateway
1214
{
15+
use HasYiPAY;
16+
1317
public function getName()
1418
{
1519
return 'YiPAY';
@@ -18,26 +22,15 @@ public function getName()
1822
public function getDefaultParameters()
1923
{
2024
return [
25+
'merchantId' => '',
2126
'key' => '',
27+
'iv' => '',
2228
'testMode' => false,
2329
];
2430
}
2531

26-
public function getKey()
27-
{
28-
return $this->getParameter('key');
29-
}
30-
31-
public function setKey($value)
32-
{
33-
return $this->setParameter('key', $value);
34-
}
35-
36-
/**
37-
* @return Message\AuthorizeRequest
38-
*/
39-
public function authorize(array $options = [])
32+
public function purchase(array $options = [])
4033
{
41-
return $this->createRequest(AuthorizeRequest::class, $options);
34+
return $this->createRequest(PurchaseRequest::class, $options);
4235
}
4336
}

src/Message/AbstractRequest.php

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,12 @@
99
*/
1010
abstract class AbstractRequest extends BaseAbstractRequest
1111
{
12-
protected $liveEndpoint = 'https://api.example.com';
12+
protected $liveEndpoint = 'https://gateway.yipay.com.tw/payment';
1313

14-
protected $testEndpoint = 'https://api-test.example.com';
14+
protected $testEndpoint = 'https://gateway-test.yipay.com.tw/payment';
1515

16-
public function getKey()
17-
{
18-
return $this->getParameter('key');
19-
}
20-
21-
public function setKey($value)
22-
{
23-
return $this->setParameter('key', $value);
24-
}
25-
26-
public function sendData($data)
27-
{
28-
$url = $this->getEndpoint().'?'.http_build_query($data, '', '&');
29-
$response = $this->httpClient->request('GET', $url);
30-
31-
$data = json_decode($response->getBody(), true);
32-
33-
return $this->createResponse($data);
34-
}
35-
36-
protected function getBaseData()
37-
{
38-
return [
39-
'transaction_id' => $this->getTransactionId(),
40-
'expire_date' => $this->getCard()->getExpiryDate('mY'),
41-
'start_date' => $this->getCard()->getStartDate('mY'),
42-
];
43-
}
44-
45-
protected function getEndpoint()
16+
public function getEndpoint()
4617
{
4718
return $this->getTestMode() ? $this->testEndpoint : $this->liveEndpoint;
4819
}
49-
50-
protected function createResponse($data)
51-
{
52-
return $this->response = new Response($this, $data);
53-
}
5420
}

src/Message/AbstractResponse.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Omnipay\YiPAY\Message;
4+
5+
use Omnipay\Common\Message\AbstractResponse as BaseAbstractResponse;
6+
7+
abstract class AbstractResponse extends BaseAbstractResponse
8+
{
9+
}

src/Message/AuthorizeRequest.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/Message/PurchaseRequest.php

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
<?php
2+
3+
namespace Omnipay\YiPAY\Message;
4+
5+
use Omnipay\YiPAY\Hasher;
6+
use Omnipay\YiPAY\Traits\HasYiPAY;
7+
8+
class PurchaseRequest extends AbstractRequest
9+
{
10+
use HasYiPAY;
11+
12+
/**
13+
* 取得付款方式
14+
*
15+
* @return int|null 付款方式
16+
*/
17+
public function getType()
18+
{
19+
return $this->getParameter('type');
20+
}
21+
22+
/**
23+
* 設定付款方式
24+
*
25+
* 1 信用卡付款
26+
* 2 信用卡 3D 付款
27+
* 3 超商代碼繳費
28+
* 4 ATM 虛擬帳號繳款
29+
*
30+
* @param int $value 付款方式 (Int, 長度: 1, 參閱《附錄 C. 付款方式》)
31+
*/
32+
public function setType($value)
33+
{
34+
$this->setParameter('type', $value);
35+
}
36+
37+
/**
38+
* 取得商家訂單編號
39+
*
40+
* @return string|null 商家訂單編號
41+
*/
42+
public function getOrderNo()
43+
{
44+
return $this->getTransactionId();
45+
}
46+
47+
/**
48+
* 設定商家訂單編號
49+
*
50+
* @param string $value 商家自訂的訂單編號 (String, 長度: 20, 唯一值不可重複)
51+
*/
52+
public function setOrderNo($value)
53+
{
54+
$this->setTransactionId($value);
55+
}
56+
57+
/**
58+
* 取得交易內容
59+
*
60+
* @return string|null 交易內容
61+
*/
62+
public function getOrderDescription()
63+
{
64+
return $this->getDescription();
65+
}
66+
67+
/**
68+
* 設定交易內容
69+
*
70+
* @param string $value 消費者購買內容 (String, 長度: 200)
71+
*/
72+
public function setOrderDescription($value)
73+
{
74+
$this->setDescription($value);
75+
}
76+
77+
/**
78+
* 取得交易備註 1
79+
*
80+
* @return string|null 交易備註 1
81+
*/
82+
public function getOrderNote1()
83+
{
84+
return $this->getParameter('orderNote1');
85+
}
86+
87+
/**
88+
* 設定交易備註 1
89+
*
90+
* @param string $value 商家自定義 1 (String, 長度: 200)
91+
*/
92+
public function setOrderNote1($value)
93+
{
94+
$this->setParameter('orderNote1', $value);
95+
}
96+
97+
/**
98+
* 取得交易備註 2
99+
*
100+
* @return string|null 交易備註 2
101+
*/
102+
public function getOrderNote2()
103+
{
104+
return $this->getParameter('orderNote2');
105+
}
106+
107+
/**
108+
* 設定交易備註 2
109+
*
110+
* @param string $value 商家自定義 2 (String, 長度: 200)
111+
*/
112+
public function setOrderNote2($value)
113+
{
114+
$this->setParameter('orderNote2', $value);
115+
}
116+
117+
public function getNotificationEmail()
118+
{
119+
return $this->getParameter('notificationEmail');
120+
}
121+
122+
// 設置與獲取 notificationEmail
123+
124+
/**
125+
* 設定成功交易 Email 通知
126+
*
127+
* 當交易成功時將交易結果寄送至商家指定的 Email,若有多筆 Email 以分號「;」區隔。請 注意此處為通知商家消費者已完成交易用
128+
*
129+
* @param string $value 成功交易 Email (String, 長度: 200)
130+
*/
131+
public function setNotificationEmail($value)
132+
{
133+
$this->setParameter('notificationEmail', $value);
134+
}
135+
136+
/**
137+
* 取得背景回傳網址
138+
*
139+
* @return string|null 背景回傳網址
140+
*/
141+
public function getBackgroundURL()
142+
{
143+
return $this->getNotifyUrl();
144+
}
145+
146+
/**
147+
* 設定背景回傳網址
148+
*
149+
* @param string $value 交易完成後 YiPay 以背景方式 POST 回傳結果的網址
150+
*/
151+
public function setBackgroundURL($value)
152+
{
153+
return $this->setNotifyUrl($value);
154+
}
155+
156+
/**
157+
* 取得交易逾時秒數
158+
*
159+
* @return int|null 交易逾時秒數
160+
*/
161+
public function getTimeout()
162+
{
163+
return $this->getParameter('timeout');
164+
}
165+
166+
/**
167+
* 設定交易逾時秒數
168+
*
169+
* @param int $value 交易逾時秒數,範圍 90 ~ 28800,無限制為 0
170+
*/
171+
public function setTimeout($value)
172+
{
173+
$this->setParameter('timeout', $value);
174+
}
175+
176+
/**
177+
* 取得進入頁面有效時間
178+
*
179+
* @return string|null 交易有效時間
180+
*/
181+
public function getValidTime()
182+
{
183+
return $this->getParameter('validTime');
184+
}
185+
186+
/**
187+
* 設定進入頁面有效時間
188+
*
189+
* @param string $value 交易有效時間 (格式: yyyyMMddHHmm)
190+
*/
191+
public function setValidTime($value)
192+
{
193+
$this->setParameter('validTime', $value);
194+
}
195+
196+
/**
197+
* 取得逾時返回網址
198+
*
199+
* @return string|null 逾時返回網址
200+
*/
201+
public function getTimeoutUrl()
202+
{
203+
return $this->getParameter('timeoutURL');
204+
}
205+
206+
/**
207+
* 設定逾時返回網址
208+
*
209+
* @param string $value 交易逾時時返回商家指定的網址
210+
*/
211+
public function setTimeoutURL($value)
212+
{
213+
$this->setParameter('timeoutURL', $value);
214+
}
215+
216+
public function getData()
217+
{
218+
$this->validate('amount', 'transactionId', 'returnUrl');
219+
220+
return array_filter([
221+
'merchantId' => $this->getMerchantId(),
222+
'type' => $this->getType() ?: 2,
223+
'amount' => (string) ((int) $this->getAmount()),
224+
'orderNo' => $this->getTransactionId(),
225+
'orderDescription' => $this->getDescription(),
226+
'orderNote1' => $this->getOrderNote1(),
227+
'orderNote2' => $this->getOrderNote2(),
228+
'notificationEmail' => $this->getNotificationEmail(),
229+
'returnURL' => $this->getReturnUrl(),
230+
'cancelURL' => $this->getCancelUrl(),
231+
'backgroundURL' => $this->getNotifyUrl(),
232+
'timeout' => $this->getTimeout(),
233+
'validTime' => $this->getValidTime(),
234+
'timeoutURL' => $this->getTimeoutUrl(),
235+
], static function ($value) {
236+
return $value !== null;
237+
});
238+
}
239+
240+
public function sendData($data)
241+
{
242+
$hasher = new Hasher($this->getKey(), $this->getIv());
243+
$data['checkCode'] = $hasher->make([
244+
'merchantId' => $data['merchantId'],
245+
'amount' => $data['amount'],
246+
'orderNo' => $data['orderNo'],
247+
'returnURL' => $data['returnURL'],
248+
'cancelURL' => $data['cancelURL'],
249+
'backgroundURL' => $data['backgroundURL'],
250+
]);
251+
252+
return $this->response = new PurchaseResponse($this, $data);
253+
}
254+
}

0 commit comments

Comments
 (0)