Skip to content

Commit 405937c

Browse files
committed
CompletePurchase
1 parent df6a7da commit 405937c

File tree

8 files changed

+134
-67
lines changed

8 files changed

+134
-67
lines changed

src/Gateway.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Omnipay\Common\AbstractGateway;
66
use Omnipay\YiPAY\Message\AuthorizeRequest;
7+
use Omnipay\YiPAY\Message\CompletePurchaseRequest;
78
use Omnipay\YiPAY\Message\PurchaseRequest;
89
use Omnipay\YiPAY\Traits\HasYiPAY;
910

@@ -33,4 +34,9 @@ public function purchase(array $options = [])
3334
{
3435
return $this->createRequest(PurchaseRequest::class, $options);
3536
}
37+
38+
public function completePurchase(array $options = [])
39+
{
40+
return $this->createRequest(CompletePurchaseRequest::class, $options);
41+
}
3642
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Omnipay\YiPAY\Message;
4+
5+
use Omnipay\Common\Exception\InvalidResponseException;
6+
use Omnipay\YiPAY\Traits\HasYiPAY;
7+
8+
class CompletePurchaseRequest extends PurchaseRequest
9+
{
10+
use HasYiPAY;
11+
12+
public function getData()
13+
{
14+
return $this->httpRequest->request->all();
15+
}
16+
17+
/**
18+
* @throws InvalidResponseException
19+
*/
20+
public function sendData($data)
21+
{
22+
$checkCode = $this->checkCode([
23+
'merchantId',
24+
'amount',
25+
'orderNo',
26+
'returnURL',
27+
'cancelURL',
28+
'backgroundURL',
29+
'transactionNo',
30+
'statusCode',
31+
'approvalCode',
32+
], $data);
33+
34+
if (! hash_equals($checkCode, $data['checkCode'])) {
35+
throw new InvalidResponseException('Invalid check code');
36+
}
37+
38+
return $this->response = new CompletePurchaseResponse($this, $data);
39+
}
40+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Omnipay\YiPAY\Message;
4+
5+
class CompletePurchaseResponse extends AbstractResponse
6+
{
7+
public function isSuccessful()
8+
{
9+
return $this->getCode() === '00';
10+
}
11+
12+
public function getCode()
13+
{
14+
return $this->data['statusCode'];
15+
}
16+
17+
public function getMessage()
18+
{
19+
return $this->data['statusMessage'];
20+
}
21+
22+
public function getTransactionId()
23+
{
24+
return $this->data['orderNo'];
25+
}
26+
27+
public function getTransactionReference()
28+
{
29+
return $this->data['transactionNo'];
30+
}
31+
}

src/Message/PurchaseRequest.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Omnipay\YiPAY\Message;
44

5-
use Omnipay\YiPAY\Hasher;
65
use Omnipay\YiPAY\Traits\HasYiPAY;
76

87
class PurchaseRequest extends AbstractRequest
@@ -239,15 +238,9 @@ public function getData()
239238

240239
public function sendData($data)
241240
{
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-
]);
241+
$data['checkCode'] = $this->checkCode([
242+
'merchantId', 'amount', 'orderNo', 'returnURL', 'cancelURL', 'backgroundURL',
243+
], $data);
251244

252245
return $this->response = new PurchaseResponse($this, $data);
253246
}

src/Message/Response.php

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

src/Traits/HasYiPAY.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Omnipay\YiPAY\Traits;
44

5+
use Omnipay\YiPAY\Hasher;
6+
57
trait HasYiPAY
68
{
79
/**
@@ -44,4 +46,13 @@ public function getIv()
4446
return $this->getParameter('iv');
4547
}
4648

49+
public function checkCode($keys, $data)
50+
{
51+
$signed = [];
52+
foreach ($keys as $key) {
53+
$signed[$key] = $data[$key];
54+
}
55+
56+
return (new Hasher($this->getKey(), $this->getIv()))->make($signed);
57+
}
4758
}

tests/GatewayTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,47 @@ public function testPurchase()
3737
self::assertTrue($response->isRedirect());
3838
self::assertEquals('POST', $response->getRedirectMethod());
3939
}
40+
41+
public function testCompletePurchase()
42+
{
43+
// 1 merchantId 商家編號 Int
44+
// 2 type 付款方式 Int 參閱《附錄 C. 付款方式》
45+
// 3 amount 交易金額 Int
46+
// 4 orderNo 商家訂單編號 String 商家傳送至乙禾的自訂訂單編號
47+
// 5 transactionNo 交易編號 String 乙禾系統的交易編號
48+
// 6 statusCode 交易回覆代碼 String 00 代表成功,其餘皆為失敗
49+
// 7 statusMessage 交易回覆訊息 String 授權代碼說明
50+
// 8 approvalCode 授權碼 String
51+
// 9 last4CardNumber 卡號末四碼 Int 信用卡號末四碼
52+
// 10 checkCode 檢查碼 String 參閱下方檢查碼組成參數
53+
// 序號 名稱 描述 範例
54+
// 1 merchantId 商家編號 1604000006
55+
// 2 amount 交易金額 1500
56+
// 3 orderNo 商家訂單編號 YP2016111503353
57+
// 8 乙禾網絡有限公司
58+
59+
$this->getHttpRequest()->request->add([
60+
'merchantId' => '1604000006',
61+
'type' => '2',
62+
'amount' => '1500',
63+
'orderNo' => 'YP2016111503353',
64+
'transactionNo' => 'C0216111500000000001',
65+
'statusCode' => '00',
66+
'statusMessage' => '成功',
67+
'approvalCode' => '629540',
68+
'last4CardNumber' => '2222',
69+
'returnURL' => 'https://gateway-test.yipay.com.tw/demo/return',
70+
'cancelURL' => 'https://gateway-test.yipay.com.tw/demo/cancel',
71+
'backgroundURL' => 'https://gateway-test.yipay.com.tw/demo/background',
72+
'checkCode' => '5a39b9c01f1f6dec16c692034081759b4d37c9aa',
73+
]);
74+
75+
$response = $this->gateway->completePurchase()->send();
76+
77+
self::assertTrue($response->isSuccessful());
78+
self::assertEquals('00', $response->getCode());
79+
self::assertEquals('成功', $response->getMessage());
80+
self::assertEquals('YP2016111503353', $response->getTransactionId());
81+
self::assertEquals('C0216111500000000001', $response->getTransactionReference());
82+
}
4083
}

tests/Message/ResponseTest.php

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

0 commit comments

Comments
 (0)