Skip to content

Commit

Permalink
add test and fix big
Browse files Browse the repository at this point in the history
  • Loading branch information
Recca Tsai committed Sep 4, 2016
1 parent 9a6ecf8 commit f222ff9
Show file tree
Hide file tree
Showing 10 changed files with 676 additions and 79 deletions.
15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{
"private": true,
"scripts": {
"prod": "gulp --production",
"dev": "gulp watch"
},
"devDependencies": {
"base-64": "^0.1.0",
"bootstrap-sass": "^3.3.6",
"gulp": "^3.9.1",
"laravel-elixir-browserify-official": "^0.1.2",
"laravel-elixir-browsersync-official": "^1.0.0",
"laravel-elixir": "^6.0.0-9",
"pako": "^1.0.1"
}
"gulp": "^3.9.1",
"laravel-elixir": "^6.0.0-10"
}
}
14 changes: 11 additions & 3 deletions src/Action/CaptureAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,20 @@ public function execute($request)

$httpRequest = new GetHttpRequest();
$this->gateway->execute($httpRequest);
if (isset($httpRequest->query['DATA']) === true) {
$model->replace($this->api->parseResult($httpRequest->query['DATA']));

if (isset($httpRequest->request['DATA']) === true) {
$model->replace($this->api->parseResult($httpRequest->request));
} else {
$token = $request->getToken();
$targetUrl = $token->getTargetUrl();

if (empty($model['U']) === true) {
$model['U'] = $targetUrl;
}

throw new HttpPostRedirect(
$this->api->getApiEndpoint(),
$this->api->prepare($model->toUnsafeArray(), $request)
$this->api->preparePayment($model->toUnsafeArray())
);
}
}
Expand Down
123 changes: 73 additions & 50 deletions src/Api.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace PayumTw\Esunbank;
namespace PayumTW\Esunbank;

use Detection\MobileDetect;
use Http\Message\MessageFactory;
use Payum\Core\Exception\Http\HttpException;
use Payum\Core\HttpClientInterface;

class Api
Expand Down Expand Up @@ -97,26 +97,6 @@ public function __construct(array $options, HttpClientInterface $client, Message
$this->messageFactory = $messageFactory;
}

/**
* @param array $fields
*
* @return array
*/
protected function doRequest($method, array $fields)
{
$headers = [];

$request = $this->messageFactory->createRequest($method, $this->getApiEndpoint(), $headers, http_build_query($fields));

$response = $this->client->send($request);

if (false == ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300)) {
throw HttpException::factory($request, $response);
}

return $response;
}

/**
* getApiEndpoint.
*
Expand All @@ -125,11 +105,11 @@ protected function doRequest($method, array $fields)
public function getApiEndpoint()
{
if ($this->options['sandbox'] === false) {
return $this->options['desktop'] === true ?
return $this->isMobile() === false ?
'https://acq.esunbank.com.tw/ACQTrans/esuncard/txnf014s' :
'https://acq.esunbank.com.tw/ACQTrans/esuncard/txnf014m';
} else {
return $this->options['desktop'] === true ?
return $this->isMobile() === false ?
'https://acqtest.esunbank.com.tw/ACQTrans/esuncard/txnf014s' :
'https://acqtest.esunbank.com.tw/ACQTrans/esuncard/txnf014m';
}
Expand All @@ -139,71 +119,98 @@ public function getApiEndpoint()
* prepare.
*
* @param array $params
* @param mixed $request
*
* @return array
*/
public function prepare(array $params, $request)
public function preparePayment(array $params)
{
$supportedParams = [
// 特店代碼
'MID' => $this->options['MID'],
// 終端機代號, EC000001(一般交易) EC000002(分期)
'TID' => 'EC000001',
// 訂單編號, 由特約商店產生,不可重複,不可 包含【_】字元,英數限用大寫
'ONO' => '',
// 交易金額, 台幣(901)
'TA' => '',
// 回覆位址, 'https://acqtest.esunbank.com.tw/ACQTrans/test/print.jsp',
'U' => $this->getRedirectUrl($request),
// 分期代碼, 三期:0100103 六期:0100106 正式環境參數由業務經辦提供
'IC' => '',
'U' => 'https://acqtest.esunbank.com.tw/ACQTrans/test/print.jsp',
// 特店代碼
'MID' => $this->options['MID'],
// 銀行紅利折抵, Y:使用銀行紅利交易。 N:不使用銀行紅利交易。
'BPF' => 'N',
// 分期代碼, 三期:0100103 六期:0100106 正式環境參數由業務經辦提供
'IC' => '',
// 交易金額, 台幣(901)
'TA' => '',
// 終端機代號, EC000001(一般交易) EC000002(分期)
'TID' => 'EC000001',
];

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

$params = json_encode($params);
if (empty($params['IC']) === true) {
unset($params['IC']);
}

$params['BPF'] = strtoupper($params['BPF']);
if ($params['BPF'] === 'N') {
unset($params['BPF']);
}

return $this->prepareRequestData($params);
}

