Skip to content

Commit

Permalink
add refund, cacncel action
Browse files Browse the repository at this point in the history
  • Loading branch information
recca0120 committed Feb 13, 2018
1 parent d449670 commit 0e01cf9
Show file tree
Hide file tree
Showing 15 changed files with 458 additions and 0 deletions.
Binary file not shown.
Binary file not shown.
36 changes: 36 additions & 0 deletions src/Action/Api/CancelTransactionAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace PayumTW\Mypay\Action\Api;

use Payum\Core\Bridge\Spl\ArrayObject;
use PayumTW\Mypay\Request\Api\CancelTransaction;
use Payum\Core\Exception\RequestNotSupportedException;

class CancelTransactionAction extends BaseApiAwareAction
{
/**
* {@inheritdoc}
*
* @param $request RefundTransaction
*/
public function execute($request)
{
RequestNotSupportedException::assertSupports($this, $request);

$details = ArrayObject::ensureArrayObject($request->getModel());

$details->validateNotEmpty(['uid', 'key']);

$details->replace($this->api->cancelTransaction((array) $details));
}

/**
* {@inheritdoc}
*/
public function supports($request)
{
return
$request instanceof CancelTransaction &&
$request->getModel() instanceof \ArrayAccess;
}
}
36 changes: 36 additions & 0 deletions src/Action/Api/RefundTransactionAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace PayumTW\Mypay\Action\Api;

use Payum\Core\Bridge\Spl\ArrayObject;
use PayumTW\Mypay\Request\Api\RefundTransaction;
use Payum\Core\Exception\RequestNotSupportedException;

class RefundTransactionAction extends BaseApiAwareAction
{
/**
* {@inheritdoc}
*
* @param $request RefundTransaction
*/
public function execute($request)
{
RequestNotSupportedException::assertSupports($this, $request);

$details = ArrayObject::ensureArrayObject($request->getModel());

$details->validateNotEmpty(['uid', 'key', 'cost']);

$details->replace($this->api->refundTransaction((array) $details));
}

/**
* {@inheritdoc}
*/
public function supports($request)
{
return
$request instanceof RefundTransaction &&
$request->getModel() instanceof \ArrayAccess;
}
}
40 changes: 40 additions & 0 deletions src/Action/CancelAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace PayumTW\Mypay\Action;

use Payum\Core\Request\Cancel;
use Payum\Core\GatewayAwareTrait;
use Payum\Core\GatewayAwareInterface;
use Payum\Core\Action\ActionInterface;
use Payum\Core\Bridge\Spl\ArrayObject;
use PayumTW\Mypay\Request\Api\CancelTransaction;
use Payum\Core\Exception\RequestNotSupportedException;

class CancelAction implements ActionInterface, GatewayAwareInterface
{
use GatewayAwareTrait;

/**
* {@inheritdoc}
*
* @param Cancel $request
*/
public function execute($request)
{
RequestNotSupportedException::assertSupports($this, $request);

$details = ArrayObject::ensureArrayObject($request->getModel());

$this->gateway->execute(new CancelTransaction($details));
}

/**
* {@inheritdoc}
*/
public function supports($request)
{
return
$request instanceof Cancel &&
$request->getModel() instanceof \ArrayAccess;
}
}
40 changes: 40 additions & 0 deletions src/Action/RefundAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace PayumTW\Mypay\Action;

use Payum\Core\Request\Refund;
use Payum\Core\GatewayAwareTrait;
use Payum\Core\GatewayAwareInterface;
use Payum\Core\Action\ActionInterface;
use Payum\Core\Bridge\Spl\ArrayObject;
use PayumTW\Mypay\Request\Api\RefundTransaction;
use Payum\Core\Exception\RequestNotSupportedException;

