#1. Alipay’s documents are relatively easy to read, mainly because there are corresponding DEMOs. The DEMO I am looking at here It is the JAVA-UTF-8 version.
2. Import DEMO and fill in the corresponding partner and key in com.alipay.config (obtained from the corresponding merchant backend). Run it directly to understand the payment process.
3. Rewrite: I am using springmvc mybatis here. The product initiates purchase (payment via Alipay), jumps to Alipay, and Alipay calls back the payment status.
1. Initiate a purchase request for the product (use the page in the DEMO directly).
[javascript]view plaincopy
#
"header">
class
="container black">class
="qrcode">class
="littlecode">"16px"src="img/little_qrcode.jpg"id="licode">
class
="showqrs"id="showqrs">class
="shtoparrow">class
="guanzhuqr">"img/guanzhu_qrcode.png"width="80">
margin-top:5px;">#### because of #"shmsg"style=
"margin-bottom:5px;"Receive important information
# This/P & GT; ;/p>
class
="container">class
="nav">"https://www.alipay.com/"class="logo">"img/alipay_logo.png"height="30px">
##class=
target="_blank">Open platform####>## .com/doc2/detail?treeId=62&articleId=103566&docType=1"
target=>Online Document li>
Since ="_blank">Technical Support
## >
# ##>Alipay instant payment (create_direct_pay_by_user)
##"content">
## "${ctx}/aliPay/open"class="alipayform"method="POST"target="_blank"> --%>
style="margin-top:60px;">
#
class
="element">class
="etitle">商户订单号:class
="einput">"text"name="WIDout_trade_no"id="out_trade_no">through ).Required (recommended to be English letters and numbers, no special characters)
#>
#
"etitle"#"text"name="WIDsubject"id="WIDsubject"value="test product 123">
##Name (subject), required (Chinese, English, numbers are recommended, no special characters allowed)# ;
##
through ;## #>"text"
name=id="WIDtotal_fee "value="0.01">
>Note: Payment amount (total_fee), required (format such as: 1.00, please be accurate to the minute)### /p>
#"text"name="WIDbody"id="WIDbody"value=
"Instant transfer test">
##, optional (recommend Chinese, English, numbers, no special characters)
##"button"
class="alisubmit"id="sbumitBtn"
value =>###
"returnAli">
class
="footer">class
="footer-sub">"http://ab.alipay.com/i/index.htm"target="_blank">关于支付宝|
"https://e.alipay.com/index.htm"target="_blank">商家中心|
"https://job.alibaba.com/zhaopin/index.htm"target="_blank">诚征英才|
"http://ab.alipay.com/i/lianxi.htm"target="_blank">联系我们|
"#"id="international"target="_blank">International Business|
"http://ab.alipay.com/i/jieshao.htm#en"target="_blank">About Alipay
支付宝版权所有
class="footer-date">2004-2016
"http://fun.alipay.com/certificate/jyxkz.htm"target="_blank">ICP证:沪B2-20150087
##
##
##2. After clicking to confirm the payment, request the background through ajax and put the returned html code directly into the above
, this form will be automatically submitted.[javascript]view plaincopy
##$(function
(){
$(
).on(
'click'function(){
## $.ajax({## type :"post",
data : {
WIDout_trade_no : $('#out_trade_no').val(),
WIDsubject : $('#WIDsubject').val(),
WIDtotal_fee : $('#WIDtotal_fee').val(),
WIDbody : $('#WIDbody').val()
},
url :"${ctx}/aliPay/open",
success :function(data) {
$('#returnAli').append(data.sHtmlText);
},
error :function(da){
# }
[javascript]
view plain
"open")
public
ResponseEntity
///////////////////////////////////// / Request parameters ////////////////////////////////////////
##// Merchant order number, in the merchant website order system Unique order number, required
//Order name, required
String total_fee = WIDtotal_fee;
##// Product description, can be empty
## String body = WIDbody;
##//Pack request parameters into an array
# Map
# sParaTemp.put("service", AlipayConfig.service);
sParaTemp.put("partner", AlipayConfig.partner);
sParaTemp.put("seller_id", AlipayConfig.seller_id);
sParaTemp.put("_input_charset", AlipayConfig.input_charset);
sParaTemp.put("payment_type", AlipayConfig.payment_type);
sParaTemp.put("notify_url", AlipayConfig.notify_url);
sParaTemp.put("return_url", AlipayConfig.return_url);
sParaTemp.put("anti_phishing_key", AlipayConfig.anti_phishing_key);
sParaTemp.put("exter_invoke_ip", AlipayConfig.exter_invoke_ip);
sParaTemp.put("out_trade_no", out_trade_no);
sParaTemp.put("subject", subject);
sParaTemp.put("total_fee", total_fee);
sParaTemp.put("body", body);
// Other business parameters are added according to the online development documents. Document address: https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.O9yorI&treeId= 62&articleId=103740&docType=1
##// Such as sParaTemp.put("Parameter name","Parameter value ");
// Build request
,"Confirm");
## model.addAttribute(, sHtmlText);
# hysWebMeetingAliService.insertSelective(sParaTemp);
returnnewResponseEntity(model, HttpStatus.OK);
}
##4. Callback: The java code in notify_url.jsp in DEMO is also directly used and slightly modified and the business code (modified status, etc.) is added;
[javascript]view plaincopy
##@RequestMapping("notify")
String notify(HttpServletRequest request){
Map
Map requestParams = request.getParameterMap();
for(Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
String name = (String) iter.next();
String[] values = (String[]) requestParams.get(name);
String valueStr ="";
for(inti = 0; i
valueStr = (i == values.length - 1) ? valueStr values[i]
: valueStr values[i]",";
}
//Garbled code solution, this code is used when garbled characters appear. If mysign and sign are not equal, you can also use this code to convert
##//valueStr = new String( valueStr.getBytes("ISO-8859-1"), "gbk");
#
## String out_trade_no = request.getParameter(
"out_trade_no"//Alipay transaction number
## String trade_no = request.getParameter("trade_no"
//trade state
## String trade_status = request.getParameter("trade_status");
##//To obtain Alipay’s notification return parameters, please refer to the page jump synchronization notification parameter list in the technical documentation (the above is only refer to)//
if(AlipayNotify.verify(params)){//Verification successful
//////////////////////////////////////////// //////////////////////////////////////////////////
##boolean
false;
## iF## (Trade_Status.equals (## "" Trade_finished "#)) {
##
## since #
//Notice:
#//After the refund date exceeds the refundable period (such as refundable within three months), the Alipay system Send transaction status notification
}elseif(trade_status.equals("TRADE_SUCCESS")){
//Determine whether the order has been processed on the merchant website
#//If it has not been processed, check it in the order system of the merchant website according to the order number (out_trade_no) Go to the details of the order and execute the merchant's business procedures
## After processing, the business procedures of the merchant will not be executed
###
//Note://Change the order status and the status in the Alipay record table to paid according to the order number
flg = hysWebMeetingAliService.changeOrderAndAliStatusSuccess(out_trade_no);
#
Please write the program according to your business logic (the above code is for reference only)——
##//out.print("success"); //Please do not modify or delete
"success";
{
///////////////////////////////////////////////////// /////////////////////////////////////////
{
//Verification failed//out.print("fail");
return"fail";
}
5. return_url: The page jump synchronization notification page path is the page that Alipay will jump back to after the payment is successful. "The complete path in http:// format is required, and custom parameters such as ?id=123 cannot be added. The external network must be accessible normally." Alipay clearly stipulates that no custom parameters can be added after the page that bounces back, so some of us are It is a bit troublesome to judge the jump based on some types. I did a trick here: first move the java code in return_url.jsp directly and modify it slightly (change to springmvc method). My bounce address is IP/aliPay/returnUrl, and then new ModelAndView ("redirect:/meeting/info") to redirect to the URL we are thinking of (see point 4 of the summary below for parameter issues).
[javascript]view plaincopy
# @RequestMapping("returnUrl")
publicModelAndView returnUrl(HttpServletRequest request){
newModelAndView("redirect:/meeting/info");
// Get Alipay Get to feedback information#
Map
Map requestParams = request.getParameterMap();
for(Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
String name = (String) iter.next();
String[] values = (String[]) requestParams.get(name);
String valueStr ="";
for(inti = 0; i
valueStr = (i == values.length - 1) ? valueStr values[i]
: valueStr values[i]",";
. If mysign and sign are not equal, you can also use this code to convert
# valueStr.getBytes("ISO-8859-1"), "utf-8");
# params.put (name, valueStr);## }
# String out_trade_no = request.getParameter("out_trade_no");
//Alipay transaction number
# String trade_no = request.getParameter("trade_no"
##
.getParameter("trade_status");
##"color:#ff0000;">String meetingId = request.getParameter(
"extra_common_param""meetingId", meetingId);
##//Get Alipay notification return parameters, Please refer to the page jump synchronization notification parameter list in the technical documentation (the above is for reference only) //
# // Calculate the notification verification results
# Verify_result = Alipaynotify.verify (Params)
if(verify_result){//Verification successful
##//////////////////////////////////// ///////////////////////////////////////////////////// //////
##
"TRADE_FINISHED") || trade_status.equals("TRADE_SUCCESS")) {#//Determine whether the order has been processed on the merchant website
##//If it has not been processed, check the details of the order in the order system of the merchant website according to the order number (out_trade_no), and Execute merchant’s business procedures
## // If you have done it, do not perform the business procedure of the merchant
## through Can be used as page art editor
#// out.println("Verification successful
## Reference)--
/////////////////////////////////////////////////// /////////////////////////////////////////////
//This page can be used for page art editing
##// ("verification failed");
}
;
5. Summary:
2. When debugging, especially callbacks, the project must be deployed to a server that can be accessed from the external network.
4. What should I do if I need to pass parameters when doing page jump synchronization notification page path? I customized a parameter at first, but I didn’t get it. Then I saw a comment in the DEMO like this Written by:
[javascript]view plaincopy
##// Other business parameters are added according to the online development document. Document address: https://doc.open.alipay.com/doc2/detail.htm? spm=a219a.7629140.0.0.O9yorI&treeId=62&articleId=103740&docType=1
##So after I opened it and looked at it, I selected The public return parameter extra_common_param is provided, and I know what this field means to me. But you should also pay attention, others explained:
[javascript]view plaincopy
##The parameters body (product description), subject (product name), extra_common_param (public return parameters) cannot contain special characters (such as: #, %, &, ), sensitive words, and cannot Use foreign languages (foreign languages that Wangwang does not support, such as Korean, Thai, Tibetan, Mongolian, Arabic);
[javascript]
view plaincopy
"extra_common_param", meetingId);
[javascript]
view plaincopy
##String meetingId = request.getParameter("extra_common_param");
mv.addObject("meetingId", meetingId);
Related recommendations:
The above is the detailed content of Detailed explanation of the case of Java calling Alipay payment interface. For more information, please follow other related articles on the PHP Chinese website!