From fbe02081e91f3610fedb58feb911aed84c038b84 Mon Sep 17 00:00:00 2001 From: recca0120 Date: Sat, 11 Nov 2023 17:35:34 +0800 Subject: [PATCH] get data from request --- src/Gateway.php | 6 +- src/Message/AcceptNotificationRequest.php | 10 ++ src/Message/CompletePurchaseRequest.php | 37 +------ src/Message/ReceiveTransactionInfoRequest.php | 98 +------------------ tests/GatewayTest.php | 30 +++--- .../Message/AcceptNotificationRequestTest.php | 15 ++- tests/Message/CompletePurchaseRequestTest.php | 6 +- .../ReceiveTransactionInfoRequestTest.php | 6 +- 8 files changed, 52 insertions(+), 156 deletions(-) diff --git a/src/Gateway.php b/src/Gateway.php index e67045d..e33b99e 100644 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -38,7 +38,8 @@ public function purchase(array $options = []) public function completePurchase(array $options = []) { - if (array_key_exists('RtnCode', $options) && (string) $options['RtnCode'] === '5') { + $data = $this->httpRequest->request->all(); + if (array_key_exists('RtnCode', $data) && (string) $data['RtnCode'] === '5') { return $this->receiveTransactionInfo($options); } @@ -47,7 +48,8 @@ public function completePurchase(array $options = []) public function acceptNotification(array $options = []) { - if (array_key_exists('RtnCode', $options) && (string) $options['RtnCode'] === '5') { + $data = $this->httpRequest->request->all(); + if (array_key_exists('RtnCode', $data) && (string) $data['RtnCode'] === '5') { return $this->receiveTransactionInfo($options); } diff --git a/src/Message/AcceptNotificationRequest.php b/src/Message/AcceptNotificationRequest.php index 221e612..5801142 100644 --- a/src/Message/AcceptNotificationRequest.php +++ b/src/Message/AcceptNotificationRequest.php @@ -15,6 +15,16 @@ public function sendData($data) return $this->response = new AcceptNotificationResponse($this, $data); } + public function getTransactionId() + { + return $this->getNotificationResponse()->getTransactionId(); + } + + public function getTransactionReference() + { + return $this->getNotificationResponse()->getTransactionId(); + } + /** * @return string */ diff --git a/src/Message/CompletePurchaseRequest.php b/src/Message/CompletePurchaseRequest.php index d3c6508..72ab167 100644 --- a/src/Message/CompletePurchaseRequest.php +++ b/src/Message/CompletePurchaseRequest.php @@ -4,23 +4,11 @@ use Omnipay\Common\Exception\InvalidRequestException; use Omnipay\Common\Message\AbstractRequest; -use Omnipay\MyCash\Traits\HasAmount; -use Omnipay\MyCash\Traits\HasDefaults; use Omnipay\MyCash\Traits\HasMyCash; -use Omnipay\MyCash\Traits\HasRtn; -use Omnipay\MyCash\Traits\HasRtnATM; -use Omnipay\MyCash\Traits\HasRtnCreditCard; -use Omnipay\MyCash\Traits\HasRtnCVS; class CompletePurchaseRequest extends AbstractRequest { use HasMyCash; - use HasDefaults; - use HasRtn; - use HasRtnCreditCard; - use HasRtnATM; - use HasRtnCVS; - use HasAmount; /** * @return array @@ -29,31 +17,14 @@ class CompletePurchaseRequest extends AbstractRequest */ public function getData() { - $this->validate('transactionId', 'MerProductID', 'MerUserID', 'amount', 'Validate'); + $data = $this->httpRequest->request->all(); + $validate = $data['Validate']; - $data = [ - 'RtnCode' => $this->getRtnCode(), - 'RtnMessage' => $this->getRtnMessage(), - 'MerTradeID' => $this->getTransactionId(), - 'MerProductID' => $this->getMerProductID(), - 'MerUserID' => $this->getMerUserID(), - 'Amount' => $this->getAmount(), - 'Auth_code' => $this->getAuthCode(), - 'CardNumber' => $this->getCardNumber(), - 'PaymentDate' => $this->getPaymentDate(), - 'ATMNo' => $this->getATMNo(), - 'StoreName' => $this->getStoreName(), - 'StoreID' => $this->getStoreID(), - 'Validate' => $this->getValidate(), - ]; - - if ($this->makeHash($data) !== $this->getValidate()) { + if ($this->makeHash($data) !== $validate) { throw new InvalidRequestException('Incorrect hash'); } - return array_filter($data, static function ($value) { - return ! empty($value); - }); + return $data; } /** diff --git a/src/Message/ReceiveTransactionInfoRequest.php b/src/Message/ReceiveTransactionInfoRequest.php index 2bdf699..d1faf58 100644 --- a/src/Message/ReceiveTransactionInfoRequest.php +++ b/src/Message/ReceiveTransactionInfoRequest.php @@ -4,85 +4,11 @@ use Omnipay\Common\Exception\InvalidRequestException; use Omnipay\Common\Message\AbstractRequest; -use Omnipay\MyCash\Traits\HasAmount; -use Omnipay\MyCash\Traits\HasDefaults; use Omnipay\MyCash\Traits\HasMyCash; -use Omnipay\MyCash\Traits\HasRtn; class ReceiveTransactionInfoRequest extends AbstractRequest { use HasMyCash; - use HasDefaults; - use HasRtn; - use HasAmount; - - /** - * @return string - */ - public function getExpireTime() - { - return $this->getParameter('ExpireTime'); - } - - /** - * @param string $value - * @return ReceiveTransactionInfoRequest - */ - public function setExpireTime($value) - { - return $this->setParameter('ExpireTime', $value); - } - - /** - * @return string - */ - public function getVatmBankCode() - { - return $this->getParameter('VatmBankCode'); - } - - /** - * @param string $value - * @return ReceiveTransactionInfoRequest - */ - public function setVatmBankCode($value) - { - return $this->setParameter('VatmBankCode', $value); - } - - /** - * @return string - */ - public function getVatmAccount() - { - return $this->getParameter('VatmAccount'); - } - - /** - * @param string $value - * @return ReceiveTransactionInfoRequest - */ - public function setVatmAccount($value) - { - return $this->setParameter('VatmAccount', $value); - } - - /** - * @return string - */ - public function getCodeNo() - { - return $this->getParameter('CodeNo'); - } - - /** - * @param string $value - * @return ReceiveTransactionInfoRequest - */ - public function setCodeNo($value) - { - return $this->setParameter('CodeNo', $value); - } /** * @return array @@ -91,30 +17,14 @@ public function setCodeNo($value) */ public function getData() { - $this->validate('transactionId', 'MerProductID', 'MerUserID', 'amount', 'Validate'); - - $data = [ - 'RtnCode' => $this->getRtnCode(), - 'RtnMessage' => $this->getRtnMessage(), - 'MerTradeID' => $this->getTransactionId(), - 'MerProductID' => $this->getMerProductID(), - 'MerUserID' => $this->getMerUserID(), - 'Amount' => $this->getAmount(), - 'PaymentDate' => $this->getPaymentDate(), - 'ExpireTime' => $this->getExpireTime(), - 'VatmBankCode' => $this->getVatmBankCode(), - 'VatmAccount' => $this->getVatmAccount(), - 'CodeNo' => $this->getCodeNo(), - 'Validate' => $this->getValidate(), - ]; + $data = $this->httpRequest->request->all(); + $validate = $data['Validate']; - if ($this->makeHash($data) !== $this->getValidate()) { + if ($this->makeHash($data) !== $validate) { throw new InvalidRequestException('validate fails'); } - return array_filter($data, static function ($value) { - return ! empty($value); - }); + return $data; } /** diff --git a/tests/GatewayTest.php b/tests/GatewayTest.php index 012dfda..72d4341 100644 --- a/tests/GatewayTest.php +++ b/tests/GatewayTest.php @@ -56,7 +56,7 @@ public function testPurchase(): void public function testCompletePurchase(): void { - $response = $this->gateway->completePurchase([ + $this->getHttpRequest()->request->add([ 'RtnCode' => '1', 'RtnMessage' => '成功', 'MerTradeID' => '20151202001', @@ -67,7 +67,8 @@ public function testCompletePurchase(): void 'CardNumber' => '4311-2222-2222-2222', 'PaymentDate' => '2016-05-06 16:41:37', 'Validate' => 'e6d2412d68c714f9e6c1185d9e6698ba', - ])->send(); + ]); + $response = $this->gateway->completePurchase()->send(); self::assertTrue($response->isSuccessful()); self::assertEquals('成功', $response->getMessage()); @@ -88,7 +89,7 @@ public function testCompletePurchase(): void public function testAcceptNotification(): void { - $request = $this->gateway->acceptNotification([ + $data = [ 'RtnCode' => '1', 'RtnMessage' => '成功', 'MerTradeID' => '20151202001', @@ -99,28 +100,20 @@ public function testAcceptNotification(): void 'CardNumber' => '4311-2222-2222-2222', 'PaymentDate' => '2016-05-06 16:41:37', 'Validate' => 'e6d2412d68c714f9e6c1185d9e6698ba', - ]); + ]; + + $this->getHttpRequest()->request->add($data); + $request = $this->gateway->acceptNotification([]); self::assertEquals('成功', $request->getMessage()); self::assertEquals('20151202001', $request->getTransactionId()); self::assertEquals(NotificationInterface::STATUS_COMPLETED, $request->getTransactionStatus()); - self::assertEquals([ - 'RtnCode' => '1', - 'RtnMessage' => '成功', - 'MerTradeID' => '20151202001', - 'MerProductID' => 'sj6511', - 'MerUserID' => 'Karl01', - 'Amount' => '30', - 'Auth_code' => '12345', - 'CardNumber' => '4311-2222-2222-2222', - 'PaymentDate' => '2016-05-06 16:41:37', - 'Validate' => 'e6d2412d68c714f9e6c1185d9e6698ba', - ], $request->getData()); + self::assertEquals($data, $request->getData()); } public function testReceiveTransactionInfo(): void { - $response = $this->gateway->completePurchase([ + $this->getHttpRequest()->request->add([ 'RtnCode' => '5', 'RtnMessage' => '成功', 'MerTradeID' => '20151202001', @@ -132,7 +125,8 @@ public function testReceiveTransactionInfo(): void 'VatmBankCode' => '123', 'VatmAccount' => '12345678', 'Validate' => '55aab2d7bec68a3a05183e3764ad4e3a', - ])->send(); + ]); + $response = $this->gateway->completePurchase()->send(); self::assertTrue($response->isSuccessful()); self::assertEquals('成功', $response->getMessage()); diff --git a/tests/Message/AcceptNotificationRequestTest.php b/tests/Message/AcceptNotificationRequestTest.php index a6341c6..726984a 100644 --- a/tests/Message/AcceptNotificationRequestTest.php +++ b/tests/Message/AcceptNotificationRequestTest.php @@ -33,8 +33,9 @@ public function testValidateFails(): void 'Validate' => 'e6d2412d68c714f9e6c1185d9e6698ba1', ]; + $this->getHttpRequest()->request->add($options); $request = new CompletePurchaseRequest($this->getHttpClient(), $this->getHttpRequest()); - $request->initialize(array_merge($this->initialize, $options)); + $request->initialize($this->initialize); $request->send(); } @@ -54,8 +55,9 @@ public function testCreditCardAcceptNotificationRequest(): void 'Validate' => 'e6d2412d68c714f9e6c1185d9e6698ba', ]; + $this->getHttpRequest()->request->add($options); $request = new AcceptNotificationRequest($this->getHttpClient(), $this->getHttpRequest()); - $request->initialize(array_merge($this->initialize, $options)); + $request->initialize($this->initialize); self::assertEquals('成功', $request->getMessage()); self::assertEquals('20151202001', $request->getTransactionId()); @@ -88,8 +90,9 @@ public function testATMAcceptNotificationRequest(): void 'Validate' => 'e6d2412d68c714f9e6c1185d9e6698ba', ]; + $this->getHttpRequest()->request->add($options); $request = new AcceptNotificationRequest($this->getHttpClient(), $this->getHttpRequest()); - $request->initialize(array_merge($this->initialize, $options)); + $request->initialize($this->initialize); self::assertEquals('成功', $request->getMessage()); self::assertEquals('20151202001', $request->getTransactionId()); @@ -122,8 +125,9 @@ public function testCVSAcceptNotificationRequest(): void 'Validate' => 'e6d2412d68c714f9e6c1185d9e6698ba', ]; + $this->getHttpRequest()->request->add($options); $request = new AcceptNotificationRequest($this->getHttpClient(), $this->getHttpRequest()); - $request->initialize(array_merge($this->initialize, $options)); + $request->initialize($this->initialize); self::assertEquals('成功', $request->getMessage()); self::assertEquals('20151202001', $request->getTransactionId()); @@ -155,8 +159,9 @@ public function testFunPointAcceptNotificationRequest(): void 'Validate' => 'e6d2412d68c714f9e6c1185d9e6698ba', ]; + $this->getHttpRequest()->request->add($options); $request = new AcceptNotificationRequest($this->getHttpClient(), $this->getHttpRequest()); - $request->initialize(array_merge($this->initialize, $options)); + $request->initialize($this->initialize); self::assertEquals('成功', $request->getMessage()); self::assertEquals('20151202001', $request->getTransactionId()); diff --git a/tests/Message/CompletePurchaseRequestTest.php b/tests/Message/CompletePurchaseRequestTest.php index 5e3f15d..92b6b52 100644 --- a/tests/Message/CompletePurchaseRequestTest.php +++ b/tests/Message/CompletePurchaseRequestTest.php @@ -32,8 +32,9 @@ public function testValidateFails(): void 'Validate' => 'e6d2412d68c714f9e6c1185d9e6698ba1', ]; + $this->getHttpRequest()->request->add($options); $request = new CompletePurchaseRequest($this->getHttpClient(), $this->getHttpRequest()); - $request->initialize(array_merge($this->initialize, $options)); + $request->initialize($this->initialize); $request->send(); } @@ -52,8 +53,9 @@ public function testValidateSuccessful(): void 'Validate' => '216f3a2f5b3665b0c5c77860d3c18692', ]; + $this->getHttpRequest()->request->add($options); $request = new CompletePurchaseRequest($this->getHttpClient(), $this->getHttpRequest()); - $request->initialize(array_merge($this->initialize, $options)); + $request->initialize($this->initialize); $response = $request->send(); self::assertTrue($response->isSuccessful()); diff --git a/tests/Message/ReceiveTransactionInfoRequestTest.php b/tests/Message/ReceiveTransactionInfoRequestTest.php index 7205707..3cf68a2 100644 --- a/tests/Message/ReceiveTransactionInfoRequestTest.php +++ b/tests/Message/ReceiveTransactionInfoRequestTest.php @@ -29,8 +29,9 @@ public function testATMReceiveTransactionInfoRequest(): void 'Validate' => '55aab2d7bec68a3a05183e3764ad4e3a', ]; + $this->getHttpRequest()->request->add($options); $request = new ReceiveTransactionInfoRequest($this->getHttpClient(), $this->getHttpRequest()); - $request->initialize(array_merge($this->initialize, $options)); + $request->initialize($this->initialize); $response = $request->send(); @@ -64,8 +65,9 @@ public function testCVSReceiveTransactionInfoRequest(): void 'Validate' => '55aab2d7bec68a3a05183e3764ad4e3a', ]; + $this->getHttpRequest()->request->add($options); $request = new ReceiveTransactionInfoRequest($this->getHttpClient(), $this->getHttpRequest()); - $request->initialize(array_merge($this->initialize, $options)); + $request->initialize($this->initialize); $response = $request->send();