微信登录网页授权与APP授权
微信JSAPI支付
微信APP支付
微信APP和JSAPI退款
支付宝手机网站支付
支付宝APP支付
支付宝退款
以上我都放到个人公众号,搜一搜:JAVA大贼船,文末有公众号二维码!觉得个人以后开发会用到的可以关注一下哦!少走点弯路…
https://opendocs.alipay.com/open/204/105297
https://opendocs.alipay.com/apis/api_1/alipay.trade.app.pay
application.yml
# 支付宝相关
ALIPAY:
# 应用ID
APP_ID:
# 应用私钥
APP_PRIVATE_KEY:
#应用公钥
APP_PUBLIC_KEY:
# 支付宝公钥
ALIPAY_PUBLIC_KEY:
# 统一签名算法
SIGN_TYPE: RSA2
# 统一字符
CHARSET: utf-8
# 返回数据格式
FORMAT: json
# 支付基础API
SERVER_URL: https://openapi.alipay.com/gateway.do
#支付宝支付回调地址
ALIPAY_NOTIFY_URL:
# 支付宝收银台会自动跳转回商户return_url指定的页面。
RETURN_URL:
# 支付宝Api版本
API_VERSION: 1.0
# 产品码
PROD_CODE:
# 终端信息
TERMINAL_INFO:
# 终端信息类型
TERMINAL_TYPE:
YmlParament
//支付宝相关
@Value("${ALIPAY.APP_ID}")
private String appId;
@Value("${ALIPAY.APP_PRIVATE_KEY}")
private String appPrivatekey;
@Value("${ALIPAY.APP_PUBLIC_KEY}")
private String appPublickey;
@Value("${ALIPAY.ALIPAY_PUBLIC_KEY}")
private String alipayPublickey;
@Value("${ALIPAY.SIGN_TYPE}")
private String signType;
@Value("${ALIPAY.CHARSET}")
private String charset;
@Value("${ALIPAY.FORMAT}")
private String format;
@Value("${ALIPAY.SERVER_URL}")
private String serverUrl;
@Value("${ALIPAY.ALIPAY_NOTIFY_URL}")
private String alipayNotifyUrl;
@Value("${ALIPAY.RETURN_URL}")
private String returnUrl;
@Value("${ALIPAY.API_VERSION}")
private String apiVersion;
@Value("${ALIPAY.PROD_CODE}")
private String prodCode;
@Value("${ALIPAY.TERMINAL_INFO}")
private String terminalInfo;
@Value("${ALIPAY.TERMINAL_TYPE}")
private String terminalType;
AlipayConfig
@Autowired
private YmlParament ymlParament;
/**
* 设置支付宝客户端
* @return
*/
@Bean
public AlipayClient setAlipayClient() {
return new DefaultAlipayClient(
ymlParament.getServerUrl(),
ymlParament.getAppId(),
ymlParament.getAppPrivatekey(),
ymlParament.getFormat(),
ymlParament.getCharset(),
//这里不要搞错,这里是应用公钥而不是支付宝公钥
ymlParament.getAppPublickey(),
ymlParament.getSignType()
);
}
/**
* App支付请求公共参数
* 参考官方链接:https://opendocs.alipay.com/open/54/106370
* @return
*/
@Bean
public AlipayTradeAppPayRequest setAlipayTradeAppPayRequest () {
return new AlipayTradeAppPayRequest();
}
ZfbPay
/*
*支付宝app支付
* @param ac 支付宝客户端,传入 @Autowired AlipayClient
* @param rquest 基础参数,传入 @Autowired AlipayTradeAppPayRequest
* @param notifyUrl 付款成功后通知的页面或方法
* @param mode 实际参数,参考https://opendocs.alipay.com/open/204/105465
* @return AlipayTradeAppPayResponse 返回类型AlipayTradeAppPayResponse
* @author aotezi
* @date 2020/4/13 14:00
*/
public static AlipayTradeAppPayResponse appPay(AlipayClient ac, AlipayTradeAppPayRequest rquest
, String notifyUrl, String orderNo, String subject, String orderAmount) throws AlipayApiException {
AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
model.setOutTradeNo(orderNo);
model.setSubject(subject);
model.setTotalAmount(orderAmount);
rquest.setBizModel(model);
rquest.setNotifyUrl(notifyUrl);
return ac.sdkExecute(rquest);
}
/**
* 支付宝支付回调验证签名
* 验证签名;参考:https://opendocs.alipay.com/open/203/105286
* @param params 参数
* @param alipayPublickey 传入支付宝公钥
* @return true 正确 false 失败
* @throws AlipayApiException
*/
public static boolean rsaCheckV1(Map<String, String> params, String alipayPublickey) throws AlipayApiException {
return AlipaySignature.rsaCheckV1(params, alipayPublickey, "utf-8", "RSA2");
}
服务层(业务逻辑略)
@Autowired
private AlipayClient alipayClient;
@Autowired
private AlipayTradeAppPayRequest alipayTradeAppPayRequest;
@Transactional
@Override
public String insertAlipayAppPayInfo(String sendOrderNo, String subject, String orderAmount) {
try {
AlipayTradeAppPayResponse alipayTradeAppPayResponse = ZfbPay.appPay(alipayClient, alipayTradeAppPayRequest,
ymlParament.getAlipayNotifyUrl(), sendOrderNo,subject, orderAmount);
if (!alipayTradeAppPayResponse.isSuccess()) {
throw new RuntimeException(alipayTradeAppPayResponse.getMsg());
}
return alipayTradeAppPayResponse.getBody()
} catch (AlipayApiException e) {
e.printStackTrace();
}
}
controller(略)
文档地址:https://opendocs.alipay.com/open/204/105296
文档地址:https://opendocs.alipay.com/open/204/105295
Payment模块管理支付功能,用于提供网页安全支付能力,支持通过Web接口进行支付操作。通过plus.payment可获取支付管理对象。
支付接口可使得网页开发人员能获取浏览器支持的支付渠道进行支付操作,而不需要安装额外的浏览器支付插件。 规范不定义支付渠道安全认证及支付操作通讯协议,由运行环境的支付模块根据支付服务器接入规范实现。
集成了微信APP支付和支付宝APP支付:https://www.html5plus.org/doc/zh_cn/payment.html
官方文档:https://opendocs.alipay.com/open/203/105286
@ApiOperation("支付宝回调")
@PostMapping("callback")
public String callback(HttpServletRequest request) throws Exception {
Object object = new Object();
Map<String, String> params = convertRequestParamsToMap(request);
log.info("支付宝回调:已回调,参数为====" + params);
try {
checkCallbackAlipay(params);
/*处理一些业务*/
} catch (Exception e) {
e.printStackTrace();
}
return ZfbUtils.RETURN_SUCCESS;
}
}
/**
* 校验签名
*验签文档:https://opendocs.alipay.com/open/203/105286
*/
private void checkCallbackAlipay(Map<String, String> params) throws Exception {
if (!"TRADE_SUCCESS".equals(params.get("trade_status"))) {
throw new Exception("支付宝支付失败!");
}
//校验签名
if (!ZfbPay.rsaCheckV1(params, ymlParament.getAlipayPublickey())) {
throw new Exception("支付宝回调签名认证失败");
}
//校验一些业务逻辑
//订单是否支付重复
//判断total_amount是否确实为该订单的实际金额(即商户订单创建时的金额)
//.....
// 校验通知中的seller_id(或者seller_email)是否为out_trade_no这笔单据的对应的操作方(有的时候,一个商户可能有多个seller_id/seller_email),
// 验证app_id是否为该商户本身。
if (!params.get("app_id").equals(ymlParament.getAppId())) {
throw new Exception("APPID不对!");
}
}
/**
* 获取支付宝回调参数
*/
private static Map<String, String> convertRequestParamsToMap(HttpServletRequest request) {
Map<String, String> retMap = new HashMap<String, String>();
Set<Map.Entry<String, String[]>> entrySet = request.getParameterMap().entrySet();
for (Map.Entry<String, String[]> entry : entrySet) {
String name = entry.getKey();
String[] values = entry.getValue();
int valLen = values.length;
if (valLen == 1) {
retMap.put(name, values[0]);
} else if (valLen > 1) {
StringBuilder sb = new StringBuilder();
for (String val : values) {
sb.append(",").append(val);
}
retMap.put(name, sb.toString().substring(1));
} else {
retMap.put(name, "");
}
}
return retMap;
}
若不想在支付回调时候去做签名校验,那我们可以主动去查询这笔订单是否支付成功
/*查询订单是否支付成功*/
AlipayTradeQueryResponse response = orderQuery(ac, params.get("out_trade_no"));
if (!"TRADE_SUCCESS".equals(response.getTradeStatus())){
throw new Exception("<==支付宝支付失败==>订单号为【"+ params.get("out_trade_no")+ "】的订单");
}
/**
* 交易查询接口,处理业务参数 https://opendocs.alipay.com/apis/api_1/alipay.trade.query#%E5%93%8D%E5%BA%94%E5%8F%82%E6%95%B0
* @param outTradeNo 订单号
* @return
* @throws AlipayApiException
*/
public static AlipayTradeQueryResponse orderQuery(AlipayClient ac , String outTradeNo) throws Exception {
AlipayTradeQueryModel model = new AlipayTradeQueryModel();
model.setOutTradeNo(outTradeNo);
AlipayTradeQueryResponse response = tradeQuery(ac,model);
if(!response.isSuccess()){
throw new Exception("调用支付宝查询接口失败");
}
return response;
}
/*交易查询接口*/
public static AlipayTradeQueryResponse tradeQuery(AlipayClient ac,AlipayTradeQueryModel model) throws AlipayApiException{
AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
request.setBizModel(model);
return ac.execute(request);
}