diff --git a/src/Gateway.php b/src/Gateway.php index 103cf9d..8c3c66f 100644 --- a/src/Gateway.php +++ b/src/Gateway.php @@ -5,6 +5,7 @@ use Omnipay\Common\AbstractGateway; use Omnipay\Gomypay\Message\AcceptNotificationRequest; use Omnipay\Gomypay\Message\CompletePurchaseRequest; +use Omnipay\Gomypay\Message\FetchTransactionRequest; use Omnipay\Gomypay\Message\PurchaseRequest; use Omnipay\Gomypay\Traits\HasGomypay; @@ -44,6 +45,11 @@ public function acceptNotification(array $options = []) return $this->createRequest(AcceptNotificationRequest::class, $options); } + public function fetchTransaction(array $options = []) + { + return $this->createRequest(FetchTransactionRequest::class, $options); + } + public function getPaymentInfo(array $options = []) { return $this->createRequest(GetPaymentInfoRequest::class, $options); diff --git a/src/Message/AbstractRequest.php b/src/Message/AbstractRequest.php index 43b6aed..c4abbd0 100644 --- a/src/Message/AbstractRequest.php +++ b/src/Message/AbstractRequest.php @@ -6,12 +6,12 @@ abstract class AbstractRequest extends BaseAbstractRequest { - protected $liveEndpoint = 'https://n.gomypay.asia/ShuntClass.aspx'; + protected $host = 'https://n.gomypay.asia/'; - protected $testEndpoint = 'https://n.gomypay.asia/TestShuntClass.aspx'; - - public function getEndpoint() + public function getUrl($uri) { - return $this->getTestMode() ? $this->testEndpoint : $this->liveEndpoint; + return $this->getTestMode() + ? $this->host.'Test'.$uri + : $this->host.$uri; } } diff --git a/src/Message/FetchTransactionRequest.php b/src/Message/FetchTransactionRequest.php new file mode 100644 index 0000000..801a933 --- /dev/null +++ b/src/Message/FetchTransactionRequest.php @@ -0,0 +1,34 @@ +validate('transactionId'); + + return [ + 'Order_No' => $this->getTransactionId(), + 'CustomerId' => $this->getCustomerId(), + 'Str_Check' => $this->getStrCheck(), + ]; + } + + public function sendData($data) + { + $response = $this->httpClient->request( + 'POST', + $this->getUrl('CallOrder.aspx'), + ['content-type' => 'application/json'], + http_build_query($data) + ); + $data = json_decode((string) $response->getBody(), true); + + return $this->response = new FetchTransactionResponse($this, $data); + } +} diff --git a/src/Message/FetchTransactionResponse.php b/src/Message/FetchTransactionResponse.php new file mode 100644 index 0000000..99dcbc1 --- /dev/null +++ b/src/Message/FetchTransactionResponse.php @@ -0,0 +1,31 @@ +getCode() === 1; + } + + 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 8bef897..2f6ac6a 100644 --- a/src/Message/PurchaseRequest.php +++ b/src/Message/PurchaseRequest.php @@ -5,10 +5,12 @@ use Omnipay\Common\Exception\InvalidRequestException; use Omnipay\Gomypay\PaymentMethod; use Omnipay\Gomypay\Traits\HasGomypay; +use Omnipay\Gomypay\Traits\HasOrderNo; class PurchaseRequest extends AbstractRequest { use HasGomypay; + use HasOrderNo; public function getSendType() { @@ -36,20 +38,6 @@ public function setPayModeNo($value) return $this->setParameter('Pay_Mode_No', $value); } - public function getOrderNo() - { - return $this->getTransactionId(); - } - - /** - * 交易單號,如無則自動帶入系統預設交易單號 - * 若使用系統預設交易畫面,交易單號不可為無 - */ - public function setOrderNo($value) - { - return $this->setTransactionId($value); - } - public function getAmount() { return parent::getAmount(); diff --git a/src/Message/PurchaseResponse.php b/src/Message/PurchaseResponse.php index 8c90c8a..d20daff 100644 --- a/src/Message/PurchaseResponse.php +++ b/src/Message/PurchaseResponse.php @@ -18,9 +18,7 @@ public function isRedirect() public function getRedirectUrl() { - return $this->request->getTestMode() - ? 'https://n.gomypay.asia/TestShuntClass.aspx' - : 'https://n.gomypay.asia/ShuntClass.aspx'; + return $this->request->getUrl('ShuntClass.aspx'); } public function getRedirectMethod() diff --git a/src/Traits/HasOrderNo.php b/src/Traits/HasOrderNo.php new file mode 100644 index 0000000..aebb5ac --- /dev/null +++ b/src/Traits/HasOrderNo.php @@ -0,0 +1,20 @@ +getTransactionId(); + } + + /** + * 交易單號,如無則自動帶入系統預設交易單號 + * 若使用系統預設交易畫面,交易單號不可為無 + */ + public function setOrderNo($value) + { + return $this->setTransactionId($value); + } +} diff --git a/tests/Message/FetchTransactionRequestTest.php b/tests/Message/FetchTransactionRequestTest.php new file mode 100644 index 0000000..73f0630 --- /dev/null +++ b/tests/Message/FetchTransactionRequestTest.php @@ -0,0 +1,34 @@ +request = new FetchTransactionRequest($this->getHttpClient(), $this->getHttpRequest()); + $this->request->initialize([ + 'CustomerId' => '83759795', + 'Str_Check' => 'tak8ebh0qg1irbs72t1a34f2qdc7jyc7', + 'test_mode' => true, + + 'transaction_id' => '2024101311', + ]); + } + + public function testGetData() + { + $this->setMockHttpResponse('FetchTransactionSuccess.txt'); + var_dump($this->request->send()); + } +} diff --git a/tests/Mock/FetchTransactionSuccess.txt b/tests/Mock/FetchTransactionSuccess.txt new file mode 100644 index 0000000..9c2fe4a --- /dev/null +++ b/tests/Mock/FetchTransactionSuccess.txt @@ -0,0 +1,39 @@ +HTTP/1.1 200 OK +Server: Apache +Date: Sun, 11 May 2014 21:17:31 GMT +Content-Type: application/json; charset=utf-8 +Status: 200 OK + +{ + "result": "1", + "ret_msg": "已繳費", + "OrderID": "2019012300000000569", + "e_Cur": "NT", + "e_money": "10000", + "PayAmount": "10000", + "e_date": "20190123", + "e_time": "18:37:59", + "p_date": "20190123", + "p_time": "18:42:17", + "e_orderno": "AH15482399052114", + "e_no": "42816104A05", + "e_outlay": "26", + "bankname": "", + "avcode": "", + "Buyer_Name": "User", + "Buyer_Mail": "paymoney159@gmail.com", + "Buyer_Telm": "0900000000", + "Buyer_Memo": "此為商城虛擬商品購買,並非實體商品交易,請勿受騙上當,並請勿代他人繳款儲值,小心觸法", + "Creditcard_No": "", + "Installment": "0", + "Shop_PaymentCode": "1610A8259J3291", + "Virtual_Account": "", + "e_PayInfo": "", + "Send_Type": "6", + "Goods_Return": "0", + "Goods_Return_Statu": "", + "pay_result": "1", + "LimitDate": "20190123", + "Market_ID": "FM", + "Shop_Store_Name": "中壢興廣店(桃園市中壢區新中北路二段489號)" +}