前言:
很多接案的人,都會碰到需要接金流的時候。而歐付寶是個台灣的金流平台。
這邊記錄下,串接的心得。我用的語言是Java, 採liferay這個portal平台,不過這份教學當然適合servlet.
不過官方技術文件,太規格導向了。應該要方便developer快速開發才是 很多資訊還是要向官方問才知道。
流程 :
- 假設有一個網購網站,有使用者下了單之後,採用信用卡付款。此時照官方使用範例
會把所有資訊弄成html的語法(html hidden fields),再加密送出去。
eg.
<input type="hidden" name="Language" value="English">
如果不懂,覺得太抽象可以用 http://dev.lovewed.tw/allpay/ 這個網站來測試整個流程(歐付寶有提供測試平台,測試信用卡 卡號)。
了解整個流程後,我們可以確定歐付寶正常無誤就可以開始串寫程式了。
- payment.jsp, 這支是負責user填完資料驗證後,且在我方server有紀錄後,送給轉到歐付寶付款用的。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="AllPay.Payment.Integration.*" %>
<%@ page import="java.util.Hashtable" %>
<%@ page import="java.util.Set" %>
<%@ page import="java.util.TreeSet" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>AllPay</title>
</head>
<body>
<%
List<String> enErrors = new ArrayList<String>();
try {
AllInOne oPayment = new AllInOne(); /* 服務參數 */
oPayment.ServiceMethod = HttpMethod.HttpPOST;
oPayment.ServiceURL = "";
oPayment.HashKey = "";
oPayment.HashIV = "";
oPayment.MerchantID = ""; /* 基本參數 */
oPayment.Send.ReturnURL = "http://172.16.30.41:8080/AllPayWeb/checkOutFeedback_All.jsp";
oPayment.Send.ClientBackURL = "http://172.16.30.41:8080/AllPayWeb/checkOutFeedback_All.jsp";
oPayment.Send.OrderResultURL = "http://172.16.30.41:8080/AllPayWeb/checkOutFeedback_All.jsp";
oPayment.Send.MerchantTradeNo = String.valueOf((new Date()).getTime());
oPayment.Send.MerchantTradeDate = new Date();
oPayment.Send.TotalAmount = new Decimal("300");
oPayment.Send.TradeDesc = AllPayFunction.genString("測試");
oPayment.Send.ChoosePayment = PaymentMethod.ALL;
oPayment.Send.Remark = AllPayFunction.genString("測試Pay");
oPayment.Send.ChooseSubPayment = PaymentMethodItem.ATM_TAISHIN;
oPayment.Send.NeedExtraPaidInfo = ExtraPaymentInfo.Yes;
oPayment.Send.DeviceSource = DeviceType.PC; oPayment.SendExtend.ExpireDate = 1;
oPayment.SendExtend.PaymentInfoURL = "http://172.16.30.41:8080/AllPayWeb/checkOutFeedback_All.jsp";
//oPayment.SendExtend.ClientRedirectURL = "http://172.16.30.41:8080/"; // 加入選購商品資料。
Item a1 = new Item();
a1.Name = "一棟房子";
a1.Price = new Decimal("300");
a1.Currency = "元";
a1.Quantity = 2;
a1.URL = "<<產品說明位址>>";
oPayment.Send.Items.add(a1); Item a2 = new Item();
a2.Name = "iPhone 6S";
a2.Price = new Decimal("400");
a2.Currency = "元";
a2.Quantity = 8;
a2.URL = "<<產品說明位址>>";
oPayment.Send.Items.add(a2); enErrors.addAll(oPayment.CheckOut(response.getWriter())); }
catch (Exception e) {
enErrors.add(e.getMessage());
}
finally {
if (enErrors.size() > 0)
out.print(enErrors);
}
%>
</body>
</html>bittorrent
- paymentresult.jsp
<%@include file="init.jsp" %>
<%
List<String> enErrors = new ArrayList<String>(); try {
//ParamUtil.print(request);
AllInOne oPayment = new AllInOne();
Hashtable<String, String> htFeedback = new Hashtable<String, String>();
//HttpServletRequest request = PortalUtil.getHttpServletRequest(actionrequest);
enErrors.addAll(oPayment.CheckOutFeedback(htFeedback, request));
Set<String> key = htFeedback.keySet();
String name[] = key.toArray(new String[key.size()]); // id = id.Replace("_", "-");
String HashKey = "";
String HashIV = ""; //Get a payment result data
String szMerchantID = "";
String szMerchantTradeNo = "";
String szPaymentDate = "";
String szPaymentType = "";
String szPaymentTypeChargeFee = "";
String szRtnCode = "";
String szRtnMsg = "";
String szSimulatePaid = "";
String szTradeAmt = "";
String szTradeDate = "";
String szTradeNo = "";
/* 使用 ATM 交易時,回傳的額外參數 */
String szBankCode = "";
String szVirtualAccount = "";
String szExpireDate = ""; String szCheckMacValue = ""; for (int i = 0; i < name.length; i++) {
/* 支付後的回傳的基本參數 */
if (name[i].equals("MerchantID"))
szMerchantID = htFeedback.get(name[i]);
else if (name[i].equals("MerchantTradeNo"))
szMerchantTradeNo = htFeedback.get(name[i]);
else if (name[i].equals("PaymentDate"))
szPaymentDate = htFeedback.get(name[i]);
else if (name[i].equals("PaymentType"))
szPaymentType = htFeedback.get(name[i]);
else if (name[i].equals("PaymentTypeChargeFee"))
szPaymentTypeChargeFee = htFeedback.get(name[i]);
else if (name[i].equals("RtnCode"))
szRtnCode = htFeedback.get(name[i]);
else if (name[i].equals("RtnMsg"))
szRtnMsg = htFeedback.get(name[i]);
else if (name[i].equals("SimulatePaid"))
szSimulatePaid = htFeedback.get(name[i]);
else if (name[i].equals("TradeAmt"))
szTradeAmt = htFeedback.get(name[i]);
else if (name[i].equals("TradeDate"))
szTradeDate = htFeedback.get(name[i]);
else if (name[i].equals("TradeNo"))
szTradeNo = htFeedback.get(name[i]);
else if(name[i].equals("BankCode"))
szBankCode = htFeedback.get(name[i]);
else if(name[i].equals("vAccount"))
szVirtualAccount = htFeedback.get(name[i]);
else if(name[i].equals("ExpireDate"))
szExpireDate = htFeedback.get(name[i]);
}
System.out.println("MerchantID = " + szMerchantID);
System.out.println("MerchantTradeNo = " + szMerchantTradeNo);
System.out.println("PaymentDate = " + szPaymentDate);
System.out.println("PaymentType = " + szPaymentType);
System.out.println("PaymentTypeChargeFee = "
+ szPaymentTypeChargeFee);
System.out.println("RtnCode = " + szRtnCode);
System.out.println("RtnMsg = " + szRtnMsg);
System.out.println("SimulatePaid = " + szSimulatePaid);
System.out.println("TradeAmt = " + szTradeAmt);
System.out.println("TradeDate = " + szTradeDate);
System.out.println("TradeNo = " + szTradeNo);
/* 使用 ATM 交易時,回傳的額外參數 */
System.out.println("BankCode = " + szBankCode);
System.out.println("vAccount = " + szVirtualAccount);
System.out.println("ExpireDate = " + szExpireDate);
out.println("");
} catch (Exception e) {
enErrors.add(e.getMessage());
} finally {
if (enErrors.size() == 0)
out.println("1|OK");
else
out.println("0|" + enErrors); }
%>bittorrent
則是user在歐付寶付款後,通知我方server用的,好讓我方server在資料庫中,紀錄成功入賬。
結尾:
基本上,只要欄位照著規格書填對。就沒有太大的問題,而看到官方有其他的問題則是參數中有 / - 等字串
所以過程中,我是已經避免此問題了。
若有問題,可以留言討論。