Skip to content

Sarai-Chinwag/stripe-integration

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Stripe Integration

Generic Stripe payment integration for WordPress plugins. Provides a single source of truth for Stripe API keys and webhook handling that multiple plugins can share.

Requirements

  • PHP 8.1+
  • WordPress 6.9+
  • Composer

Installation

  1. Clone or download the plugin to wp-content/plugins/stripe-integration
  2. Run composer install --no-dev
  3. Activate the plugin
  4. Configure keys in Settings > Stripe Integration
  5. Add the webhook URL to your Stripe Dashboard

Configuration

API Keys

Go to Settings > Stripe Integration and enter:

  • Test Mode: Enable for development/testing
  • Test Secret Key: Your Stripe test secret key (starts with sk_test_)
  • Test Publishable Key: Your Stripe test publishable key (starts with pk_test_)
  • Live Secret Key: Your Stripe live secret key (starts with sk_live_)
  • Live Publishable Key: Your Stripe live publishable key (starts with pk_live_)
  • Webhook Secret: Your webhook endpoint secret (starts with whsec_)

Webhook

The webhook URL is: https://yoursite.com/wp-json/stripe-integration/v1/webhook

Add this URL in your Stripe Dashboard under Developers > Webhooks.

Usage

Creating a Checkout Session

use StripeIntegration\StripeClient;

$result = StripeClient::create_checkout_session([
    'mode' => 'payment',
    'line_items' => [
        [
            'price_data' => [
                'currency' => 'usd',
                'unit_amount' => 2000, // $20.00
                'product_data' => [
                    'name' => 'My Product',
                ],
            ],
            'quantity' => 1,
        ],
    ],
    'success_url' => home_url('/success?session_id={CHECKOUT_SESSION_ID}'),
    'cancel_url' => home_url('/cancel'),
    'metadata' => [
        'plugin' => 'my-plugin',
        'order_id' => 123,
    ],
]);

if (is_wp_error($result)) {
    // Handle error
} else {
    // Redirect to $result['url']
}

Handling Webhooks

// Listen for checkout completed events
add_action('stripe_integration_webhook_checkout_session_completed', function($session, $event) {
    // Only handle events for your plugin
    if (($session['metadata']['plugin'] ?? '') !== 'my-plugin') {
        return;
    }
    
    $order_id = $session['metadata']['order_id'] ?? null;
    // Process the successful payment...
}, 10, 2);

// Listen for subscription cancellations
add_action('stripe_integration_webhook_customer_subscription_deleted', function($subscription, $event) {
    // Handle subscription cancellation...
}, 10, 2);

Available Webhook Actions

Event names with dots are converted to underscores:

  • stripe_integration_webhook_checkout_session_completed
  • stripe_integration_webhook_payment_intent_succeeded
  • stripe_integration_webhook_payment_intent_payment_failed
  • stripe_integration_webhook_invoice_paid
  • stripe_integration_webhook_invoice_payment_failed
  • stripe_integration_webhook_customer_subscription_created
  • stripe_integration_webhook_customer_subscription_updated
  • stripe_integration_webhook_customer_subscription_deleted
  • stripe_integration_webhook_charge_refunded
  • stripe_integration_webhook (fires for all events)

Available Filters

// Modify checkout params before creation
add_filter('stripe_integration_checkout_params', function($params, $context) {
    // Add custom metadata
    $params['metadata']['custom_field'] = 'value';
    return $params;
}, 10, 2);

// Modify webhook response
add_filter('stripe_integration_webhook_response', function($response, $event) {
    return $response;
}, 10, 2);

Helper Functions

// Check if Stripe is configured
if (stripe_integration_is_configured()) {
    // Stripe is ready to use
}

// Get publishable key for frontend
$publishable_key = stripe_integration_get_publishable_key();

// Check if in test mode
if (stripe_integration_is_test_mode()) {
    // Using test keys
}

// Get webhook URL
$webhook_url = stripe_integration_get_webhook_url();

Other StripeClient Methods

use StripeIntegration\StripeClient;

// Create billing portal session
$portal = StripeClient::create_billing_portal_session($customer_id, $return_url);

// Create refund
$refund = StripeClient::create_refund($payment_intent_id, $amount_in_cents, 'reason');

// Cancel subscription
$result = StripeClient::cancel_subscription($subscription_id, $immediately = false);

// Update subscription
$result = StripeClient::update_subscription($subscription_id, ['metadata' => ['key' => 'value']]);

// Get customer invoices
$invoices = StripeClient::get_invoices($customer_id, $limit = 10);

// Create customer
$customer = StripeClient::create_customer(['email' => 'user@example.com']);

// Retrieve checkout session
$session = StripeClient::retrieve_checkout_session($session_id);

// Validate configuration (test connection)
$result = StripeClient::validate_configuration(); // returns true or WP_Error

WordPress Abilities API (WordPress 6.9+)

If running WordPress 6.9+ with the Abilities API available, these abilities are registered for AI agents:

  • stripe_get_config - Get configuration status (without secrets)
  • stripe_test_connection - Test the API connection
  • stripe_get_webhook_url - Get the webhook URL

License

GPL v2 or later

About

Generic Stripe payment integration for WordPress plugins

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages