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')
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_orpk_live_
Prerequisites
Before implementing WooPay checkout, ensure you have:- Supported country - Your business must be registered in one of the supported countries
- WooCommerce Payments plugin installed and configured
- WooPay enabled in WooCommerce Payments settings
- A valid cart with items added via CoCart API
- Customer billing address information
- 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:Method A: Admin Notice Helper (Recommended)
Add this temporary code to yourfunctions.php or custom plugin:
- Add the code above to your site temporarily
- Navigate to WordPress Admin Dashboard
- Look for the “WooCommerce Payments - Stripe Credentials” notice
- Copy your Publishable Key (starts with
pk_test_orpk_live_) - Important: Remove the helper code after copying
Method B: Browser Console (Simpler)
- Go to your WooCommerce checkout page
- Press
F12to open Developer Tools - Go to the Console tab
- Run this command:
Step 2: Load Stripe SDK
Add Stripe.js to your frontend application:Step 3: Initialize Stripe Elements
Set up Stripe with your WooCommerce Payments publishable key:- Vanilla JavaScript
- React
- HTML Form
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:| Card Number | Scenario |
|---|---|
4242424242424242 | Successful payment |
4000000000000002 | Card declined |
4000000000009995 | Insufficient funds |
4000000000000069 | Expired card |
4000000000000127 | Incorrect CVC |
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:- Go to WooCommerce → Settings → Payments
- Click WooCommerce Payments
- Check “Enable test mode”
- 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
WooCommerce Payments Not Available in My Country
WooCommerce Payments Not Available in My Country
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)
- Uninstall WooCommerce Payments (if installed)
- Install WooCommerce Stripe Gateway plugin
- Create a direct Stripe account at stripe.com
- Follow the Stripe tutorial instead
- Use
payment_method: 'stripe'in your checkout code
- 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
- Your business registration location determines eligibility, not your personal residence
- UAE only supports Business accounts (not Individual accounts)
- Check if your country supports WooCommerce Payments
- Check if your country supports Stripe directly
'Stripe is not defined' Error
'Stripe is not defined' Error
Problem: Stripe.js hasn’t loaded before initialization.Solution: Wait for the script to load:
Payment Method Not Accepted
Payment Method Not Accepted
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
Card Element Not Displaying
Card Element Not Displaying
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
Publishable Key Not Found
Publishable Key Not Found
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
- WooCommerce Payments Documentation
- Stripe Elements Documentation
- Stripe API Reference
- CoCart Checkout API Reference
- Payment Processing Fundamentals
- Stripe Gateway Tutorial - For standalone Stripe plugin users
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.