diff --git a/src/Message/CompletePurchaseRequest.php b/src/Message/CompletePurchaseRequest.php index 8850e93..e058770 100644 --- a/src/Message/CompletePurchaseRequest.php +++ b/src/Message/CompletePurchaseRequest.php @@ -4,222 +4,28 @@ use Ecpay\Sdk\Response\VerifiedArrayResponse; use Exception; -use Omnipay\Common\Exception\InvalidRequestException; use Omnipay\Common\Exception\InvalidResponseException; use Omnipay\Common\Message\AbstractRequest; -use Omnipay\ECPay\Traits\HasCustomFields; use Omnipay\ECPay\Traits\HasDefaults; use Omnipay\ECPay\Traits\HasECPay; -use Omnipay\ECPay\Traits\HasMerchantTradeNo; -use Omnipay\ECPay\Traits\HasStoreID; class CompletePurchaseRequest extends AbstractRequest { use HasECPay; use HasDefaults; - use HasMerchantTradeNo; - use HasStoreID; - use HasCustomFields; - - /** - * @return string - */ - public function getPaymentDate() - { - return $this->getParameter('PaymentDate'); - } - - /** - * @param string $value - * @return $this - */ - public function setPaymentDate($value) - { - return $this->setParameter('PaymentDate', $value); - } - - /** - * @return string - */ - public function getPaymentType() - { - return $this->getParameter('PaymentType'); - } - - /** - * @param string $value - * @return $this - */ - public function setPaymentType($value) - { - return $this->setParameter('PaymentType', $value); - } - - /** - * @return string - */ - public function getPaymentTypeChargeFee() - { - return $this->getParameter('PaymentTypeChargeFee'); - } - - /** - * @param string $value - * @return $this - */ - public function setPaymentTypeChargeFee($value) - { - return $this->setParameter('PaymentTypeChargeFee', $value); - } - - /** - * @return string - */ - public function getRtnCode() - { - return $this->getParameter('RtnCode'); - } - - /** - * @param string $value - * @return $this - */ - public function setRtnCode($value) - { - return $this->setParameter('RtnCode', $value); - } - - /** - * @return string - */ - public function getRtnMsg() - { - return $this->getParameter('RtnMsg'); - } - - /** - * @param string $value - * @return $this - */ - public function setRtnMsg($value) - { - return $this->setParameter('RtnMsg', $value); - } - - /** - * @return string - */ - public function getSimulatePaid() - { - return $this->getParameter('SimulatePaid'); - } - - /** - * @param string $value - * @return $this - */ - public function setSimulatePaid($value) - { - return $this->setParameter('SimulatePaid', $value); - } - - /** - * @return string - */ - public function getTradeAmt() - { - return $this->getParameter('TradeAmt'); - } - - /** - * @param string $value - * @return $this - */ - public function setTradeAmt($value) - { - return $this->setParameter('TradeAmt', $value); - } - - /** - * @return string - */ - public function getTradeDate() - { - return $this->getParameter('TradeDate'); - } - - /** - * @param string $value - * @return $this - */ - public function setTradeDate($value) - { - return $this->setParameter('TradeDate', $value); - } - - /** - * @return string - */ - public function getTradeNo() - { - return $this->getTransactionReference(); - } - - /** - * @param string $value - * @return $this - */ - public function setTradeNo($value) - { - return $this->setTransactionReference($value); - } - - /** - * @return string - */ - public function getCheckMacValue() - { - return $this->getParameter('CheckMacValue'); - } - - /** - * @param string $value - * @return $this - */ - public function setCheckMacValue($value) - { - return $this->setParameter('CheckMacValue', $value); - } /** * @return array * - * @throws InvalidRequestException * @throws InvalidResponseException */ public function getData() { - $this->validate('MerchantID', 'CheckMacValue'); + $data = $this->httpRequest->request->all(); + $this->setTransactionId($data['MerchantTradeNo']); + $this->setTransactionReference($data['TradeNo']); - return $this->checkMacValue([ - 'CustomField1' => $this->getCustomField1(), - 'CustomField2' => $this->getCustomField2(), - 'CustomField3' => $this->getCustomField3(), - 'CustomField4' => $this->getCustomField4(), - 'MerchantID' => $this->getMerchantID(), - 'MerchantTradeNo' => $this->getMerchantTradeNo(), - 'PaymentDate' => $this->getPaymentDate(), - 'PaymentType' => $this->getPaymentType(), - 'PaymentTypeChargeFee' => $this->getPaymentTypeChargeFee(), - 'RtnCode' => $this->getRtnCode(), - 'RtnMsg' => $this->getRtnMsg(), - 'SimulatePaid' => $this->getSimulatePaid(), - 'StoreID' => $this->getStoreID(), - 'TradeAmt' => $this->getTradeAmt(), - 'TradeDate' => $this->getTradeDate(), - 'TradeNo' => $this->getTransactionReference(), - 'CheckMacValue' => $this->getCheckMacValue(), - ]); + return $this->checkMacValue($data); } /** diff --git a/tests/GatewayTest.php b/tests/GatewayTest.php index bbf5957..94178ad 100644 --- a/tests/GatewayTest.php +++ b/tests/GatewayTest.php @@ -20,21 +20,20 @@ public function setUp(): void { parent::setUp(); + $this->options = ['testMode' => true]; $this->gateway = new StubGateway($this->getHttpClient(), $this->getHttpRequest()); + $this->gateway->initialize($this->options); + } - $this->options = [ + public function testPurchase() + { + $response = $this->gateway->purchase([ 'transactionId' => uniqid('MerchantTradeNo', true), 'amount' => 2000, 'description' => 'description', 'returnUrl' => 'https://foo.bar/return_url', 'notifyUrl' => 'https://foo.bar/notify_url', - 'testMode' => true, - ]; - } - - public function testPurchase() - { - $response = $this->gateway->purchase(array_merge($this->options, []))->send(); + ])->send(); self::assertFalse($response->isSuccessful()); self::assertTrue($response->isRedirect()); @@ -44,7 +43,7 @@ public function testPurchase() public function testCompletePurchase() { - $response = $this->gateway->completePurchase(array_merge($this->options, [ + $this->getHttpRequest()->request->add([ 'CustomField1' => '', 'CustomField2' => '', 'CustomField3' => '', @@ -62,7 +61,8 @@ public function testCompletePurchase() 'TradeDate' => '2019/09/02 15:49:16', 'TradeNo' => '1909021549160081', 'CheckMacValue' => 'E7EC8DDC6C5C51B1A4D8BEA261246066858B38184C55FD3DD3D6DFF53F535A64', - ]))->send(); + ]); + $response = $this->gateway->completePurchase()->send(); self::assertTrue($response->isSuccessful()); self::assertEquals('Succeeded', $response->getMessage()); @@ -70,7 +70,7 @@ public function testCompletePurchase() public function testAcceptNotification() { - $response = $this->gateway->acceptNotification(array_merge($this->options, [ + $this->getHttpRequest()->request->add([ 'CustomField1' => '', 'CustomField2' => '', 'CustomField3' => '', @@ -88,7 +88,8 @@ public function testAcceptNotification() 'TradeDate' => '2019/09/02 15:49:16', 'TradeNo' => '1909021549160081', 'CheckMacValue' => 'E7EC8DDC6C5C51B1A4D8BEA261246066858B38184C55FD3DD3D6DFF53F535A64', - ]))->send(); + ]); + $response = $this->gateway->acceptNotification()->send(); self::assertTrue($response->isSuccessful()); self::assertEquals('Succeeded', $response->getMessage()); diff --git a/tests/Message/AcceptNotificationRequestTest.php b/tests/Message/AcceptNotificationRequestTest.php index 75c382e..05d26d7 100644 --- a/tests/Message/AcceptNotificationRequestTest.php +++ b/tests/Message/AcceptNotificationRequestTest.php @@ -2,7 +2,6 @@ namespace Omnipay\ECPay\Tests\Message; -use Omnipay\Common\Exception\InvalidRequestException; use Omnipay\Common\Exception\InvalidResponseException; use Omnipay\Common\Message\NotificationInterface; use Omnipay\ECPay\Message\AcceptNotificationRequest; @@ -11,12 +10,11 @@ class AcceptNotificationRequestTest extends TestCase { /** - * @throws InvalidRequestException * @throws InvalidResponseException */ public function testGetData() { - $options = [ + $data = [ 'CustomField1' => '', 'CustomField2' => '', 'CustomField3' => '', @@ -36,18 +34,19 @@ public function testGetData() 'CheckMacValue' => 'E7EC8DDC6C5C51B1A4D8BEA261246066858B38184C55FD3DD3D6DFF53F535A64', ]; + $this->getHttpRequest()->request->add($data); $request = new AcceptNotificationRequest($this->getHttpClient(), $this->getHttpRequest()); - $request->initialize(array_merge([ + $request->initialize([ 'HashKey' => '5294y06JbISpM5x9', 'HashIV' => 'v77hoKGq4kWxNNIS', 'EncryptType' => '1', 'MerchantID' => '2000132', - ], $options)); + ]); $request->setTestMode(true); - self::assertEquals($options, $request->getData()); + self::assertEquals($data, $request->getData()); - return [$request, $options]; + return [$request, $data]; } /** @@ -56,10 +55,10 @@ public function testGetData() public function testSendData($results) { $notification = $results[0]; - $options = $results[1]; + $data = $results[1]; - self::assertEquals($options['MerchantTradeNo'], $notification->getTransactionId()); - self::assertEquals($options['TradeNo'], $notification->getTransactionReference()); + self::assertEquals($data['MerchantTradeNo'], $notification->getTransactionId()); + self::assertEquals($data['TradeNo'], $notification->getTransactionReference()); self::assertEquals(NotificationInterface::STATUS_COMPLETED, $notification->getTransactionStatus()); self::assertEquals('Succeeded', $notification->getMessage()); self::assertEquals('1|OK', $notification->getReply()); @@ -70,7 +69,7 @@ public function testInvalidCheckMacValue() $this->expectException(InvalidResponseException::class); $this->expectExceptionMessage('CheckMacValue verify fail'); - $options = [ + $data = [ 'CustomField1' => '', 'CustomField2' => '', 'CustomField3' => '', @@ -90,13 +89,14 @@ public function testInvalidCheckMacValue() 'CheckMacValue' => '7EC8DDC6C5C51B1A4D8BEA261246066858B38184C55FD3DD3D6DFF53F535A64', ]; + $this->getHttpRequest()->request->add($data); $request = new AcceptNotificationRequest($this->getHttpClient(), $this->getHttpRequest()); - $request->initialize(array_merge([ + $request->initialize([ 'HashKey' => '5294y06JbISpM5x9', 'HashIV' => 'v77hoKGq4kWxNNIS', 'EncryptType' => '1', 'MerchantID' => '2000132', - ], $options)); + ]); $request->setTestMode(true); $request->send(); } diff --git a/tests/Message/CompletePurchaseRequestTest.php b/tests/Message/CompletePurchaseRequestTest.php index ee84c50..d01418c 100644 --- a/tests/Message/CompletePurchaseRequestTest.php +++ b/tests/Message/CompletePurchaseRequestTest.php @@ -2,7 +2,6 @@ namespace Omnipay\ECPay\Tests\Message; -use Omnipay\Common\Exception\InvalidRequestException; use Omnipay\Common\Exception\InvalidResponseException; use Omnipay\ECPay\Message\CompletePurchaseRequest; use Omnipay\Tests\TestCase; @@ -10,12 +9,11 @@ class CompletePurchaseRequestTest extends TestCase { /** - * @throws InvalidRequestException * @throws InvalidResponseException */ public function testGetData() { - $options = [ + $data = [ 'CustomField1' => '', 'CustomField2' => '', 'CustomField3' => '', @@ -35,18 +33,19 @@ public function testGetData() 'CheckMacValue' => 'E7EC8DDC6C5C51B1A4D8BEA261246066858B38184C55FD3DD3D6DFF53F535A64', ]; + $this->getHttpRequest()->request->add($data); $request = new CompletePurchaseRequest($this->getHttpClient(), $this->getHttpRequest()); - $request->initialize(array_merge([ + $request->initialize([ 'HashKey' => '5294y06JbISpM5x9', 'HashIV' => 'v77hoKGq4kWxNNIS', 'EncryptType' => '1', 'MerchantID' => '2000132', - ], $options)); - $request->setTestMode(true); + 'testMode' => true, + ]); - self::assertEquals($options, $request->getData()); + self::assertEquals($data, $request->getData()); - return [$request->send(), $options]; + return [$request->send(), $data]; } /** @@ -55,13 +54,13 @@ public function testGetData() public function testSendData($result) { $response = $result[0]; - $options = $result[1]; + $data = $result[1]; self::assertTrue($response->isSuccessful()); self::assertEquals('Succeeded', $response->getMessage()); - self::assertEquals($options['RtnCode'], $response->getCode()); - self::assertEquals($options['TradeNo'], $response->getTransactionReference()); - self::assertEquals($options['MerchantTradeNo'], $response->getTransactionId()); + self::assertEquals($data['RtnCode'], $response->getCode()); + self::assertEquals($data['TradeNo'], $response->getTransactionReference()); + self::assertEquals($data['MerchantTradeNo'], $response->getTransactionId()); } public function testInvalidCheckMacValue() @@ -69,7 +68,7 @@ public function testInvalidCheckMacValue() $this->expectException(InvalidResponseException::class); $this->expectExceptionMessage('CheckMacValue verify fail'); - $options = [ + $data = [ 'CustomField1' => '', 'CustomField2' => '', 'CustomField3' => '', @@ -89,14 +88,15 @@ public function testInvalidCheckMacValue() 'CheckMacValue' => '7EC8DDC6C5C51B1A4D8BEA261246066858B38184C55FD3DD3D6DFF53F535A64', ]; + $this->getHttpRequest()->request->add($data); $request = new CompletePurchaseRequest($this->getHttpClient(), $this->getHttpRequest()); - $request->initialize(array_merge([ + $request->initialize([ 'HashKey' => '5294y06JbISpM5x9', 'HashIV' => 'v77hoKGq4kWxNNIS', 'EncryptType' => '1', 'MerchantID' => '2000132', - ], $options)); - $request->setTestMode(true); + 'testMode' => true, + ]); $request->send(); }