整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

调用支付宝第三方支付接口详解(沙箱环境)

调用支付宝第三方支付接口详解(沙箱环境)

文作者:cuifuan注:这里的支付是沙箱模拟支付

1、注册蚂蚁金服开放平台

蚂蚁金服开放平台注册地址:

https://open.alipay.com/platform/home.htm

支付宝扫码登陆 -> 注册为自主研发者


蚂蚁金服页面

2、配置你的沙箱支付宝


配置你的沙箱支付宝

支付宝提供一键生成工具便于开发者生成一对RSA2密钥:

https://docs.open.alipay.com/291/105971

该工具使用需要java环境

windows安装java环境:

https://blog.csdn.net/edison_03/article/details/79757591

Mac安装java环境:

https://www.cnblogs.com/xqx-qyy/p/7659805.html



生成RSA2密钥

注意:生成时一定要选择PKCS8+2048


配置RSA2密钥

将应用网关和回调地址更改为:

https://www.alipay.com

AES密钥不用管

然后往下会有支付宝沙箱安卓端工具,下载,以供后续支付使用

进入页面左侧导航栏沙箱账号,沙箱安卓端安装后用买家账号登陆

到这里基本配置就完了,下面进入大家喜欢的代码时间:

3、新建一个配置类 AlipayConfig.java

package com.alipay.config;
import java.io.FileWriter;
import java.io.IOException;
/* *
 *类名:AlipayConfig
 *作者:有梦想一起实现
 */
