BETA
My accountGet started
Feb 112025
Total.js Platform
5 min read

Building SaaS Website #11: Payment Integration

In this installment, we'll explore how to integrate payment systems into our TotalGPT application. We'll focus on two payment processors: - Ligdicash (for African mobile money and card payments) - Stripe (for global card payments)

Building SaaS Website #11: Payment Integration

In this installment, we'll explore how to integrate payment systems into our TotalGPT application. By adding payment capabilities, we'll transform our website from a simple landing page into a fully functional SaaS platform capable of handling subscriptions and processing payments.

Understanding Payment Processors

We'll be implementing two different payment processors, each serving specific geographical needs:

Ligdicash

  • Specializes in African payment methods
  • Supports mobile money services (Orange Money, MTN Mobile Money, Moov Money)
  • Handles local credit card payments
  • Perfect for West African markets
  • Processes payments in XOF (West African CFA franc)
  • Popular in countries like Burkina Faso, Niger, Togo, CΓ΄te d'Ivoire, and Senegal

Stripe

  • Global payment solution
  • Primarily handles credit card payments
  • Available in 40+ countries
  • Supports multiple currencies
  • Provides extensive developer tools
  • Ideal for international customers
  • Known for robust security and ease of use

Why Two Payment Processors?

Having multiple payment processors helps us:

  1. Reach more customers by supporting their preferred payment methods
  2. Provide backup payment options if one system is unavailable
  3. Offer local payment methods where they're most relevant
  4. Handle different currencies efficiently

What We'll Cover

In this guide, we'll walk through:

  1. Setting up both payment processors
  2. Creating payment workflows
  3. Handling successful and failed payments
  4. Managing subscriptions
  5. Sending payment notifications via WhatsApp

Let's begin with The Payment Flow...


The Payment Flow - A User's Journey

Step 1: Choosing a Plan

When a user visits TotalGPT, they:

  1. Browse available plans on the pricing page
  2. Click on their preferred plan
  3. Get taken to the plan details page

Step 2: Starting Payment

On the plan details page, the user:

  1. Enters their phone number
  2. Selects subscription duration (1 month, 3 months, etc.)
  3. Clicks the "Pay Now" button

Step 3: Payment Process

After clicking "Pay Now":

  1. User receives a WhatsApp message with their payment link
  2. Gets redirected to the payment gateway (Ligdicash or Stripe)
  3. Completes payment using their preferred method (mobile money or card)

Step 4: Completion

After payment:

  1. User gets redirected back to TotalGPT website
  2. Receives confirmation on WhatsApp if payment is successful
  3. Their subscription is automatically activated

Step 5: If Something Goes Wrong

If payment fails:

  1. User sees a failure message
  2. Gets option to try again
  3. Can contact support if needed

Behind the Scenes

Here's what happens in the background:

  1. TotalGPT creates a payment invoice
  2. Sends it to the payment provider (Ligdicash/Stripe)
  3. Records the order in the database
  4. Waits for payment confirmation
  5. Updates user's subscription when payment succeeds
  6. Sends notifications via WhatsApp

Important Features

  • WhatsApp notifications keep users informed
  • Automatic subscription activation
  • Support for different subscription durations
  • Secure payment processing
  • Error handling if something goes wrong

Let's start with Ligdicash integration.

Setting Up Ligdicash

1. Create Definition File

First, let's create a definition file for Ligdicash integration:

2. Controller Setup

Next, let's implement the payment routes in our controller:

Key Components Explained

  1. Definition File Setup
    • Creates a singleton Ligdicash client
    • Stores client in MAIN for global access
    • Handles configuration via CONF object
  1. Checkout Process
    • Validates user and plan
    • Creates invoice with plan details
    • Generates payment URL
    • Creates order record
    • Handles success/failure scenarios
  1. Callback Handling
    • Processes payment notifications
    • Updates order status
    • Updates user subscriptions
    • Sends notifications
  1. Database Integration
    • Uses Total.js DATA object for database operations
    • Maintains order records
    • Updates user subscriptions
  1. Notification System
    • Sends WhatsApp messages to users
    • Notifies administrators of successful payments
    • Handles error scenarios

Adding Stripe Integration

First, we need to install Stripe and set up a definition file, similar to how we did with Ligdicash.

1. Create Stripe Definition File

Create /definitions/stripe.js:

2. Update Controller

In /controllers/default.js, add new route handlers:

3. Add Routes

Add these routes to your exports.install:

4. Update Payment Form

Give users the option to choose their payment method:

Key Differences from Ligdicash

  1. Currency: Stripe uses cents (multiply by 100)
  2. Webhook Handling: Different event structure
  3. Payment Methods: Cards only (unless you add specific local payment methods)
  4. Session-based: Uses Stripe Checkout Sessions
  5. International Focus: Better for global payments

Important Notes

  1. Testing: Use Stripe test keys during development
  2. Webhooks: Set up Stripe webhook endpoint for live updates
  3. Error Handling: Implement proper error handling for failed payments
  4. Currency Conversion: Handle currency conversion if needed
  5. SSL: Ensure your website uses HTTPS

What's Next?

In the next blog post, we'll cover Stripe integration and compare the implementation differences between Ligdicash and Stripe. We'll also explore how to create a unified payment interface that works with multiple payment providers.

Stay tuned for more Total.js development insights! πŸš€