Implement WeChat Pay V3 subscription billing functionality (预约扣费功能/连续包月功能) by Copilot · Pull Request #3688 · binarywang/WxJava
This PR implements the WeChat Pay V3 subscription billing functionality (预约扣费功能/连续包月功能) as requested by the community. The subscription billing feature enables merchants to automatically charge users on a recurring basis for services like monthly subscriptions, with proper user authorization.
What's Added
Service Interface
SubscriptionBillingService- Main service interface providing subscription billing operations
Bean Classes
Complete request/response model classes following WeChat Pay V3 API specification:
SubscriptionScheduleRequest/Result- Schedule future billingSubscriptionQueryResult- Query subscription statusSubscriptionCancelRequest/Result- Cancel subscriptionsSubscriptionInstantBillingRequest/Result- Execute immediate billingSubscriptionTransactionQueryRequest/Result- Query billing historySubscriptionAmount- Amount and currency informationBillingPlan- Billing plan configuration (monthly/weekly/daily/yearly)
Service Implementation
SubscriptionBillingServiceImpl- Complete implementation following existing V3 API patterns- Integrated into
WxPayServiceinterface andBaseWxPayServiceImpl - Proper error handling and exception management
Features Supported
- Schedule Subscription Billing - Create advance billing schedules for recurring payments
- Query Subscription Status - Check subscription details and current status
- Cancel Subscriptions - Cancel existing subscription schedules
- Instant Billing - Execute immediate charges (useful for makeup payments)
- Query Transaction Records - Retrieve billing history with pagination
Usage Example
// Get the subscription billing service SubscriptionBillingService service = wxPayService.getSubscriptionBillingService(); // Create a monthly subscription SubscriptionScheduleRequest request = new SubscriptionScheduleRequest(); request.setOutTradeNo("monthly_vip_" + System.currentTimeMillis()); request.setOpenid("user_openid"); request.setDescription("VIP会员 - 连续包月"); request.setScheduleTime("2024-09-01T10:00:00+08:00"); // Set amount SubscriptionAmount amount = new SubscriptionAmount(); amount.setTotal(3000); // 30 RMB in cents amount.setCurrency("CNY"); request.setAmount(amount); // Configure monthly billing plan BillingPlan plan = new BillingPlan(); plan.setPlanType("MONTHLY"); plan.setPeriod(1); plan.setTotalCount(12); // 12 months request.setBillingPlan(plan); // Execute the subscription SubscriptionScheduleResult result = service.scheduleSubscription(request);
Implementation Details
The implementation follows WeChat Pay V3 API documentation: https://pay.weixin.qq.com/doc/v3/merchant/4012161105
- API Endpoints: All 5 core subscription billing endpoints are supported
- Authentication: Uses existing V3 signature and certificate handling
- Error Handling: Leverages existing WxPayException framework
- Code Style: Passes all checkstyle validations and follows project patterns
- Testing: Includes basic test structure (disabled by default due to credential requirements)
- Documentation: Comprehensive usage guide with examples provided
Code Quality
✅ Builds successfully with existing codebase
✅ Passes all checkstyle validations
✅ Follows established architectural patterns
✅ Comprehensive JavaDoc documentation
✅ Proper exception handling
This implementation provides production-ready support for WeChat's subscription billing capabilities while maintaining full compatibility with the existing WxJava architecture.
Fixes #3593.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.