WooCommerce
WooCommerce Payment Gateway Integration Without Checkout Bloat
WooCommerce payment gateway integration is the work of connecting a store to a payment provider with a gateway class, a verified webhook endpoint and a checkout that stays fast. In my builds I register the gateway through woocommerce_payment_gateways, verify every webhook with hash_equals, and load checkout scripts only where they are needed.
What WooCommerce payment gateway integration really covers
A payment gateway integration is not the checkout form alone. It is three separate jobs: a gateway class that WooCommerce can discover, a server-side endpoint that accepts provider callbacks, and a narrow set of front-end assets that render the payment fields. Most problems I am called in to fix come from collapsing these into one plugin file that loads everywhere. A clean build extends WC_Payment_Gateway, keeps settings in the options table via init_form_fields, and hands card entry to the provider hosted fields or tokenisation so raw card data never touches the site. The result is easier to audit, easier to debug and cheaper to run on a slow phone.
- A gateway class extending WC_Payment_Gateway with its own id and title
- A REST or admin-ajax endpoint that accepts provider callbacks
- Settings declared in init_form_fields, not hard-coded keys
- Hosted fields or tokens so card numbers never reach the server
Register the gateway with one filter
WooCommerce discovers gateways through the woocommerce_payment_gateways filter. I add my class to that array once, on plugins_loaded after WooCommerce is active, and guard the call with a class_exists check so the site never fatals when WooCommerce is disabled. Inside the class, init_form_fields declares the enabled flag, title, description, sandbox toggle and API keys; get_option reads them back. process_payment handles the customer action: create the intent through the provider SDK, return the standard redirect array on success, or call wc_add_notice and return failure with a clear message. Keeping every HTTP call in one method means a provider change touches one file, not the theme. Saif Al Jahan builds these as small single-purpose plugins, never as theme code.
- add_filter( "woocommerce_payment_gateways", ...) on plugins_loaded
- class_exists( "WC_Payment_Gateway" ) guard before instantiating
- Declare keys in init_form_fields and read them with get_option
- Return the standard redirect array from process_payment, or wc_add_notice on failure
Verify every webhook before you trust it
A webhook URL is a public endpoint, so treat every request as hostile until it is verified. My baseline is HMAC-SHA256: read the raw body with file_get_contents( "php://input" ), compute hash_hmac( "sha256", $raw, $secret ), and compare it with hash_equals against the provider signature header. hash_equals matters because plain equality leaks timing information. I also check a timestamp header and reject anything older than five minutes to stop replay, and I check the event id against a transient so duplicate deliveries cannot complete the same order twice. Only after those checks do I call wc_get_order, confirm the order total and currency, and run payment_complete with the transaction id. Signature verification goes before any order write.
- Read the raw body once; re-serialising JSON breaks the signature
- hash_equals( $expected, $received ) for constant-time comparison
- Reject timestamps outside a five-minute window
- Store the event id in a transient to make handling idempotent
- Log failures with wc_get_logger(), never card data
Keep checkout free of bloat
Checkout is the one page where a third-party script costs real money, so I load nothing there by default. Scripts are registered with wp_register_script and enqueued only when is_checkout() is true and the gateway is actually selected; the provider hosted-fields SDK stays off product and blog pages. I strip unused gateways with the woocommerce_available_payment_gateways filter instead of asking customers to scan five options. Where possible I use vanilla JavaScript and the provider tokenisation widget rather than a bundled jQuery plugin. Every field gets a label, a clear error message and autocomplete attributes, which helps both accessibility and completion. On a throttled 4G profile I watch INP and LCP on the checkout route specifically, not just on the home page.
- Enqueue gateway JS only on is_checkout() and only for the selected method
- Dequeue unused gateways with woocommerce_available_payment_gateways
- Load the provider SDK with defer, never on non-checkout templates
- Use autocomplete attributes on card-name and address fields
- Measure LCP and INP on the checkout route under throttling
Handle states, refunds and errors predictably
The webhook should update state, not create a second source of truth. I map provider events to WooCommerce statuses deliberately: a succeeded payment calls payment_complete, a failure adds an order note and leaves the order pending, and a refund event calls wc_create_refund when the amount matches. Every branch is written so a repeated delivery produces the same final state. For customers, errors stay specific — an expired card, insufficient funds, an abandoned 3DS challenge — and I never expose provider stack traces in wc_add_notice. I'm Saif Al Jahan, a WordPress and PHP developer in Mymensingh, Bangladesh, and I document these state maps at webdev-saif.com so the next developer can follow the flow without guessing.
Test in sandbox, then verify in production
Before launch I run the provider sandbox end to end: successful charge, declined card, 3DS challenge, refund and a replayed webhook. I log to wc_get_logger() with a dedicated source and keep logs only while WP_DEBUG is on, because gateway logs collect order data. In production I send one real low-value transaction, confirm the order transition and confirm the signature check still passes behind any caching or proxy layer. Then I re-run Lighthouse on the checkout page and compare it with the pre-integration baseline. A gateway that works but adds a second of main-thread work is still a regression, even when the payment succeeds.
- Sandbox matrix: success, decline, 3DS, refund, duplicate webhook
- wc_get_logger() with a dedicated source; disable logs after launch
- One low-value live transaction to verify the production keys
- Re-check LCP and INP against the pre-integration checkout baseline
Frequently asked questions
What does WooCommerce payment gateway integration involve?
It involves three pieces: a gateway class that extends WC_Payment_Gateway and is registered through the woocommerce_payment_gateways filter, a verified server-side webhook endpoint that updates order status, and a small set of checkout-only assets for the payment fields. Card entry stays with provider hosted fields or tokens, so raw card numbers never pass through the WordPress site.
How do I verify a payment webhook in WooCommerce?
Read the raw request body, compute an HMAC-SHA256 digest with your webhook secret, and compare it to the signature header using hash_equals, which is constant-time. Check the timestamp is within about five minutes and store the event id in a transient so a repeated delivery cannot complete the order twice. Only then update the order.
Do I need a plugin for a custom payment gateway?
You need code, but not necessarily a commercial plugin. A small custom plugin or mu-plugin with one gateway class, a settings page built from init_form_fields, and a REST route for callbacks is often cleaner than a multipurpose plugin with options you will never use. Keep it out of the theme so theme updates cannot break payments.
Why is my checkout slow after adding a gateway?
Usually because the gateway script or provider SDK loads on every page, or because several unused gateways are rendered at once. Enqueue assets only on is_checkout(), remove unwanted gateways with woocommerce_available_payment_gateways, and load the SDK with defer. Measure INP and LCP on the checkout route on a throttled connection rather than guessing from the home page score.
Can I store card details in my WooCommerce gateway?
No, and you should not try. Let the payment provider collect card data through hosted fields or a tokenised iframe and send you a token or payment intent id. Your gateway stores that reference, the order id and the transaction id. This keeps the site out of most PCI scope and avoids writing sensitive data to logs.