Skip to main content
This tutorial was written by Claude Code (an AI) and has not yet been reviewed. Follow along with caution. If the tutorial was helpful or a specific part was not clear/correct, please provide feedback at the bottom of the page. Thank you.
This guide covers integrating WooPay (WooCommerce Payments) with CoCart Preview API. Requires CoCart v4.6+ and WooCommerce Payments configured with WooPay enabled.

Overview

WooPay is the express checkout feature of WooCommerce Payments, which is built on Stripe’s infrastructure. This means you can use the Stripe SDK directly with your WooCommerce Payments account for headless checkout implementations.
This tutorial is specifically for WooCommerce Payments users. If you’re using the standalone WooCommerce Stripe Gateway plugin, see the Stripe tutorial instead. Both use Stripe’s infrastructure, but require different payment method IDs.Key Differences:
  • WooCommerce Payments: Available in 39 countries, managed by WooCommerce/Automattic, includes WooPay express checkout. (payment_method: 'woocommerce_payments')
  • Stripe Gateway: Available in 45+ countries, direct Stripe integration, full control over Stripe account. (payment_method: 'stripe')
View supported countries

Why This Works

WooCommerce Payments operates on Stripe’s payment processing infrastructure:
  • Your WooCommerce Payments account is a Stripe Connect account
  • You can use Stripe.js v3 for client-side payment processing
  • All Stripe SDK features are available to you
  • Your publishable key starts with pk_test_ or pk_live_

Prerequisites

Before implementing WooPay checkout, ensure you have:
  1. Supported country - Your business must be registered in one of the supported countries
  2. WooCommerce Payments plugin installed and configured
  3. WooPay enabled in WooCommerce Payments settings
  4. A valid cart with items added via CoCart API
  5. Customer billing address information
  6. Stripe.js v3 loaded in your frontend

Integration Flow

1

Get Your Stripe Credentials

Extract your Stripe publishable key from WooCommerce Payments
2

Initialize Stripe SDK

Set up Stripe.js with your WooCommerce Payments credentials
3

Create Payment Elements

Build the payment form using Stripe Elements
4

Collect Payment Details

Let customers securely enter payment information
5

Complete Checkout

Submit checkout with payment data to CoCart

Step 1: Get Your Stripe Credentials

Since WooCommerce Payments runs on Stripe, you need to extract your Stripe publishable key. Choose one of these methods: Add this temporary code to your functions.php or custom plugin:
Steps:
  1. Add the code above to your site temporarily
  2. Navigate to WordPress Admin Dashboard
  3. Look for the “WooCommerce Payments - Stripe Credentials” notice
  4. Copy your Publishable Key (starts with pk_test_ or pk_live_)
  5. Important: Remove the helper code after copying

Method B: Browser Console (Simpler)

  1. Go to your WooCommerce checkout page
  2. Press F12 to open Developer Tools
  3. Go to the Console tab
  4. Run this command:
Security Reminders:
  • ✅ Publishable keys are safe to expose client-side (that’s their purpose)
  • ⚠️ NEVER expose secret/private keys
  • 🔒 Always use test mode keys during development (pk_test_)
  • 🗑️ Remove helper code after obtaining keys

Step 2: Load Stripe SDK

Add Stripe.js to your frontend application:
Or via npm/yarn if using a build system:

Step 3: Initialize Stripe Elements

Set up Stripe with your WooCommerce Payments publishable key:

Step 4: Create Payment Method

When the customer submits the form, create a payment method using Stripe:

Step 5: Complete Checkout

Process the checkout with CoCart using the payment method:

Complete Integration Example

Here’s a complete, production-ready implementation:

Error Handling

Handle common WooPay/Stripe errors gracefully:

Testing

Test Card Numbers

Use Stripe’s test cards in test mode:

Test Details

  • CVC: Any 3-digit number (4 digits for Amex)
  • Expiry: Any future date
  • Postal Code: Any valid postal code

Enable Test Mode