public class AlipayConfig{
 // ↓↓↓↓↓↓↓↓↓↓请在这里配置您的基本信息↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
 // 应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号
 public static String app_id="APPID";//例:2016082600317257
 // 商户私钥,您的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="你的服务器地址/项目名称/notify_url";
 // 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
 /**
 * 此页面是同步返回用户页面,也就是用户支付后看到的页面,上面的notify_url是异步返回商家操作,谢谢
 * 要是看不懂就找度娘,或者多读几遍,或者去看支付宝第三方接口API,不看API直接拿去就用,遇坑不怪别人
 */
 public static String return_url=" 你的服务器地址/项目名称/return_url";
 // 签名方式
 public static String sign_type="RSA2";
 // 字符编码格式
 public static String charset="gbk";
 // 支付宝网关
 public static String gatewayUrl="https://openapi.alipaydev.com/gateway.do";
 // 日志地址
 public static String log_path="D:/logs/";
 // ↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
 /**
 * 写日志,方便测试(看网站需求,也可以改成把记录存入数据库)
 *
 * @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();
 }
 }
 }
 }
}

如果你是在本地测试,支付完成不会跳转回调页面,那么就需要外网了

推荐一个东西,叫内网穿透,只要你电脑tomcat启动,可以连接外网,就可以使用。

NATAPP 提供免费的测试足够:

https://natapp.cn/

ngrok或者frp以及其他免费开源,自行搜索了解

4、引入依赖包

<dependency>
 <groupId>com.pentahohub.nexus</groupId>
 <artifactId>alipay-sdk-java</artifactId>
 <version>20150820220052</version>
</dependency>

如果上面的依赖失效或者无法使用,依赖下载地址:

http://central.maven.org/maven2/com/pentahohub/nexus/alipay-sdk-java/20150820220052/alipay-sdk-java-20150820220052.jar

5、支付调用的接口

/**
 * 快捷支付调用支付宝支付接口
 * @param model,id,payables, 
 * @throws IOException,AlipayApiException
 * @return Object
 * @author 有梦想一起实现
 */
 @RequestMapping("alipaySum")
 public Object alipayIumpSum(Model model, String payables, String subject, String body, HttpServletResponse response)
 throws Exception {
 // 获得初始化的AlipayClient
 AlipayClient alipayClient=new DefaultAlipayClient(AlipayConfigInfo.gatewayUrl, AlipayConfigInfo.app_id,
 AlipayConfigInfo.merchant_private_key, "json", AlipayConfigInfo.charset,
 AlipayConfigInfo.alipay_public_key, AlipayConfigInfo.sign_type);
 // 设置请求参数
 AlipayTradePagePayRequest alipayRequest=new AlipayTradePagePayRequest();
 alipayRequest.setReturnUrl(AlipayConfigInfo.return_url);
 alipayRequest.setNotifyUrl(AlipayConfigInfo.notify_url2);
 SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmssSSS");
 // 商户订单号,商户网站订单系统中唯一订单号,必填
 String out_trade_no=sdf.format(new Date());
 // 付款金额,必填
 String total_amount=payables.replace(",", "");
 alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\"," + "\"total_amount\":\"" + total_amount
 + "\"," + "\"subject\":\"" + subject + "\"," + "\"body\":\"" + body + "\","
 + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
 // 请求
 String result=alipayClient.pageExecute(alipayRequest).getBody();
 // System.out.println(result);
 AlipayConfigInfo.logResult(result);// 记录支付日志
 response.setContentType("text/html; charset=gbk");
 PrintWriter out=response.getWriter();
 out.print(result);
 return null;
 }

参数传入是必须有的,不然会报订单信息有误。

如果有其他额外参数,请参考支付宝第三方API文档:

https://docs.open.alipay.com/api_1/alipay.trade.create/

6、支付完成回调

这里支付完成会回调两个接口,notify_url和return_url,就是在配置类配置的两个接口:

1、notify_url接口->异步回调的后台操作

/**
 * 支付完成回调验证操作
 * @param response,request
 * @throws Exception
 * @return void
 * @author 有梦想一起实现
 */
 @RequestMapping("notify_url")
 public void Notify(HttpServletResponse response, HttpServletRequest request) throws Exception {
 System.out.println("----------------------------notify_url------------------------");
 // 商户订单号
 String out_trade_no=new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "GBK");
 // 付款金额
 String total_amount=new String(request.getParameter("total_amount").getBytes("ISO-8859-1"), "GBK");
 // 支付宝交易号
 String trade_no=new String(request.getParameter("trade_no").getBytes("ISO-8859-1"), "GBK");
 // 交易说明
 String cus=new String(request.getParameter("body").getBytes("ISO-8859-1"), "GBK");
 // 交易状态
 String trade_status=new String(request.getParameter("trade_status").getBytes("ISO-8859-1"), "GBK");
 if (trade_status.equals("TRADE_SUCCESS")) {//支付成功商家操作
 //下面是我写的一个简单的插入操作,根据你的操作自行编写
 /*Map<Object, Object> map=new HashMap<Object, Object>();
 map.put("cuId", Integer.valueOf(cus));
 RepaymentPlan repaymentPlan=new RepaymentPlan();
 Integer id=Integer.valueOf(out_trade_no);
 double payablesCheck=Double.valueOf(total_amount);
 RepaymentPlan repayCheck=serviceMain.selectByPrimaryKey(id);
 double total=repayCheck.getPayables();
 if (Double.valueOf(total_amount) < repayCheck.getPayables()) {
 map.put("ubalance", total - Double.valueOf(total_amount));
 serviceMain.updateCusMoney(map);
 }
 repaymentPlan.setId(id);
 repaymentPlan.setActualPayment(total);
 repaymentPlan.setRepaymentStatus(1);
 int i=serviceMain.updateByPrimaryKeySelective(repaymentPlan);
 System.out.println("---------------------还款影响行数----------------------------" + i);*/
 }
 }

2、return_url 接口->同步通知返回的是页面

/**
 * 同步通知的页面的Controller
 * 我这边就简单的返回了一个页面
 * @param request,response
 * @throws InterruptedException
 */
 @RequestMapping("return_url")
 public String Return_url() throws InterruptedException {
 return "alipayexit";
 }

7、请求支付接口的JSP的页面

<form name=alipayment action=alipay.trade.page.pay.jsp method=post
 target="_blank">
 <div id="body1" class="show" name="divcontent">
 <dl class="content">
 <dt>商户订单号 :</dt>
 <dd>
 <input id="WIDout_trade_no" name="WIDout_trade_no" />
 </dd>
 <hr class="one_line">
 <dt>订单名称 :</dt>
 <dd>
 <input id="WIDsubject" name="WIDsubject" />
 </dd>
 <hr class="one_line">
 <dt>付款金额 :</dt>
 <dd>
 <input id="WIDtotal_amount" name="WIDtotal_amount" />
 </dd>
 <hr class="one_line">
 <dt>商品描述:</dt>
 <dd>
 <input id="WIDbody" name="WIDbody" />
 </dd>
 <hr class="one_line">
 <dt></dt>
 <dd id="btn-dd">
 <span class="new-btn-login-sp">
 <button class="new-btn-login" type="submit"
 style="text-align: center;">付 款</button>
 </span> <span class="note-help">如果您点击“付款”按钮,即表示您同意该次的执行操作。</span>
 </dd>
 </dl>
 </div>
 </form>
