pay-java-parent

repository·develop·Indexed 25 days ago

https://github.com/egzosn/pay-java-parent

A lightweight, framework-independent Java SDK for integrating third-party payment gateways including Alipay, WeChat Pay, PayPal, and Fuiou. It provides standardized interfaces for executing QR code, APP, Mini-Program, and PC web payments, as well as managing refunds, transaction queries, and fund transfers. The SDK includes tools for signature verification, callback handling via PayMessageHandler and PayMessageInterceptor, and configurable network settings through HttpConfigStorage.

Tokens
10K
Snippets
36
Records
67
Agent score
84%

What's inside pay-java-parent

  1. Overview of pay-java-parent SDK

    develop

    The pay-java-parent project is a lightweight Java SDK for integrating multiple third-party payment providers including WeChat Pay, Alipay, UnionPay, PayPal, Payoneer, and others.

    Key characteristics:

    • Framework Agnostic: Does not depend on any MVC framework (like Spring MVC) or Servlet containers. It is designed as a pure utility tool that can be easily embedded into any system.
    • Minimal Dependencies: Relies only on httpclient, fastjson, log4j, and com.google.zxing.
    • Flexible Integration: Supports HTTP and asynchronous requests, HTTP proxies, and connection pooling.
    • Modular Architecture: The project is split into core specifications, web support (for callbacks), demos, and individual channel implementation libraries.
  2. Handle YouDian payment callbacks

    develop

    To process asynchronous notifications (callbacks), use service.getParameter2Map to convert the HttpServletRequest parameters and input stream into a Map<String, Object>. Then, use service.verify(params) to validate the signature and ensure the payment was successful.

    /*-----------回调处理-------------------*/
    // Assuming 'request' is an HttpServletRequest
    Map<String, Object> params = service.getParameter2Map(request.getParameterMap(), request.getInputStream());
    
    if (service.verify(params)) {
        System.out.println("支付成功");
        return;
    }
    System.out.println("支付失败");
    /*-----------回调处理-------------------*/
  3. Implement WeChat Pay Callbacks

    develop

    You can handle payment notifications using two methods:

    1. Interceptor Pattern: Add a WxPayMessageInterceptor and set a WxPayMessageHandler (e.g., a Spring bean) to decouple business logic from the payment SDK.
    2. Manual Verification: Use service.getParameter2Map and service.verify to manually validate the request parameters and signature.
  4. Configure FuiouPayConfigStorage for Fuiou Payment

    develop

    To use Fuiou payment, you must first initialize FuiouPayConfigStorage with your merchant credentials and callback URLs. You can also specify the signature type, input charset, and whether you are using a sandbox/test environment.

    FuiouPayConfigStorage fuiouPayConfigStorage = new FuiouPayConfigStorage();
    fuiouPayConfigStorage.setMchntCd("合作者id");
    fuiouPayConfigStorage.setKeyPublic("支付密钥");
    fuiouPayConfigStorage.setKeyPrivate("支付密钥");
    fuiouPayConfigStorage.setNotifyUrl("异步回调地址");
    fuiouPayConfigStorage.setReturnUrl("同步回调地址");
    fuiouPayConfigStorage.setSignType("MD5");
    fuiouPayConfigStorage.setInputCharset("utf-8");
    //是否为测试账号,沙箱环境
    fuiouPayConfigStorage.setTest(true);
  5. Configure YouDian (友店) Pay settings

    develop

    To use YouDian payment, initialize a WxYouDianPayConfigStorage object with your merchant credentials. You must provide the private encryption key, the public key for asynchronous notification signing, your YouDian account, and the signing type.

    WxYouDianPayConfigStorage wxPayConfigStorage = new WxYouDianPayConfigStorage();
    wxPayConfigStorage.setKeyPrivate("友店所提供的加密串");
    wxPayConfigStorage.setKeyPublic("线下支付异步通知加签密钥");
    wxPayConfigStorage.setSeller("友店账号");
    wxPayConfigStorage.setSignType("签名方式");
    wxPayConfigStorage.setInputCharset("utf-8");
    wxPayConfigStorage.setTest(true);
  6. Implement Alipay Payment Callbacks

    develop

    There are two ways to handle payment callbacks:

    1. Manual Verification: Use service.getParameter2Map to parse the request and service.verify(params) to validate the signature.
    2. Interceptor Pattern: For better separation of concerns, add an AliPayMessageInterceptor and set a PayMessageHandler (e.g., a Spring bean) using service.addPayMessageInterceptor and service.setPayMessageHandler.
  7. Configure Payoneer payment settings

    develop

    Initialize PayoneerConfigStorage with your merchant credentials and environment settings. You must specify the programId, userName, and apiPassword. Use setTest(true) to enable the sandbox environment.

    PayoneerConfigStorage configStorage = new PayoneerConfigStorage();
    configStorage.setProgramId("商户id");
    configStorage.setMsgType(MsgType.json);
    configStorage.setInputCharset("utf-8");
    configStorage.setUserName("PayoneerPay 用户名");
    configStorage.setApiPassword("PayoneerPay API password");
    configStorage.setTest(true);
  8. Use pay-spring-boot-starter for Spring Boot integration

    develop

    For developers using Spring Boot, there is a dedicated starter that provides automated configuration. This allows for aggregated payment processing with minimal code, enabling you to focus on business logic rather than individual payment gateway integrations.

    Refer to the pay-spring-boot-starter-parent repository for details.

  9. Handle Fuiou Payment Callbacks

    develop

    When receiving a callback, use service.getParameter2Map to convert the HttpServletRequest parameters and input stream into a Map. Then, use service.verify(params) to validate the signature and ensure the payment was successful.

    /*-----------回调处理-------------------*/
    //HttpServletRequest request;
    Map<String, Object> params = service.getParameter2Map(request.getParameterMap(), request.getInputStream());
    if (service.verify(params)){
        System.out.println("支付成功");
        return;
    }
    System.out.println("支付失败");
    /*-----------回调处理-------------------*/
  10. Configure PayPal payment settings

    develop

    To use PayPal, you must first initialize a PayPalConfigStorage object with your merchant credentials and callback URLs. Use setTest(true) for sandbox testing.

    PayPalConfigStorage storage = new PayPalConfigStorage();
    storage.setClientID("商户id");
    storage.setClientSecret("商户密钥");
    storage.setTest(true);
    // URL to redirect to after payment
    storage.setReturnUrl("http://127.0.0.1:8088/pay/success");
    // URL for cancellation or asynchronous notifications
    storage.setNotifyUrl("http://127.0.0.1:8088/pay/cancel");
  11. Initialize PayResponse with ApyAccount

    develop

    To use the SDK, you must first initialize a PayResponse object using an ApyAccount instance. The ApyAccount contains the specific credentials (AppID, Keys, Notify URLs, etc.) for a particular payment provider (e.g., AliPay, WeChat Pay).

    Calling payResponse.init(apyAccount) sets up the appropriate PayService and configures the PayMessageRouter for handling callbacks.

    // Assuming ApyAccount is retrieved from a DAO
    ApyAccount apyAccount = dao.get(id);
    PayResponse payResponse = new PayResponse();
    spring.autowireBean(payResponse);
    payResponse.init(apyAccount);