class RefundAction implements ActionInterface, GatewayAwareInterface
{
use GatewayAwareTrait;

/**
* {@inheritdoc}
*
* @param Refund $request
*/
public function execute($request)
{
RequestNotSupportedException::assertSupports($this, $request);

$details = ArrayObject::ensureArrayObject($request->getModel());

$this->gateway->execute(new RefundTransaction($details));
}

/**
* {@inheritdoc}
*/
public function supports($request)
{
return
$request instanceof Refund &&
$request->getModel() instanceof \ArrayAccess;
}
}
65 changes: 65 additions & 0 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ public function createTransaction(array $params)
'user_sn' => null,
// 消費者家用電話(白天電話)
'user_phone' => null,
// 行動電話國碼(預設886)​
'user_cellphone_code' => '886',
// 消費者行動電話
'user_cellphone' => null,
// 消費者 E­Mail
Expand All @@ -113,6 +115,8 @@ public function createTransaction(array $params)
'user_birthday' => null,
// 訂單總金額(如為定期定額付費,此為一期的金額) = 物品 之總價加總 ­ 折價  必要  必要
'cost' => null,
// 預設交易幣別
'currency' => 'TWD',
// 訂單編號(訂單編號建議不要重覆)  必要  必要
'order_id' => null,
// 消費者來源 IP  必要  必要
Expand All @@ -129,6 +133,8 @@ public function createTransaction(array $params)
'regular' => null,
// 總期數(如為 12 期即代入 12,如果為不限期數,請代入  0,如非定期定額付費,不需傳此參數
'regular_total' => null,
// 1.定期定額式付費編號 2.定期分期式付費編號
'group_id' => null,
// 票券總產生張數
'voucher_total_count' => null,
// 物品總金額
Expand All @@ -147,6 +153,16 @@ public function createTransaction(array $params)
'limit_pay_days' => null,
// 運費,
'shipping_fee' => null,
// 啟用快速結帳
'enable_quickpay' => 1,
// 啟用電子錢包
'enable_ewallet' => 0,
// 消費者完成電子錢包卡號綁定後 ,直接使用本參數,系統會自動 從綁定卡號扣款 若使用本參數,pfn將自動限制為 信用卡與海外信用卡兩種交易(虛 擬卡號在消費者啟用電子錢包時 ,會背景告知相關資訊,
'virtual_pan' => null,
// 1.支付頁面模式,mypay顯示結果 (預設) 2.背景發動扣款(直接回傳交易回 報參數)
'ewallet_type' => 1,
// 定期扣款起扣日(若未指定日期, 或小於今日則將判為當日扣)
'regular_first_charge_date' => null,
];

$supportedItemParams = ['id', 'name', 'cost', 'amount'];
Expand Down Expand Up @@ -211,6 +227,55 @@ public function getTransactionData(array $params)
);
}

/**
* refundTransaction.
*
* @param array $params
* @return array
*/
public function refundTransaction(array $params)
{
$supportedParams = [
'store_uid' => $this->options['store_uid'],
'uid' => null,
'key' => null,
'cost' => null,
];

$params = array_filter(array_replace(
$supportedParams,
array_intersect_key($params, $supportedParams)
));

return $this->doRequest(
$this->encrypter->encryptRequest($this->options['store_uid'], $params, 'api/refund')
);
}

/**
* cancelTransaction.
*
* @param array $params
* @return array
*/
public function cancelTransaction(array $params)
{
$supportedParams = [
'store_uid' => $this->options['store_uid'],
'uid' => null,
'key' => null,
];

$params = array_filter(array_replace(
$supportedParams,
array_intersect_key($params, $supportedParams)
));

return $this->doRequest(
$this->encrypter->encryptRequest($this->options['store_uid'], $params, 'api/refundcancel')
);
}