<!--这里的target为_blank是新打开一个窗口-->

支付宝接口的SDK&DEMO地址:

https://docs.open.alipay.com/270/106291/

获取更多JAVA干货内容,转发+关注。私信我“资料”。

:这里的支付是沙箱模拟支付

1、注册蚂蚁金服开放平台

蚂蚁金服开放平台注册地址:

https://open.alipay.com/platform/home.htm

支付宝扫码登陆 -> 注册为自主研发者

蚂蚁金服页面

2、配置你的沙箱支付宝

配置你的沙箱支付宝

支付宝提供一键生成工具便于开发者生成一对RSA2密钥:

https://docs.open.alipay.com/291/105971

该工具使用需要java环境

windows安装java环境:

https://blog.csdn.net/edison_03/article/details/79757591

Mac安装java环境:

https://www.cnblogs.com/xqx-qyy/p/7659805.html

生成RSA2密钥

注意:生成时一定要选择PKCS8+2048

配置RSA2密钥

将应用网关和回调地址更改为:

https://www.alipay.com

AES密钥不用管

然后往下会有支付宝沙箱安卓端工具,下载,以供后续支付使用

进入页面左侧导航栏沙箱账号,沙箱安卓端安装后用买家账号登陆

到这里基本配置就完了,下面进入大家喜欢的代码时间:

3、新建一个配置类 AlipayConfig.java

package com.alipay.config;
import java.io.FileWriter;
import java.io.IOException;
/* *
 *类名:AlipayConfig
 *作者:有梦想一起实现
 */
