Skip to content

Commit

Permalink
get data from request
Browse files Browse the repository at this point in the history
  • Loading branch information
recca0120 committed Nov 11, 2023
1 parent a861373 commit fbe0208
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 156 deletions.
6 changes: 4 additions & 2 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down
10 changes: 10 additions & 0 deletions src/Message/AcceptNotificationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
37 changes: 4 additions & 33 deletions src/Message/CompletePurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

/**
Expand Down
98 changes: 4 additions & 94 deletions src/Message/ReceiveTransactionInfoRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}

/**
Expand Down
30 changes: 12 additions & 18 deletions tests/GatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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());
Expand All @@ -88,7 +89,7 @@ public function testCompletePurchase(): void

public function testAcceptNotification(): void
{
$request = $this->gateway->acceptNotification([
$data = [
'RtnCode' => '1',
'RtnMessage' => '成功',
'MerTradeID' => '20151202001',
Expand All @@ -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',
Expand All @@ -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());
Expand Down
15 changes: 10 additions & 5 deletions tests/Message/AcceptNotificationRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down
6 changes: 4 additions & 2 deletions tests/Message/CompletePurchaseRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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());
Expand Down
6 changes: 4 additions & 2 deletions tests/Message/ReceiveTransactionInfoRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit fbe0208

Please sign in to comment.