protected function prepareRequestData($params, $option = JSON_UNESCAPED_SLASHES)
{
return [
'data' => $params,
'mac' => hash('sha256', $params.$this->options['M']),
'data' => json_encode($params, $option),
'mac' => $this->calculateHash($params, $option),
'ksn' => 1,
];
}

/**
* getRedirectUrl.
*
* @param mixed $request
* @param array $params
*
* @return string
*/
public function getRedirectUrl($request)
public function calculateHash($params, $option = JSON_UNESCAPED_SLASHES)
{
if (is_array($params) === true) {
$params = json_encode($params, $option);
}

return hash('sha256', $params.$this->options['M']);
}

protected function verifyHash($params)
{
$scheme = parse_url($request->getToken()->getTargetUrl());
return true;

$result = false;
if ($params['MACD'] === $this->calculateHash($params['DATA'])) {
$result = true;
}

return sprintf('%s://%s%s', $scheme['scheme'], $scheme['host'], $scheme['path']);
return $result;
}

/**
* parseResult.
*
* @param mixed $result
* @param mixed $params
*
* @return array
*/
public function parseResult($result)
public function parseResult($params)
{
$data = [];
parse_str(str_replace(',', '&', $result), $data);
$data['statusReason'] = $this->getStatusReason($data['RC']);
$result = [];
parse_str(str_replace(',', '&', $params['DATA']), $result);
$result = array_merge($params, $result);
if ($this->verifyHash($params) === false) {
$result['RC'] = 'GF';
}
$result['statusReason'] = $this->getStatusReason($result['RC']);

return $data;
return $result;
}

/**
Expand All @@ -222,4 +229,20 @@ public function getStatusReason($code)

return $statusReason;
}

/**
* isMobile.
*
* @return bool
*/
protected function isMobile()
{
if (isset($this->options['mobile']) === true && is_null($this->options['mobile']) === false) {
return $this->options['mobile'];
}

$detect = new MobileDetect();

return ($detect->isMobile() === false && $detect->isTablet() === false) ? false : true;
}
}
4 changes: 2 additions & 2 deletions src/ApiV1.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

namespace PayumTw\Esunbank;
namespace PayumTW\Esunbank;

use Http\Message\MessageFactory;
use Payum\Core\Exception\Http\HttpException;
use Payum\Core\HttpClientInterface;

class Api
class ApiV1
{
/**
* @var HttpClientInterface
Expand Down
19 changes: 3 additions & 16 deletions src/EsunbankGatewayFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace PayumTW\Esunbank;

use Detection\MobileDetect;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\GatewayFactory;
use PayumTW\Esunbank\Action\CaptureAction;
Expand All @@ -26,9 +25,9 @@ protected function populateConfig(ArrayObject $config)

if (false == $config['payum.api']) {
$config['payum.default_options'] = [
'MID' => '',
'M' => '',
'desktop' => $this->isDesktop(),
'MID' => '8089000016',
'M' => 'WEGSC0Q7BAJGTQYL8BV8KRQRZXH6VK0B',
'mobile' => null,
'sandbox' => true,
];

Expand All @@ -42,16 +41,4 @@ protected function populateConfig(ArrayObject $config)
};
}
}

/**
* isDesktop.
*
* @return bool [description]
*/
protected function isDesktop()
{
$detect = new MobileDetect();

return $detect->isMobile() === false && $detect->isTablet() === false;
}
}
Loading

0 comments on commit f222ff9

Please sign in to comment.