public class AlipayConfig{
 // ↓↓↓↓↓↓↓↓↓↓请在这里配置您的基本信息↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
 // 应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号
 public static String app_id="APPID";//例:2016082600317257
 // 商户私钥,您的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="你的服务器地址/项目名称/notify_url";
 // 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
 /**
 * 此页面是同步返回用户页面,也就是用户支付后看到的页面,上面的notify_url是异步返回商家操作,谢谢
 * 要是看不懂就找度娘,或者多读几遍,或者去看支付宝第三方接口API,不看API直接拿去就用,遇坑不怪别人
 */
 public static String return_url=" 你的服务器地址/项目名称/return_url";
 // 签名方式
 public static String sign_type="RSA2";
 // 字符编码格式
 public static String charset="gbk";
 // 支付宝网关
 public static String gatewayUrl="https://openapi.alipaydev.com/gateway.do";
 // 日志地址
 public static String log_path="D:/logs/";
 // ↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
 /**
 * 写日志,方便测试(看网站需求,也可以改成把记录存入数据库)
 *
 * @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();
 }
 }
 }
 }
}

如果你是在本地测试,支付完成不会跳转回调页面,那么就需要外网了

推荐一个东西,叫内网穿透,只要你电脑tomcat启动,可以连接外网,就可以使用。

NATAPP 提供免费的测试足够:

https://natapp.cn/

ngrok或者frp以及其他免费开源,自行搜索了解

4、引入依赖包

<dependency>
 <groupId>com.pentahohub.nexus</groupId>
 <artifactId>alipay-sdk-java</artifactId>
 <version>20150820220052</version>
</dependency>

如果上面的依赖失效或者无法使用,依赖下载地址:

http://central.maven.org/maven2/com/pentahohub/nexus/alipay-sdk-java/20150820220052/alipay-sdk-java-20150820220052.jar

5、支付调用的接口

/**
 * 快捷支付调用支付宝支付接口
 * @param model,id,payables, 
 * @throws IOException,AlipayApiException
 * @return Object
 * @author 有梦想一起实现
 */
 @RequestMapping("alipaySum")
 public Object alipayIumpSum(Model model, String payables, String subject, String body, HttpServletResponse response)
 throws Exception {
 // 获得初始化的AlipayClient
 AlipayClient alipayClient=new DefaultAlipayClient(AlipayConfigInfo.gatewayUrl, AlipayConfigInfo.app_id,
 AlipayConfigInfo.merchant_private_key, "json", AlipayConfigInfo.charset,
 AlipayConfigInfo.alipay_public_key, AlipayConfigInfo.sign_type);
 // 设置请求参数
 AlipayTradePagePayRequest alipayRequest=new AlipayTradePagePayRequest();
 alipayRequest.setReturnUrl(AlipayConfigInfo.return_url);
 alipayRequest.setNotifyUrl(AlipayConfigInfo.notify_url2);
 SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmssSSS");
 // 商户订单号,商户网站订单系统中唯一订单号,必填
 String out_trade_no=sdf.format(new Date());
 // 付款金额,必填
 String total_amount=payables.replace(",", "");
 alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\"," + "\"total_amount\":\"" + total_amount
 + "\"," + "\"subject\":\"" + subject + "\"," + "\"body\":\"" + body + "\","
 + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
 // 请求
 String result=alipayClient.pageExecute(alipayRequest).getBody();
 // System.out.println(result);
 AlipayConfigInfo.logResult(result);// 记录支付日志
 response.setContentType("text/html; charset=gbk");
 PrintWriter out=response.getWriter();
 out.print(result);
 return null;
 }

参数传入是必须有的,不然会报订单信息有误。

如果有其他额外参数,请参考支付宝第三方API文档:

https://docs.open.alipay.com/api_1/alipay.trade.create/

6、支付完成回调

这里支付完成会回调两个接口,notify_url和return_url,就是在配置类配置的两个接口:

1、notify_url接口->异步回调的后台操作

/**
 * 支付完成回调验证操作
 * @param response,request
 * @throws Exception
 * @return void
 * @author 有梦想一起实现
 */
 @RequestMapping("notify_url")
 public void Notify(HttpServletResponse response, HttpServletRequest request) throws Exception {
 System.out.println("----------------------------notify_url------------------------");
 // 商户订单号
 String out_trade_no=new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"), "GBK");
 // 付款金额
 String total_amount=new String(request.getParameter("total_amount").getBytes("ISO-8859-1"), "GBK");
 // 支付宝交易号
 String trade_no=new String(request.getParameter("trade_no").getBytes("ISO-8859-1"), "GBK");
 // 交易说明
 String cus=new String(request.getParameter("body").getBytes("ISO-8859-1"), "GBK");
 // 交易状态
 String trade_status=new String(request.getParameter("trade_status").getBytes("ISO-8859-1"), "GBK");
 if (trade_status.equals("TRADE_SUCCESS")) {//支付成功商家操作
 //下面是我写的一个简单的插入操作,根据你的操作自行编写
 /*Map<Object, Object> map=new HashMap<Object, Object>();
 map.put("cuId", Integer.valueOf(cus));
 RepaymentPlan repaymentPlan=new RepaymentPlan();
 Integer id=Integer.valueOf(out_trade_no);
 double payablesCheck=Double.valueOf(total_amount);
 RepaymentPlan repayCheck=serviceMain.selectByPrimaryKey(id);
 double total=repayCheck.getPayables();
 if (Double.valueOf(total_amount) < repayCheck.getPayables()) {
 map.put("ubalance", total - Double.valueOf(total_amount));
 serviceMain.updateCusMoney(map);
 }
 repaymentPlan.setId(id);
 repaymentPlan.setActualPayment(total);
 repaymentPlan.setRepaymentStatus(1);
 int i=serviceMain.updateByPrimaryKeySelective(repaymentPlan);
 System.out.println("---------------------还款影响行数----------------------------" + i);*/
 }
 }

2、return_url 接口->同步通知返回的是页面

/**
 * 同步通知的页面的Controller
 * 我这边就简单的返回了一个页面
 * @param request,response
 * @throws InterruptedException
 */
 @RequestMapping("return_url")
 public String Return_url() throws InterruptedException {
 return "alipayexit";
 }

7、请求支付接口的JSP的页面

<form name=alipayment action=alipay.trade.page.pay.jsp method=post
 target="_blank">
 <div id="body1" class="show" name="divcontent">
 <dl class="content">
 <dt>商户订单号 :</dt>
 <dd>
 <input id="WIDout_trade_no" name="WIDout_trade_no" />
 </dd>
 <hr class="one_line">
 <dt>订单名称 :</dt>
 <dd>
 <input id="WIDsubject" name="WIDsubject" />
 </dd>
 <hr class="one_line">
 <dt>付款金额 :</dt>
 <dd>
 <input id="WIDtotal_amount" name="WIDtotal_amount" />
 </dd>
 <hr class="one_line">
 <dt>商品描述:</dt>
 <dd>
 <input id="WIDbody" name="WIDbody" />
 </dd>
 <hr class="one_line">
 <dt></dt>
 <dd id="btn-dd">
 <span class="new-btn-login-sp">
 <button class="new-btn-login" type="submit"
 style="text-align: center;">付 款</button>
 </span> <span class="note-help">如果您点击“付款”按钮,即表示您同意该次的执行操作。</span>
 </dd>
 </dl>
 </div>
 </form>
<!--这里的target为_blank是新打开一个窗口-->

支付宝接口的SDK&DEMO地址:

https://docs.open.alipay.com/270/106291/

作者:cuifuan

来源: Java知音

商业用途请与作者联系!

、场景:公司需要在网站上进行支付宝支付。

二、API:使用支付宝开放平台的支付能力-即时到账接口。支付宝开放平台链接

三、分析:

1、支付宝的文档比较容易看,主要是有相应的DEMO,我这里看的DEMO是 JAVA-UTF-8版本。

2、导入DEMO,在com.alipay.config中填入对应的partner和key(在对应的商户后台获取)就可以直接运行了解支付流程了。

3、改写:我这边使用的是springmvc+mybatis。商品发起购买(走支付宝支付)、跳转到支付宝、支付宝回调支付状态。

四、实现:

1、商品发起购买请求(将DEMO中的页面直接拿过来用了)。

[javascript] view plain copy

<body>

<div class="header">

<div class="container black">

<div class="qrcode">

<div class="littlecode">

<img width="16px" src="img/little_qrcode.jpg" id="licode">

<div class="showqrs" id="showqrs">

<div class="shtoparrow"></div>

<div class="guanzhuqr">

<img src="img/guanzhu_qrcode.png" width="80">

<div class="shmsg" style="margin-top:5px;">

请扫码关注

</div>

<div class="shmsg" style="margin-bottom:5px;">

接收重要信息

</div>

</div>

</div>

</div>

</div>

</div>

<div class="container">

<div class="nav">

<a href="https://www.alipay.com/" class="logo"><img src="img/alipay_logo.png" height="30px"></a>

<span class="divier"></span>

<a href="http://open.alipay.com/platform/home.htm" class="open" target="_blank">开放平台</a>

<ul class="navbar">

<li><a href="https://doc.open.alipay.com/doc2/detail?treeId=62&articleId=103566&docType=1" target="_blank">在线文档</a></li>

<li><a href="https://cschannel.alipay.com/portal.htm?sourceId=213" target="_blank">技术支持</a></li>

</ul>

</div>

</div>

<div class="container blue">

<div class="title">支付宝即时到账(create_direct_pay_by_user)</div>

</div>

</div>

<div class="content">

<%-- <form action="${ctx}/aliPay/open" class="alipayform" method="POST" target="_blank"> --%>

<div class="element" style="margin-top:60px;">

<div class="legend">支付宝即时到账交易接口快速通道 </div>

</div>

<div class="element">

<div class="etitle">商户订单号:</div>

<div class="einput"><input type="text" name="WIDout_trade_no" id="out_trade_no"></div>

<br>

<div class="mark">注意:商户订单号(out_trade_no).必填(建议是英文字母和数字,不能含有特殊字符)</div>

</div>

<div class="element">

<div class="etitle">商品名称:</div>

<div class="einput"><input type="text" name="WIDsubject" id="WIDsubject" value="test商品123"></div>

<br>

<div class="mark">注意:产品名称(subject),必填(建议中文,英文,数字,不能含有特殊字符)</div>

</div>

<div class="element">

<div class="etitle">付款金额:</div>

<div class="einput"><input type="text" name="WIDtotal_fee" id="WIDtotal_fee" value="0.01"></div>

<br>

<div class="mark">注意:付款金额(total_fee),必填(格式如:1.00,请精确到分)</div>

</div>

<div class="element">

<div class="etitle">商品描述:</div>

<div class="einput"><input type="text" name="WIDbody" id="WIDbody" value="即时到账测试"></div>

<br>

<div class="mark">注意:商品描述(body),选填(建议中文,英文,数字,不能含有特殊字符)</div>

</div>

<div class="element">

<input type="button" class="alisubmit" id="sbumitBtn" value="确认支付">

</div>

</div>

<div id="returnAli"></div>

<div class="footer">

<p class="footer-sub">

<a href="http://ab.alipay.com/i/index.htm" target="_blank">关于支付宝</a><span>|</span>

<a href="https://e.alipay.com/index.htm" target="_blank">商家中心</a><span>|</span>

<a href="https://job.alibaba.com/zhaopin/index.htm" target="_blank">诚征英才</a><span>|</span>

<a href="http://ab.alipay.com/i/lianxi.htm" target="_blank">联系我们</a><span>|</span>

<a href="#" id="international" target="_blank">International Business</a><span>|</span>

<a href="http://ab.alipay.com/i/jieshao.htm#en" target="_blank">About Alipay</a>

<br>

<span>支付宝版权所有</span>

<span class="footer-date">2004-2016</span>

<span><a href="http://fun.alipay.com/certificate/jyxkz.htm" target="_blank">ICP证:沪B2-20150087</a></span>

</p>

</div>

</body>

2、点击确认支付之后,这里通过ajax请求后台,将返回的一段html代码直接放到上面的<div id="returnALi"></div>中,这个表单会自动提交。

[javascript] view plain copy

$(function (){

$("#sbumitBtn").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){

}

});

})

});

