jspai独立接口,优化案例

This commit is contained in:
egzosn
2024-08-29 11:35:13 +08:00
parent 92d19b0d4b
commit 2ce5c82ccf
16 changed files with 216 additions and 35 deletions

View File

@@ -72,6 +72,13 @@
<scope>compile</scope>
</dependency>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<version>1.3.5</version>
</dependency>
<!-- /spring -->
<dependency>
<groupId>javax.servlet</groupId>

View File

@@ -18,6 +18,7 @@ import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.egzosn.pay.ali.api.AliPayConfigStorage;
import com.egzosn.pay.ali.api.AliPayService;
import com.egzosn.pay.ali.bean.AliRefundResult;
@@ -32,6 +33,7 @@ import com.egzosn.pay.common.bean.PayOrder;
import com.egzosn.pay.common.bean.RefundOrder;
import com.egzosn.pay.common.http.HttpConfigStorage;
import com.egzosn.pay.common.http.UriVariables;
import com.egzosn.pay.common.util.MapGen;
import com.egzosn.pay.common.util.sign.SignUtils;
import com.egzosn.pay.demo.request.QueryOrder;
import com.egzosn.pay.demo.service.handler.AliPayMessageHandler;
@@ -181,6 +183,29 @@ public class AliPayController {
}
/**
* 刷卡付,pos主动扫码付款(条码付)
*
* @param openid 授权码,条码等
* @param price 金额
* @return 支付结果
*/
@RequestMapping(value = "minapp")
public Map<String, Object> minapp(BigDecimal price, String openid) {
//获取对应的支付账户操作工具可根据账户id
//条码付
PayOrder order = new PayOrder("egan order", "egan order", null == price ? BigDecimal.valueOf(0.01) : price, UUID.randomUUID().toString().replace("-", ""), AliTransactionType.BAR_CODE);
//声波付
// PayOrder order = new PayOrder("egan order", "egan order", null == price ? BigDecimal.valueOf(0.01) : price, UUID.randomUUID().toString().replace("-", ""), AliTransactionType.WAVE_CODE);
//设置授权码,条码等
order.setOpenid(openid);
order.addAttr("op_app_id", "小程序支付中商户实际经营主体的小程序应用的appid也即最终唤起收银台支付所在的小程序的应用id");
//预订单结果
Map<String, Object> params = service.jsApi(order);
//这里开发者自行处理
return params;
}
/**
* 刷卡付,pos主动扫码付款(条码付)
*
@@ -210,6 +235,27 @@ public class AliPayController {
//这里开发者自行处理
return params;
}
/**
* 刷卡付,pos主动扫码付款(条码付)
*
* @param openid 授权码,条码等
* @param price 金额
* @return 支付结果
*/
@RequestMapping(value = "jsapi")
public Map<String, Object> jsapi(BigDecimal price, String openid) {
//获取对应的支付账户操作工具可根据账户id
//条码付
PayOrder order = new PayOrder("egan order", "egan order", null == price ? BigDecimal.valueOf(0.01) : price, UUID.randomUUID().toString().replace("-", ""), AliTransactionType.BAR_CODE);
order.setOpenid(openid);
//支付结果
Map<String, Object> orderInfo = service.orderInfo(order);
Map<String, Object> bizContent = new MapGen<>("biz_content", orderInfo.get("biz_content")).getAttr();
JSONObject result = service.getHttpRequestTemplate().postForObject(service.getReqUrl() + "?" + UriVariables.getMapToParameters(orderInfo), bizContent, JSONObject.class);
//这里开发者自行处理
return result;
}
/**
* 支付回调地址 方式一

View File

@@ -165,7 +165,7 @@ public class PayController {
PayOrder order = new PayOrder("订单title", "摘要", null == price ? BigDecimal.valueOf(0.01) : price, UUID.randomUUID().toString().replace("-", ""), PayType.valueOf(payResponse.getStorage().getPayType()).getTransactionType("JSAPI"));
order.setOpenid(openid);
Map orderInfo = payResponse.getService().orderInfo(order);
Map<String, Object> orderInfo = payResponse.getService().jsApi(order);
orderInfo.put("code", 0);
return orderInfo;
@@ -211,9 +211,9 @@ public class PayController {
Map<String, Object> params = payResponse.getService().microPay(order);
PayConfigStorage storage = payResponse.getService().getPayConfigStorage();
//校验
if (payResponse.getService().verify(params)) {
if (payResponse.getService().verify(new NoticeParams(params))) {
PayMessage message = new PayMessage(params, storage.getPayType());
//支付校验通过后的处理
//支付校验通过后的处理,,路由的方式已经不建议使用了
payResponse.getRouter().route(message);
}
//这里开发者自行处理

View File

@@ -124,7 +124,7 @@ public class WxPayController {
PayOrder order = new PayOrder("订单title", "摘要", null == price ? BigDecimal.valueOf(0.01) : price, UUID.randomUUID().toString().replace("-", ""), WxTransactionType.JSAPI);
order.setOpenid(openid);
Map orderInfo = service.orderInfo(order);
Map<String, Object> orderInfo = service.jsApi(order);
orderInfo.put("code", 0);
return orderInfo;

View File

@@ -126,7 +126,7 @@ public class WxV3CombinePayController {
subOrder.setSubMchid("服务商必填----二级商户商户号,由微信支付生成并下发。服务商子商户的商户号,被合单方。直连商户不用传二级商户号。 ");
subOrders.add(subOrder);
order.setSubOrders(subOrders);
Map orderInfo = service.orderInfo(order);
Map orderInfo = service.jsApi(order);
orderInfo.put("code", 0);
return orderInfo;
}

View File

@@ -98,17 +98,18 @@ public class WxV3PayController {
/**
* 公众号支付
*
*
* @param openid openid
* @param price 金额
* @param price 金额
* @return 返回jsapi所需参数
*/
@RequestMapping(value = "jsapi")
@RequestMapping(value = "jsapi" )
public Map toPay(String openid, BigDecimal price) {
PayOrder order = new PayOrder("订单title", "摘要", null == price ? BigDecimal.valueOf(0.01) : price, UUID.randomUUID().toString().replace("-", ""), WxTransactionType.JSAPI);
PayOrder order = new PayOrder("订单title", "摘要", null == price ? BigDecimal.valueOf(0.01) : price, UUID.randomUUID().toString().replace("-", ""), com.egzosn.pay.wx.bean.WxTransactionType.JSAPI);
order.setOpenid(openid);
Map orderInfo = service.orderInfo(order);
Map<String, Object> orderInfo = service.jsApi(order);
orderInfo.put("code", 0);
return orderInfo;

View File

@@ -8,7 +8,12 @@ import com.egzosn.pay.common.bean.BasePayType;
import com.egzosn.pay.common.bean.CertStoreType;
import com.egzosn.pay.common.bean.TransactionType;
import com.egzosn.pay.common.util.sign.SignUtils;
import com.egzosn.pay.demo.service.handler.FuiouPayMessageHandler;
import com.egzosn.pay.demo.service.handler.PayPalPayMessageHandler;
import com.egzosn.pay.demo.service.handler.PayoneerMessageHandler;
import com.egzosn.pay.demo.service.handler.UnionPayMessageHandler;
import com.egzosn.pay.demo.service.handler.WxPayMessageHandler;
import com.egzosn.pay.demo.service.handler.YouDianPayMessageHandler;
import com.egzosn.pay.fuiou.api.FuiouPayConfigStorage;
import com.egzosn.pay.fuiou.api.FuiouPayService;
import com.egzosn.pay.fuiou.bean.FuiouTransactionType;
@@ -64,7 +69,9 @@ public enum PayType implements BasePayType {
aliPayConfigStorage.setInputCharset("utf-8");
//是否为测试账号,沙箱环境
aliPayConfigStorage.setTest(true);
return new AliPayService(aliPayConfigStorage);
AliPayService aliPayService = new AliPayService(aliPayConfigStorage);
aliPayService.setPayMessageHandler(new WxPayMessageHandler(apyAccount.getPayId()));
return aliPayService;
}
@Override
@@ -102,7 +109,7 @@ public enum PayType implements BasePayType {
httpConfigStorage.setCertStoreType(CertStoreType.PATH);
return new WxPayService(wxPayConfigStorage, httpConfigStorage);*/
WxPayService wxPayService = new WxPayService(wxPayConfigStorage);
wxPayService.setPayMessageHandler(new WxPayMessageHandler(1));
wxPayService.setPayMessageHandler(new WxPayMessageHandler(apyAccount.getPayId()));
return wxPayService;
}
@@ -131,7 +138,9 @@ public enum PayType implements BasePayType {
wxPayConfigStorage.setSeller(apyAccount.getSeller());
wxPayConfigStorage.setInputCharset(apyAccount.getInputCharset());
wxPayConfigStorage.setTest(apyAccount.isTest());
return new WxYouDianPayService(wxPayConfigStorage);
final WxYouDianPayService wxYouDianPayService = new WxYouDianPayService(wxPayConfigStorage);
wxYouDianPayService.setPayMessageHandler(new YouDianPayMessageHandler(apyAccount.getPayId()));
return wxYouDianPayService;
}
/**
@@ -157,7 +166,9 @@ public enum PayType implements BasePayType {
fuiouPayConfigStorage.setPayType(apyAccount.getPayType().toString());
fuiouPayConfigStorage.setInputCharset(apyAccount.getInputCharset());
fuiouPayConfigStorage.setTest(apyAccount.isTest());
return new FuiouPayService(fuiouPayConfigStorage);
final FuiouPayService fuiouPayService = new FuiouPayService(fuiouPayConfigStorage);
fuiouPayService.setPayMessageHandler(new FuiouPayMessageHandler(apyAccount.getPayId()));
return fuiouPayService;
}
@Override
@@ -192,7 +203,9 @@ public enum PayType implements BasePayType {
unionPayConfigStorage.setPayType(apyAccount.getPayType().toString());
unionPayConfigStorage.setInputCharset(apyAccount.getInputCharset());
unionPayConfigStorage.setTest(apyAccount.isTest());
return new UnionPayService(unionPayConfigStorage);
final UnionPayService unionPayService = new UnionPayService(unionPayConfigStorage);
unionPayService.setPayMessageHandler(new UnionPayMessageHandler(apyAccount.getPayId()));
return unionPayService;
}
@Override
@@ -214,7 +227,9 @@ public enum PayType implements BasePayType {
configStorage.setApiPassword(apyAccount.getPrivateKey());
//是否为沙箱
configStorage.setTest(true);
return new PayoneerPayService(configStorage);
final PayoneerPayService payoneerPayService = new PayoneerPayService(configStorage);
payoneerPayService.setPayMessageHandler(new PayoneerMessageHandler(apyAccount.getPayId()));
return payoneerPayService;
//以下不建议进行使用,会引起两次请求的问题
//Basic Auth
@@ -243,7 +258,9 @@ public enum PayType implements BasePayType {
storage.setReturnUrl(apyAccount.getReturnUrl());
//取消按钮转跳地址,这里兼容的做法
storage.setNotifyUrl(apyAccount.getNotifyUrl());
return new PayPalPayService(storage);
final PayPalPayService payPalPayService = new PayPalPayService(storage);
payPalPayService.setPayMessageHandler(new PayPalPayMessageHandler(apyAccount.getPayId()));
return payPalPayService;
}
@Override

View File

@@ -52,9 +52,13 @@ public class PayResponse {
//根据不同的账户类型 初始化支付配置
this.service = apyAccount.getPayType().getPayService(apyAccount);
this.storage = service.getPayConfigStorage();
//这里设置http请求配置
// service.setRequestTemplateConfigStorage(getHttpConfigStorage());
buildRouter(apyAccount.getPayId());
}
/**
@@ -87,7 +91,9 @@ public class PayResponse {
* 配置路由
*
* @param payId 指定账户id用户多微信支付多支付宝支付
* @deprecated 不再推荐使用路由方式回调或拦截器直接在payService中设置并获取使用
*/
@Deprecated
private void buildRouter(Integer payId) {
router = new PayMessageRouter(this.service);
router

View File

@@ -1,27 +1,23 @@
package com.egzosn.pay.demo.service.handler;
import com.egzosn.pay.common.api.PayMessageHandler;
import com.egzosn.pay.common.api.PayService;
import java.util.Map;
import com.egzosn.pay.common.bean.PayMessage;
import com.egzosn.pay.common.bean.PayOutMessage;
import com.egzosn.pay.common.exception.PayErrorException;
import com.egzosn.pay.paypal.api.PayPalPayService;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.util.Map;
/**
* PayPal支付回调处理器
* Created by ZaoSheng on 2016/6/1.
*
*/
@Component
public class PayPalPayMessageHandler implements PayMessageHandler<PayMessage, PayPalPayService> {
public class PayPalPayMessageHandler extends BasePayMessageHandler<PayMessage, PayPalPayService> {
public PayPalPayMessageHandler(Integer payId) {
super(payId);
}
@Override
public PayOutMessage handle(PayMessage payMessage, Map<String, Object> context, PayPalPayService payService) throws PayErrorException {