21

支付宝支付新版 SDK 上线,让支付触手可及

 3 years ago
source link: https://mp.weixin.qq.com/s?__biz=MzA3OTUyNjkwMw%3D%3D&%3Bmid=2656656691&%3Bidx=1&%3Bsn=1bee40fc08d67bd77d3c2cacb1f178d9
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

点击▲关注 “ 爪哇妹 ” 给公众号标星置顶

更多摄影技巧 第一时间直达

jEFJJvA.jpg!mobile

前言

撸主最早接触支付是在11年左右,那会支付宝还可以申请个人支付接口,接入Discuz是爽的不行。后来又接触了企业支付,中间陆陆续续踩坑无数,为了不让以后的开发者小伙伴们踩坑,撸主在17年左右把踩坑项目开源了,一直也在断断续续的更新,期间也帮助了不少做支付的小伙伴。

新的开始

回头来看,代码是写的真烂,但是凑合能用,也实在是没有重写的动力。机缘巧合,结识了一位做支付的朋友,让我拾起了重写的动力。最近把支付宝和微信支付的部分逻辑进行了重写,引入了新的配置方式和新的SDK。没错,最近支付宝支付又升级了!

配置

坐标 pom.xml 引入新版支付宝 SDK:

<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-easysdk</artifactId>
<version>2.1.0</version>
</dependency>

配置文件 application-dev.properties 引入支付宝参数:

cp.ali.pay.protocol = http
cp.ali.pay.gatewayHost = openapi.alipaydev.com
cp.ali.pay.signType = RSA2
cp.ali.pay.appId = 2016101300680499
cp.ali.pay.merchantPrivateKey = ******
cp.ali.pay.aliPayPublicKey = ******
cp.ali.pay.notifyUrl = ""
cp.ali.pay.encryptKey = ""

定义实体类,获取配置参数:

@Data
@ConfigurationProperties(prefix = "cp.ali.pay")
public class CpAliPayProperties {

private String protocol;
private String gatewayHost;
private String signType;
private String appId;
private String merchantPrivateKey;
private String aliPayPublicKey;
private String notifyUrl;
private String encryptKey;
}

定义支付宝参数工具类,并初始化 Factory:

/**
* 初始化支付
* 爪哇笔记 https://blog.52itstyle.vip
* @author 小柒2012
*/

@Component
@Configuration
@EnableConfigurationProperties({CpAliPayProperties.class})
public class CpPayUtils {

private CpAliPayProperties aliPay;

public CpPayUtils(CpAliPayProperties aliPay) {
this.aliPay = aliPay;
Config config = new Config();
config.protocol = aliPay.getProtocol();
config.gatewayHost = aliPay.getGatewayHost();
config.signType = aliPay.getSignType();
config.appId = aliPay.getAppId();
/**
* 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
*/

config.merchantPrivateKey = aliPay.getMerchantPrivateKey();
config.alipayPublicKey = aliPay.getAliPayPublicKey();
config.notifyUrl = aliPay.getNotifyUrl();
config.encryptKey = aliPay.getEncryptKey();
Factory.setOptions(config);
}

public CpAliPayProperties getConfig(){
return aliPay;
}
}

发起API调,以创建当面付收款二维码为例:

AlipayTradePrecreateResponse response = Payment.FaceToFace()
.preCreate("Apple iPhone13 128G", "202012108856987", "6999.00");
// 处理响应或异常
if (ResponseChecker.success(response)) {
System.out.println("调用成功");
} else {
System.err.println("调用失败,原因:" + response.msg + "," + response.subMsg);
}

设置独立的异步通知地址:

Factory.Payment.FaceToFace()
// 此处设置的异步通知地址的优先级高于全局Config中配置的异步通知地址
.asyncNotify("https://blog.52itstyle.vip/callback")
.preCreate("Apple iPhone13 256G", "202012108856987", "6999.00");

小结

新版 SDK 对开放能力的 API 进行了更加贴近高频场景的精心设计与裁剪,简化了服务端调用方式,支持灵活的动态扩展方式,同样可以满足低频参数、低频 API 的使用需求。让开发者享受极简编程体验,快速访问支付宝开放平台开放的各项核心能力。

源码

支付服务:支付宝,微信,银联详细代码案例

https://gitee.com/52itstyle/spring-boot-pay

END  -

支付宝微信一码付上线了,欢迎体验

分享六个经典的 SpringBoot 开源项目

一套通用的后台管理系统,赚钱就靠它了!

SpringBoot 开发案例之接入腾讯云短信

推荐一款基于 Java 的身份证号码识别系统

分享一个支付大屏实时监控数据平台

推荐一款清爽的实时监控大屏附安装教程

微信支付收银台功能上线了

ZZruUn.jpg!mobile

你点的每个在看,我都认真当成了喜欢


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK