This guide covers integrating PayPal with CoCart Preview API. Requires CoCart v4.6+ and a configured PayPal payment gateway.
Overview
PayPal integration with CoCart uses PayPal’s JavaScript SDK to create a seamless checkout experience. Customers can pay with their PayPal account, credit cards, or other payment methods supported by PayPal.Prerequisites
Before implementing PayPal checkout, ensure you have:- PayPal payment gateway configured in WooCommerce
- PayPal JavaScript SDK loaded in your frontend
- A valid cart with items added
- Customer billing address information
Integration Flow
1
Initialize PayPal Buttons
Set up PayPal SDK with your store’s client ID and cart information
2
Handle Payment Creation
Create PayPal order when customer clicks Pay with PayPal
3
Handle Payment Approval
Process the PayPal payment approval and capture transaction details
4
Complete Checkout
Submit checkout with PayPal order ID to CoCart for order creation
Step 1: Load PayPal SDK
Include the PayPal JavaScript SDK in your page:YOUR_PAYPAL_CLIENT_ID with your actual PayPal client ID from your WooCommerce PayPal settings.
Step 2: Get Cart Information
Get the cart total and currency for PayPal order creation:- JavaScript
- PHP
Step 3: HTML Structure
Your checkout form should include containers for PayPal buttons and customer information:Step 4: Initialize PayPal Buttons
Set up PayPal buttons with your cart information:Step 5: Process Checkout with PayPal Data
Submit the checkout with PayPal transaction information:Complete Integration Example
Here’s a complete working example with form validation:Error Handling
Handle common PayPal errors:Testing
For development and testing:- Use PayPal Sandbox accounts
- Test with different PayPal account types (personal, business)
- Test payment cancellations
- Test network failures during payment
Troubleshooting
Common Issues
PayPal Buttons Not Displaying
PayPal Buttons Not Displaying
Wrong Client ID or Currency Error
Wrong Client ID or Currency Error
Problem: “Invalid client ID” or currency mismatch errors.Solution: Ensure SDK configuration matches your store:
- Get client ID from WooCommerce → Settings → Payments → PayPal
- Verify currency matches your store’s currency
- Use sandbox client ID for testing, live client ID for production
- Check that your PayPal account supports the currency
'createOrder' Function Errors
'createOrder' Function Errors
Problem: Errors when creating PayPal order.Solution: Verify order data structure:
- Ensure amount.value is a valid number or string
- Verify currency_code matches SDK initialization
- Check that cart total is greater than zero
- Validate all required billing/shipping fields are provided
Payment Approved But Order Not Created
Payment Approved But Order Not Created
Problem: PayPal payment succeeds but WooCommerce order fails to create.Solution: Debug the onApprove callback:
- Check browser console for error details
- Verify billing address is complete and valid
- Check PHP error logs for server-side issues
- Ensure payment_method is set to ‘paypal’
- Verify WooCommerce PayPal gateway is active
Buttons Disabled or Grayed Out
Buttons Disabled or Grayed Out
'INSTRUMENT_DECLINED' Error
'INSTRUMENT_DECLINED' Error
Problem: Customer’s payment method is declined by PayPal.Solution: Handle gracefully:Common reasons:
- Insufficient funds in PayPal account
- Payment method (bank/card) linked to PayPal is invalid
- PayPal account restrictions or limitations
- International transaction not supported
Payment Cancellation Issues
Payment Cancellation Issues
Problem: onCancel not triggered or not handled properly.Solution: Implement comprehensive cancellation handling:
- Test cancellation flow thoroughly
- Ensure UI resets to allow retry
- Don’t treat cancellation as an error
- Provide clear next steps for customer
Sandbox vs Production Mode Issues
Sandbox vs Production Mode Issues
Problem: Works in sandbox but fails in production.Solution: Verify production configuration:Checklist for going live:
- Switch to live client ID in WooCommerce settings
- Update SDK script to use live client ID
- Verify PayPal account is verified and approved for production
- Test with real PayPal account (small amount)
- Configure production webhooks
- Enable live payment processing in WooCommerce
Webhook Events Not Received
Webhook Events Not Received
Problem: Not receiving PayPal webhook notifications.Solution: Configure webhooks properly:
- Go to PayPal Developer Dashboard → My Apps & Credentials
- Select your app
- Scroll to Webhooks section
- Add webhook URL:
https://yoursite.com/wc-api/wc_gateway_paypal_express_helper/ - Subscribe to events:
- Payment sale completed
- Payment sale refunded
- Payment capture completed
- Test webhook delivery
- Check webhook signature verification in WooCommerce logs
Amount Mismatch Errors
Amount Mismatch Errors
Problem: “Amount mismatch” errors during checkout.Solution: Ensure consistency:
- Use consistent decimal formatting (2 decimal places)
- Match currency codes everywhere
- Don’t modify cart during PayPal flow
- Verify cart hasn’t changed between order creation and checkout
Debug Mode
Enable detailed logging for troubleshooting:Getting Help
If issues persist:- PayPal Developer Forums: Visit PayPal Developer Community
- PayPal Documentation: Check PayPal JavaScript SDK documentation
- WooCommerce Logs: Review logs at WooCommerce → Status → Logs
- Browser Console: Always check for JavaScript errors
- Network Tab: Monitor API calls in browser DevTools
Best Practices
User Experience
- Validate forms before enabling PayPal buttons
- Show clear loading states during processing
- Handle payment cancellations gracefully
- Provide clear error messages
Security
- Use HTTPS for all requests
- Validate data server-side
- Never expose sensitive keys client-side
- Handle webhook validations properly
Performance
- Load PayPal SDK asynchronously
- Cache payment contexts when possible
- Minimize API calls
- Handle timeouts appropriately
Accessibility
- Ensure buttons are keyboard accessible
- Provide alternative payment methods
- Use proper ARIA labels
- Test with screen readers
Always test your PayPal integration thoroughly using PayPal’s sandbox environment before going live. Configure webhook endpoints to handle payment notifications and updates.