diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 535809e..6440400 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -7,8 +7,7 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" - stopOnFailure="false" - syntaxCheck="false"> + stopOnFailure="false"> ./tests/ diff --git a/src/Message/CompletePurchaseRequest.php b/src/Message/CompletePurchaseRequest.php index 905eca0..a2f7c8f 100644 --- a/src/Message/CompletePurchaseRequest.php +++ b/src/Message/CompletePurchaseRequest.php @@ -2,288 +2,26 @@ namespace Omnipay\MyPay\Message; +use JsonException; use Omnipay\Common\Message\AbstractRequest as BaseAbstractRequest; -use Omnipay\MyPay\Traits\HasAmount; -use Omnipay\MyPay\Traits\HasEcho; -use Omnipay\MyPay\Traits\HasKey; -use Omnipay\MyPay\Traits\HasOrderInfo; -use Omnipay\MyPay\Traits\HasOrderResult; use Omnipay\MyPay\Traits\HasStore; -use Omnipay\MyPay\Traits\HasUid; class CompletePurchaseRequest extends BaseAbstractRequest { use HasStore; - use HasKey; - use HasUid; - use HasOrderInfo; - use HasOrderResult; - use HasEcho; - use HasAmount; - - /** - * 交易回傳碼(參閱附錄二). - * - * @param string $value - * @return $this - */ - public function setPrc($value) - { - return $this->setParameter('prc', $value); - } - - /** - * @return string - */ - public function getPrc() - { - return $this->getParameter('prc'); - } - - /** - * 卡號/VA/超商代碼 - * - * @param string $value - * @return $this - */ - public function setCardno($value) - { - return $this->setParameter('cardno', $value); - } - - /** - * @return string - */ - public function getCardno() - { - return $this->getParameter('cardno'); - } - - /** - * 銀行交易授權碼 - * - * @param string $value - * @return $this - */ - public function setAcode($value) - { - return $this->setParameter('acode', $value); - } - - /** - * @return string - */ - public function getAcode() - { - return $this->getParameter('acode'); - } - - /** - * 實際交易金額. - * - * @param string $value - * @return $this - */ - public function setActualCost($value) - { - return $this->setParameter('actual_cost', $value); - } - - /** - * @return string - */ - public function getActualCost() - { - return $this->getParameter('actual_cost'); - } - - /** - * 實際交易幣別. - * - * @param string $value - * @return $this - */ - public function setActualCurrency($value) - { - return $this->setParameter('actual_currency', $value); - } - - /** - * @return string - */ - public function getActualCurrency() - { - return $this->getParameter('actual_currency'); - } - - /** - * 愛心捐款金額(幣別同實際交易幣別). - * - * @param string $value - * @return $this - */ - public function setLoveCost($value) - { - return $this->setParameter('love_cost', $value); - } - - /** - * @return string - */ - public function getLoveCost() - { - return $this->getParameter('love_cost'); - } - - /** - * 回傳訊息. - * - * @param string $value - * @return $this - */ - public function setRetmsg($value) - { - return $this->setParameter('retmsg', $value); - } - - /** - * @return string - */ - public function getRetmsg() - { - return $this->getParameter('retmsg'); - } - - /** - * 交易完成時間(YYYYMMDDHHmmss). - * - * @param string $value - * @return $this - */ - public function setFinishtime($value) - { - return $this->setParameter('finishtime', $value); - } - - /** - * @return string - */ - public function getFinishtime() - { - return $this->getParameter('finishtime'); - } - - /** - * 扣款名稱(定期定額/定期分期交易專用). - * - * @param string $value - * @return $this - */ - public function setPaymentName($value) - { - return $this->setParameter('payment_name', $value); - } - - /** - * @return string - */ - public function getPaymentName() - { - return $this->getParameter('payment_name'); - } - - /** - * 期數 (定期定額/定期分期交易專用). - * - * @param int $value - * @return $this - */ - public function setNois($value) - { - return $this->setParameter('nois', $value); - } - - /** - * @return int|null - */ - public function getNois() - { - return $this->getParameter('nois'); - } - - /** - * 銀行代碼 虛擬帳號資訊. - * - * @param string $value - * @return $this - */ - public function setBankId($value) - { - return $this->setParameter('bank_id', $value); - } - - /** - * @return string - */ - public function getBankId() - { - return $this->getParameter('bank_id'); - } - - /** - * 有效日期虛擬帳號、超商代碼、無卡分期資訊. - * - * @param string $value - * @return $this - */ - public function setExpiredDate($value) - { - return $this->setParameter('expired_date', $value); - } - - /** - * @return string|null - */ - public function getExpiredDate() - { - return $this->getParameter('expired_date'); - } /** * @return array + * @throws JsonException */ public function getData() { - return [ - 'key' => $this->getKey(), - 'prc' => $this->getPrc(), - 'finishtime' => $this->getFinishtime(), - 'uid' => $this->getTransactionReference(), - 'order_id' => $this->getOrderId(), - 'user_id' => $this->getUserId(), - 'cost' => $this->getCost(), - 'currency' => $this->getCurrency(), - 'actual_cost' => $this->getActualCost(), - 'actual_currency' => $this->getActualCurrency(), - 'love_cost' => $this->getLoveCost(), - 'retmsg' => $this->getRetmsg(), - 'pfn' => $this->getPfn(), - 'trans_type' => $this->getTransType(), - 'redeem' => $this->getRedeem(), - 'payment_name' => $this->getPaymentName(), - 'nois' => $this->getNois(), - 'group_id' => $this->getGroupId(), - 'bank_id' => $this->getBankId(), - 'expired_date' => $this->getExpiredDate(), - 'result_type' => $this->getResultType(), - 'result_content_type' => $this->getResultContentType(), - 'result_content' => $this->getResultContent(), - 'echo_0' => $this->getEcho0(), - 'echo_1' => $this->getEcho1(), - 'echo_2' => $this->getEcho2(), - 'echo_3' => $this->getEcho3(), - 'echo_4' => $this->getEcho4(), - ]; + $data = $this->httpRequest->request->all(); + if (array_key_exists('result_content', $data)) { + $data['result_content'] = json_decode($data['result_content'], true, 512, JSON_THROW_ON_ERROR); + } + + return $data; } /** diff --git a/tests/GatewayTest.php b/tests/GatewayTest.php index 360db4e..7ca4723 100644 --- a/tests/GatewayTest.php +++ b/tests/GatewayTest.php @@ -6,7 +6,6 @@ use Omnipay\MyPay\Gateway; use Omnipay\MyPay\Item; use Omnipay\Tests\GatewayTestCase; -use Symfony\Component\HttpFoundation\Request; class GatewayTest extends GatewayTestCase { @@ -16,7 +15,7 @@ class GatewayTest extends GatewayTestCase /** * @var Encryptor */ - private $encryption; + private $encryptor; /** * @var string @@ -32,10 +31,9 @@ public function setUp(): void { parent::setUp(); - $httpRequest = Request::createFromGlobals(); - $httpRequest->server->set('REMOTE_ADDR', '127.0.0.1'); - $this->gateway = new Gateway($this->getHttpClient(), $httpRequest); - $this->encryption = new Encryptor($this->storeKey); + $this->getHttpRequest()->server->add(['REMOTE_ADDR' => '127.0.0.1']); + $this->gateway = new Gateway($this->getHttpClient(), $this->getHttpRequest()); + $this->encryptor = new Encryptor($this->storeKey); $this->gateway->initialize([ 'store_uid' => $this->storeUid, 'store_key' => $this->storeKey, @@ -64,8 +62,8 @@ public function testPurchase() $body = (string) $this->getMockClient()->getLastRequest()->getBody(); parse_str($body, $params); - $service = $this->encryption->decrypt($params['service']); - $options = $this->encryption->decrypt($params['encry_data']); + $service = $this->encryptor->decrypt($params['service']); + $options = $this->encryptor->decrypt($params['encry_data']); self::assertEquals($this->storeUid, $params['store_uid']); self::assertEquals(['service_name' => 'api', 'cmd' => 'api/orders'], $service); @@ -87,7 +85,7 @@ public function testPurchase() public function testCompletePurchase() { - $options = [ + $this->getHttpRequest()->request->add([ 'key' => 'dee886ee19ddbb97e2968a1a8777fc7d', 'prc' => '250', 'finishtime' => '20210523141536', @@ -116,9 +114,9 @@ public function testCompletePurchase() 'echo_2' => null, 'echo_3' => null, 'echo_4' => null, - ]; + ]); - $response = $this->gateway->completePurchase($options)->send(); + $response = $this->gateway->completePurchase()->send(); self::assertTrue($response->isSuccessful()); self::assertEquals('250', $response->getCode()); @@ -129,7 +127,7 @@ public function testCompletePurchase() public function testAcceptNotification() { - $options = [ + $this->getHttpRequest()->request->add([ 'key' => 'dee886ee19ddbb97e2968a1a8777fc7d', 'prc' => '250', 'finishtime' => '20210523141536', @@ -158,9 +156,9 @@ public function testAcceptNotification() 'echo_2' => null, 'echo_3' => null, 'echo_4' => null, - ]; + ]); - $response = $this->gateway->acceptNotification($options); + $response = $this->gateway->acceptNotification(); self::assertEquals('付款完成', $response->getMessage()); self::assertEquals('8888', $response->getReply()); diff --git a/tests/Message/CompletePurchaseRequestTest.php b/tests/Message/CompletePurchaseRequestTest.php index c55c970..d37b3b4 100644 --- a/tests/Message/CompletePurchaseRequestTest.php +++ b/tests/Message/CompletePurchaseRequestTest.php @@ -8,6 +8,16 @@ class CompletePurchaseRequestTest extends TestCase { + /** + * @var string + */ + private $storeUid = '398800730001'; + + /** + * @var string + */ + private $storeKey = 'Xd668CSjnXQLD26Hia8vapkOgGXAv68s'; + /** * @var PurchaseRequest */ @@ -16,9 +26,7 @@ class CompletePurchaseRequestTest extends TestCase public function setUp(): void { parent::setUp(); - - $this->request = new CompletePurchaseRequest($this->getHttpClient(), $this->getHttpRequest()); - $this->request->initialize([ + $data = [ 'uid' => '86579', 'key' => 'dee886ee19ddbb97e2968a1a8777fc7d', 'prc' => '250', @@ -54,16 +62,32 @@ public function setUp(): void 'echo_2' => null, 'echo_3' => null, 'echo_4' => null, + ]; + + $httpRequest = $this->getHttpRequest(); + $httpRequest->request->add($data); + $this->request = new CompletePurchaseRequest($this->getHttpClient(), $httpRequest); + $this->request->initialize([ + 'store_uid' => $this->storeUid, + 'store_key' => $this->storeKey, + 'locale' => 'en', ]); } public function testGetData() { self::assertEquals([ + 'uid' => '86579', 'key' => 'dee886ee19ddbb97e2968a1a8777fc7d', 'prc' => '250', 'finishtime' => '20210523141536', - 'uid' => '86579', + 'cardno' => '490706******5101', + 'acode' => 'AA1234', + 'issuing_bank' => '合作金庫', + 'issuing_bank_uid' => '006', + 'transaction_mode' => '1', + 'supplier_name' => '高鉅金融', + 'supplier_code' => 'T0', 'order_id' => 'd5jUed1tkQ9cDaD1', 'user_id' => 'DoSuccess3D', 'cost' => '1000',