springboot+支付宝条码支付开发详解
背景:项目原有乐刷聚合支付,无法参加支付宝、微信等支付机构的官方活动
需求:增加原生支付(支付宝条码支付)
方法:
一、官方文档:https://docs.open.alipay.com/194/106039
二、没有真实企业账号的可以使用沙箱环境:https://docs.open.alipay.com/200/105311
三、两种方法实现:
(一)基于支付宝标准SDK:alipay-sdk-java
1、pom文件中添加SDK依赖
地址:https://mvnrepository.com/artifact/com.alipay.sdk/alipay-sdk-java
2、配置支付宝参数
在系统常量类里追加(我这里配的是沙箱参数,就直接写在实现类里)

3.实现代码
package com.bhp.aaa.bbb.service; import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeCancelRequest;
import com.alipay.api.request.AlipayTradePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeCancelResponse;
import com.alipay.api.response.AlipayTradePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.google.common.collect.ImmutableMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wustrive.java.core.request.ViewResult;
import org.wustrive.java.dao.jdbc.dao.BaseDao; import javax.servlet.http.HttpServletRequest;
import java.util.Map; @Service
public class AlipayService1 {
// 正式
/*private static String gateway = SysConstants.Alipay.gateway;
private static String appId = SysConstants.Alipay.app_id;
private static String privateKey = SysConstants.Alipay.app_private_key;
private static String publicKey = SysConstants.Alipay.alipay_public_key;*/ // 沙箱
//网关
private static String gateway = "https://openapi.alipaydev.com/gateway.do";
//应用ID
private static String appId = "你自己的APPID";
//应用秘钥
private static String privateKey = "你自己的应用秘钥";
//支付宝公钥
private static String publicKey = "你自己的支付宝公钥"; @Autowired
private BaseDao baseDao; // 初始化一个统一的客户端
AlipayClient alipayClient = new DefaultAlipayClient(gateway,appId,privateKey,"json","utf-8",publicKey,"RSA2"); // 发起支付
public ViewResult alipayPay(AppUser appUser, String outTradeNo, String payMoney, String authCode){ ViewResult viewResult = ViewResult.newInstance();
String sql = "SELECT short_name FROM sys_merchants WHERE id=:merchantsId";
String short_name = baseDao.queryForString(sql, ImmutableMap.of("merchantsId",appUser.getMerchantsId())); JSONObject data = new JSONObject();
data.put("out_trade_no",outTradeNo);
data.put("scene","bar_code");
data.put("auth_code",authCode);
data.put("subject",short_name+"消费");
data.put("store_id",appUser.getShopId());
data.put("total_amount",payMoney); AlipayTradePayRequest request = new AlipayTradePayRequest();
//request.setNotifyUrl(notifyUrl);
request.setBizContent(data.toJSONString());
try {
AlipayTradePayResponse response = alipayClient.execute(request);
if(response.isSuccess()){
viewResult.success(response.getBody());
} else {
viewResult.fail(response.getBody());
}
} catch (AlipayApiException e) {
viewResult.fail(e);
e.printStackTrace();
}finally {
return viewResult;
}
} // 发起查询
public ViewResult alipayQuery(String trade_no ){
ViewResult viewResult = ViewResult.newInstance();
JSONObject data = new JSONObject();
data.put("trade_no",trade_no);
AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
request.setBizContent(data.toJSONString());
try {
AlipayTradeQueryResponse response = alipayClient.execute(request);
if(response.isSuccess()){
viewResult.success(response.getBody());
} else {
viewResult.fail(response.getBody());
}
} catch (AlipayApiException e) {
viewResult.fail(e);
e.printStackTrace();
} finally {
return viewResult;
}
} // 发起撤销
public ViewResult alipayCancel(String trade_no){
ViewResult viewResult = ViewResult.newInstance();
JSONObject data = new JSONObject();
data.put("trade_no",trade_no);
AlipayTradeCancelRequest request = new AlipayTradeCancelRequest();
request.setBizContent(data.toJSONString());
try {
AlipayTradeCancelResponse response = alipayClient.execute(request);
if(response.isSuccess()){
viewResult.success(response.getBody());
} else {
viewResult.fail(response.getBody());
}
} catch (AlipayApiException e) {
viewResult.fail(e);
e.printStackTrace();
} finally {
return viewResult;
}
} // 发起退款
public ViewResult alipayRefund(String trade_no,String refund_amount){
ViewResult viewResult = ViewResult.newInstance();
JSONObject data = new JSONObject();
data.put("trade_no",trade_no);
data.put("refund_amount",refund_amount);
AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
request.setBizContent(data.toJSONString());
try {
AlipayTradeRefundResponse response = alipayClient.execute(request);
if(response.isSuccess()){
viewResult.success(response.getBody());
} else {
viewResult.fail(response.getBody());
}
} catch (AlipayApiException e) {
viewResult.fail(e);
e.printStackTrace();
} finally {
return viewResult;
}
} }
支付宝条码支付方法(一)
未完,待续……
springboot+支付宝条码支付开发详解的更多相关文章
- ****基于H5的微信支付开发详解[转]
这次总结一下用户在微信内打开网页时,可以调用微信支付完成下单功能的模块开发,也就是在微信内的H5页面通过jsApi接口实现支付功能.当然了,微信官网上的微信支付开发文档也讲解的很详细,并且有实现代码可 ...
- 基于H5的微信支付开发详解
这次总结一下用户在微信内打开网页时,可以调用微信支付完成下单功能的模块开发,也就是在微信内的H5页面通过jsApi接口实现支付功能.当然了,微信官网上的微信支付开发文档也讲解的很详细,并且有实现代码可 ...
- ***PHP基于H5的微信支付开发详解(CI框架)
这次总结一下用户在微信内打开网页时,可以调用微信支付完成下单功能的模块开发,也就是在微信内的H5页面通过jsApi接口实现支付功能.当然了,微信官网上的微信支付开发文档也讲解的很详细,并且有实现代码可 ...
- springboot项目--传入参数校验-----SpringBoot开发详解(五)--Controller接收参数以及参数校验----https://blog.csdn.net/qq_31001665/article/details/71075743
https://blog.csdn.net/qq_31001665/article/details/71075743 springboot项目--传入参数校验-----SpringBoot开发详解(五 ...
- EasyPR--开发详解(6)SVM开发详解
在前面的几篇文章中,我们介绍了EasyPR中车牌定位模块的相关内容.本文开始分析车牌定位模块后续步骤的车牌判断模块.车牌判断模块是EasyPR中的基于机器学习模型的一个模块,这个模型就是作者前文中从机 ...
- 【转发】NPAPI开发详解,Windows版
NPAPI开发详解,Windows版 9 jiaofeng601, +479 9人支持,来自Meteor.猪爪.hanyuxinting更多 .是非黑白 .Yuan Xulei.hyolin.Andy ...
- 热烈祝贺华清远见《ARM处理器开发详解》第2版正式出版
2014年6月,由华清远见研发中心组织多名业 内顶尖讲师编写的<ARM处理器开发详解>一书正式出版.本书以S5PV210处理器为平台,详细介绍了嵌入式系统开发的各个主要环节,并注重实践,辅 ...
- 嵌入式Linux应用程序开发详解------(创建守护进程)
嵌入式Linux应用程序开发详解 华清远见 本文只是阅读文摘. 创建一个守护进程的步骤: 1.创建一个子进程,然后退出父进程: 2.在子进程中使用创建新会话---setsid(): 3.改变当前工作目 ...
- iOS原生地图开发详解
在上一篇博客中:http://my.oschina.net/u/2340880/blog/414760.对iOS中的定位服务进行了详细的介绍与参数说明,在开发中,地位服务往往与地图框架结合使用,这篇博 ...
随机推荐
- 跨站脚本攻击(存储型xss)笔记(二)
测试目标字段:页面下方的红色框位置. 由于编辑状态是编辑器,所以有可能出现跨站! 我插了个input然而并没有当代码执行 可能有些测试人员就认为被过滤掉了,因为尝试了各种尖括号.js事件.转义.编码等 ...
- R018---RPA是什么东东?
1.缘起 这个问题,很多文章回答过,一直想站在客户角度写个答案,今天正好. 2.RPA的名字 RPA是英文Robotic Process Automation的缩写,中文爱翻译为“流程自动化机器人” ...
- .net持续集成cake篇之使用vs或者vscode来辅助开发cake脚本
使用Visual Studio来开发工具 前面我们都是通过手写或者复制的方法来编写Cake文件,Cake使用的是C#语言,如果仅使用简单的文本编辑器来编写显然效率是非常低下的,本节我们讲解如何使用ca ...
- vue-cli3.x中使用axios发送请求,配合webpack中的devServer编写本地mock数据接口(get/post/put/delete)
vue-cli3.x中使用axios发送请求,配合webpack中的devServer编写本地mock数据接口(get/post/put/delete) 手把手式笔记 Axios配置 安装 axios ...
- linux作业控制和文件系统
一.作业控制 [root@tianyun ~]# sleep 2000运行一个程序,当前终端无法输入. 1 直接运行后台程序.暂停一个前台程序.[root@tianyun ~]# sleep 300 ...
- ArcGIS API For JavaScript 开发(三)使用小部件设计页面框架
其实上一个的鹰眼.比例尺.图例等都是小部件:这篇文章主要是页面布局设计,dojo提供了非常多的小部件,从功能的角度可以分为3大类:表单小部件.布局小部件和应用小部件. 表单小部件于HTML中的表单部件 ...
- jmeter性能小试全流程
大纲: 1.添加线程组:虚拟用户 2.添加测试对象:比如http请求 3.查看结果 一.添加线程组. 1.线程是what: JMeter是由Java实现的,并且使用一个Java线程来模拟一个用户,因此 ...
- 【Java中级】(三)IO
1. 流分为字节流和字符流 2. 字节流下面常用的又有数据流和对象流 3. 字符流下面常用的又有缓存流 文件对象 文件和文件夹都用File表示 //file path : 文件的绝对路径或相对路径Fi ...
- sysctl -p详解
个人一般sysctl -p 或sysctl -a比较多使用 sysctl配置与显示在/proc/sys目录中的内核参数.可以用sysctl来设置或重新设置联网功能,如IP转发.IP碎片去除以及源路由检 ...
- linux初学者-磁盘拉伸缩减篇
linux初学者-磁盘拉伸缩减篇 在系统的使用过程中,往往会出现这样的问题,由于刚开始无法估计需要的磁盘空间,导致后期磁盘空间不够,使得数据没地方存储,又或者后期磁盘空间过大,造成资源的浪费.这种在使 ...