Conditional Payment Authorization
When processing payments, you might want to handle certain orders differently based on specific criteria. For example, high-value orders might require manual review before charging the customer, while standard orders can be processed immediately. This code sample shows how to automatically authorize (but not capture) payments when the order total exceeds a certain threshold.
This approach gives you control over payment timing based on order characteristics, allowing you to hold funds for orders that need additional verification while maintaining a smooth checkout experience for typical purchases.
When would you use this?
Conditional authorization is particularly useful for businesses that need different payment handling based on order value, product types, or customer characteristics. Instead of authorizing all payments or none at all, you can make intelligent decisions based on the specific order details.
Common use cases include holding payments for high-value orders that require fraud verification, authorizing payments for orders containing restricted products that need additional checks, or implementing different payment flows for new customers versus returning customers.
The Code
This example authorizes payments (without immediate capture) when the order total exceeds $500, while allowing immediate capture for smaller orders.
/**
* Conditionally authorize payments based on order total
* @param array $args Payment intent arguments
* @param WC_Order $order The order object
* @return array Modified payment intent arguments
*/
add_filter('wc_stripe_payment_intent_args', function($args, $order){
if($order->get_total() > 500){
$args['capture_method'] = 'manual';
}else{
$args['capture_method'] = 'automatic';
}
return $args;
}, 10, 2);
How the Code Works
The code uses the wc_stripe_payment_intent_args
filter, which runs when Stripe payment intents are being created. This filter receives two parameters: the payment intent arguments array that will be sent to Stripe, and the WooCommerce order object containing all the order details.
The function checks the order total using $order->get_total()
and compares it to the $500 threshold. When the total exceeds this amount, it sets capture_method
to 'manual'
, which tells Stripe to authorize the payment without immediately capturing the funds. For orders under the threshold, it sets capture_method
to 'automatic'
for immediate capture.
Customizing the Authorization Logic
You can easily modify the authorization conditions based on your business needs. Here are some common variations:
Multiple threshold levels:
if($order->get_total() > 1000){
$args['capture_method'] = 'manual';
// Add additional processing for very high-value orders
} elseif($order->get_total() > 500){
$args['capture_method'] = 'manual';
} else {
$args['capture_method'] = 'automatic';
}
Check for specific product categories:
$authorize = false;
foreach($order->get_items() as $item){
$product = $item->get_product();
if($product && in_array(15, $product->get_category_ids())){
$authorize = true;
break;
}
}
$args['capture_method'] = $authorize ? 'manual' : 'automatic';
Authorize for new customers:
$customer = new WC_Customer($order->get_customer_id());
if($customer->get_order_count() === 0){
$args['capture_method'] = 'manual';
} else {
$args['capture_method'] = 'automatic';
}
Important Considerations
Stripe payment authorizations are valid for up to 7 days, after which they expire and cannot be captured. Make sure your order fulfillment and review processes can complete within this timeframe, or you'll need to process a new payment.
When payments are authorized but not captured, customers will see a pending charge on their payment method. Be sure to communicate your payment process clearly to avoid confusion or customer service inquiries about pending charges.
Testing Your Implementation
To verify the authorization logic is working correctly, create test orders with totals both above and below your threshold in your Stripe test environment. Check your Stripe dashboard to confirm that high-value orders show as "Authorized" while smaller orders show as "Succeeded" (captured immediately).
You can also add logging to track when authorization conditions are met:
add_filter('wc_stripe_payment_intent_args', function($args, $order){
if($order->get_total() > 500){
$args['capture_method'] = 'manual';
wc_stripe_log_info("Order {$order->get_id()} set to manual capture - total: {$order->get_total()}");
} else {
$args['capture_method'] = 'automatic';
}
return $args;
}, 10, 2);
Remember to update the threshold amount and conditions to match your specific business requirements before deploying to your live site.