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 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:
  1. PayPal payment gateway configured in WooCommerce
  2. PayPal JavaScript SDK loaded in your frontend
  3. A valid cart with items added
  4. 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:
Replace 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:
Important: Your PayPal client ID in the JavaScript SDK must exactly match the one configured in your WooCommerce PayPal settings.

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:
  1. Use PayPal Sandbox accounts
  2. Test with different PayPal account types (personal, business)
  3. Test payment cancellations
  4. Test network failures during payment

Troubleshooting

Common Issues

Problem: PayPal buttons don’t appear on the page.Solution: Check these common issues:
  • Verify the PayPal SDK script tag is present and loading
  • Check client ID is correct and matches WooCommerce settings
  • Ensure container element exists in DOM
  • Look for JavaScript errors in browser console
  • Verify no CSS is hiding the container
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
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
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
Problem: PayPal buttons appear but are disabled.Solution: Common causes:
  • Verify form validation isn’t blocking buttons
  • Check for pointer-events: none CSS
  • Ensure buttons finished rendering completely
  • Look for overlay elements covering buttons
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
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
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
Problem: Not receiving PayPal webhook notifications.Solution: Configure webhooks properly:
  1. Go to PayPal Developer Dashboard → My Apps & Credentials
  2. Select your app
  3. Scroll to Webhooks section
  4. Add webhook URL: https://yoursite.com/wc-api/wc_gateway_paypal_express_helper/
  5. Subscribe to events:
    • Payment sale completed
    • Payment sale refunded
    • Payment capture completed
  6. Test webhook delivery
  7. Check webhook signature verification in WooCommerce logs
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:
  1. PayPal Developer Forums: Visit PayPal Developer Community
  2. PayPal Documentation: Check PayPal JavaScript SDK documentation
  3. WooCommerce Logs: Review logs at WooCommerce → Status → Logs
  4. Browser Console: Always check for JavaScript errors
  5. 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.