Skip to content

Commit

Permalink
get input data from Request
Browse files Browse the repository at this point in the history
  • Loading branch information
recca0120 committed Nov 11, 2023
1 parent 672fb90 commit d05584c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 290 deletions.
3 changes: 1 addition & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
stopOnFailure="false">
<testsuites>
<testsuite name="Omnipay Test Suite">
<directory>./tests/</directory>
Expand Down
278 changes: 8 additions & 270 deletions src/Message/CompletePurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
26 changes: 12 additions & 14 deletions tests/GatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -16,7 +15,7 @@ class GatewayTest extends GatewayTestCase
/**
* @var Encryptor
*/
private $encryption;
private $encryptor;

/**
* @var string
Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -87,7 +85,7 @@ public function testPurchase()

public function testCompletePurchase()
{
$options = [
$this->getHttpRequest()->request->add([
'key' => 'dee886ee19ddbb97e2968a1a8777fc7d',
'prc' => '250',
'finishtime' => '20210523141536',
Expand Down Expand Up @@ -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());
Expand All @@ -129,7 +127,7 @@ public function testCompletePurchase()

public function testAcceptNotification()
{
$options = [
$this->getHttpRequest()->request->add([
'key' => 'dee886ee19ddbb97e2968a1a8777fc7d',
'prc' => '250',
'finishtime' => '20210523141536',
Expand Down Expand Up @@ -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());
Expand Down
Loading

0 comments on commit d05584c

Please sign in to comment.