DEV Community

Cover image for Integrating Payment Gateways (e.g., Paystack or Stripe) in Your Web Apps
Raji moshood
Raji moshood

Posted on

Integrating Payment Gateways (e.g., Paystack or Stripe) in Your Web Apps

Want to accept payments online? Whether you're building an e-commerce store, a SaaS platform, or a donation page, integrating a payment gateway like Paystack or Stripe is essential. In this guide, we’ll walk you through the step-by-step process of integrating these gateways into your web app so you can securely process payments.

  1. Choosing the Right Payment Gateway

Before integrating, consider:

Paystack – Best for businesses in Africa (Nigeria, Ghana, South Africa, etc.)

Stripe – Ideal for global businesses with a wider range of features

Both support card payments, bank transfers, USSD, and mobile money.

  1. Setting Up a Paystack Integration

Step 1: Create a Paystack Account

Sign up at Paystack

Get your Public and Secret API Keys from the dashboard

Step 2: Install Paystack in Your React/Next.js App

npm install react-paystack

Enter fullscreen mode Exit fullscreen mode

Step 3: Create a Payment Component

import React from "react";
import { PaystackButton } from "react-paystack";

const PayButton = ({ email, amount }: { email: string; amount: number }) => {
  const publicKey = "your-paystack-public-key"; // Replace with your key
  const onSuccess = (reference: any) => alert("Payment Successful!");
  const onClose = () => alert("Payment Canceled!");

  const paystackProps = {
    email,
    amount: amount * 100, // Convert Naira to Kobo
    publicKey,
    text: "Pay Now",
    onSuccess,
    onClose,
  };

  return <PaystackButton {...paystackProps} />;
};

export default PayButton;

Enter fullscreen mode Exit fullscreen mode

Step 4: Add to Checkout Page

import PayButton from "../components/PayButton";

const Checkout = () => {
  return (
    <div>
      <h1>Checkout</h1>
      <PayButton email="customer@example.com" amount={5000} />
    </div>
  );
};

export default Checkout;

Enter fullscreen mode Exit fullscreen mode

✅ Done! Now you can accept payments with Paystack.

  1. Setting Up a Stripe Integration

Step 1: Create a Stripe Account

Sign up at Stripe

Get your Publishable and Secret Keys

Step 2: Install Stripe SDK

npm install @stripe/react-stripe-js @stripe/stripe-js

Enter fullscreen mode Exit fullscreen mode

Step 3: Set Up Stripe Context

import { Elements } from "@stripe/react-stripe-js";
import { loadStripe } from "@stripe/stripe-js";
import CheckoutForm from "../components/CheckoutForm";

const stripePromise = loadStripe("your-stripe-public-key");

const Checkout = () => (
  <Elements stripe={stripePromise}>
    <CheckoutForm />
  </Elements>
);

export default Checkout;

Enter fullscreen mode Exit fullscreen mode

Step 4: Create Checkout Form

import { CardElement, useStripe, useElements } from "@stripe/react-stripe-js";

const CheckoutForm = () => {
  const stripe = useStripe();
  const elements = useElements();

  const handleSubmit = async (event: any) => {
    event.preventDefault();
    if (!stripe || !elements) return;

    const { error, paymentIntent } = await stripe.confirmCardPayment(
      "your-client-secret",
      {
        payment_method: {
          card: elements.getElement(CardElement)!,
        },
      }
    );

    if (error) alert(error.message);
    else alert("Payment successful!");
  };

  return (
    <form onSubmit={handleSubmit}>
      <CardElement />
      <button type="submit" disabled={!stripe}>Pay</button>
    </form>
  );
};

export default CheckoutForm;

Enter fullscreen mode Exit fullscreen mode

✅ Done! Your Stripe integration is live.

  1. Comparing Paystack vs Stripe

Conclusion

Both Paystack and Stripe offer powerful tools to accept payments online. If you’re targeting African markets, Paystack is your best choice. For global payments, go with Stripe.

Paystack #Stripe #PaymentIntegration #WebDevelopment #NextJS #ReactJS #Ecommerce

Top comments (0)