/**
* verifyHash.
*
Expand Down
4 changes: 4 additions & 0 deletions src/MypayGatewayFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
use Http\Client\Socket\Client as HttpSocketClient;
use Http\Adapter\Guzzle5\Client as HttpGuzzle5Client;
use Http\Adapter\Guzzle6\Client as HttpGuzzle6Client;
use PayumTW\Mypay\Action\Api\CancelTransactionAction;
use PayumTW\Mypay\Action\Api\CreateTransactionAction;
use PayumTW\Mypay\Action\Api\RefundTransactionAction;
use PayumTW\Mypay\Action\Api\GetTransactionDataAction;

class MypayGatewayFactory extends GatewayFactory
Expand Down Expand Up @@ -117,6 +119,8 @@ protected function populateConfig(ArrayObject $config)
'payum.action.convert_payment' => new ConvertPaymentAction(),

'payum.action.api.create_transaction' => new CreateTransactionAction(),
'payum.action.api.refund_transaction' => new RefundTransactionAction(),
'payum.action.api.cancel_transaction' => new CancelTransactionAction(),
'payum.action.api.get_transaction_data' => new GetTransactionDataAction(),
]);

Expand Down
9 changes: 9 additions & 0 deletions src/Request/Api/CancelTransaction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace PayumTW\Mypay\Request\Api;

use Payum\Core\Request\Generic;

class CancelTransaction extends Generic
{
}
9 changes: 9 additions & 0 deletions src/Request/Api/RefundTransaction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace PayumTW\Mypay\Request\Api;

use Payum\Core\Request\Generic;

class RefundTransaction extends Generic
{
}
33 changes: 33 additions & 0 deletions tests/Action/Api/CancelTransactionActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace PayumTW\Mypay\Tests\Action\Api;

use Mockery as m;
use PHPUnit\Framework\TestCase;
use Payum\Core\Bridge\Spl\ArrayObject;
use PayumTW\Mypay\Request\Api\CancelTransaction;
use PayumTW\Mypay\Action\Api\CancelTransactionAction;

class CancelTransactionActionTest extends TestCase
{
protected function tearDown()
{
m::close();
}

public function testExecute()
{
$action = new CancelTransactionAction();
$request = new CancelTransaction(new ArrayObject(['uid' => 'foo', 'key' => 'foo']));

$action->setApi(
$api = m::mock('PayumTW\Mypay\Api')
);

$api->shouldReceive('cancelTransaction')->once()->with((array) $request->getModel())->andReturn($params = ['foo' => 'bar']);

$action->execute($request);

$this->assertSame(array_merge(['uid' => 'foo', 'key' => 'foo'], $params), (array) $request->getModel());
}
}
33 changes: 33 additions & 0 deletions tests/Action/Api/RefundTransactionActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace PayumTW\Mypay\Tests\Action\Api;

use Mockery as m;
use PHPUnit\Framework\TestCase;
use Payum\Core\Bridge\Spl\ArrayObject;
use PayumTW\Mypay\Request\Api\RefundTransaction;
use PayumTW\Mypay\Action\Api\RefundTransactionAction;

class RefundTransactionActionTest extends TestCase
{
protected function tearDown()
{
m::close();
}

public function testExecute()
{
$action = new RefundTransactionAction();
$request = new RefundTransaction(new ArrayObject(['uid' => 'foo_uid', 'key' => 'foo_key', 'cost' => 'foo_cost']));

$action->setApi(
$api = m::mock('PayumTW\Mypay\Api')
);

$api->shouldReceive('refundTransaction')->once()->with((array) $request->getModel())->andReturn($params = ['foo' => 'bar']);

$action->execute($request);

$this->assertSame(array_merge(['uid' => 'foo_uid', 'key' => 'foo_key', 'cost' => 'foo_cost'], $params), (array) $request->getModel());
}
}
31 changes: 31 additions & 0 deletions tests/Action/CancelActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace PayumTW\Mypay\Tests\Action;

use Mockery as m;
use Payum\Core\Request\Cancel;
use PHPUnit\Framework\TestCase;
use Payum\Core\Bridge\Spl\ArrayObject;
use PayumTW\Mypay\Action\CancelAction;

class CancelActionTest extends TestCase
{
protected function tearDown()
{
m::close();
}

public function testExecute()
{
$action = new CancelAction();
$request = new Cancel(new ArrayObject([]));

$action->setGateway(
$gateway = m::mock('Payum\Core\GatewayInterface')
);

$gateway->shouldReceive('execute')->once()->with('PayumTW\Mypay\Request\Api\CancelTransaction');

$this->assertNull($action->execute($request));
}
}
Loading

0 comments on commit 0e01cf9

Please sign in to comment.