Ensure WooCommerce Payments is in test mode:
  1. Go to WooCommerce → Settings → Payments
  2. Click WooCommerce Payments
  3. Check “Enable test mode”
  4. Save changes

WooPay Express Checkout

WooPay also offers express checkout features. To check if WooPay express is enabled:
WooPay Express Checkout integration is beyond the scope of this tutorial. This guide focuses on standard credit card processing using WooCommerce Payments’ Stripe infrastructure.

Best Practices

Security

  • Never expose secret keys client-side
  • Always use HTTPS in production
  • Validate billing data before submission
  • Use test mode for development
  • Remove helper code from production

User Experience

  • Show loading states during processing
  • Provide clear, specific error messages
  • Enable real-time card validation
  • Support mobile-friendly inputs
  • Display accepted card types

Error Handling

  • Handle network failures gracefully
  • Log errors for debugging
  • Provide retry mechanisms
  • Never expose technical details to users
  • Test all error scenarios

Performance

  • Load Stripe.js asynchronously
  • Minimize unnecessary API calls
  • Implement request timeouts
  • Cache publishable keys appropriately
  • Monitor payment processing times

Troubleshooting

Common Issues

Problem: Cannot install or activate WooCommerce Payments in your country.Solution: WooCommerce Payments is available in 39 countries. If your country is not on this list, use WooCommerce Stripe Gateway instead.WooCommerce Payments Supported Countries (39): Australia, Austria, Belgium, Bulgaria, Canada, Croatia, Cyprus, Czech Republic, Denmark, Estonia, Finland, France, Germany, Greece, Hong Kong, Hungary, Ireland, Italy, Japan, Latvia, Lithuania, Luxembourg, Malta, Netherlands, New Zealand, Norway, Poland, Portugal, Romania, Singapore, Slovakia, Slovenia, Spain, Sweden, Switzerland, UAE, United Kingdom, United StatesIf your country is NOT listed above:Option 1: Use WooCommerce Stripe Gateway (Recommended)
  1. Uninstall WooCommerce Payments (if installed)
  2. Install WooCommerce Stripe Gateway plugin
  3. Create a direct Stripe account at stripe.com
  4. Follow the Stripe tutorial instead
  5. Use payment_method: 'stripe' in your checkout code
Why Stripe Gateway for unsupported countries?
  • Available in 45+ countries (more than WooCommerce Payments)
  • Includes additional countries like Brazil, India, Indonesia, Malaysia, Mexico, Thailand
  • Direct control over your Stripe account
  • Same Stripe infrastructure and security
  • Full API access for headless commerce
Important Notes:
Problem: Stripe.js hasn’t loaded before initialization.Solution: Wait for the script to load:
Problem: Backend doesn’t recognize payment method.Solution: Ensure you’re using the correct payment method ID:
  • Use 'woocommerce_payments' (not 'stripe')
  • Verify WooCommerce Payments is enabled and active
  • Check that WooCommerce Payments is configured in test/live mode appropriately
  • Important: If your country is not in the 39 supported countries, WooCommerce Payments won’t work - use Stripe Gateway instead
Problem: Card input field doesn’t appear.Solution: Verify the following:
  • Element ID matches: cardElement.mount('#card-element')
  • Container exists in DOM before mount
  • No CSS conflicts hiding the element
  • Check browser console for errors
Problem: Can’t retrieve WooCommerce Payments credentials.Solution:
  • Ensure WooCommerce Payments is fully set up and connected
  • Verify you’re on a checkout page when using browser console method
  • Check that WooCommerce Payments is activated (not just installed)
  • Try the admin notice helper method instead

Additional Resources

Always thoroughly test your WooPay integration in test mode before enabling live payments. Verify that webhooks are configured and orders are being created correctly in WooCommerce.
Remember: WooCommerce Payments uses Stripe’s infrastructure, so all Stripe documentation and best practices apply to your implementation. The key difference is using payment_method: 'woocommerce_payments' when submitting to CoCart’s Checkout API.