From 94d5aa47fc9a04eb4c13f2841fbb3b282bc2378e Mon Sep 17 00:00:00 2001 From: recca0120 Date: Sun, 13 Oct 2024 23:53:27 +0800 Subject: [PATCH] get payment info --- src/Message/GetPaymentInfoRequest.php | 28 ++++++++++++- src/Message/GetPaymentInfoResponse.php | 22 ++++++++++- src/Message/PurchaseRequest.php | 3 +- tests/Message/GetPaymentInfoRequestTest.php | 44 +++++++++++++++++++++ tests/Message/PurchaseRequestTest.php | 3 +- 5 files changed, 96 insertions(+), 4 deletions(-) diff --git a/src/Message/GetPaymentInfoRequest.php b/src/Message/GetPaymentInfoRequest.php index e4e5ee8..3968c8e 100644 --- a/src/Message/GetPaymentInfoRequest.php +++ b/src/Message/GetPaymentInfoRequest.php @@ -2,15 +2,41 @@ namespace Omnipay\Gomypay\Message; +use Omnipay\Common\Exception\InvalidRequestException; +use Omnipay\Common\Exception\InvalidResponseException; +use Omnipay\Gomypay\Traits\HasGomypay; + class GetPaymentInfoRequest extends AbstractRequest { + use HasGomypay; + public function getData() { - return $this->httpRequest->request->all(); + return $this->httpRequest->query->all(); } + /** + * @throws InvalidRequestException + * @throws InvalidResponseException + */ public function sendData($data) { + $data = array_merge($data, [ + 'CustomerId' => $this->getCustomerId(), + 'Amount' => (int) $this->getAmount(), + 'Str_Check' => $this->getStrCheck(), + ]); + + $keys = ['result', 'e_orderno', 'CustomerId', 'Amount', 'OrderID', 'Str_Check']; + $plainText = ''; + foreach ($keys as $key) { + $plainText .= $data[$key]; + } + $hash = md5($plainText); + if (! hash_equals($hash, $data['str_check'])) { + throw new InvalidResponseException('Invalid check'); + } + return $this->response = new GetPaymentInfoResponse($this, $data); } } diff --git a/src/Message/GetPaymentInfoResponse.php b/src/Message/GetPaymentInfoResponse.php index 99c3426..b7a6ed5 100644 --- a/src/Message/GetPaymentInfoResponse.php +++ b/src/Message/GetPaymentInfoResponse.php @@ -6,6 +6,26 @@ class GetPaymentInfoResponse extends AbstractResponse { public function isSuccessful() { - // TODO: Implement isSuccessful() method. + return false; + } + + public function getCode() + { + return $this->data['result']; + } + + public function getMessage() + { + return $this->data['ret_msg']; + } + + public function getTransactionId() + { + return $this->data['e_orderno']; + } + + public function getTransactionReference() + { + return $this->data['OrderID']; } } diff --git a/src/Message/PurchaseRequest.php b/src/Message/PurchaseRequest.php index cea4821..b7093b9 100644 --- a/src/Message/PurchaseRequest.php +++ b/src/Message/PurchaseRequest.php @@ -423,6 +423,7 @@ public function getCvsData() { $eReturn = $this->getEReturn(); $strCheck = $eReturn ? $this->getStrCheck() : null; + $returnUrl = $this->getPaymentInfoUrl() ?: $this->getReturnUrl(); return [ 'Send_Type' => PaymentMethod::CVS, @@ -435,7 +436,7 @@ public function getCvsData() 'Buyer_Telm' => $this->getBuyerTelm(), 'Buyer_Mail' => $this->getBuyerMail(), 'Buyer_Memo' => $this->getDescription(), - 'Return_url' => $this->getReturnUrl(), + 'Return_url' => $returnUrl, 'Callback_Url' => $this->getCallbackUrl(), 'e_return' => $eReturn, 'Str_Check' => $strCheck, diff --git a/tests/Message/GetPaymentInfoRequestTest.php b/tests/Message/GetPaymentInfoRequestTest.php index 14d5611..c0770eb 100644 --- a/tests/Message/GetPaymentInfoRequestTest.php +++ b/tests/Message/GetPaymentInfoRequestTest.php @@ -22,4 +22,48 @@ public function setUp(): void 'Str_Check' => '2b1bef9d8ab6a81e9a2739c6ecc64ef8', ]); } + + public function testGetVirtualAccountData() + { + $this->getHttpRequest()->query->replace([ + 'Send_Type' => '4', + 'result' => '1', + 'OrderID' => '2020050700000000001', + 'e_orderno' => '2020050701', + 'e_payaccount' => '013 - 國泰世華 - 0055600701508856', + 'LimitDate' => '2020/07/01', + 'ret_msg' => '取號成功', + 'str_check' => 'bf577c7a76d440a797c1716aff9c01c9', + ]); + $this->request->setAmount('50'); + $response = $this->request->send(); + + $this->assertFalse($response->isSuccessful()); + $this->assertEquals('取號成功', $response->getMessage()); + $this->assertEquals('1', $response->getCode()); + $this->assertEquals('2020050701', $response->getTransactionId()); + $this->assertEquals('2020050700000000001', $response->getTransactionReference()); + } + + public function testGetCvsData() + { + $this->getHttpRequest()->query->replace([ + 'Send_Type' => '6', + 'StoreType' => '3', + 'result' => '1', + 'OrderID' => '2020050700000000001', + 'e_orderno' => '2020050701', + 'PinCode' => 'GMPA2018383076', + 'ret_msg' => '取號成功', + 'str_check' => 'bf577c7a76d440a797c1716aff9c01c9', + ]); + $this->request->setAmount('50'); + $response = $this->request->send(); + + $this->assertFalse($response->isSuccessful()); + $this->assertEquals('取號成功', $response->getMessage()); + $this->assertEquals('1', $response->getCode()); + $this->assertEquals('2020050701', $response->getTransactionId()); + $this->assertEquals('2020050700000000001', $response->getTransactionReference()); + } } diff --git a/tests/Message/PurchaseRequestTest.php b/tests/Message/PurchaseRequestTest.php index 984eb21..16a952b 100644 --- a/tests/Message/PurchaseRequestTest.php +++ b/tests/Message/PurchaseRequestTest.php @@ -214,6 +214,7 @@ public function testGetVirtualAccountData() public function testGetCvsData() { $this->request->setPaymentMethod(PaymentMethod::CVS); + $this->request->setPaymentInfoUrl('https://foo.bar/payment_info_url'); $this->request->setStoreType(3); $response = $this->request->send(); @@ -235,7 +236,7 @@ public function testGetCvsData() 'Buyer_Telm' => '0912345678', 'Buyer_Mail' => 'foo@bar.com', 'Buyer_Memo' => 'good to drink', - 'Return_url' => 'https://foo.bar/return_url', + 'Return_url' => 'https://foo.bar/payment_info_url', 'Callback_Url' => 'https://foo.bar/callback_url', ], $data); }