3、后台controller中,基本是将demo中的alipayapi.jsp直接拿来用了,不同的是,参数的传递是自己定义的,返回方式符合apringmvc要求,并且根据业务需求保存了状态为未支付的订单信息。

[javascript] view plain copy

@RequestMapping("open")

public ResponseEntity<HttpEntity> open(Model model, String WIDout_trade_no, String WIDsubject, String WIDtotal_fee,

String WIDbody) {

//////////////////////////////////// 请求参数//////////////////////////////////////

// 商户订单号,商户网站订单系统中唯一订单号,必填

String out_trade_no=WIDout_trade_no;

// 订单名称,必填

String subject=WIDsubject;

// 付款金额,必填

String total_fee=WIDtotal_fee;

// 商品描述,可空

String body=WIDbody;

// 把请求参数打包成数组

Map<String, String> sParaTemp=new HashMap<String, String>();

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);

// 其他业务参数根据在线开发文档,添加参数.文档地址:https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.O9yorI&treeId=62&articleId=103740&docType=1

// 如sParaTemp.put("参数名","参数值");

// 建立请求

String sHtmlText=AlipaySubmit.buildRequest(sParaTemp, "get", "确认");

model.addAttribute("sHtmlText", sHtmlText);

// 保存支付记录

hysWebMeetingAliService.insertSelective(sParaTemp);

return new ResponseEntity(model, HttpStatus.OK);

}

4、回调:也是直接将DEMO中的notify_url.jsp中的java代码拿来稍微做了修改和加上业务代码(修改状态等);

[javascript] view plain copy

@RequestMapping("notify")

@ResponseBody

public String notify(HttpServletRequest request){

//获取支付宝POST过来反馈信息

Map<String,String> params=new HashMap<String,String>();

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 < values.length; i++) {

valueStr=(i==values.length - 1) ? valueStr + values[i]

: valueStr + values[i] + ",";

}

//乱码解决,这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化

//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");

//交易状态

String trade_status=request.getParameter("trade_status");

//获取支付宝的通知返回参数,可参考技术文档中页面跳转同步通知参数列表(以上仅供参考)//

if(AlipayNotify.verify(params)){//验证成功

//////////////////////////////////////////////////////////////////////////////////////////

//请在这里加上商户的业务逻辑程序代码

//——请根据您的业务逻辑来编写程序(以下代码仅作参考)——

boolean flg=false;

if(trade_status.equals("TRADE_FINISHED")){

//判断该笔订单是否在商户网站中已经做过处理

//如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序

//请务必判断请求时的total_fee、seller_id与通知时获取的total_fee、seller_id为一致的

//如果有做过处理,不执行商户的业务程序

//注意:

//退款日期超过可退款期限后(如三个月可退款),支付宝系统发送该交易状态通知

} else if (trade_status.equals("TRADE_SUCCESS")){

//判断该笔订单是否在商户网站中已经做过处理

//如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序

//请务必判断请求时的total_fee、seller_id与通知时获取的total_fee、seller_id为一致的

//如果有做过处理,不执行商户的业务程序

//注意:

//付款完成后,支付宝系统发送该交易状态通知

//根据订单号将订单状态和支付宝记录表中状态都改为已支付

flg=hysWebMeetingAliService.changeOrderAndAliStatusSuccess(out_trade_no);

}

//——请根据您的业务逻辑来编写程序(以上代码仅作参考)——

//out.print("success"); //请不要修改或删除

if(flg){

return "success";

}else{

return "fail";

}

//////////////////////////////////////////////////////////////////////////////////////////

}else{//验证失败

//out.print("fail");

return "fail";

}

}

