Skip to content

Instantly share code, notes, and snippets.

@recca0120
Last active December 10, 2016 03:20
Show Gist options
  • Save recca0120/e18d8d992c5e3e7eff5d56d0bb34f909 to your computer and use it in GitHub Desktop.
Save recca0120/e18d8d992c5e3e7eff5d56d0bb34f909 to your computer and use it in GitHub Desktop.
<?php
use Mockery as m;
use PayumTW\Allpay\Api;
class ApiTest extends PHPUnit_Framework_TestCase
{
public function tearDown()
{
m::close();
}
public function test_create_transaction()
{
/*
|------------------------------------------------------------
| Set
|------------------------------------------------------------
*/
$options = [
'MerchantID' => '2000132',
'HashKey' => '5294y06JbISpM5x9',
'HashIV' => 'v77hoKGq4kWxNNIS',
'sandbox' => false,
];
$params = [
'ReturnURL' => 'http://www.allpay.com.tw/receive.php',
'MerchantTradeNo' => 'Test'.time(),
'MerchantTradeDate' => date('Y/m/d H:i:s'),
'TotalAmount' => 2000,
'TradeDesc' => 'good to drink',
'ChoosePayment' => PaymentMethod::ALL,
'Items' => [
[
'Name' => '歐付寶黑芝麻豆漿',
'Price' => 2000,
'Currency' => '元',
'Quantity' => 1,
'URL' => 'dedwed',
],
],
];
$httpClient = m::mock('Payum\Core\HttpClientInterface');
$message = m::mock('Http\Message\MessageFactory');
/*
|------------------------------------------------------------
| Expectation
|------------------------------------------------------------
*/
$api = new Api($options, $httpClient, $message);
/*
|------------------------------------------------------------
| Assertion
|------------------------------------------------------------
*/
$params = $api->createTransaction($params);
$this->assertSame(CheckMacValue::generate($params, $options['HashKey'], $options['HashIV'], 0), $params['CheckMacValue']);
$this->assertSame('https://payment.allpay.com.tw/Cashier/AioCheckOut/V2', $api->getApiEndpoint());
}
public function test_get_transaction_data()
{
/*
|------------------------------------------------------------
| Set
|------------------------------------------------------------
*/
$options = [
'MerchantID' => '2000132',
'HashKey' => '5294y06JbISpM5x9',
'HashIV' => 'v77hoKGq4kWxNNIS',
'sandbox' => false,
];
$params = [
'response' => [
'MerchantID' => '2000132',
'MerchantTradeNo' => '57CBC66A39F82',
'PayAmt' => '340',
'PaymentDate' => '2016/09/04 15:03:08',
'PaymentType' => 'Credit_CreditCard',
'PaymentTypeChargeFee' => '3',
'RedeemAmt' => '0',
'RtnCode' => '1',
'RtnMsg' => 'Succeeded',
'SimulatePaid' => '0',
'TradeAmt' => '340',
'TradeDate' => '2016/09/04 14:59:13',
'TradeNo' => '1609041459136128',
'CheckMacValue' => '6812D213BF2C5B9377EBF101607BF2DF',
],
];
$httpClient = m::mock('Payum\Core\HttpClientInterface');
$message = m::mock('Http\Message\MessageFactory');
/*
|------------------------------------------------------------
| Expectation
|------------------------------------------------------------
*/
$api = new Api($options, $httpClient, $message);
/*
|------------------------------------------------------------
| Assertion
|------------------------------------------------------------
*/
$params = $api->getTransactionData($params);
$expected = [
'response' => [
'MerchantID' => '2000132',
'MerchantTradeNo' => '57CBC66A39F82',
'PayAmt' => '340',
'PaymentDate' => '2016/09/04 15:03:08',
'PaymentType' => 'Credit_CreditCard',
'PaymentTypeChargeFee' => '3',
'RedeemAmt' => '0',
'RtnCode' => '1',
'RtnMsg' => 'Succeeded',
'SimulatePaid' => '0',
'TradeAmt' => '340',
'TradeDate' => '2016/09/04 14:59:13',
'TradeNo' => '1609041459136128',
'CheckMacValue' => '6812D213BF2C5B9377EBF101607BF2DF',
'statusReason' => '成功',
],
];
foreach ($expected['response'] as $key => $value) {
$this->assertSame($value, $params[$key]);
}
$this->assertSame('https://payment.allpay.com.tw/Cashier/AioCheckOut/V2', $api->getApiEndpoint());
}
public function test_parse_result_fail()
{
/*
|------------------------------------------------------------
| Set
|------------------------------------------------------------
*/
$options = [
'MerchantID' => '2000132',
'HashKey' => '5294y06JbISpM5x9',
'HashIV' => 'v77hoKGq4kWxNNIS',
'sandbox' => false,
];
$params = [
'response' => [
'MerchantID' => '2000132',
'MerchantTradeNo' => '57CBC66A39F82',
'PayAmt' => '340',
'PaymentDate' => '2016/09/04 15:03:08',
'PaymentType' => 'Credit_CreditCard',
'PaymentTypeChargeFee' => '3',
'RedeemAmt' => '0',
'RtnCode' => '1',
'RtnMsg' => 'Succeeded',
'SimulatePaid' => '0',
'TradeAmt' => '340',
'TradeDate' => '2016/09/04 14:59:13',
'TradeNo' => '1609041459136128',
'CheckMacValue' => '6812D213BF2C5B9377EBF101607BFEEE',
],
];
$httpClient = m::mock('Payum\Core\HttpClientInterface');
$message = m::mock('Http\Message\MessageFactory');
/*
|------------------------------------------------------------
| Expectation
|------------------------------------------------------------
*/
$api = new Api($options, $httpClient, $message);
/*
|------------------------------------------------------------
| Assertion
|------------------------------------------------------------
*/
$result = $api->getTransactionData($params);
$this->assertSame('10400002', $result['RtnCode']);
}
public function test_sandbox()
{
/*
|------------------------------------------------------------
| Set
|------------------------------------------------------------
*/
$options = [
'MerchantID' => '2000132',
'HashKey' => '5294y06JbISpM5x9',
'HashIV' => 'v77hoKGq4kWxNNIS',
'sandbox' => true,
];
$params = [
];
$httpClient = m::mock('Payum\Core\HttpClientInterface');
$message = m::mock('Http\Message\MessageFactory');
/*
|------------------------------------------------------------
| Expectation
|------------------------------------------------------------
*/
$api = new Api($options, $httpClient, $message);
/*
|------------------------------------------------------------
| Assertion
|------------------------------------------------------------
*/
$this->assertSame('https://payment-stage.allpay.com.tw/Cashier/AioCheckOut/V2', $api->getApiEndpoint());
}
}
<?php
use Mockery as m;
use PayumTW\Allpay\Api;
class ApiTest extends PHPUnit_Framework_TestCase
{
public function tearDown()
{
m::close();
}
public function test_create_transaction()
{
/*
|------------------------------------------------------------
| Arrange
|------------------------------------------------------------
*/
$httpClient = m::spy('Payum\Core\HttpClientInterface');
$message = m::spy('Http\Message\MessageFactory');
$sdk = m::spy('PayumTW\Allpay\Bridge\Allpay\AllInOne');
$sdk->Send = [
'ReturnURL' => '',
'ClientBackURL' => '',
'OrderResultURL' => '',
'MerchantTradeNo' => '',
'MerchantTradeDate' => '',
'PaymentType' => 'aio',
'TotalAmount' => '',
'TradeDesc' => '',
'ChoosePayment' => PaymentMethod::ALL,
'Remark' => '',
'ChooseSubPayment' => PaymentMethodItem::None,
'NeedExtraPaidInfo' => ExtraPaymentInfo::No,
'DeviceSource' => '',
'IgnorePayment' => '',
'PlatformID' => '',
'InvoiceMark' => InvoiceState::No,
'Items' => [],
'EncryptType' => EncryptType::ENC_MD5,
'UseRedeem' => UseRedeem::No,
];
$options = [
'MerchantID' => '2000132',
'HashKey' => '5294y06JbISpM5x9',
'HashIV' => 'v77hoKGq4kWxNNIS',
'sandbox' => false,
];
$params = [
'ReturnURL' => 'http://www.allpay.com.tw/receive.php',
'MerchantTradeNo' => 'Test'.time(),
'MerchantTradeDate' => date('Y/m/d H:i:s'),
'TotalAmount' => 2000,
'TradeDesc' => 'good to drink',
'ChoosePayment' => PaymentMethod::ALL,
'Items' => [
[
'Name' => '歐付寶黑芝麻豆漿',
'Price' => 2000,
'Currency' => '元',
'Quantity' => 1,
'URL' => 'dedwed',
],
],
];
/*
|------------------------------------------------------------
| Act
|------------------------------------------------------------
*/
$api = new Api($options, $httpClient, $message, $sdk);
/*
|------------------------------------------------------------
| Assert
|------------------------------------------------------------
*/
$api->createTransaction($params);
$this->assertSame($options['HashKey'], $sdk->HashKey);
$this->assertSame($options['HashIV'], $sdk->HashIV);
$this->assertSame($options['MerchantID'], $sdk->MerchantID);
$this->assertSame($api->getApiEndpoint('AioCheckOut'), $sdk->ServiceURL);
$this->assertSame($params['ReturnURL'], $sdk->Send['ReturnURL']);
$sdk->shouldHaveReceived('CheckOut')->once();
}
public function test_cancel_transaction()
{
/*
|------------------------------------------------------------
| Arrange
|------------------------------------------------------------
*/
$httpClient = m::spy('Payum\Core\HttpClientInterface');
$message = m::spy('Http\Message\MessageFactory');
$sdk = m::spy('PayumTW\Allpay\Bridge\Allpay\AllInOne');
$sdk->Action = [
'MerchantTradeNo' => '',
'TradeNo' => '',
'Action' => ActionType::C,
'TotalAmount' => 0,
];
$options = [
'MerchantID' => '2000132',
'HashKey' => '5294y06JbISpM5x9',
'HashIV' => 'v77hoKGq4kWxNNIS',
'sandbox' => false,
];
$params = [
'MerchantTradeNo' => '12345',
'TradeNo' => '12345',
'TotalAmount' => 0,
];
/*
|------------------------------------------------------------
| Act
|------------------------------------------------------------
*/
$api = new Api($options, $httpClient, $message, $sdk);
/*
|------------------------------------------------------------
| Assert
|------------------------------------------------------------
*/
$api->cancelTransaction($params);
$this->assertSame($options['HashKey'], $sdk->HashKey);
$this->assertSame($options['HashIV'], $sdk->HashIV);
$this->assertSame($options['MerchantID'], $sdk->MerchantID);
$this->assertSame($api->getApiEndpoint('DoAction'), $sdk->ServiceURL);
$this->assertSame($params['MerchantTradeNo'], $sdk->Action['MerchantTradeNo']);
$this->assertSame($params['TradeNo'], $sdk->Action['TradeNo']);
$this->assertSame($params['TotalAmount'], $sdk->Action['TotalAmount']);
$sdk->shouldHaveReceived('DoAction')->once();
}
public function test_refund_transaction()
{
/*
|------------------------------------------------------------
| Arrange
|------------------------------------------------------------
*/
$httpClient = m::spy('Payum\Core\HttpClientInterface');
$message = m::spy('Http\Message\MessageFactory');
$sdk = m::spy('PayumTW\Allpay\Bridge\Allpay\AllInOne');
$sdk->ChargeBack = [
'MerchantTradeNo' => '',
'TradeNo' => '',
'ChargeBackTotalAmount' => 0,
'Remark' => '',
];
$options = [
'MerchantID' => '2000132',
'HashKey' => '5294y06JbISpM5x9',
'HashIV' => 'v77hoKGq4kWxNNIS',
'sandbox' => false,
];
$params = [
'MerchantTradeNo' => '12345',
'TradeNo' => '12345',
'ChargeBackTotalAmount' => 0,
'Remark' => '',
];
/*
|------------------------------------------------------------
| Act
|------------------------------------------------------------
*/
$api = new Api($options, $httpClient, $message, $sdk);
/*
|------------------------------------------------------------
| Assert
|------------------------------------------------------------
*/
$api->refundTransaction($params);
$this->assertSame($options['HashKey'], $sdk->HashKey);
$this->assertSame($options['HashIV'], $sdk->HashIV);
$this->assertSame($options['MerchantID'], $sdk->MerchantID);
$this->assertSame($api->getApiEndpoint('AioChargeback'), $sdk->ServiceURL);
$this->assertSame($params['MerchantTradeNo'], $sdk->ChargeBack['MerchantTradeNo']);
$this->assertSame($params['TradeNo'], $sdk->ChargeBack['TradeNo']);
$this->assertSame($params['ChargeBackTotalAmount'], $sdk->ChargeBack['ChargeBackTotalAmount']);
$sdk->shouldHaveReceived('AioChargeback')->once();
}
public function test_get_transaction_data_when_response_from_request()
{
/*
|------------------------------------------------------------
| Arrange
|------------------------------------------------------------
*/
$httpClient = m::spy('Payum\Core\HttpClientInterface');
$message = m::spy('Http\Message\MessageFactory');
$sdk = m::spy('PayumTW\Allpay\Bridge\Allpay\AllInOne');
$sdk->ChargeBack = [
'MerchantTradeNo' => '',
'TradeNo' => '',
'ChargeBackTotalAmount' => 0,
'Remark' => '',
];
$options = [
'MerchantID' => '2000132',
'HashKey' => '5294y06JbISpM5x9',
'HashIV' => 'v77hoKGq4kWxNNIS',
'sandbox' => false,
];
$params = [
'response' => [
'MerchantID' => '2000132',
'MerchantTradeNo' => '57CBC66A39F82',
'PayAmt' => '340',
'PaymentDate' => '2016/09/04 15:03:08',
'PaymentType' => 'Credit_CreditCard',
'PaymentTypeChargeFee' => '3',
'RedeemAmt' => '0',
'RtnCode' => '1',
'RtnMsg' => 'Succeeded',
'SimulatePaid' => '0',
'TradeAmt' => '340',
'TradeDate' => '2016/09/04 14:59:13',
'TradeNo' => '1609041459136128',
'CheckMacValue' => '6812D213BF2C5B9377EBF101607BF2DF',
],
];
/*
|------------------------------------------------------------
| Act
|------------------------------------------------------------
*/
$api = new Api($options, $httpClient, $message, $sdk);
/*
|------------------------------------------------------------
| Assert
|------------------------------------------------------------
*/
$this->assertSame($params['response'], $api->getTransactionData($params));
$this->assertSame($options['HashKey'], $sdk->HashKey);
$this->assertSame($options['HashIV'], $sdk->HashIV);
$this->assertSame($options['MerchantID'], $sdk->MerchantID);
}
public function test_get_transaction_data_when_response_from_request_and_verify_hash_is_fail()
{
/*
|------------------------------------------------------------
| Arrange
|------------------------------------------------------------
*/
$httpClient = m::spy('Payum\Core\HttpClientInterface');
$message = m::spy('Http\Message\MessageFactory');
$sdk = m::spy('PayumTW\Allpay\Bridge\Allpay\AllInOne');
$sdk->ChargeBack = [
'MerchantTradeNo' => '',
'TradeNo' => '',
'ChargeBackTotalAmount' => 0,
'Remark' => '',
];
$options = [
'MerchantID' => '2000132',
'HashKey' => '5294y06JbISpM5x9',
'HashIV' => 'v77hoKGq4kWxNNIS',
'sandbox' => false,
];
$params = [
'response' => [
'MerchantID' => '2000132',
'MerchantTradeNo' => '57CBC66A39F82',
'PayAmt' => '340',
'PaymentDate' => '2016/09/04 15:03:08',
'PaymentType' => 'Credit_CreditCard',
'PaymentTypeChargeFee' => '3',
'RedeemAmt' => '0',
'RtnCode' => '1',
'RtnMsg' => 'Succeeded',
'SimulatePaid' => '0',
'TradeAmt' => '340',
'TradeDate' => '2016/09/04 14:59:13',
'TradeNo' => '1609041459136128',
'CheckMacValue' => '',
],
];
/*
|------------------------------------------------------------
| Act
|------------------------------------------------------------
*/
$sdk->shouldReceive('CheckOutFeedback')->andThrow('Exception');
$api = new Api($options, $httpClient, $message, $sdk);
/*
|------------------------------------------------------------
| Assert
|------------------------------------------------------------
*/
$this->assertSame([
'RtnCode' => '10400002',
], $api->getTransactionData($params));
$this->assertSame($options['HashKey'], $sdk->HashKey);
$this->assertSame($options['HashIV'], $sdk->HashIV);
$this->assertSame($options['MerchantID'], $sdk->MerchantID);
$sdk->shouldHaveReceived('CheckOutFeedback')->once();
}
public function test_get_transaction_data_when_response_from_query_info()
{
/*
|------------------------------------------------------------
| Arrange
|------------------------------------------------------------
*/
$httpClient = m::spy('Payum\Core\HttpClientInterface');
$message = m::spy('Http\Message\MessageFactory');
$sdk = m::spy('PayumTW\Allpay\Bridge\Allpay\AllInOne');
$sdk->Query = [
'MerchantTradeNo' => '',
'TimeStamp' => '',
];
$options = [
'MerchantID' => '2000132',
'HashKey' => '5294y06JbISpM5x9',
'HashIV' => 'v77hoKGq4kWxNNIS',
'sandbox' => false,
];
$params = [
'MerchantTradeNo' => '5832985816073',
];
/*
|------------------------------------------------------------
| Act
|------------------------------------------------------------
*/
$api = new Api($options, $httpClient, $message, $sdk);
/*
|------------------------------------------------------------
| Assert
|------------------------------------------------------------
*/
$api->getTransactionData($params);
$this->assertSame($options['HashKey'], $sdk->HashKey);
$this->assertSame($options['HashIV'], $sdk->HashIV);
$this->assertSame($options['MerchantID'], $sdk->MerchantID);
$this->assertSame($api->getApiEndpoint('QueryTradeInfo'), $sdk->ServiceURL);
$sdk->shouldHaveReceived('QueryTradeInfo')->once();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment