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 0b84766 commit c042bb6
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 239 deletions.
202 changes: 4 additions & 198 deletions src/Message/CompletePurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
25 changes: 13 additions & 12 deletions tests/GatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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' => '',
Expand All @@ -62,15 +61,16 @@ 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());
}

public function testAcceptNotification()
{
$response = $this->gateway->acceptNotification(array_merge($this->options, [
$this->getHttpRequest()->request->add([
'CustomField1' => '',
'CustomField2' => '',
'CustomField3' => '',
Expand All @@ -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());
Expand Down
26 changes: 13 additions & 13 deletions tests/Message/AcceptNotificationRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -11,12 +10,11 @@
class AcceptNotificationRequestTest extends TestCase
{
/**
* @throws InvalidRequestException
* @throws InvalidResponseException
*/
public function testGetData()
{
$options = [
$data = [
'CustomField1' => '',
'CustomField2' => '',
'CustomField3' => '',
Expand All @@ -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];
}

/**
Expand All @@ -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());
Expand All @@ -70,7 +69,7 @@ public function testInvalidCheckMacValue()
$this->expectException(InvalidResponseException::class);
$this->expectExceptionMessage('CheckMacValue verify fail');

$options = [
$data = [
'CustomField1' => '',
'CustomField2' => '',
'CustomField3' => '',
Expand All @@ -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();
}
Expand Down
Loading

0 comments on commit c042bb6

Please sign in to comment.