5、return_url:页面跳转同步通知页面路径,就是支付成功后,支付宝回跳的一个页面。“需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问”,支付宝明确规定了回跳的页面后面不能自定义参数,那么有的我们就是根据一些类型去判断跳转的就有点麻烦了。我这里做了一个取巧的做法:先把return_url.jsp中的java代码也直接搬过来,略做修改(换成springmvc的方式),我的回跳地址就是IP/aliPay/returnUrl,然后再new ModelAndView("redirect:/meeting/info")进行重定向到我们想到的url中,(参数问题看下文总结第4点吧)。

[javascript] view plain copy

@RequestMapping("returnUrl")

public ModelAndView returnUrl(HttpServletRequest request){

ModelAndView mv=new ModelAndView("redirect:/meeting/info");

//获取支付宝GET过来反馈信息

Map<String,String> params=new HashMap<String,String>();

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 < values.length; i++) {

valueStr=(i==values.length - 1) ? valueStr + values[i]

: valueStr + values[i] + ",";

}

//乱码解决,这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化

//valueStr=new String(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");

//交易状态

String trade_status=request.getParameter("trade_status");

<span style="color:#ff0000;">String meetingId=request.getParameter("extra_common_param");

mv.addObject("meetingId", meetingId);</span>

//获取支付宝的通知返回参数,可参考技术文档中页面跳转同步通知参数列表(以上仅供参考)//

//计算得出通知验证结果

boolean verify_result=AlipayNotify.verify(params);

if(verify_result){//验证成功

//////////////////////////////////////////////////////////////////////////////////////////

//请在这里加上商户的业务逻辑程序代码

//——请根据您的业务逻辑来编写程序(以下代码仅作参考)——

if(trade_status.equals("TRADE_FINISHED") || trade_status.equals("TRADE_SUCCESS")){

//判断该笔订单是否在商户网站中已经做过处理

//如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序

//如果有做过处理,不执行商户的业务程序

}

//该页面可做页面美工编辑

// out.println("验证成功<br />");

//——请根据您的业务逻辑来编写程序(以上代码仅作参考)——

//////////////////////////////////////////////////////////////////////////////////////////

}else{

//该页面可做页面美工编辑

// out.println("验证失败");

}

return mv;

}

五、总结:

1、支付宝集成比较简答,稍微看下DEMO,跑一下了解了流程就比较容易了。

2、调试的时候,特别是回调,要把项目部署到能够外网访问的服务器上。

3、我没有遇到上面莫名其妙的问题,如果遇到了的话,可以联系我,或者看下Eclipse远程debug这篇文章,进行问题的跟踪。

4、做页面跳转同步通知页面路径时,需要传递参数怎么办,我一开始是自定义了一个参数,可是没有取到,然后我看到DEMO中有一句注释是这么写的:

[javascript] view plain copy

// 其他业务参数根据在线开发文档,添加参数.文档地址:https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.O9yorI&treeId=62&articleId=103740&docType=1

所以我打开看了之后,选择了公用回传参数extra_common_param,我自己知道这个字段对于我来说是什么含义。不过也要注意,人家说明了:

[javascript] view plain copy

参数body(商品描述)、subject(商品名称)、extra_common_param(公用回传参数)不能包含特殊字符(如:#、%、&、+)、敏感词汇,也不能使用外国文字(旺旺不支持的外文,如:韩文、泰语、藏文、蒙古文、阿拉伯语);

open方法中设值:

[javascript] view plain copy

sParaTemp.put("extra_common_param", meetingId);

returnUrl方法中取值,并作为重定向参数:

[javascript] view plain copy

String meetingId=request.getParameter("extra_common_param");

mv.addObject("meetingId", meetingId);