Stripe (Global Payments)

The artiframe add stripe extension integrates the stripe/stripe-php package into your project, allowing you to seamlessly process global payments, subscriptions, and payouts via the src/Service/StripeService.php wrapper.

1. Installation and Configuration

Install the extension from your terminal:

terminal
$ artiframe add stripe

After installation, the required environment variables will be added to your .env file. Enter the API keys obtained from your Stripe Dashboard.

.env
STRIPE_PUBLIC_KEY=pk_test_...
STRIPE_SECRET_KEY=sk_test_...
STRIPE_WEBHOOK_SECRET=whsec_...
2. Checkout Sessions (Hosted Payment Page)

Stripe Checkout is the easiest and most secure way to collect payments. Instead of designing a payment form, you direct the user to a secure payment page hosted by Stripe.

CheckoutController.php
<?php
use Src\Service\StripeService;

$stripe = new StripeService();

$session = $stripe->createCheckoutSession([
    'payment_method_types' => ['card'],
    'line_items' => [[
        'price_data' => [
            'currency' => 'usd',
            'product_data' => [
                'name' => 'Premium Plan',
            ],
            'unit_amount' => 2000, // Amount in cents ($20.00)
        ],
        'quantity' => 1,
    ]],
    'mode' => 'payment',
    'success_url' => 'https://yoursite.com/success?session_id={CHECKOUT_SESSION_ID}',
    'cancel_url' => 'https://yoursite.com/cancel',
]);

// Redirect the user to Stripe Checkout
header("Location: " . $session->url);
exit;
3. Subscriptions (Recurring Payments)

You can easily assign recurring billing plans configured on the Stripe Dashboard to a user.

SubscriptionController.php
<?php
use Src\Service\StripeService;

$stripe = new StripeService();

// 1. Create a Customer in Stripe
$customer = $stripe->createCustomer([
    'email' => '[email protected]',
    'payment_method' => 'pm_card_visa', // Payment Method ID usually received from frontend (Stripe Elements)
    'invoice_settings' => [
        'default_payment_method' => 'pm_card_visa',
    ],
]);

// 2. Start the Subscription
$subscription = $stripe->createSubscription([
    'customer' => $customer->id,
    'items' => [
        ['price' => 'price_1Hh1...'], // Price ID from Stripe Dashboard
    ],
    'expand' => ['latest_invoice.payment_intent'],
]);

echo "Subscription Status: " . $subscription->status;
4. Webhooks (Asynchronous Notifications)

Stripe notifies your application via Webhooks about background events like successful recurring payments or failed charges.

WebhookController.php
<?php
use Src\Service\StripeService;

// Route: /stripe/webhook
$payload = file_get_contents('php://input');
$sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];

$stripe = new StripeService();

try {
    $event = $stripe->constructWebhookEvent($payload, $sig_header);
} catch(\UnexpectedValueException $e) {
    http_response_code(400); // Invalid payload
    exit();
} catch(\Stripe\Exception\SignatureVerificationException $e) {
    http_response_code(400); // Invalid signature
    exit();
}

// Handle the event based on its type
switch ($event->type) {
    case 'payment_intent.succeeded':
        $paymentIntent = $event->data->object;
        // Update database: Order Paid!
        break;
    case 'customer.subscription.deleted':
        $subscription = $event->data->object;
        // Update database: Subscription Cancelled!
        break;
    default:
        // Unexpected event type
}

http_response_code(200);