Comprehensive Java SDK for integrating with WeChat Pay services including unified orders, refunds, enterprise payments, red packets, profit sharing, and marketing services
npx @tessl/cli install tessl/maven-com-github-binarywang--weixin-java-pay@4.7.0WeChat Payment Java SDK is a comprehensive Java library for integrating with WeChat Pay (微信支付) services. It provides complete support for both WeChat Pay API v2 and v3, enabling developers to implement various payment functionalities including unified orders, refunds, enterprise payments, red packets, profit sharing, and marketing services with built-in security, certificate management, and multi-merchant support.
<dependency><groupId>com.github.binarywang</groupId><artifactId>weixin-java-pay</artifactId><version>4.7.0</version></dependency>import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.github.binarywang.wxpay.config.WxPayConfig;For specific services:
import com.github.binarywang.wxpay.service.EntPayService;
import com.github.binarywang.wxpay.service.RedpackService;
import com.github.binarywang.wxpay.service.ProfitSharingService;For request/response beans:
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult;
import com.github.binarywang.wxpay.constant.WxPayConstants;
// Initialize configuration
WxPayConfig config = new WxPayConfig();
config.setAppId("your-app-id");
config.setMchId("your-merchant-id");
config.setMchKey("your-merchant-key");
config.setKeyPath("/path/to/cert.p12"); // Certificate path for secure operations
// Create service instance
WxPayService wxPayService = new WxPayServiceImpl();
wxPayService.setConfig(config);
// Create unified order for payment
WxPayUnifiedOrderRequest request = WxPayUnifiedOrderRequest.newBuilder()
.outTradeNo("order-20230101-001")
.totalFee(1) // Amount in cents
.body("Test Product")
.tradeType(WxPayConstants.TradeType.JSAPI)
.openid("user-openid-for-jsapi")
.build();
// Execute payment request
WxPayUnifiedOrderResult result = wxPayService.unifiedOrder(request);
// Extract payment parameters for frontend
Map<String, String> payParams = result.getPayInfo();WeChat Payment Java SDK is built around several key components:
WxPayService) and specialized services for different payment scenariosWxPayConfig for merchant credentials, certificates, and environment settingsEssential payment processing functionality including order creation, querying, and cancellation. Supports all major WeChat Pay payment methods with both v2 and v3 API protocols.
// Unified order creation (v2)
WxPayUnifiedOrderResult unifiedOrder(WxPayUnifiedOrderRequest request) throws WxPayException;
// Unified order creation (v3)
WxPayUnifiedOrderV3Result unifiedOrderV3(TradeTypeEnum tradeType, WxPayUnifiedOrderV3Request request) throws WxPayException;
// Order querying
WxPayOrderQueryResult queryOrder(String transactionId, String outTradeNo) throws WxPayException;
// Order closing
WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxPayException;Comprehensive refund processing with support for full and partial refunds, refund querying, and notification handling for both API versions.
// Create refund (v2)
WxPayRefundResult refund(WxPayRefundRequest request) throws WxPayException;
// Create refund (v3)
WxPayRefundV3Result refundV3(WxPayRefundV3Request request) throws WxPayException;
// Query refund status
WxPayRefundQueryResult refundQuery(WxPayRefundQueryRequest request) throws WxPayException;Support for all WeChat Pay payment methods including JSAPI (Mini Program/Official Account), Native (QR Code), APP, H5 (Mobile Web), Micropay (Barcode), and Face Payment.
// Micropay (barcode/QR scanning)
WxPayMicropayResult micropay(WxPayMicropayRequest request) throws WxPayException;
// Face payment
WxPayFacepayResult facepay(WxPayFacepayRequest request) throws WxPayException;
// Generate payment parameters for different platforms
Map<String, String> getPayInfo();
WxPayAppOrderResult createAppOrder(WxPayUnifiedOrderResult result);
WxPayMpOrderResult createMpOrder(WxPayUnifiedOrderResult result);Enterprise-level payment services including payments to individuals, red packet distribution, and profit sharing functionality.
// Enterprise payments to individuals
EntPayResult entPay(EntPayRequest request) throws WxPayException;
// Red packet distribution
WxPaySendRedpackResult sendRedpack(WxPaySendRedpackRequest request) throws WxPayException;
// Profit sharing
ProfitSharingResult profitSharing(ProfitSharingRequest request) throws WxPayException;Configuration management, certificate handling, signature processing, and multi-merchant support for enterprise deployments.
// Configuration management
void setConfig(WxPayConfig config);
void addConfig(String mchId, WxPayConfig wxPayConfig);
boolean switchover(String mchId);
// Certificate management
void initV3HttpClient() throws WxPayException;
String getCertSerialNo();
// Security utilities
String createSign(Map<String, String> params, SignType signType) throws WxPayException;
boolean checkSign(Map<String, String> params) throws WxPayException;Webhook processing for payment notifications with automatic signature verification and result parsing for various event types.
// Parse payment notifications
WxPayOrderNotifyResult parseOrderNotifyResult(String xmlData) throws WxPayException;
WxPayNotifyV3Result parseOrderNotifyV3Result(String jsonData, SignatureHeader header) throws WxPayException;
// Parse refund notifications
WxPayRefundNotifyResult parseRefundNotifyResult(String xmlData) throws WxPayException;
WxPayRefundNotifyV3Result parseRefundNotifyV3Result(String jsonData, SignatureHeader header) throws WxPayException;Specialized services including e-commerce platform support, marketing coupons, transfer services, Pay Score, complaint management, payroll, business circle, and bill downloads.
// Marketing and coupons
MarketingFavorService getMarketingFavorService();
MarketingBusiFavorService getMarketingBusiFavorService();
// E-commerce platform
EcommerceService getEcommerceService();
// Transfer services
TransferService getTransferService();
MerchantTransferService getMerchantTransferService();
PartnerTransferService getPartnerTransferService();
// Pay Score services
PayScoreService getPayScoreService();
PartnerPayScoreService getPartnerPayScoreService();
// Payroll and business services
PayrollService getPayrollService();
BusinessCircleService getBusinessCircleService();
// Complaint management
ComplaintService getComplaintsService();
// Bill downloads
WxPayBillResult downloadBill(WxPayBillRequest request) throws WxPayException;