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中的定位服务进行了详细的介绍与参数说明,在开发中,地位服务往往与地图框架结合使用,这篇博 ...
随机推荐
- Mllib数据类型(密集向量和稀疏向量)
1.局部向量 Mllib支持2种局部向量类型:密集向量(dense)和稀疏向量(sparse). 密集向量由double类型的数组支持,而稀疏向量则由两个平行数组支持. example: 向量(5.2 ...
- pycharm install python packaging tools时遇到AttributeError: '_NamespacePath' object has no attribute 'sort'错误
pycharm install python packaging tools时报错AttributeError: '_NamespacePath' object has no attribute 's ...
- python 的深浅拷贝问题
深浅拷贝概念 基本类型和引用类型数据拷贝的问题.因为基本类型的数据大小是固定的,所以他保存在栈内存中:而引用类型的数据大小不固定,因而保存在堆内存中,单引用类型在栈内存中只保存一个指向堆内存的指针. ...
- Spring Cloud中Hystrix、Ribbon及Feign的熔断关系是什么?
导读 今天和大家聊一聊在Spring Cloud微服务框架实践中,比较核心但是又很容易把人搞得稀里糊涂的一个问题,那就是在Spring Cloud中Hystrix.Ribbon以及Feign它们三者之 ...
- 个人永久性免费-Excel催化剂功能第99波-手机号码归属地批量查询
高潮过往趋于平静,送上简单的手机号码归属地查询,因接口有数量限制,仅能满足少量数据需求,如有大规模数据却又想免费获得,这就成为无解了,数据有价,且用且珍惜. 业务使用场景 除了日常自带的手机各种管家为 ...
- 【干货干货】hyperledger fabric 之动态添加组织/修改配置 (Fabric-java-sdk) 下
我们接着上一节来讲: 在熟悉动态增加组织或修改配置的步骤后,我们就可以使用java的api来完成动态增加组织或修改配置了: 废话不多说,直接上干货: 1,预制条件 org3的证书以及组织3的MSP详情 ...
- 记一次愚蠢的经历--String不可变性
前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 记录一次在写代码时愚蠢的操作,本文涉及到的知识点:S ...
- docker实战(一)之Tomcat的安装
docker号称分分钟就可以将环境构建完成,这话一点也不假,因为docker在使用软件时只需要从官方 仓库中拉取对应的镜像就行了.docker的使用前需要了解两个名词--镜像和容器.这两 ...
- Tomcat(Windows)
百度云:链接:http://pan.baidu.com/s/1pKYrf79 密码:56t0 官网下载网址:http://archive.apache.org/dist/tomcat/tomca ...
- Jquery 小结
1. 名词解释 实例对象:var p1=new Person(); p1就是实例对象 构造:function Person(){} 原型对象:在 JavaScript 中,每当定义一个对象(函数也是 ...