支付宝个人收款解决方案
步骤
- 创建
网页/移动应用
https://open.alipay.com/develop/manage - 设置
接口加签方式
- 添加
FROM平台
- alipay.open.auth.userauth.cancelled 【用户授权取消消息】
- alipay.trade.refund.depositback.completed【收单退款冲退完成通知】
- 产品绑定
当面付
注意事项
- 下载
密钥工具
https://opendocs.alipay.com/common/02kipk
开发
- 引入
pom
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-easysdk</artifactId>
<version>2.2.1</version>
</dependency>
- 样例代码
package com.mall;
import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.facetoface.models.AlipayTradePrecreateResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mall.entity.resp.AlipayTradeBaseResponse;
import com.mall.util.NumberUtil;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
/**
* grant
* 19/11/2021 11:52 上午
* 描述:
*/
public class AlipayTest {
public static void main(String[] args) throws Exception {
// 1. 设置参数(全局只需设置一次)
Factory.setOptions(getOptions());
try {
// 2. 发起API调用(以创建当面付收款二维码为例)
AlipayTradePrecreateResponse response = Factory.Payment.FaceToFace()
.preCreate("话费充值", NumberUtil.genOrderNo(), "0.1");
// 3. 处理响应或异常
if (ResponseChecker.success(response)) {
System.out.println("调用成功");
System.out.println(response.httpBody);
AlipayTradeBaseResponse alipayTradeBaseResponse = new ObjectMapper().readValue(response.httpBody, AlipayTradeBaseResponse.class);
System.out.println(alipayTradeBaseResponse);
} else {
System.err.println("调用失败,原因:" + response.msg + "," + response.subMsg);
}
} catch (Exception e) {
System.err.println("调用遭遇异常,原因:" + e.getMessage());
throw new RuntimeException(e.getMessage(), e);
}
}
private static Config getOptions() throws IOException {
Config config = new Config();
config.protocol = "https";
config.gatewayHost = "openapi.alipay.com";
config.signType = "RSA2";
config.appId = "2021002193625906";
// 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
config.merchantPrivateKey =
IOUtils.toString(
Thread.currentThread().getContextClassLoader().getResourceAsStream(
"config/appPrivate.txt"
)
);//"<-- 请填写您的应用私钥,例如:MIIEvQIBADANB ... ... -->";
//注:证书文件路径支持设置为文件系统中的路径或CLASS_PATH中的路径,优先从文件系统中加载,加载失败后会继续尝试从CLASS_PATH中加载
config.merchantCertPath = "config/appCertPublicKey_2021002193625906.crt";
config.alipayCertPath = "config/alipayCertPublicKey_RSA2.crt";
config.alipayRootCertPath = "config/alipayRootCert.crt";
//注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可
// config.alipayPublicKey = "<-- 请填写您的支付宝公钥,例如:MIIBIjANBg... -->";
//可设置异步通知接收服务地址(可选)
config.notifyUrl = "http://110.40.196.223:28390/payNotify/alipay";
//可设置AES密钥,调用AES加解密相关接口时需要(可选)
// config.encryptKey = "<-- 请填写您的AES密钥,例如:aa4BtZ4tspm2wnXLb1ThQA== -->";
return config;
}
}