JAVA-Servlet專案接入支付寶網站支付
阿新 • • 發佈:2019-02-19
支付寶電腦支付流程,Servlet版
感謝原博主,大佬無視即可,工作一年多第一次寫部落格,不足之處請諒解
package com.alipay.config;
import java.io.FileWriter;
import java.io.IOException;
/* *
*類名:AlipayConfig
*功能:基礎配置類
*詳細:設定帳戶有關資訊及返回路徑
*修改日期:2017-04-05
*說明:
*以下程式碼只是為了方便商戶測試而提供的樣例程式碼,商戶可以根據自己網站的需要,按照技術文件編寫,並非一定要使用該程式碼。
*該程式碼僅供學習和研究支付寶介面使用,只是提供一個參考。
*/
public class AlipayConfig {
//↓↓↓↓↓↓↓↓↓↓請在這裡配置您的基本資訊↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
// 應用ID,您的APPID,收款賬號既是您的APPID對應支付寶賬號
public static String app_id = "2016081******";
// 商戶私鑰,您的PKCS8格式RSA2私鑰,官方有金鑰生產工具,下載地址在下方
public static String merchant_private_key = "";
// 支付寶公鑰,檢視地址:https://openhome.alipay.com/platform/keyManage.htm 對應APPID下的支付寶公鑰。
public static String alipay_public_key = "";
// 伺服器非同步通知頁面路徑 需http://格式的完整路徑,不能加?id=123這類自定義引數,必須外網可以正常訪問
public static String notify_url = "http://localhost:8080/NotifyServlet";//為防止未知的異常,後臺業務邏輯收到非同步通知後執行
// 頁面跳轉同步通知頁面路徑 需http://格式的完整路徑,不能加?id=123這類自定義引數,必須外網可以正常訪問
public static String return_url = "http://localhost:8080/1.jsp" ;
// 簽名方式
public static String sign_type = "RSA2";
// 字元編碼格式
public static String charset = "utf-8";
// 支付寶閘道器,此處注意下,沙箱測試和正式環境,閘道器是不同的,詳情檢視官方文件
public static String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";
// 支付寶閘道器
public static String log_path = "C:\\";
//↑↑↑↑↑↑↑↑↑↑請在這裡配置您的基本資訊↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
/**
* 寫日誌,方便測試(看網站需求,也可以改成把記錄存入資料庫)
* @param sWord 要寫入日誌裡的文字內容
*/
public static void logResult(String sWord) {
FileWriter writer = null;
try {
writer = new FileWriter(log_path + "alipay_log_" + System.currentTimeMillis()+".txt");
writer.write(sWord);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
生產金鑰工具:
第二步建立完畢訂單後發起付款請求
package com.alipay.config;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
public class OrderServlet extends HttpServlet{
@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("開始處理OrderServlet的服務");
//建立訂單,隨意寫了個類,把屬性值寫死了,這裡就不貼程式碼了
Order order = new Order();
//向支付寶傳送請求
//獲得初始化的AlipayClient
AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id,
AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key,
AlipayConfig.sign_type);
//設定請求引數
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
alipayRequest.setReturnUrl(AlipayConfig.return_url);
alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
//商戶訂單號,商戶網站訂單系統中唯一訂單號,必填
String out_trade_no = order.getOrderID()+"";
//付款金額,必填
String total_amount = order.getOrderPay()+"";
//訂單名稱,必填
String subject = order.getOrderName();
//商品描述,可空
String body = order.getMessage();
alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\"," + "\"total_amount\":\""
+ total_amount + "\"," + "\"subject\":\"" + subject + "\"," + "\"body\":\"" + body + "\","
+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
//若想給BizContent增加其他可選請求引數,以增加自定義超時時間引數timeout_express來舉例說明
//alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
// + "\"total_amount\":\""+ total_amount +"\","
// + "\"subject\":\""+ subject +"\","
// + "\"body\":\""+ body +"\","
// + "\"timeout_express\":\"10m\","
// + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
//請求引數可查閱【電腦網站支付的API文件-alipay.trade.page.pay-請求引數】章節
AlipayTradePagePayResponse alipayResponse = null;
try {
alipayResponse=alipayClient.pageExecute(alipayRequest);
System.out.println(alipayResponse.getBody());
System.out.println(alipayResponse.getMsg());
} catch (AlipayApiException e) {
e.printStackTrace();
}
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write(alipayResponse.getBody());
}
}
最後對支付寶返回的引數進行業務處理
package com.alipay.config;
import java.io.IOException;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class NotifyServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Map<String,String[]> parameterMap=request.getParameterMap();
//業務處理
for(String key : parameterMap.keySet()) {
System.out.println("KEY:"+key+"--value:"+parameterMap.get(key));
}
//成功處理後返回success
response.getWriter().write("success");
}
}
JAR包
web.xml配置