← Back to list

How to Integrate Stripe Payment Gateway with PHP in 2025

A Simple Guide to Accept Card Payments Using Stripe Elements and PHP

Jamal Derdiwala · 2025-08-21 03:15 · 4 claps · 2.9 min read
#strip #payments #php #web-development #2025
Open on Medium ↗
Wiki topics: FIN · Fintech & Banking 🌐 · Web Development

How to Integrate Stripe Payment Gateway with PHP in 2025

A Simple Guide to Accept Card Payments Using Stripe Elements and PHP

In 2025, Stripe remains one of the easiest and most developer-friendly ways to accept online payments. If you’re building a PHP-based web app and want to start accepting card payments securely, this guide is for you.

I’ll walk through integrating Stripe’s frontend (using Stripe.js and Elements) with a PHP backend using the official Stripe SDK.

Prerequisites

Before we dive into the code, make sure you have:

  • A Stripe account
  • PHP 7.4+ with Composer
  • A web server running locally or online
  • Basic HTML/CSS/JavaScript knowledge

File Structure

Here’s what we’ll build:

/project-root
├── index.php
├── payment.php
├── success.php
├── styles.css
└── vendor/ (generated via Composer)

Step 1: Install Stripe PHP SDK

Use Composer to install the Stripe PHP SDK:

composer require stripe/stripe-php

Step 2: Frontend with Stripe Elements (index.php)

This file displays a simple payment form with a card input field powered by Stripe Elements.

<?php
    $stripePublishableKey = 'pk_test_...'; // Use your own key
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Stripe Payment</title>
    <link rel="stylesheet" href="styles.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://js.stripe.com/v3/"></script>
</head>
<body>
    <h2>Payment App</h2>
    <form id="payment-form">
        <input type="number" name="amount" placeholder="Enter Amount">
        <div id="card-element"></div>
        <div id="error-message"></div>
        <button type="submit" id="submit">Pay</button>
    </form>

    <script>
        const stripe = Stripe('<?php echo $stripePublishableKey; ?>');
        const elements = stripe.elements();
        const cardElement = elements.create('card', { hidePostalCode: true });
        cardElement.mount('#card-element');

        $('#payment-form').on('submit', function (e) {
            e.preventDefault();
            const amount = $('input[name="amount"]').val();

            stripe.createPaymentMethod({ type: 'card', card: cardElement })
            .then(function (result) {
                if (result.error) {
                    $('#error-message').text(result.error.message);
                } else {
                    $.post('payment.php', {
                        amount: amount,
                        payment_method: result.paymentMethod.id
                    }, function (response) {
                        if (response.status === 'success') {
                            window.location.href = 'success.php';
                        } else {
                            $('#error-message').text(response.message);
                        }
                    }, 'json');
                }
            });
        });
    </script>
</body>
</html>

Step 3: Backend PHP Logic (payment.php)

This file securely processes the payment using the Stripe SDK and your secret key.

<?php
require "vendor/autoload.php";

\Stripe\Stripe::setApiKey('sk_test_...'); // Your Stripe Secret Key

header('Content-Type: application/json');

$amount = isset($_POST['amount']) ? (int) $_POST['amount'] : 0;

try {
    $paymentIntent = \Stripe\PaymentIntent::create([
        'amount' => $amount * 100, // Convert to cents
        'currency' => 'usd',
        'payment_method' => $_POST['payment_method'],
        'confirm' => true,
        'capture_method' => 'automatic',
        'automatic_payment_methods' => [
            'enabled' => true,
            'allow_redirects' => 'never',
        ],
    ]);

    if ($paymentIntent->status === 'succeeded') {
        echo json_encode([
            'status' => 'success',
            'client_secret' => $paymentIntent->client_secret,
            'message' => 'Payment successful'
        ]);
    } else {
        echo json_encode([
            'status' => 'failed',
            'message' => 'Payment failed'
        ]);
    }
} catch (Exception $e) {
    echo json_encode([
        'status' => 'failed',
        'message' => $e->getMessage()
    ]);
}
?>

Step 4: Success Page (success.php)

Simple confirmation for the user after payment.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Payment Successful</title>
</head>
<body>
    <div class="success-container">
        <h1>✅ Payment Successful!</h1>
        <p>Thank you for your payment. Your transaction has been completed successfully.</p>
        <a href="index.php" class="btn">Return to Home</a>
    </div>
</body>
</html>

Step 5: Add Styling (styles.css)

Here’s some basic styling to make the form look modern and clean.

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background-color: #f0f2f5;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
    margin: 0;
    flex-direction: column;
}

form#payment-form {
    background-color: #fff;
    padding: 30px 40px;
    border-radius: 10px;
    box-shadow: 0 0 15px rgba(0, 0, 0, 0.08);
    width: 100%;
    max-width: 420px;
}

form#payment-form input[type="number"],
#card-element {
    width: 100%;
    padding: 12px 14px;
    margin-bottom: 20px;
    font-size: 16px;
    border: 1px solid #ccc;
    border-radius: 6px;
    background-color: #fafafa;
}

#submit {
    width: 100%;
    padding: 14px;
    background-color: #6772e5;
    color: #fff;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    transition: background-color 0.3s ease;
}

#submit:hover {
    background-color: #5469d4;
}

#error-message {
    font-size: 14px;
    color: #e63946;
}

Testing Tips

  • Use Stripe’s test card: 4242 4242 4242 4242 with any future expiry and CVC.
  • Don’t use real card details in test mode.
  • Monitor logs in Stripe Dashboard.

Stripe is powerful, flexible, and secure — but also developer-friendly. With this PHP + JavaScript approach, you’re now ready to accept payments on any project in 2025 and beyond.

If you found this guide helpful or want to connect, feel free to reach out:

👉 Connect with me on LinkedIn

Thanks for reading!


메타데이터
post_id
a939bdcc4201
slug
how-to-integrate-stripe-payment-gateway-with-php-in-2025-a939bdcc4201
url
https://medium.com/@derdiwalajamal/how-to-integrate-stripe-payment-gateway-with-php-in-2025-a939bdcc4201
canonical_url
https://medium.com/@derdiwalajamal/how-to-integrate-stripe-payment-gateway-with-php-in-2025-a939bdcc4201
author_url
https://medium.com/@derdiwalajamal
status
ok
fetched_at
2026-07-18 02:21:18