電腦網站支付開發(Alipay)
阿新 • • 發佈:2018-12-31
環境 : 開發者當前所在公司支付寶上線賬號(配置SDK公共請求引數)
文件 : https://docs.open.alipay.com/270/alipay.trade.page.pay
下載SDK和demo: https://docs.open.alipay.com/270/106291/
支付過程 : 前端帶訂單引數請求後端,後端帶 訂單資訊和驗籤引數(SDK配置) 生成請求支付寶的表單字串:"<form>...</form><script>...</script>" 返回給前端,前端js用選擇器 選擇div的屬性名.html(後端返回的資料) 得到支付二維碼, 掃完之後回撥return_url 和 notify_url
流程圖:
Java Demo:
OrderPayPO order = new OrderPayPO(...);
insertEntity2Cache(order);
// SDK 公共請求類,包含公共請求引數,以及封裝了簽名與驗籤,開發者無需關注簽名與驗籤
AlipayClient client = new DefaultAlipayClient(JinHonConfig.URL, JinHonConfig.APPID,
JinHonConfig.RSA_PRIVATE_KEY, JinHonConfig.FORMAT, JinHonConfig.CHARSET,
JinHonConfig.ALIPAY_PUBLIC_KEY,JinHonConfig.SIGNTYPE);
// 設定請求引數
AlipayTradePagePayRequest alipayRequest=new AlipayTradePagePayRequest();
// 設定非同步通知地址
alipayRequest.setNotifyUrl(JinHonConfig.notify_url+notifyUrl);
// 設定同步地址
alipayRequest.setReturnUrl(JinHonConfig.return_url);
// 商戶訂單號,商戶網站訂單系統中唯一訂單號,必填
String out_trade_no = order.getOrder_no();
// 訂單名稱,必填
String subject = order.getGoods_info();
// 付款金額,必填
String total_amount = order.getPay_amount().toString();
// 商品描述,可空
String body = order.getOrder_param();
// 超時時間 可空
String timeout_express = "10m";
//穿透引數(必須轉碼)*
String passback_params = "";try {
passback_params = URLEncoder.encode(session, "Utf-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 商品列表資訊,可空
alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
+ "\"subject\":\""+ subject +"\","
+ "\"total_amount\":\""+ total_amount +"\","
+ "\"body\":\""+ body +"\","
+ "\"passback_params\":\"" +passback_params+"\","
+ "\"timeout_express\":\""+ timeout_express +"\","
+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
// form表單生產
ReqAlipayParamVO alipayParam = new ReqAlipayParamVO();
try {
// 呼叫SDK生成表單
String form = client.pageExecute(alipayRequest).getBody();
return form ;//返回 :"<form>...</form><script>...</script>"
} catch (AlipayApiException e) {
e.printStackTrace();
JinHonApi.service.info(e.getErrMsg());
}
return "";