Building a WooCommerce Batch Payment Processor: A Deep Dive
In the world of e-commerce, payment processing is a critical component that directly impacts both business operations and customer experience. Today, we’ll explore how to implement a batch payment processing system in WooCommerce that allows you to collect orders throughout the week and process their payments in batches.
Understanding the Architecture
At its core, our batch payment processor introduces a new order status called “batch-pending” and modifies WooCommerce’s standard payment flow. Here’s how it works:
- Customer places an order
- Order is intercepted before payment processing
- Order status is set to “batch-pending”
- Payment is processed later in a batch
The Implementation
1. Custom Order Status
First, we register a new custom order status that will hold our orders until processing:
phpCopyfunction register_batch_pending_order_status() {
register_post_status('wc-batch-pending', array(
'label' => 'Batch Pending',
'public' => true,
'show_in_admin_status_list' => true,
'show_in_admin_all_list' => true,
'exclude_from_search' => false,
'label_count' => _n_noop('Batch Pending <span class="count">(%s)</span>',
'Batch Pending <span class="count">(%s)</span>')
));
}
2. Payment Interception
The key to batch processing is intercepting the payment at the right moment:
phpCopyfunction intercept_stripe_payment($result, $order_id) {
$order = wc_get_order($order_id);
if (!$order) return $result;
// Set to batch-pending
$order->update_status('batch-pending', 'Order queued for batch processing');
return array(
'result' => 'success',
'redirect' => $order->get_checkout_order_received_url()
);
}
3. Batch Processing
When it’s time to process the payments, our processor collects all batch-pending orders and processes them:
phpCopyfunction process_batch_payments() {
$orders = wc_get_orders(array(
'status' => 'batch-pending',
'limit' => -1,
));
foreach ($orders as $order) {
try {
$gateway = new WC_Gateway_Stripe();
$result = $gateway->process_payment($order->get_id());
if ($result['result'] === 'success') {
$order->add_order_note('Batch payment processed successfully.');
}
} catch (Exception $e) {
$order->add_order_note('Batch payment error: ' . $e->getMessage());
}
}
}
Advanced Features
Automated Weekly Processing
The system automatically schedules processing for a specific time:
phpCopyfunction schedule_batch_processing() {
if (!wp_next_scheduled('process_batch_payments')) {
wp_schedule_event(strtotime('next Monday'), 'weekly', 'process_batch_payments');
}
}
Manual Processing Option
For flexibility, we’ve added a manual processing button in the admin interface:
phpCopyfunction add_batch_process_button() {
global $post;
if (!$post) return;
$order = wc_get_order($post->ID);
if (!$order || $order->get_status() !== 'batch-pending') return;
?>
<button type="button" class="button process-batch-payment">
Process Batch Payment
</button>
<?php
}
Security Considerations
The implementation includes several security measures:
- Permission checks for manual processing
- Nonce verification for AJAX calls
- Proper sanitization of order IDs
- Status validation before processing
Customer Experience
From the customer’s perspective, the process is seamless:
- They complete checkout normally
- Receive a confirmation message
- Their order shows as “Batch Pending”
- Payment is processed during the next batch run
What’s Next?
Consider these potential enhancements:
- Email notifications when batch processing completes
- Custom batch schedules per product category
- Priority processing for certain order types
- Detailed batch processing reports
Technical Requirements
To implement this system, you need:
- WooCommerce 3.0+
- WordPress 5.0+
- Stripe payment gateway
- WP-Cron enabled for automated processing
Conclusion
Batch payment processing can significantly streamline your e-commerce operations, especially for businesses that prefer to handle payments in scheduled batches. This implementation provides a robust foundation that can be customized to meet specific business needs.
Remember to thoroughly test the system in a staging environment before deploying to production, as it modifies core payment flows in WooCommerce.
Note: The code snippets shown are part of a larger implementation. Always backup your site before implementing payment-related modifications.