Background
When developing a mini program delivery system following online tutorials, you may encounter situations where a personal WeChat mini program cannot enable WeChat Pay functionality. This article describes how to skip the payment module for testing purposes.
WeChat Pay Sequence Diagram
Skipping WeChat Pay essentially involves bypassing the communication between the mini program and WeChat backend servers. The standard payment flow involves multiple requests to WeChat's payment gateway, which can be circumvented for development and testing scenarios.
Implementation
Frontend Mini Program
Directly redirect the user after order submission without invoking the wx.requestPayment API. Modify the order confirmation flow to immediately proceed to the success page upon order creation.
Admin Backend
The backend modifications involve two key changes:
- Comment out or bypass the WeChat Pay utility call that generates pre-trade orders
- Directly update database payment status after order creation
Service Implementation
/**
* Process order payment
* For development/testing: skips actual WeChat Pay integration
*
* @param paymentDTO order payment data
* @return payment result object
*/
public PaymentResultVO processOrderPayment(PaymentDTO paymentDTO) {
// Get current authenticated user
Long currentUserId = SecurityContextHolder.getContext().getCurrentUserId();
User userAccount = userRepository.findById(currentUserId);
// Original WeChat Pay call would generate pre-trade订单:
// JSONObject tradeResult = weChatPayService.createPreorder(
// paymentDTO.getOrderNumber(),
// new BigDecimal("0.01"),
// "Delivery Order",
// userAccount.getOpenId()
// );
// Simulate payment success response
JSONObject simulatedResponse = new JSONObject();
simulatedResponse.put("transactionId", generateTransactionId());
simulatedResponse.put("tradeState", "SUCCESS");
PaymentResultVO result = simulatedResponse.toJavaObject(PaymentResultVO.class);
// Update order status to paid - create entity using setters
Order orderEntity = new Order();
orderEntity.setOrderNumber(paymentDTO.getOrderNumber());
orderEntity.setPaymentStatus(PaymentStatus.COMPLETED);
orderEntity.setOrderStatus(OrderStatus.PENDING);
orderEntity.setPaymentTime(LocalDateTime.now());
orderRepository.updatePaymentStatus(orderEntity);
return result;
}
Database Mapper
/**
* Updates order payment information directly
* Skips WeChat Pay callback verification
*
* @param order order entity with updated payment details
*/
@Update("UPDATE order_table SET payment_status = #{paymentStatus}, " +
"order_status = #{orderStatus}, payment_time = #{paymentTime} " +
"WHERE order_number = #{orderNumber}")
void modifyPaymentStatus(Order order);
Important Considerations
Compilation Warnings
When using Lombok's @Builder annotation, you may encounter warnings about missing non-nullable fields. This occurs because the database table contains columns without default values, and the builder pattern doesn't initialize all required fields. While this doesn't affect compilation or runtime behavior, it can be resolved by using traditional setter methods instead:
Order orderRecord = new Order();
orderRecord.setOrderNumber(paymentDTO.getOrderNumber());
orderRecord.setOrderStatus(OrderStatus.PENDING);
orderRecord.setPaymentStatus(PaymentStatus.COMPLETED);
orderRecord.setPaymentTime(LocalDateTime.now());
Impact on Downstream Features
Order Notifications: In the standard flow, WeChat Pay sends a callback to the merchant system upon successful payment. This callback typically triggers the order notification logic. When bypassing WeChat Pay, this callback never occurs, which means any notification logic placed in the payment callback handler will not execute. To test the notification feature, you may need to manually trigger the notification service after payment processing.
Customer Reminders: The customer reminder functionality remains unaffectde as it operates independently of the payment flow.