Use this guide to integrate an existing WooCommerce payment gateway with WooCommerce Subscriptions. You need a working gateway integration, a staging store, and a provider sandbox to verify the result.
Note:
This is a developer-level doc. If you are unfamiliar working with code and resolving potential conflicts, we recommend you work with a Woo Agency Partner for larger projects, or find a WooCommerce developer on Codeable for smaller customization work. We are unable to provide support for customization under our Support Policy.
Scope of support:
We are unable to provide support for customizations under our Support Policy. If you need to customize a snippet or extend its functionality, we recommend working with a Woo Agency Partner or a WooCommerce developer on Codeable.
Step 1: Registering Support for Subscriptions
↑ Back to topDeclare subscriptions in the gateway’s supports array after initializing the gateway. Keep the existing capabilities. Checkout still applies the normal gateway availability checks.
$this->supports[] = 'subscriptions';
Step 1.1: Registering Support for Subscription Management Features
↑ Back to topAdd each applicable feature to supports after you implement and test it. For example, subscription_cancellation, subscription_suspension, and subscription_reactivation declare status changes your gateway can handle. subscription_amount_changes and subscription_date_changes declare support for edits to a subscription’s amount and schedule.
Use multiple_subscriptions only after testing sign-ups that create more than one subscription in a single order. Register and test the payment method separately if your extension supports the Checkout block.
Step 2: Processing a Subscription Sign-Up
↑ Back to topIn your gateway’s process_payment() method, follow the standard WooCommerce checkout flow. Use the initial order for the payment due at sign-up and the subscription records for recurring amounts and dates.
Initial Payment
↑ Back to topUse $order->get_total() for the amount due on the initial order. Handle a zero total, which can occur with a free trial. If your gateway needs a payment method for future charges, set it up through the provider’s supported mechanism even when no payment is due.
Price Per Period
↑ Back to topRead the current recurring total from each WC_Subscription with get_total(). For WooCommerce-scheduled payments, use the amount passed to the renewal handler. Discounts, shipping, tax, and other changes can affect the renewal total.
Sign-up Fee
↑ Back to topThe initial order total already reflects applicable sign-up fees. When a provider needs the configured fee as a distinct input, use WC_Subscriptions_Product::get_sign_up_fee( $product ) on the relevant subscription product or variation. This is a product setting. Use the initial order for the total you charge the customer.
Billing Period
↑ Back to topUse get_billing_period() and get_billing_interval() on the subscription. For example, a period of month and an interval of 3 mean every three months.
Free Trial Period
↑ Back to topUse get_date( 'trial_end' ) and get_date( 'next_payment' ) on the subscription to identify scheduled dates. The date accessor returns GMT by default. Check that a date exists before using it to schedule a payment.
Subscription Length
↑ Back to topCheck get_date( 'end' ) on the subscription for its scheduled end. An open-ended subscription has no scheduled end date.
Putting it all Together
↑ Back to topUse wcs_get_subscriptions_for_order() to find the subscriptions associated with the sign-up order. Keep provider identifiers scoped to the corresponding subscription so later events resolve to the right record. See Subscription Function and Property Reference for object methods.
Step 3: Subscription Management
↑ Back to topDecide whether WooCommerce or the payment provider controls the renewal schedule. Keep a single owner for each schedule and test status changes and retries for that path.
Order Status & Subscription Status Binding
↑ Back to topRecord payment results on the WooCommerce order that represents the transaction. Subscriptions listens for order status changes and applies the corresponding updates to linked subscriptions. The initial order and a renewal order represent different payments. For a renewal, use the renewal order supplied to your handler.
Subscription payment and status management
↑ Back to topUse the Subscriptions Action Reference for lifecycle hooks. For a provider callback, verify the event using the provider’s supported mechanism, look up the specific local record, and handle repeat deliveries without recording a payment twice.
Recurring Payments Processed by the Gateway
Add gateway_scheduled_payments to supports when the provider controls the billing schedule. Subscriptions skips its gateway charge dispatch for that subscription. Your integration must reconcile the provider’s confirmed payment and lifecycle events with the local subscription and its renewal order.
Recurring Payments Processed by your Extension
When WooCommerce controls the schedule, subscribe to woocommerce_scheduled_subscription_payment_{gateway_id}. Subscriptions passes the renewal amount and a WC_Order renewal order. For example, place this registration in the constructor of your gateway class after setting its ID. The gateway must provide the process_scheduled_renewal method.
add_action(
'woocommerce_scheduled_subscription_payment_' . $this->id,
array( $this, 'process_scheduled_renewal' ),
10,
2
);
Implement the callback using your provider’s saved payment method flow. Identify the charge by its renewal order and use your provider’s safeguards against duplicate charges. Account for asynchronous payment confirmation before updating the order.
Activating a Subscription
Record a successful initial payment with the WooCommerce order API. Subscriptions handles activation from the relevant order status transition when the subscription is eligible. Check the initial order and every linked subscription in your sign-up test.
Canceling a subscription
Declare subscription_cancellation if the gateway supports cancellation. For automatic subscriptions, Subscriptions passes the WC_Subscription to woocommerce_subscription_cancelled_{gateway_id} when the local subscription reaches Cancelled. Use the provider reference on that subscription to update the provider if your gateway manages an external agreement.
Subscriptions may first enter Pending Cancellation to retain prepaid access. If your gateway manages the provider schedule, verify that canceling in WooCommerce prevents future renewal charges while preserving access for the prepaid term.
Cancellations at the Gateway
If the provider reports a cancellation, match the event to the affected subscription. Apply the status appropriate to its remaining prepaid term. Treat an agreement that is already canceled at the provider as settled when the local status update runs.
Expiring a Subscription
Subscriptions can schedule an expiration for a subscription with an end date. Verify the provider schedule ends when expected if the provider controls billing. Record a provider expiration only after matching it to the subscription.
Recording Payments
After the provider confirms the renewal payment, call payment_complete( $transaction_id ) on the associated renewal order. The transaction ID is the provider’s reference for the confirmed payment. Subscriptions can then update the linked subscription from the order transition.
Recording Failed Payments
For a confirmed failure, update the corresponding renewal order to failed with an explanatory order note. An unconfirmed or still-processing payment needs its own pending handling. Keep the provider’s error details free of payment credentials.
Order API vs Individual Subscription API
↑ Back to topUse the renewal order to record the result of a payment. Use the subscription object for subscription-specific dates, gateway references, and lifecycle events. Check which subscription a provider event refers to before updating it, especially when an initial order created several subscriptions.
Subscription Management Example
↑ Back to topFor a scheduled WooCommerce renewal, use the renewal order passed to the gateway handler and record the provider’s result on that order. For a provider-scheduled renewal, reconcile the provider event with its corresponding local subscription and renewal order. Test that a duplicate event leaves the same order and subscription state.
Step 4: Failed Payments
↑ Back to topCheck the renewal order and subscription after a decline. A failure on the latest renewal order can place the subscription On hold. If the store uses the Failed Payment Retry System, verify that the retry behavior matches the store’s rules and gateway capabilities.
Step 5: Recurring Payment Method Changes
↑ Back to topHandle subscriber-initiated changes and changes made while paying for a failed renewal. The future payment method must be associated with the subscription.
5.1: Supporting Subscriber Payment Method Changes
↑ Back to topDeclare subscription_payment_method_change_customer if your extension supports customer-initiated changes. The change flow uses a zero total. Test that the provider’s saved payment method is updated for the subscription without taking a payment.
5.2: Updating the Payment Method After a Failure
↑ Back to topUse woocommerce_subscription_failing_payment_method_updated_{gateway_id} to update gateway-specific payment information on the subscription after a customer pays for a failed renewal. The hook receives the WC_Subscription and renewal order. Use WooCommerce object methods to save any gateway-specific references your integration needs.
Step 6: Testing Renewal Payments
↑ Back to topUse a staging store with your provider’s sandbox or test mode. Follow Testing Subscription Renewal Payments to trigger a renewal on a fictional subscription.
- Test a paid sign-up, a zero-total sign-up, and an automatic renewal. Match the provider transaction to the local order and confirm the subscription status and next payment date.
- Run a renewal using the provider’s documented test decline. Verify the renewal order status, subscription status and notes, and any scheduled retry.
- Test an eligible payment method change and any supported status or schedule changes. Repeat a test notification to verify that the integration records the result once.
Troubleshoot renewal payments
↑ Back to topPayment succeeded but the renewal order is still pending
↑ Back to topIf the provider reports a successful payment and the order is still pending, confirm that the provider transaction matches the local renewal order. Check the provider event history, gateway log and order notes near the event time. In WordPress admin, open WooCommerce > Status > Logs to inspect available gateway and fatal error logs. Your hosting provider can help locate PHP errors outside WooCommerce.