一行代码实现支付聚合pay-spring-boot-starter组件快速入门

黄弘深
2023-12-01

支付聚合spring boot组件


pay-spring-boot-starter 是一个基于spring-boot实现自动化配置的支付对接, 让你真正做到一行代码实现支付聚合, 让你可以不用理解支付怎么对接,只需要专注你的业务

特性

  1. 项目第三方依赖极少,依托于spring boot与pay-java,项目精简,不用担心项目迁移问题
  2. 一行代码解决配置,一行代码发起支付,一行代码处理回调并且业务与支付完全隔离
  3. 项目扩展性极强极灵活,组件中暴露大量接口,实现对应接口重写加入spring容器即可覆盖全部功能
  4. 引入pay-java具体支付组件即可激活某一支付功能,代码可以不用任何修改即可使用

快速入门开始

一. pom 中引入

  1. 引入 pay-spring-boot-starter
<dependency>
	<groupId>com.egzosn</groupId>
	<artifactId>pay-spring-boot-starter</artifactId>
	<version>0.0.4</version>
</dependency>
  1. 引入 你需要对接的基于pay-java-parent支付开发包,具体支付模块 "{module-name}" 为具体的支付渠道的模块名 pay-java-ali,pay-java-wx等
 <dependency>
        <groupId>com.egzosn</groupId>
        <artifactId>{module-name}</artifactId>
        <version>2.13.1</version>
</dependency>

二、商户配置

编写一个基于com.egzosn.pay.spring.boot.core.PayServiceConfigurer的子类,在对应的方法种配置需要的信息:商户信息,回调处理器等等

1. 内存方式(支付宝案例)


@Configuration
public class MerchantPayServiceConfigurer implements PayServiceConfigurer {
    @Autowired
    private AliPayMessageHandler aliPayMessageHandler;

    /**
     * 商户配置 * * @param merchants 商户配置
     */
    @Override
    public void configure(MerchantDetailsServiceConfigurer merchants) {
        merchants.inMemory()
                .ali()
                .detailsId("1")
                .appid("2016080400165436")
                .keyPrivate("MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKroe/8h5vC4L6T+B2WdXiVwGsMvUKgb2XsKix6VY3m2wcf6tyzpNRDCNykbIwGtaeo7FshN+qZxdXHLiIam9goYncBit/8ojfLGy2gLxO/PXfzGxYGs0KsDZ+ryVPPmE34ZZ8jiJpR0ygzCFl8pN3QJPJRGTJn5+FTT9EF/9zyZAgMBAAECgYAktngcYC35u7cQXDk+jMVyiVhWYU2ULxdSpPspgLGzrZyG1saOcTIi/XVX8Spd6+B6nmLQeF/FbU3rOeuD8U2clzul2Z2YMbJ0FYay9oVZFfp5gTEFpFRTVfzqUaZQBIjJe/xHL9kQVqc5xHlE/LVA27/Kx3dbC35Y7B4EVBDYAQJBAOhsX8ZreWLKPhXiXHTyLmNKhOHJc+0tFH7Ktise/0rNspojU7o9prOatKpNylp9v6kux7migcMRdVUWWiVe+4ECQQC8PqsuEz7B0yqirQchRg1DbHjh64bw9Kj82EN1/NzOUd53tP9tg+SO97EzsibK1F7tOcuwqsa7n2aY48mQ+y0ZAkBndA2xcRcnvOOjtAz5VO8G7R12rse181HjGfG6AeMadbKg30aeaGCyIxN1loiSfNR5xsPJwibGIBg81mUrqzqBAkB+K6rkaPXJR9XtzvdWb/N3235yPkDlw7Z4MiOVM3RzvR/VMDV7m8lXoeDde2zQyeMOMYy6ztwA6WgE1bhGOnQRAkEAouUBv1sVdSBlsexX15qphOmAevzYrpufKgJIRLFWQxroXMS7FTesj+f+FmGrpPCxIde1dqJ8lqYLTyJmbzMPYw==")
                .keyPublic("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDIgHnOn7LLILlKETd6BFRJ0GqgS2Y3mn1wMQmyh9zEyWlz5p1zrahRahbXAfCfSqshSNfqOmAQzSHRVjCqjsAw1jyqrXaPdKBmr90DIpIxmIyKXv4GGAkPyJ/6FTFY99uhpiq0qadD/uSzQsefWo0aTvP/65zi3eof7TcZ32oWpwIDAQAB")
                .inputCharset("utf-8")
                .notifyUrl("http://pay.egzosn.com/payBack1.json")
                .returnUrl("http://pay.egzosn.com/payBack1.html")
                .pid("2088102169916436")
                .seller("2088102169916436")
                .signType(SignUtils.RSA.getName())
                .test(true)
                .and();
    }

    /**
     * 商户配置 * * @param configurer 支付消息配置
     */
    @Override
    public void configure(PayMessageConfigurer configurer) {
        PaymentPlatform aliPaymentPlatform = PaymentPlatforms.getPaymentPlatform(AliPaymentPlatform.platformName);
        configurer.addHandler(aliPaymentPlatform, aliPayMessageHandler);
    }
}

2. jdbc方式

jdbc前奏需要把数据表与商户渠道信息在表中预设好

  1. 表结构脚本 https://gitee.com/egzosn/pay-spring-boot-starter-parent/blob/master/doc/sql/merchant_pay.sql

  2. 表数据脚本(微信案例):

