2016年3月2日 星期三

allpay 整合筆記


介接測試相關資訊





sample code :
====================================================================

<?php
//特殊字元置換
function _replaceChar($value)
{
$search_list = array('%2d', '%5f', '%2e', '%21', '%2a', '%28', '%29');
$replace_list = array('-', '_', '.', '!', '*', '(', ')');
$value = str_replace($search_list, $replace_list ,$value);
return $value;
}
//產生檢查碼
function _getMacValue($hash_key, $hash_iv, $form_array)
{
$encode_str = "HashKey=" . $hash_key;
foreach ($form_array as $key => $value)
{
$encode_str .= "&" . $key . "=" . $value;
}
$encode_str .= "&HashIV=" . $hash_iv;
$encode_str = strtolower(urlencode($encode_str));
$encode_str = _replaceChar($encode_str);
return md5($encode_str);
}
//仿自然排序法
function merchantSort($a,$b)
{
return strcasecmp($a, $b);
}
//------------------------------------------交易輸入參數------------------------------------------------------
//訂單編號
$trade_no = "StageTest".time();
//交易金額
$total_amt = "100";
//交易描述
$trade_desc = "交易測試(測試)";
//如果商品名稱有多筆,需在金流選擇頁一行一行顯示商品名稱的話,商品名稱請以井號分隔(#)
$item_name = "交易測試(測試)";
//交易返回頁面
$return_url = "http://www.allpay.com.tw/receive.php";
//交易通知網址
$client_back_url = "http://www.allpay.com.tw/receive.php";
//選擇預設付款方式
$choose_payment = "ALL";
//是否需要額外的付款資訊
$needExtraPaidInfo = "Y";
//Alipay 必要參數
$alipay_item_name = $item_name;
$alipay_item_counts = 1;
$alipay_item_price = $total_amt;
$alipay_email = 'stage_test@allpay.com.tw';
$alipay_phone_no = '0911222333';
$alipay_user_name = 'Stage Test';
/***************以下為測試環境資訊(若轉換為正式環境,請修改以下參數值)**********************/
//交易網址(測試環境)
$gateway_url = "http://payment-stage.allpay.com.tw/Cashier/AioCheckOut";
//商店代號
$merchant_id = "2000132";
//HashKey
$hash_key = "5294y06JbISpM5x9";
//HashIV
$hash_iv = "v77hoKGq4kWxNNIS";
/********************************************************************************************/
$form_array = array(
"MerchantID" => $merchant_id,
"MerchantTradeNo" => $trade_no,
"MerchantTradeDate" => date("Y/m/d H:i:s"),
"PaymentType" => "aio",
"TotalAmount" => $total_amt,
"TradeDesc" => $trade_desc,
"ItemName" => $item_name,
"ReturnURL" => $return_url,
"ChoosePayment" => $choose_payment,
"ClientBackURL" => $client_back_url,
"NeedExtraPaidInfo" => $needExtraPaidInfo,
# Alipay 必要參數
"AlipayItemName" => $alipay_item_name,
"AlipayItemCounts" => $alipay_item_counts,
"AlipayItemPrice" => $alipay_item_price,
"Email" => $alipay_email,
"PhoneNo" => $alipay_phone_no,
"UserName" => $alipay_user_name
);
# 調整ksort排序規則--依自然排序法(大小寫不敏感)
uksort($form_array, 'merchantSort');
# 取得 Mac Value
$form_array['CheckMacValue'] = _getMacValue($hash_key, $hash_iv, $form_array);
$html_code = '<form target="_blank" method=post action="' . $gateway_url . '">';
foreach ($form_array as $key => $val) {
$html_code .= "<input type='text' name='" . $key . "' value='" . $val . "'><BR>";
}
$html_code .= "<input class='button04' type='submit' value='送出'>";
$html_code .= "</form>";
echo $html_code;
?>

2016年2月29日 星期一

JavaScript Form Validation



HTML Form Example

<form name="myForm" action="demo_form.asp" onsubmit="return validateForm()" method="post">
Name: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>

function validateForm() {
    var x = document.forms["myForm"]["fname"].value;
    if (x == null || x == "") {
        alert("Name must be filled out");
        return false;
    }
}

Reference : http://www.w3schools.com/js/js_validation.asp

2016年2月26日 星期五

整合smilepay 的筆記 -> 獲得繳費紀錄





實例:
為了方便了解各參數的帶法,以下列實例是以網址列方式作範例,實際撰寫時不建議以此方式。
https://ssl.smse.com.tw/api/SPPayment.asp?Rvg2c=1&Dcvc=5240&Od_sob=123456&Amount=500&Pur_name=王大明&Tel_number=037376006&Mobile_number=0961238006&Address=苗栗市莊敬街95號&Email=service@smse.com.tw&Invoice_name=訊航科技股份有限公司&Invoice_num=80129529&Remark=備註&Roturl=http://www.test.com/test.asp&Pay_gdry=TW&Pay_zg=2
&Verify_key=19FF9E6EBB0D7CCC3C34AC8482AFFFA8

解析XML資料
實例:
<SmilePay>
   <Status>1</Status>
   <Desc>Succeeded</Desc>

   <
Rvg2c>1</Rvg2c>
   <Dcvc>
5240</Dcvc>
   <SmilePayNO>00E0001000002913643</SmilePayNO>
   <Data_id><Data_id/>
   <Amount>500</Amount>
   <AtmBankNo>004</AtmBankNo>
   <AtmNo>31905792913643</AtmNo>
</SmilePay>

Xml Tag說明 (請注意大小寫)
Tag說明長度備註
Status取號狀態5參閱狀態表
Desc描述 參閱狀態表內容欄
Dcvc商家代號  
SmilePayNOSmilePay追蹤碼50查詢時使用
Data_id商家訂單編號  
Amount金額  
PayEndDate繳款截止期限 2013/10/8 23:59:00限超商帳單及 ibon、LifeET、
FaniPort 、虛擬帳號有效
AtmBankNo銀行代號3Pay_zg =2秀出
AtmNo虛擬帳號14Pay_zg =2秀出
Barcode1條碼19Pay_zg =3秀出
Barcode2條碼216Pay_zg =3秀出
Barcode3條碼315Pay_zg =3秀出
IbonNoIbon繳費代碼20Pay_zg =4秀出
FamiNO全家繳費代碼14Pay_zg =6秀出
Status 狀態表說明 (請注意大小寫)
狀態碼內容備註
1Succeeded取號成功
-2001Dcvc or Rvg2c Error無參數碼及商家代號錯誤
-2002Verify_key is Error檢查碼錯誤
-2003Pay_zg Error無付款模式
-2004Deadline_date or Deadline_time Error日期格式錯誤
-2005Amount is empty無交易金額
-2006711 C2C Item error7-11超商取貨付款相關參數錯誤
-3001Dcvc Fail無此商家代號
-3002The Dcvc number is offline商家使用期限已到期
-3003Can not use this Pay_zg service(Store)商家未開啟此付款方式
-3004Amount Fail超過付款模式設定金額
-3005Can not use this Pay_zg service(SmilePay)SmielPay關閉此付款方式
-4000Atm Payment Fail此銀行不提供ATM帳號
-4001Get ibon,famiport Fail超商代碼取號錯誤
-4002Smse service Fail簡訊服務忙錄中
-4003Ibon service Failibon 服務忙錄中
-4004Famiport service FillFami 服務忙錄中
-4005Smse balance Fail簡訊餘額不足
-5000Smse service FailSmse 執行錯誤
-5001Ibon service Failibon 執行錯誤
-5002Famiport service Failfami 執行錯誤
-5003711 C2C service Fail7-11超商取貨付款 執行錯誤
-6001Payment errorPayment error

以下是範例

<?php
include("../config.inc.php");
include($_SiteGLOBAL['Include_ROOT']."/db_connect.inc.php");

//  先從database 查詢

//step 0. SELECT FROM 方案 查出Amount金額和Od_sob方案名稱
$sql="SELECT id,name,price,isDel,case_type,total_cost,cost_per_unit,game_type FROM {$_SiteGLOBAL['dbtable']}.buyCase WHERE id='{$_POST["buyCase_id"]}' LIMIT 1";
$result=db_query($connect,$sql);
list($buyCaseID,$buyCaseName,$buyCasePrice,$isDel,$case_type,$total_cost,$cost_per_unit,$game_type)=db_fetch_row($result);
if($isDel==1 || empty($buyCaseID) || empty($_SESSION['user_id'])){
echo <<<EOF
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script><!--
top.location.replace("/order.php");
//--></script>
EOF;
exit;
}

$_REQUEST['Dcvc']="3582";   // 商家代號
$_REQUEST['Rvg2c']="1";       //
$_REQUEST['Verify_key']="516E4C2B00A6B8F5DBB1817ADDA8D54C"; // 檢查碼

$_REQUEST['Pay_gdry']="TW"; //
$_REQUEST['Roturl']="http://{$_SiteGLOBAL["DOMAIN"]}/pay/Roturl.php"; // 交易完成後要回送的位置
$_REQUEST['Data_id']=substr($_SiteGLOBAL["DOMAIN"],0,2).time(); // 訂單號碼

$_REQUEST['Pur_name']=$_POST["Pur_name"]; // 購買人姓名
$_REQUEST['Mobile_number']=$_POST["Mobile_number"]; // 聯絡電話
$_REQUEST['Email']=$_POST["Email"]; // 電子信箱
$_REQUEST['Pay_zg']=$_POST["Pay_zg"]; // 收費模式
if($_REQUEST['Pay_zg']=="44"){
$Pay_zg=4;
}else{
$Pay_zg=$_REQUEST['Pay_zg'];
}

$_REQUEST['Od_sob']=$buyCaseName; // 消費項目
$_REQUEST['Amount']=$buyCasePrice;  // 應付費金額
$_REQUEST['Remark']="case_type=".$case_type."&total_cost=".$total_cost."&cost_per_unit=".$cost_per_unit."&game_type=".$game_type; // 備註

//讀取傳送內容
$postData = "Dcvc=".$_REQUEST['Dcvc'];
$postData = $postData . "&Rvg2c=".$_REQUEST['Rvg2c'];
$postData = $postData . "&Verify_key=".$_REQUEST['Verify_key'];
$postData = $postData . "&Od_sob=".$_REQUEST['Od_sob'];
$postData = $postData . "&Pay_zg=".$Pay_zg;
$postData = $postData . "&Data_id=".$_REQUEST['Data_id'];
$postData = $postData . "&Deadline_date=".$_REQUEST['Deadline_date'];//非必填
$postData = $postData . "&Deadline_time=".$_REQUEST['Deadline_time'];//非必填
$postData = $postData . "&Amount=".$_REQUEST['Amount'];
$postData = $postData . "&Pur_name=".$_REQUEST['Pur_name'];
$postData = $postData . "&Tel_number=".$_REQUEST['Tel_number'];//非必填
$postData = $postData . "&Mobile_number=".$_REQUEST['Mobile_number'];
$postData = $postData . "&Address=".$_REQUEST['Address'];//非必填
$postData = $postData . "&Email=".$_REQUEST['Email'];
$postData = $postData . "&Remark=".$_REQUEST['Remark'];
$postData = $postData . "&Invoice_name=".$_REQUEST['Invoice_name'];//非必填
$postData = $postData . "&Invoice_num=".$_REQUEST['Invoice_num'];//非必填
$postData = $postData . "&Roturl=".$_REQUEST['Roturl'];

//執行取號動作
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://ssl.smse.com.tw/api/SPPayment.asp");
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$string = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($string);

if($xml->Status=="1"){
//取號成功
$sql="INSERT INTO {$_SiteGLOBAL['dbtable']}.smilepay_record(Data_id, Dcvc, Rvg2c, Verify_key, Od_sob, Pay_zg, buyCase_id, Amount, user_id, Pur_name, Mobile_number, Email, Roturl, Pay_gdry, pay_info_json,Remark,points,createtime) VALUES ('".db_escape($_REQUEST['Data_id'])."', '".db_escape($_REQUEST['Dcvc'])."', '".db_escape($_REQUEST['Rvg2c'])."', '".db_escape($_REQUEST['Verify_key'])."', '".db_escape($_REQUEST['Od_sob'])."', '".db_escape($_REQUEST['Pay_zg'])."', '".db_escape($_REQUEST['buyCase_id'])."', '".db_escape($_REQUEST['Amount'])."', '".db_escape($_SESSION['user_id'])."', '".db_escape($_REQUEST['Pur_name'])."', '".db_escape($_REQUEST['Mobile_number'])."', '".db_escape($_REQUEST['Email'])."', '".db_escape($_REQUEST['Roturl'])."', 'TW', '".db_escape(json_encode($xml))."', '".db_escape($_REQUEST['Remark'])."','{$total_cost}','".date("Y-m-d H:i:s")."')";
db_query($connect,$sql);
echo <<<EOF
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script><!--
top.location.replace("/order_success.php?o={$_REQUEST['Data_id']}");
//--></script>
EOF;
exit;
}else{//其他錯誤
$error="";
switch($xml->Status){
case "-2001":$error="無參數碼及商家代號錯誤";break;
case "-2002":$error="檢查碼錯誤";break;
case "-2003":$error="無付款模式";break;
case "-2004":$error="日期格式錯誤";break;
case "-2005":$error="無交易金額";break;
case "-3001":$error="無此商家代號";break;
case "-3002":$error="商家使用期限已到期";break;
case "-3003":$error="無提供此付款方式";break;
case "-3004":$error="超過付款模式設定金額";break;
case "-4000":$error="此銀行不提供 ATM 帳號";break;
case "-4001":$error="超商代碼取號錯誤";break;
case "-4002":$error="簡訊服務忙錄中";break;
case "-4003":$error="ibon 服務忙錄中";break;
case "-4004":$error="Fami 服務忙錄中";break;
case "-4005":$error="簡訊餘額不足";break;
case "-5000":$error="Smse 執行錯誤";break;
case "-5001":$error="ibon 執行錯誤";break;
case "-5002":$error="fami 執行錯誤";break;
case "-6001":$error="Payment error";break;
default:$error="金流正在維護中,請稍後再試。";break;
}
echo <<<EOF
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script><!--
alert("{$error}");
top.location.replace("/order.php");
//--></script>
EOF;
exit;
}
?>



https://ssl.smse.com.tw/pay_gr/pay_help_paydc_Background_api.ASP