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.
- PHP 8.1+
- WordPress 6.9+
- Composer
- Clone or download the plugin to
wp-content/plugins/stripe-integration - Run
composer install --no-dev - Activate the plugin
- Configure keys in Settings > Stripe Integration
- Add the webhook URL to your Stripe Dashboard
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_)
The webhook URL is: https://yoursite.com/wp-json/stripe-integration/v1/webhook
Add this URL in your Stripe Dashboard under Developers > Webhooks.
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']
}// 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);Event names with dots are converted to underscores:
stripe_integration_webhook_checkout_session_completedstripe_integration_webhook_payment_intent_succeededstripe_integration_webhook_payment_intent_payment_failedstripe_integration_webhook_invoice_paidstripe_integration_webhook_invoice_payment_failedstripe_integration_webhook_customer_subscription_createdstripe_integration_webhook_customer_subscription_updatedstripe_integration_webhook_customer_subscription_deletedstripe_integration_webhook_charge_refundedstripe_integration_webhook(fires for all events)
// 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);// 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();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_ErrorIf 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 connectionstripe_get_webhook_url- Get the webhook URL
GPL v2 or later