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中的定位服务进行了详细的介绍与参数说明,在开发中,地位服务往往与地图框架结合使用,这篇博 ...
随机推荐
- Golang 高效实践之并发实践
前言 在我前面一篇文章Golang受欢迎的原因中已经提到,Golang是在语言层面(runtime)就支持了并发模型.那么作为编程人员,我们在实践Golang的并发编程时,又有什么需要注意的点呢?下面 ...
- 对于springboot的几种注入方法的个人看法
最近在知乎上面看到一篇关于程序员面试的问题,面试官问我们一般有几种注入的方法,这几种注入的方法分别在什么时候运用比合理,当时我看到这个时候懵逼了,由于我自己也是刚刚接触springboot不久,所以就 ...
- linux 定时任务 crontabs 安装及使用方法
boom 安装 crontab yum install crontabs centos7 自带了我没有手动去装 启动/关闭 service crond start // 启动服务 service cr ...
- c语言进阶12-线性表之顺序表
一. 线性表的定义 为什么要学习线性表呢? 因为我们日常生活中存在种数据关系,计算机程序是为了解决日常生活的数据关系,因此我们要学习线性表. 线性表是什么呢? 线性表是由n个元素组成的有限序列. 需 ...
- 小白开学Asp.Net Core 《九》
小白开学Asp.Net Core <九> — — 前端篇(不务正业) 在<小白开学Asp.Net Core 三>中使用了X-admin 2.x 和 Layui将管理后端的界面重 ...
- python包-logging-hashlib-openpyxl模块-深浅拷贝-04
包 包: # 包是一系列模块文件的结合体,表现形式是文件夹,该文件夹内部通常会包含一个__init__.py文件,本质上还是一个模块 包呢,就是前两篇博客中提到的,模块的四种表现形式中的第三种 # 把 ...
- 《VR入门系列教程》之14---面向大众的Unity3D
大众化的游戏引擎--Unity3D 并不是所有VR应用都是游戏,然而现在做VR开发的几乎都会用专业游戏引擎来做,因为游戏引擎既满足了一个引擎的要求又可以方便地制作出高品质的VR应用.一个游戏引 ...
- linux初学者-mail篇
linux初学者-mail篇 邮件是在生活中比较常用的一个工具,在linux系统中的邮件也是.在linux中,邮件的发送所用的服务时postfix,邮件的接收所用的服务是pop(110端口).ima ...
- python中的元类(metaclass)
认识python中元类的准备工作. 1,首先需要明白一个概念就是python中一切皆为对象. input: class Trick(object): pass ') print type(1234) ...
- 显示Mac隐藏文件的命令:
设置查看隐藏文件的方法如下:打开终端,输入命名 显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool true 隐藏 ...