支付宝开发文档中心
注意:个人无法使用此功能,因为个人申请使用是不会通过的
1、电脑网站支付,手机网站支付,app支付
查看接入准备,里面详细介绍了创建应用和配置应用的过程,还介绍了接口调用配置的使用即调用支付宝API客户端的配置 分两种模式: 1.一种是公钥模式加签 2.另一种是公钥证书模式加签: 需要注意的是公钥证书模式加签提交数据使用certificateExecute方法
依赖: 依赖位于:
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.33.26.ALL</version>
</dependency>
请详细查看每个(app支付,手机网站支付,电脑网站支付)的接入准备
1.1、异步通知介绍
1.app支付的位于 2.手机网站支付(电脑网站支付类似)的位于
1.2、API和请求示例介绍
app支付,手机网站支付,电脑网站支付类似
2、当面付
当面付文档中心 流程和上面类似
3、小程序支付接入
创建小程序过程详细看一遍(比较麻烦):https://opendocs.alipay.com/mini/development 需要其中的AppID 小程序API:
小程序支付接入 需要注意小程序支付接入和当面付-接入准备一样 请求示例: 需要的详细参数: 注意:buyer_id为必填
获取小程序用户身份user_id(buyer_id)/验证小程序用户/用户授权 my.getAuthCode
4、代码
实现了小程序支付,电脑网站支付,手机网站支付,当面付,及支付回调
yaml中配置 需要注意的是: #通知地址不能直接写成localhost:9090/xxx等,需要使用内网穿透免费的内网穿透工具-飞鸽穿透,什么是内网穿透
alipay-payment:
#支付宝小程序
alipay-mini-app:
#AppID
app-id: "xxx"
#应用私钥
app-private-key-string: "xxx"
#应用公钥证书路径(绝对位置)
app-public-cert-path: "G:/OEM/project/xxxx"
#支付宝公钥证书路径(绝对位置)
alipay-public-cert-path: "G:/OEM/project/xxxx"
#支付宝根证书路径(绝对位置)
alipay-root-cert-path: "G:/OEM/project/xxxx"
#通知地址
#通知地址不能直接写成localhost:9090/xxx等,需要使用内网穿透
notify-url: "http://dreamrenderx.ifast3.vipnps.vip/alipay-payment/alipayMiniAppPaymentNotify"
#Web、Wap、App程序
web:
#AppID
app-id: "xxx"
#应用私钥
app-private-key-string: "xxxx"
#应用公钥证书路径(绝对位置)
app-public-cert-path: "G:/OEM/project/xxxx"
#支付宝公钥证书路径(绝对位置)
alipay-public-cert-path: "G:/OEM/project/xxxx"
#支付宝根证书路径(绝对位置)
alipay-root-cert-path: "G:/OEM/project/xxxx"
#通知地址
notify-url: "http://dreamrenderx.ifast3.vipnps.vip/alipay-payment/webPaymentNotify"
配置支付宝调用客户端,这里用的是公钥证书模式加签:
package com.xunan.demo.config;
import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Slf4j
@Configuration
public class AlipayPaymentConfig {
String GATEWAY_URL = "https://openapi.alipay.com/gateway.do";
String REQUEST_FORMAT = "json";
String REQUEST_CHARSET = "UTF-8";
String SIGN_TYPE = "RSA2";
@Value("${alipay-payment.alipay-mini-app.app-id}")
String ALIPAY_MINI_APP_APP_ID;
@Value("${alipay-payment.alipay-mini-app.app-private-key-string}")
String ALIPAY_MINI_APP_APP_PRIVATE_KEY_STRING;
@Value("${alipay-payment.alipay-mini-app.app-public-cert-path}")
String ALIPAY_MINI_APP_APP_PUBLIC_CERT_PATH;
@Value("${alipay-payment.alipay-mini-app.alipay-public-cert-path}")
String ALIPAY_MINI_APP_ALIPAY_PUBLIC_CERT_PATH;
@Value("${alipay-payment.alipay-mini-app.alipay-root-cert-path}")
String ALIPAY_MINI_APP_ALIPAY_ROOT_CERT_PATH;
@Value("${alipay-payment.web.app-id}")
String WEB_APP_ID;
@Value("${alipay-payment.web.app-private-key-string}")
String WEB_APP_PRIVATE_KEY_STRING;
@Value("${alipay-payment.web.app-public-cert-path}")
String WEB_APP_PUBLIC_CERT_PATH;
@Value("${alipay-payment.web.alipay-public-cert-path}")
String WEB_ALIPAY_PUBLIC_CERT_PATH;
@Value("${alipay-payment.web.alipay-root-cert-path}")
String WEB_ALIPAY_ROOT_CERT_PATH;
private AlipayClient commonCertClientBuilder(String appID,
String appPrivateKeyString,
String appPublicCertPath,
String alipayPublicCertPath,
String alipayRootCertPath) {
try {
CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
certAlipayRequest.setServerUrl(GATEWAY_URL);
certAlipayRequest.setAppId(appID);
certAlipayRequest.setFormat(REQUEST_FORMAT);
certAlipayRequest.setCharset(REQUEST_CHARSET);
certAlipayRequest.setSignType(SIGN_TYPE);
certAlipayRequest.setPrivateKey(appPrivateKeyString);
certAlipayRequest.setCertPath(appPublicCertPath);
certAlipayRequest.setAlipayPublicCertPath(alipayPublicCertPath);
certAlipayRequest.setRootCertPath(alipayRootCertPath);
return new DefaultAlipayClient(certAlipayRequest);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Bean
public AlipayClient webAlipayClient() {
try {
return commonCertClientBuilder(
WEB_APP_ID,
WEB_APP_PRIVATE_KEY_STRING,
WEB_APP_PUBLIC_CERT_PATH,
WEB_ALIPAY_PUBLIC_CERT_PATH,
WEB_ALIPAY_ROOT_CERT_PATH);
} catch (Exception exception) {
exception.printStackTrace();
return null;
}
}
@Bean
public AlipayClient alipayMiniAppAlipayClient() {
try {
return commonCertClientBuilder(
ALIPAY_MINI_APP_APP_ID,
ALIPAY_MINI_APP_APP_PRIVATE_KEY_STRING,
ALIPAY_MINI_APP_APP_PUBLIC_CERT_PATH,
ALIPAY_MINI_APP_ALIPAY_PUBLIC_CERT_PATH,
ALIPAY_MINI_APP_ALIPAY_ROOT_CERT_PATH);
} catch (Exception exception) {
exception.printStackTrace();
return null;
}
}
}
业务实现:
package com.xunan.demo.service;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradeCreateRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradePayRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeCreateResponse;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradePayResponse;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.xunan.demo.pojo.CommonResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class AlipayPaymentService {
@Value("${alipay-payment.alipay-mini-app.notify-url}")
String ALIPAY_MINI_APP_PAYMENT_NOTIFY_URL;
@Value("${alipay-payment.web.notify-url}")
String WEB_PAYMENT_NOTIFY_URL;
@Resource
AlipayClient alipayMiniAppAlipayClient;
@Resource
AlipayClient webAlipayClient;
public String alipayMiniAppPaymentNotify() {
return "success";
}
public String webPaymentNotify() {
return "success";
}
public CommonResult<AlipayTradeCreateResponse> getAlipayMiniAppPayData(Double price, String subject, String buyerId) {
if (alipayMiniAppAlipayClient == null) {
return new CommonResult<>(false, "内部错误", null);
}
AlipayTradeCreateRequest request = new AlipayTradeCreateRequest();
request.setNotifyUrl(ALIPAY_MINI_APP_PAYMENT_NOTIFY_URL);
String outTradeNumber = IdUtil.simpleUUID();
JSONObject requestData = new JSONObject();
requestData.put("out_trade_no", outTradeNumber);
requestData.put("total_amount", price);
requestData.put("subject", subject);
requestData.put("buyer_id", buyerId);
request.setBizContent(requestData.toString());
try {
AlipayTradeCreateResponse response = alipayMiniAppAlipayClient.certificateExecute(request);
return new CommonResult<>(true, "成功", response);
} catch (AlipayApiException e) {
e.printStackTrace();
return new CommonResult<>(false, "内部错误", null);
}
}
public CommonResult<AlipayTradePagePayResponse> getPagePayData(Double price, String subject) {
if (webAlipayClient == null) {
return new CommonResult<>(false, "内部错误", null);
}
AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
request.setNotifyUrl(WEB_PAYMENT_NOTIFY_URL);
String outTradeNumber = IdUtil.simpleUUID();
JSONObject requestData = new JSONObject();
requestData.put("out_trade_no", outTradeNumber);
requestData.put("total_amount", price);
requestData.put("subject", subject);
requestData.put("product_code", "FAST_INSTANT_TRADE_PAY");
request.setBizContent(requestData.toString());
try {
AlipayTradePagePayResponse response = webAlipayClient.certificateExecute(request);
return new CommonResult<>(true, "成功", response);
} catch (AlipayApiException e) {
e.printStackTrace();
return new CommonResult<>(false, "内部错误", null);
}
}
public CommonResult<AlipayTradeWapPayResponse> getWapPayData(Double price, String subject, String quitUrl) {
if (webAlipayClient == null) {
return new CommonResult<>(false, "内部错误", null);
}
AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
request.setNotifyUrl(WEB_PAYMENT_NOTIFY_URL);
String outTradeNumber = IdUtil.simpleUUID();
JSONObject requestData = new JSONObject();
requestData.put("out_trade_no", outTradeNumber);
requestData.put("total_amount", price);
requestData.put("subject", subject);
requestData.put("product_code", "QUICK_WAP_WAY");
requestData.put("quit_url", quitUrl);
request.setBizContent(requestData.toString());
try {
AlipayTradeWapPayResponse response = webAlipayClient.certificateExecute(request);
return new CommonResult<>(true, "成功", response);
} catch (AlipayApiException e) {
e.printStackTrace();
return new CommonResult<>(false, "内部错误", null);
}
}
public CommonResult<AlipayTradePayResponse> codePay(Double price, String subject, String authCode) {
if (webAlipayClient == null) {
return new CommonResult<>(false, "内部错误", null);
}
AlipayTradePayRequest request = new AlipayTradePayRequest();
request.setNotifyUrl(WEB_PAYMENT_NOTIFY_URL);
String outTradeNumber = IdUtil.simpleUUID();
JSONObject requestData = new JSONObject();
requestData.put("out_trade_no", outTradeNumber);
requestData.put("total_amount", price);
requestData.put("subject", subject);
requestData.put("scene", "bar_code");
requestData.put("auth_code", authCode);
request.setBizContent(requestData.toString());
try {
AlipayTradePayResponse response = webAlipayClient.certificateExecute(request);
return new CommonResult<>(true, "成功", response);
} catch (AlipayApiException e) {
e.printStackTrace();
return new CommonResult<>(false, "内部错误", null);
}
}
}
完整代码
https://gitee.com/xunan29/paymentdemo-boot-project
|