INSERT INTO merchant_details(details_id,pay_type,appid,mch_id,cert_store_type,key_private,key_public,key_cert,key_cert_pwd,notify_url,return_url,sign_type,seller,sub_app_id,sub_mch_id,input_charset,is_test) VALUES ( '3','wxPay','公众账号ID','合作者id(商户号)','URL','密钥','转账公钥,转账时必填','http://www.egzosn.com/certs/apiclient_cert.p12','默认为商户号','http://www.pay.egzosn.com/payBack3.json','http://www.pay.egzosn.com/payBack3.json','MD5',NULL,NULL,NULL,'UTF-8','1'); 

  1. 商户配置
@Configuration
public class MerchantPayServiceConfigurer implements PayServiceConfigurer {

   @Autowired
   private JdbcTemplate jdbcTemplate;
   /**
    * 商户配置
    *
    * @param merchants 商户配置
    */
   @Override
   public void configure(MerchantDetailsServiceConfigurer merchants)  {
//        数据库文件存放 /doc/sql目录下
       merchants.jdbc(jdbcTemplate);
   }
   /**
    * 商户配置
    *
    * @param configurer 支付消息配置
    */
   @Override
   public void configure(PayMessageConfigurer configurer) {
       configurer.addHandler(PaymentPlatforms.getPaymentPlatform(WxPaymentPlatform.platformName), new WxPayMessageHandler());
   }
}

三、发起支付

在你需要用到的类中注入 com.egzosn.pay.spring.boot.core.PayServiceManager 这个类是一个支付相关的操作类,拿到该类的引用即可对任意支付进行操作

  1. 简单使用,微信扫码付
    @Autowired
    private PayServiceManager manager;
	
    MerchantPayOrder payOrder = new MerchantPayOrder("3", "NATIVE", "订单title", "摘要", new BigDecimal(0.01), UUID.randomUUID().toString().replace("-", ""));
    //二维码字节数组
    byte[] image = manager.toQrPay(payOrder);
  1. 案例demo
@RequestMapping("pay")
@Controller
public class PayMerchantController {

   @Autowired
   private PayServiceManager manager;

   /**
    * 网页支付
    * @param detailsId 列表id
    * @param wayTrade 交易方式
    * @return 网页
    */
   @ResponseBody
   @RequestMapping(value = "toPay.html", produces = "text/html;charset=UTF-8")
   public String toPay(String detailsId, String wayTrade, BigDecimal price) {
       MerchantPayOrder payOrder = new MerchantPayOrder(detailsId, wayTrade, "订单title", "摘要", null == price ? new BigDecimal(0.01) : price, UUID.randomUUID().toString().replace("-", ""));
       return manager.toPay(payOrder);
   }
   /**
    * 二维码
    * @param detailsId 列表id
    * @param wayTrade 交易方式
    * @return 二维码
    */
   @ResponseBody
   @RequestMapping(value = "toQrPay.jpg", produces = "image/jpeg;charset=UTF-8")
   public byte[] toQrPay(String detailsId, String wayTrade, BigDecimal price) throws IOException {
       MerchantPayOrder payOrder = new MerchantPayOrder(detailsId, wayTrade, "订单title", "摘要", null == price ? new BigDecimal(0.01) : price, UUID.randomUUID().toString().replace("-", ""));
       return manager.toQrPay(payOrder);
   }
   /**
    * 二维码信息
    * @param detailsId 列表id
    * @param wayTrade 交易方式
    * @return 二维码信息
    */
   @ResponseBody
   @RequestMapping(value = "getQrPay.json")
   public String getQrPay(String detailsId, String wayTrade, BigDecimal price) throws IOException {
       MerchantPayOrder payOrder = new MerchantPayOrder(detailsId, wayTrade, "订单title", "摘要", null == price ? new BigDecimal(0.01) : price, UUID.randomUUID().toString().replace("-", ""));
       return manager.getQrPay(payOrder);
   }


   /**
    * 支付回调地址
    * @param request 请求
    * @param detailsId 列表id
    * @return 支付是否成功
    * @throws IOException IOException
    * 拦截器相关增加, 详情查看{@link com.egzosn.pay.common.api.PayService#addPayMessageInterceptor(PayMessageInterceptor)}
    * <p>
    * 业务处理在对应的PayMessageHandler里面处理,在哪里设置PayMessageHandler,详情查看{@link com.egzosn.pay.common.api.PayService#setPayMessageHandler(com.egzosn.pay.common.api.PayMessageHandler)}
    * </p>
    * 如果未设置 {@link com.egzosn.pay.common.api.PayMessageHandler} 那么会使用默认的 {@link com.egzosn.pay.common.api.DefaultPayMessageHandler}
    */
   @RequestMapping(value = "payBack{detailsId}.json")
   public String payBack(HttpServletRequest request, @PathVariable String detailsId) throws IOException {
       //业务处理在对应的PayMessageHandler里面处理,在哪里设置PayMessageHandler,详情查看com.egzosn.pay.common.api.PayService.setPayMessageHandler()
       return manager.payBack(detailsId, request.getParameterMap(), request.getInputStream());
   }

}

本项目在以下代码托管网站

码云:https://gitee.com/egzosn/pay-spring-boot-starter-parent
GitHub:https://github.com/egzosn/pay-spring-boot-starter-parent

本项目基础实现 pay-java-parent

码云:https://gitee.com/egzosn/pay-java-parent
GitHub:https://github.com/egzosn/pay-java-parent
 类似资料: