Skip to content

canovack/stripe-api-sample

Repository files navigation

Stripe PHP Payment Example

Author: Abbas Can Ovacık

This project demonstrates a simple Stripe payment integration using PHP, Docker, and Stripe.js. It allows users to enter billing and card details, process payments, and handle 3D Secure authentication.

Features

  • Stripe Elements for secure card input
  • Billing address and contact information collection
  • Payment processing via Stripe PaymentIntents API
  • 3D Secure (authentication) support
  • SweetAlert2 integration for elegant success/error notifications
  • Phone number input masking with Inputmask
  • Enhanced payment status handling with detailed feedback
  • Dockerized PHP 8.2 + Apache environment

Recent Improvements

  • SweetAlert2 Integration: Replaced basic HTML notifications with elegant, user-friendly alerts
  • 📱 Phone Input Masking: Added automatic phone number formatting (+1 (999) 999-9999)
  • 🔧 Enhanced Error Handling: Improved payment status feedback with detailed error messages
  • 🌐 Internationalization: All UI messages and alerts in English
  • 🎨 Better UX: Form automatically resets after successful payment

Project Structure

  • public/ — Main web application files
    • card-payment.html — Payment form UI with Stripe Elements integration
    • payment.js — Stripe.js integration, form handling, and SweetAlert2 notifications
    • payment.css — Styling for the payment form
    • create.php — Backend endpoint to create Stripe PaymentIntent
    • complete.php — Endpoint to handle post-payment status validation
    • complete.html — Payment completion page
    • complete.js — Client-side completion handling
    • status.php — Payment status check endpoint
    • public_key.php — Endpoint to expose the Stripe publishable key securely
  • secrets.php — Loads Stripe secret key and publishable key from environment (.env)
  • .env — Environment variables (Stripe keys, etc.) - Not tracked in git
  • .env.example — Environment variables template
  • composer.json / composer.lock — PHP dependencies (Stripe PHP SDK)
  • Dockerfile — PHP 8.2 + Apache setup
  • docker-compose.yml — Development environment orchestration
  • LICENSE — MIT License

Setup

  1. Clone the repository
  2. Configure Stripe keys
    • Copy .env.example to .env and fill in your Stripe keys:
      STRIPE_SECRET_KEY=sk_test_xxx
      STRIPE_PUBLISHABLE_KEY=pk_test_xxx
    • secrets.php will automatically load keys from .env
    • public/public_key.php exposes only the publishable key to frontend
  3. Install dependencies
    • Run:
      docker compose run --rm composer install
  4. Start the application
    • Run:
      docker compose up -d
    • Access the app at http://localhost:8080

Usage

  • Open http://localhost:8080/card-payment.html in your browser
  • Enter billing and card details (phone number is automatically formatted)
  • Submit the form to process payment
  • If 3D Secure is required, you will be redirected for authentication
  • Payment status will be displayed via SweetAlert2 notification
  • For testing, use Stripe test card: 4242 4242 4242 4242 with any future expiry date and CVC

Security Notes

  • Never expose your Stripe secret key publicly
  • public_key.php is used to expose only the publishable key
  • This example is for educational and test purposes only
  • For production, ensure PCI compliance and secure key management

License

This project is licensed under the MIT License - see the LICENSE file for details.

Copyright (c) 2026–present Abbas Can Ovacık

Technologies & Libraries

About

Stripe PaymentIntents example using PHP, Docker, and Stripe.js

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published