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中的定位服务进行了详细的介绍与参数说明,在开发中,地位服务往往与地图框架结合使用,这篇博 ...
随机推荐
- [记录]优化Linux 的内核参数来提高服务器并发处理能力
优化Linux 的内核参数来提高服务器并发处理能力PS:在服务器硬件资源额定有限的情况下,最大的压榨服务器的性能,提高服务器的并发处理能力,是很多运维技术人员思考的问题.要提高Linux 系统下的负载 ...
- Python 爬虫:豆瓣电影Top250,包括电影导演、类型、年份、主演
结果输出到文本文件中. import codecs import requests from bs4 import BeautifulSoup headers={'User-Agent': 'Mozi ...
- SP1805 HISTOGRA - Largest Rectangle in a Histogram 题解
题目链接:https://www.luogu.org/problemnew/show/SP1805 分析: 我们可以用一个单调栈由低到高来存储它的高度,并用数组对每个高度记录一下它前面(包括它自己)一 ...
- 实验吧--web--你真的会php吗
---恢复内容开始--- 实验吧的一道题php审计题.拉下来写一写. http://ctf5.shiyanbar.com/web/PHP/index.php 打开之后说have fun 那就抓包来看看 ...
- springboot4自动配置的原理(浅层)
自动配置的原理(浅层) @Configuration //这是一个配置类 @EnableConfigurationProperties(HttpProperties.class)//启用Configu ...
- [leetcode] 392. Is Subsequence (Medium)
原题 判断子序列 /** * @param {string} s * @param {string} t * @return {boolean} */ var isSubsequence = func ...
- 【ML入门】李宏毅机器学习笔记01-Learning Map
版权声明:小博主水平有限,希望大家多多指导.本文仅代表作者本人观点,转载请联系知乎原作者——BG大龍. 目录 1 什么是机器学习? 2 机器学习的3个步骤 3 李宏毅老师的机器学习课程 4 按“模型的 ...
- 高级MySQL
一.MySQL的架构介绍 1.高级MySQL MySQL内核 SQL优化 MySQL服务器的优化 各种参数常亮设定 查询语句优化 主从复制 软硬件升级 容灾备份 SQL编程 2.MySQL的Linux ...
- FD limit (65535) too low for maxconn=65535/maxsock=131084.Please raise 'ulimit-n' to 131084 or more to avoid any trouble.
在安装完Haproxy启动时报错,诸如:FD limit (65535) too low for maxconn=65535/maxsock=131084.Please raise 'ulimit-n ...
- 19个心得,明明白白说Linux下的负载均衡
一.目前网站架构一般分成负载均衡层.web层和数据库层,我其实一般还会多加一层,即文件服务器层,因为现在随着网站的PV越来越多,文件服务器的压力也越来越大;不过随着moosefs.DRDB+Heart ...