blogs

Stripe Payment Intent and Confirmation

Overview

Stripe Payment Intents API provides a flexible and powerful way to handle payments securely on your website. Payment Intents allow you to create and confirm payments asynchronously, enabling more control over the payment process and better handling of potential errors.

Steps for Using Payment Intents and Confirmation:

1. Set Up Stripe and Obtain API Keys:

2. Install Stripe JavaScript Library:

3. Create a Payment Intent:

4. Handle Payment Confirmation:

5. Implement Additional Features:

Example Code Snippets:

Server-Side Code (PHP) to Create Payment Intent:

<?php

// Include Stripe PHP library
require_once 'vendor/autoload.php';

// Set your Stripe API key
\Stripe\Stripe::setApiKey('sk_test_YourSecretKey');

// Retrieve payment amount from POST request
$amount = $_POST['amount'];

try {
    // Create a Payment Intent with the provided amount
    $payment_intent = \Stripe\PaymentIntent::create([
        'amount' => $amount,
        'currency' => 'usd',
    ]);

    // Return the Payment Intent status
    echo json_encode(['status' => $payment_intent->status]);
} catch (\Stripe\Exception\ApiErrorException $e) {
    // Handle any errors from Stripe API
    echo json_encode(['error' => $e->getMessage()]);
}

// Function to create Payment Intent and handle payment confirmation
function handlePayment(amount) {
    // Make a POST request to create Payment Intent
    fetch('create_payment_intent.php', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({ amount: amount })
    })
    .then(function(response) {
        return response.json();
    })
    .then(function(data) {
        // Check Payment Intent status returned from server
        if (data.status === 'requires_payment_method') {
            // Payment Intent created successfully, confirm payment
            confirmPaymentIntent(data.payment_intent_id);
        } else {
            // Payment Intent creation failed or status not as expected
            console.error('Payment Intent creation failed or status not as expected.');
            // Example: Update UI to display error message
            document.getElementById('payment-status').innerText = 'Payment Intent creation failed!';
        }
    })
    .catch(function(error) {
        // Handle any errors from the server or network
        console.error('Error:', error);
        // Example: Update UI to display error message
        document.getElementById('payment-status').innerText = 'Error creating Payment Intent!';
    });
}

// Function to confirm Payment Intent on the client-side
function confirmPaymentIntent(paymentIntentId) {
    // Confirm the Payment Intent with the provided ID
    // This part will be similar to the previous example for confirming payment
    // You can reuse the confirmPayment function from the previous example
}

// Call the function with the desired payment amount when needed
handlePayment(1000); // Example: Handle payment for $10.00

Overview