DEV Community

Cover image for I Built a WooCommerce Payment Gateway for a Government Portal — Here's How AI Made It Possible
Digvijay singh
Digvijay singh

Posted on

I Built a WooCommerce Payment Gateway for a Government Portal — Here's How AI Made It Possible

Intro
Most WooCommerce payment gateway tutorials use Stripe or Razorpay — well-documented, with SDKs and community support everywhere. My project was different. I had to build a custom gateway for a government portal — sparse documentation, non-standard callback behavior, and zero Stack Overflow answers.

What got me through it was a combination of 4+ years of PHP/WordPress experience and two AI tools: Claude and ChatGPT. In this article I'll walk you through exactly what I built, where AI genuinely helped, and where it failed me.

The problem
The client needed to accept payments via PSU's government payment infrastructure — think portal callbacks, strict server-side verification, and an API that behaved nothing like a modern payment SDK. WooCommerce has no official support for this. There was no existing plugin. I had to build from scratch.

The requirements were:

  • Redirect customer to PSU payment portal on checkout
  • Handle payment callback (success/failure) from PSU's server
  • Verify the transaction server-side before updating order status
  • Send email confirmations
  • Admin configuration panel for API credentials

How I used AI — and how I didn't
I used both Claude and ChatGPT throughout this project. Here's the honest breakdown:

Boilerplate & architecture
I asked Claude to scaffold the WooCommerce payment gateway class structure. It gave me a clean starting point with WC_Payment_Gateway extended correctly — something that would have taken me 30 minutes to piece together from docs.

Debugging PHP logic
Whenever I hit a logic error — wrong hook priority, order status not updating — I pasted the relevant function into ChatGPT. It caught a missing wc_reduce_stock_levels() call I'd overlooked for 2 hours.

Where AI failed: payment callbacks
This is where I had to rely on my own experience. BSNL's callback didn't follow standard patterns. Neither AI tool had seen this API before. They gave me generic answers. I had to read the portal's PDF documentation line by line, test with live transactions, and debug raw POST data myself. AI got me 60% there. The last 40% was pure engineering.

code snippet -
The callback handler

Here's a simplified version of the payment callback logic. The key insight: always verify the transaction server-side before trusting the callback data.

public function payment_callback() {
    $order_id  = sanitize_text_field( $_POST['order_id'] ?? '' );
    $txn_id    = sanitize_text_field( $_POST['txn_id'] ?? '' );
    $status    = sanitize_text_field( $_POST['status'] ?? '' );

    if ( empty( $order_id ) || empty( $txn_id ) ) {
        wp_die( 'Invalid callback data', 400 );
    }

    // Always verify server-side — never trust callback alone
    $verified = $this->verify_transaction( $txn_id );

    $order = wc_get_order( $order_id );

    if ( $verified && $status === 'SUCCESS' ) {
        $order->payment_complete( $txn_id );
        $order->add_order_note( 'Payment verified via BSNL. TXN: ' . $txn_id );
    } else {
        $order->update_status( 'failed', 'Payment verification failed.' );
    }
wp_redirect( $this->get_return_url( $order ) );
    exit;
}
Enter fullscreen mode Exit fullscreen mode

The verify_transaction() method makes a separate server-to-server API call to BSNL to confirm the payment — this is the critical step most tutorials skip.

What I learned

  • AI tools are excellent for scaffolding and boilerplate — use them aggressively for that
  • For undocumented or niche APIs, AI hallucinates. Always verify against official docs
  • Always verify payments server-side. Never trust client-side or callback data alone
  • Government portals often have quirky behavior — log everything during development
  • Claude was better at architecture decisions. ChatGPT was faster at quick debugging snippets

Outro
This was one of the more challenging plugins I've shipped — not because the code was complex, but because the external system was unpredictable. AI compressed my development time significantly, but the real work was still mine: reading the docs, testing edge cases, and understanding WooCommerce's order lifecycle deeply enough to plug everything together.

If you're building a custom payment gateway — government portal or otherwise — happy to answer questions in the comments.


GitHub: github.com/singhdigvijay99
Portfolio: singhdigvijay99.github.io/portfolio

Top comments (0)