java實現網站paypal支付功能並且異步修改訂單的狀態:步驟如下
第一步:去paypal的官網https://www.paypal.com注冊一個個人賬號,在創建沙箱測試賬號時需要用到
第二步:paypal提供了模擬的測試環境,我們需要在https://www.sandbox.paypal.com/去創建一個虛擬賣家賬號和買家賬號
(必須用第一步注冊的真實賬號才能登錄進去,沙箱賬號是登錄不進去的)
第三步:登錄成功后創建賣家、買家賬號,如果不知道在哪創建賬號可以直接點擊這個鏈接https://developer.paypal.com/developer/accounts/
如下圖:
上面幾個就是我創建的賣家和買家測試賬號,如果創建不了的可以私信我
第四步:編寫java代碼,把必要的數據傳給paypal就行,比如金額、產品描述等等
第五步:編寫異步回調路徑的java代碼
/**
* pay pal支付返回信息
* @param request
* @param response
*/
@RequestMapping("payPal/recharge")
public void payPal(HttpServletRequest request,HttpServletResponse response,HttpSession session) throws IOException, ParseException {
String itemNumber="";
Enumeration en = request.getParameterNames();
while (en.hasMoreElements()) {
String paramName = (String) en.nextElement();
String paramValue = request.getParameter(paramName);
if(paramName.equals("item_number")){
itemNumber=paramValue;
}
}
String id[]=itemNumber.split(",");
PrintWriter out=response.getWriter();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String sDate = sdf.format(new Date());
Date date = sdf.parse(sDate);
String str1 = request.getParameter("tx");
/*正式環境下
String str2 = "&at=sTvmKEM1YR2EmQXW3VyBrqYWiX-8_wr0Sj5w2DQ5uqGoakHYOCKcFsaAAU4";
*/
String str2 = "&at=VmjfBuVl1vbSC6bMV7xvROqisIsrMpKftSx_bLbAnNr-UO2JsLnAR2wfzK8";
String str = "?tx=" + str1 + "&cmd=_notify-synch" + str2;
/*
String str = "?tx=" + str1 + "&cmd=_notify-validate" + str2;
*/
/* 正式環境下
String payPalUrl = "https://www.paypal.com/cgi-bin/webscr";
*/
String payPalUrl = "https://www.sandbox.paypal.com/cgi-bin/webscr";
payPalUrl = payPalUrl + str;
URL u = new URL(payPalUrl);
URLConnection uc = u.openConnection();
uc.setDoOutput(true);
uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
PrintWriter pw = new PrintWriter(uc.getOutputStream());
pw.println(str);
pw.close();
//接受PayPal對IPN回發的回復信息
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
String line = "";
String txn_id = ""; //paypal的號碼
String item_name = "";//本地訂單號
String contact_phone = "";
int i = 0;
String res = "";
String msg = "";
while ((line = in.readLine()) != null) {
i = i + 1;
if (i == 1) {
res = line;
}
if (res.equals("SUCCESS")) {
if (line.indexOf("txn_id=") != -1) {
txn_id = line.replace("txn_id=", "");
} else if (line.indexOf("item_name=") != -1) {
item_name = line.replace("item_name=", "");
} else if (line.indexOf("contact_phone=") != -1) {
contact_phone = line.replace("contact_phone=", "");
}
}
}
if (!txn_id.equals("") && !item_name.equals("")) {
UserRecord userRecord=userRecordService.findById(Integer.parseInt(id[1]));
userRecord.setCommitDate(date);
userRecord.setHandler(id[2]);
userRecord.setState(0);
userRecordService.updateUserRecord(userRecord);//修改數據庫的字段信息
msg = "Pay for success! Please wait for delivery! Your Order Number: " + txn_id + " !";
} else {
msg = "Sorry ! Your operating error! Please contact website administrator !!";
}
out.print("<script>alert('" + msg + "');location.href='" + request.getContextPath() + "/goto/back'</script>");//支付完畢返回 用戶信息頁 !
}
注意)本地是無法進行異步調試,要在外網才行,也可以使用nat123將本地映射到外網。另外,如果異步返回時接收不到相關的數據,有可能是你沒開通PDT數據傳輸功能,在網站付款
習慣里面可以設定,如下圖:

原文博客的鏈接地址:https://cnblogs.com/qzf/