Hoyo 提供原廠都沒有的測試工具
—
下載綠界串接文件
需要下載 「全方位金流 API 技術文件」以及「PHP SDK」
撰寫文件時為 2018-6-29 , API 文件版本 5.1.17
—
流程
雖然金流 API 文件第 2 章有交易流程說明,可是第一次使用看懂的機率是極低的,所以 Hoyo 另外整理為
- 送出一個訂單表單送到「介接路徑」
- 準備接收綠界付款結果的資訊
—
訂單表單
網頁表單 – f.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width"/> <title></title> <style> label{ display: block; } </style> </head> <body> <form id="idFormAioCheckOut" method="post" action="Ecpay.php"> <label>動作 (Action):<input type="text" name="ServiceURL" value="https://payment-stage.ecpay.com.tw/Cashier/AioCheckOut/V5" class="form-control"/></label> <label>編號 (MerchantTradeNo): <input type="text" name="MerchantTradeNo" value="oikidA0000001" class="form-control"/> 不可重複使用。英數字大小寫混合 </label> <label class="col-xs-12">時間 (MerchantTradeDate): <input type="text" name="MerchantTradeDate" value="2017/06/30 00:00:00" class="form-control"/> yyyy/MM/dd HH:mm:ss </label> <label class="col-xs-12">類型 (PaymentType): <input type="text" name="PaymentType" value="aio" class="form-control"/> aio </label> <label class="col-xs-12">金額 (TotalAmount): <input type="text" name="TotalAmount" value="29999" class="form-control"/> 請帶整數,不可有小數點 僅限新台幣 金額不可為 0 元 CVS&BARCODE 最低限制為 30 元,最高限制為 30,000 元 </label> <label class="col-xs-12">描述 (TradeDesc): <input type="text" name="TradeDesc" value="Desc" class="form-control"/> </label> <label class="col-xs-12">名稱 (ItemName): <input type="text" name="ItemName" value="A#B" class="form-control"/> 商品名稱以符號 # 分 </label> <label class="col-xs-12">回傳網址 (ReturnURL): <input type="text" name="ReturnURL" value="http://tn.sly-ha.com.tw/demo/hoyo/ECPay.php" class="form-control"/> </label> <label class="col-xs-12">付款方式 (ChoosePayment): <input type="text" name="ChoosePayment" value="ALL"/> Credit:信用卡及 AndroidPay AndroidPay: AndroidPay WebATM:網路 ATM ATM:自動櫃員機 CVS:超商代碼 BARCODE:超商條碼 ALL:不指定 </label> <input type="hidden" name="MerchantID" value="2000132" /> <input type="hidden" name="HashKey" value="5294y06JbISpM5x9" /> <input type="hidden" name="HashIV" value="v77hoKGq4kWxNNIS" /> <button type="submit" class="btn btn-default">綠界線上支付</button> </form> </body> </html> |
- MerchantTradeNo : 訂單編號,一個編號只能用一次,所以針對實際情況不能直接將網站的訂單編號送出,必須使用一個對應的變動編號,這個問題後面再討論
- ReturnURL : 必須要可以對外訪問的網址,所以沒有自己的網站是不能接線上支付 (沒有意義)
- MerchantID : 測試的商家 value 都要根據 API 文件設定
- HashKey 同上
- HashIV 同上
送給 PHP 計算 CheckMacValue – Ecpay.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?php require_once 'ECPay.Payment.Integration.php'; $obj = new \ECPay_AllInOne(); //服務參數 $obj->ServiceURL = $_POST['ServiceURL']; $obj->HashKey = $_POST['HashKey']; $obj->HashIV = $_POST['HashIV']; $obj->MerchantID = $_POST['MerchantID']; // $obj->Send['MerchantTradeNo'] = $_POST['MerchantTradeNo']; $obj->Send['MerchantTradeDate'] = $_POST['MerchantTradeDate']; $obj->Send['PaymentType'] = $_POST['PaymentType']; $obj->Send['TotalAmount'] = (int)$_POST['TotalAmount']; $obj->Send['TradeDesc'] = $_POST['TradeDesc']; $obj->Send['ReturnURL'] = $_POST['ReturnURL']; $obj->Send['ChoosePayment'] = $_POST['ChoosePayment']; $obj->Send['CreditInstallment'] = $_POST['CreditInstallment']; //訂單的商品資料 array_push($obj->Send['Items'], array( 'Name' => 'aaa', 'Price' => 100, 'Currency' => "元", 'Quantity' => (int)"1" ) ); //產生訂單(auto submit至ECPay) //$obj->CheckOut(); $Response = (string)$obj->CheckOutString(); echo $Response; // 自動將表單送出 echo '<script>document.getElementById("__ecpayForm").submit();</script>'; |
- PHP 的作用只有計算 CheckMacValue 這個值之後附加到表單再送到綠界
- 送出之後的所有錯誤綠界都會清楚的回應
—
接收付款結果資訊
payEcpay.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<?php require_once 'ECPay.Payment.Integration.php'; // 將 post 資料轉成字串 儲存 SaveData $String = print_r( $_POST, true ); file_put_contents( '/tmp/ECPay.txt', $String, FILE_APPEND ); define( 'ECPay_MerchantID', '2000132' ); define( 'ECPay_HashKey', '5294y06JbISpM5x9' ); define( 'ECPay_HashIV', 'v77hoKGq4kWxNNIS' ); // 重新整理回傳參數。 $arParameters = $_POST; foreach ($arParameters as $keys => $value) { if ($keys != 'CheckMacValue') { if ($keys == 'PaymentType') { $value = str_replace('_CVS', '', $value); $value = str_replace('_BARCODE', '', $value); $value = str_replace('_CreditCard', '', $value); } if ($keys == 'PeriodType') { $value = str_replace('Y', 'Year', $value); $value = str_replace('M', 'Month', $value); $value = str_replace('D', 'Day', $value); } $arFeedback[$keys] = $value; } } // 計算出 CheckMacValue $CheckMacValue = ECPay_CheckMacValue::generate( $arParameters, ECPay_HashKey, ECPay_HashIV ); // 必須要支付成功並且驗證碼正確 if ( $_POST['RtnCode'] =='1' && $CheckMacValue == $_POST['CheckMacValue'] ){ // // 要處理的程式放在這裡,例如將線上服務啟用、更新訂單資料庫付款資訊等 // } // 接收到資訊回應綠界 echo '1|OK'; |
- 根據回傳格式說明,至少要成功支付以及驗證碼正確才進行後續處理,否則可能是造假回傳資訊
—
模擬綠界回傳表單
可以自己設計表單模擬綠界回傳送出資訊,方便接收除錯
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width"/> <title></title> </head> <body> <form action="payEcpay.php" method="post"> <input type="text" name="CheckMacValue" value="82843FAB05F04943B6777A1502D2BDCF"/> <input type="text" name="CustomField1" value=""/> <input type="text" name="CustomField2" value=""/> <input type="text" name="CustomField3" value=""/> <input type="text" name="CustomField4" value=""/> <input type="text" name="MerchantID" value="2000132"/> <input type="text" name="MerchantTradeNo" value="ABC1530014538"/> <input type="text" name="PaymentDate" value="2018/06/26 20:05:00"/> <input type="text" name="PaymentType" value="Credit_CreditCard"/> <input type="text" name="PaymentTypeChargeFee" value="0"/> <input type="text" name="RtnCode" value="1"/> <input type="text" name="RtnMsg" value="paid"/> <input type="text" name="SimulatePaid" value="0"/> <input type="text" name="StoreID" value=""/> <input type="text" name="TradeAmt" value="2600"/> <input type="text" name="TradeDate" value="2018/06/26 20:02:19"/> <input type="text" name="TradeNo" value="1806262002197261"/> <button type="submit">s</button> </form> </body> </html> |
—
綠界回傳資訊
使用 print_r($_POST); 把回傳資訊展開大概就是長這樣
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
Array ( [MerchantID] => 2000132 [MerchantTradeNo] => ABC1486720783 [PayAmt] => 1000 [PaymentDate] => 2017/02/10 18:00:29 [PaymentType] => Credit_CreditCard [PaymentTypeChargeFee] => 50 [RedeemAmt] => 0 [RtnCode] => 1 [RtnMsg] => 交易成功 [SimulatePaid] => 0 [TradeAmt] => 1000 [TradeDate] => 2017/02/10 17:59:49 [TradeNo] => 1702101759496095 [CheckMacValue] => 35409F1AAF0509FC055BAE340899F6BBB37AE303D49B3C00CCF264A0645E5DFD ) |
—
17,502 total views, 2 views today