1. Scenario: The company needs to make Alipay payment on the website.
2. API: Use the payment capability of Alipay’s open platform-instant payment interface. Alipay Open Platform Link
3. Analysis:
#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.
4. Implementation:
1. Initiate a purchase request for the product (use the page in the DEMO directly).
[javascript] view plain copy
#
- # #=
"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=
"open"-
target="_blank">Open platform ## ##> ## .com/doc2/detail?treeId=62&articleId=103566&docType=1"
target= - "_blank"
>Online Document li>
Since ="_blank">Technical Support
# # p> ## >
-
# ##>Alipay instant payment (create_direct_pay_by_user)
- #
## "content">
## "${ctx}/aliPay/open" class="alipayform" method="POST" target="_blank"> --%>
- # ##"element"
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)
# - #"element"
>
-
#
"etitle" - >Product name:
-
#"text" name="WIDsubject" id="WIDsubject" value="test product 123">
## Name (subject), required (Chinese, English, numbers are recommended, no special characters allowed) # ;
##
-
through ; ## #>"text"
name= - "WIDtotal_fee"
id="WIDtotal_fee " value="0.01">
>Note: Payment amount (total_fee), required (format such as: 1.00, please be accurate to the minute) ## -
# /p>
# > -
>
## -
, optional (recommend Chinese, English, numbers, no special characters)
# # "button"
-
class="alisubmit" id="sbumitBtn"
value = - "Confirm payment"
> ## #
"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 plain copy
"text" name="WIDbody" id= "WIDbody" value=
"Instant transfer test"
##$(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 copy
"open")
public
ResponseEntity
- String WIDbody) {
///////////////////////////////////// / Request parameters ////////////////////////////////////////
##// Merchant order number, in the merchant website order system Unique order number, required
- ## String out_trade_no = WIDout_trade_no;
-
//Order name, required
# String subject = WIDsubject; -
-
##// Product description, can be empty
-
## String body = WIDbody;
-
##//Pack request parameters into an array
# Map
sParaTemp = new HashMap(); # 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
## String sHtmlText = AlipaySubmit.buildRequest(sParaTemp, - "get"
, "Confirm");
## model.addAttribute( " sHtmlText" -
, sHtmlText);
-
// Save payment record # hysWebMeetingAliService.insertSelective(sParaTemp);
return new ResponseEntity(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 plain copy
##@RequestMapping("notify")
- @ResponseBody
- ##public
String notify(HttpServletRequest request){
- //Get Alipay POST feedback information
Map
params = new HashMap (); 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 (int i = 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");
- # params.put(name , valueStr);
-
#
-
-
-
-
## String out_trade_no = request.getParameter(
"out_trade_no" - );
-
-
-
## 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
-
//////////////////////////////////////////// //////////////////////////////////////////////////
## - //Please add the merchant’s business logic program code here
-
- flg =
false;
## iF ## (Trade_Status.equals (## "" Trade_finished "#)) {
# - //Determine whether the order has been processed on the merchant website
- //If no processing has been done, check the details of the order in the order system of the merchant website according to the order number (out_trade_no), and execute the merchant's business procedures
## since #
-
//Notice:
# //After the refund date exceeds the refundable period (such as refundable within three months), the Alipay system Send transaction status notification
} else if (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
- # The total_fee and seller_id are consistent with the total_fee and seller_id obtained during the notification.
-
## #
//Note: -
- # Notification of the transaction status
//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";
## }- else
{
///////////////////////////////////////////////////// /////////////////////////////////////////
## }- else
{
//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 plain copy
# @RequestMapping("returnUrl")
-
public ModelAndView returnUrl(HttpServletRequest request){
- ModelAndView mv =
new ModelAndView("redirect:/meeting/info");
## // Get Alipay Get to feedback information #
Map
params = new HashMap (); 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 (int i = 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");
-
-
# String trade_no = request.getParameter("trade_no"
); ##
.getParameter("trade_status");
-
## "color:#ff0000;">String meetingId = request.getParameter(
"extra_common_param" - );
- # mv.addObject(
"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
- #Boolean
# Verify_result = Alipaynotify.verify (Params)
if(verify_result){//Verification successful
## //////////////////////////////////// ///////////////////////////////////////////////////// //////
- //— —Please write the program according to your business logic (the following code is for reference only)——
- (trade_status.equals(
"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:
1. Comparison of Alipay integration Short answer, it will be easier if you take a look at the DEMO and run it to understand the process. 2. When debugging, especially callbacks, the project must be deployed to a server that can be accessed from the external network.
3. I have not encountered the above inexplicable problems. If so, you can contact me, or read the Eclipse remote debugging article to track the problem.
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 plain copy
##// 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 plain copy
##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 plain copy
- ##sParaTemp.put(
"extra_common_param", meetingId);
Get the value in the returnUrl method and use it as a redirection parameter:
[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!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools
