Skip to content

Commit

Permalink
增加aftee邏輯及範例
Browse files Browse the repository at this point in the history
  • Loading branch information
payuni committed Apr 20, 2023
1 parent d65bee5 commit 73c8331
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 40 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ $result = $payuniApi->ResultProcess($requestData);
* 信用卡Token(約定) => credit_bind_query
* 信用卡Token取消(約定/記憶卡號) => credit_bind_cancel
* 愛金卡退款(ICASH) => trade_refund_icash
* 後支付退款(AFTEE) => trade_refund_aftee
* 其餘請參考[範例](https://github.com/payuni/PHP_SDK/tree/main/examples)

* 原生php
Expand Down
15 changes: 15 additions & 0 deletions examples/trade/Trade.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,19 @@ public function tradeRefundIcash(){
];
$result = $this->payuniApi->UniversalTrade($encryptInfo, 'trade_refund_icash');
}
/**
* trade refund aftee sample code
* @author presco.
* @datetime 2023/04/20
*
*/
public function tradeRefundAftee(){
$encryptInfo = [
'MerID' => 'abc',
'TradeNo' => '1665472985627866043',
'TradeAmt' => 100,
'Timestamp' => time()
];
$result = $this->payuniApi->UniversalTrade($encryptInfo, 'trade_refund_aftee');
}
}
87 changes: 47 additions & 40 deletions src/PayuniApi.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright 2022 PRESCO. All rights reserved.
Expand All @@ -14,11 +15,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Payuni\Sdk;

use Exception;

class PayuniApi
{
// public $encryptInfo, $merKey, $merIV, $apiUrl, $parameter, $curlUrl;
public function __construct(string $key, string $iv, string $type = '')
{
$this->encryptInfo = '';
Expand All @@ -43,7 +47,8 @@ public function __construct(string $key, string $iv, string $type = '')
* @author Yifan
* @ dateTime 2022-08-23
*/
public function UniversalTrade(array $encryptInfo, string $tradeType) {
public function UniversalTrade(array $encryptInfo, string $tradeType)
{
$this->encryptInfo = $encryptInfo;
$contrast = [
'upp' => 'upp',
Expand All @@ -56,11 +61,12 @@ public function UniversalTrade(array $encryptInfo, string $tradeType) {
'credit_bind_query' => 'credit_bind/query',
'credit_bind_cancel' => 'credit_bind/cancel',
'trade_refund_icash' => 'trade/common/refund/icash',
'trade_refund_aftee' => 'trade/common/refund/aftee',
];
$checkArr = $this->CheckParams();
if ( $checkArr['success'] ) {
if ($checkArr['success']) {
try {
switch ( $tradeType ) {
switch ($tradeType) {
case 'upp': // 交易建立 整合式支付頁
case 'atm': // 交易建立 虛擬帳號幕後
case 'cvs': // 交易建立 超商代碼幕後
Expand Down Expand Up @@ -112,6 +118,7 @@ public function UniversalTrade(array $encryptInfo, string $tradeType) {
}
break;
case 'trade_refund_icash': // 愛金卡退款(ICASH)
case 'trade_refund_aftee': // 後支付退款(AFTEE)
if ($this->encryptInfo['TradeNo'] == null || $this->encryptInfo['TradeNo'] == '') {
throw new Exception('TradeNo is not setting');
}
Expand All @@ -130,17 +137,14 @@ public function UniversalTrade(array $encryptInfo, string $tradeType) {
if ($tradeType == 'upp') {
$this->HtmlApi();
exit;
}
else {
} else {
$result = $this->CurlApi();
return $this->ResultProcess($result);
}
}
catch ( Exception $e ) {
} catch (Exception $e) {
return ['success' => false, 'message' => $e->getMessage()];
}
}
else {
} else {
return $checkArr;
}
}
Expand All @@ -149,38 +153,35 @@ public function UniversalTrade(array $encryptInfo, string $tradeType) {
* @ author Yifan
* @ dateTime 2022-08-26
*/
public function ResultProcess($result) {
public function ResultProcess($result)
{
try {
if (is_array($result)) {
$resultArr = $result;
}
else {
} else {
$resultArr = json_decode($result, true);
if (!is_array($resultArr)){
if (!is_array($resultArr)) {
throw new Exception('Result must be an array');
}
}
if ("ERROR" == $resultArr['Status'] ) {
if ("ERROR" == $resultArr['Status']) {
return ['success' => true, 'message' => $resultArr];
}
if (isset($resultArr['EncryptInfo'])){
if (isset($resultArr['HashInfo'])){
if (isset($resultArr['EncryptInfo'])) {
if (isset($resultArr['HashInfo'])) {
$chkHash = $this->HashInfo($resultArr['EncryptInfo']);
if ( $chkHash != $resultArr['HashInfo'] ) {
if ($chkHash != $resultArr['HashInfo']) {
throw new Exception('Hash mismatch');
}
$resultArr['EncryptInfo'] = $this->Decrypt($resultArr['EncryptInfo']);
return ['success' => true, 'message' => $resultArr];
}
else {
} else {
throw new Exception('missing HashInfo');
}
}
else {
} else {
throw new Exception('missing EncryptInfo');
}
}
catch ( Exception $e ) {
} catch (Exception $e) {
return ['success' => false, 'message' => $e->getMessage()];
}
}
Expand All @@ -189,7 +190,8 @@ public function ResultProcess($result) {
* @ author Yifan
* @ dateTime 2022-08-23
*/
private function CheckParams() {
private function CheckParams()
{
try {
if ($this->encryptInfo['MerID'] == null || $this->encryptInfo['MerID'] == '') {
throw new Exception('MerID is not setting');
Expand All @@ -198,8 +200,7 @@ private function CheckParams() {
throw new Exception('Timestamp is not setting');
}
return ['success' => true, 'message' => 'params is set correctly'];
}
catch (Exception $e) {
} catch (Exception $e) {
return ['success' => false, 'message' => $e->getMessage()];
}
}
Expand All @@ -208,9 +209,10 @@ private function CheckParams() {
* @author Yifan
* @ dateTime 2022-08-23
*/
private function SetParams(string $type = '') {
private function SetParams(string $type = '')
{
$isPlatForm = '';
if(!empty($this->encryptInfo['IsPlatForm'])){
if (!empty($this->encryptInfo['IsPlatForm'])) {
$isPlatForm = $this->encryptInfo['IsPlatForm'];
unset($this->encryptInfo['IsPlatForm']);
}
Expand All @@ -225,13 +227,14 @@ private function SetParams(string $type = '') {
* @ author Yifan
* @ dateTime 2022-08-25
*/
private function HtmlApi() {
private function HtmlApi()
{
$htmlprint = "<html><body onload='document.getElementById(\"upp\").submit();'>";
$htmlprint .= "<form action='".$this->curlUrl."' method='post' id='upp'>";
$htmlprint .= "<input name='MerID' type='hidden' value='".$this->parameter['MerID']."' />";
$htmlprint .= "<input name='Version' type='hidden' value='".$this->parameter['Version']."' />";
$htmlprint .= "<input name='EncryptInfo' type='hidden' value='".$this->parameter['EncryptInfo']."' />";
$htmlprint .= "<input name='HashInfo' type='hidden' value='".$this->parameter['HashInfo']."' />";
$htmlprint .= "<form action='" . $this->curlUrl . "' method='post' id='upp'>";
$htmlprint .= "<input name='MerID' type='hidden' value='" . $this->parameter['MerID'] . "' />";
$htmlprint .= "<input name='Version' type='hidden' value='" . $this->parameter['Version'] . "' />";
$htmlprint .= "<input name='EncryptInfo' type='hidden' value='" . $this->parameter['EncryptInfo'] . "' />";
$htmlprint .= "<input name='HashInfo' type='hidden' value='" . $this->parameter['HashInfo'] . "' />";
$htmlprint .= "</form></body></html>";
echo $htmlprint;
}
Expand All @@ -240,7 +243,8 @@ private function HtmlApi() {
* @ author Yifan
* @ dateTime 2022-08-23
*/
private function CurlApi() {
private function CurlApi()
{
$curlOptions = array(
CURLOPT_URL => $this->curlUrl,
CURLOPT_HEADER => false,
Expand All @@ -263,15 +267,17 @@ private function CurlApi() {
* 加密
*
*/
private function Encrypt() {
private function Encrypt()
{
$tag = '';
$encrypted = openssl_encrypt(http_build_query($this->encryptInfo), 'aes-256-gcm', trim($this->merKey), 0, trim($this->merIV), $tag);
return trim(bin2hex($encrypted . ':::' . base64_encode($tag)));
}
/**
* 解密
*/
private function Decrypt(string $encryptStr = '') {
private function Decrypt(string $encryptStr = '')
{
list($encryptData, $tag) = explode(':::', hex2bin($encryptStr), 2);
$encryptInfo = openssl_decrypt($encryptData, 'aes-256-gcm', trim($this->merKey), 0, trim($this->merIV), base64_decode($tag));
parse_str($encryptInfo, $encryptArr);
Expand All @@ -280,7 +286,8 @@ private function Decrypt(string $encryptStr = '') {
/**
* hash
*/
private function HashInfo(string $encryptStr = '') {
return strtoupper(hash('sha256', $this->merKey.$encryptStr.$this->merIV));
private function HashInfo(string $encryptStr = '')
{
return strtoupper(hash('sha256', $this->merKey . $encryptStr . $this->merIV));
}
}
}

0 comments on commit 73c8331

Please sign in to comment.