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 general payment processing concepts with CoCart Preview API. For specific gateway implementations, see our dedicated tutorials.
Overview
The CoCart Checkout API supports various payment processing patterns, from simple server-side methods to complex client-side integrations. Understanding these fundamentals will help you implement any payment gateway effectively.Payment Method Types
Server-Side Payment Methods
These payment methods process entirely on the server and require minimal client-side code:Bank Transfer (BACS)
Customer transfers money directly to your bank account
Check Payments
Customer mails a physical check for the order
Cash on Delivery
Payment collected when the order is delivered
Cryptocurrency
Various crypto payment gateways (BitPay, Coinbase, etc.)
Client-Side Payment Methods
These require customer interaction and client-side processing before checkout:Credit Card Gateways
Stripe, Square, Authorize.Net - require card tokenization
Digital Wallets
PayPal, Apple Pay, Google Pay - handle payment authorization
Buy Now, Pay Later
Klarna, Afterpay, Sezzle - require payment plan setup
Bank Payments
ACH, SEPA, Open Banking - require bank authorization
Payment Processing Approaches
Production Implementation (Recommended)
For production applications, use direct gateway integration:- Credit Card Processing
- Digital Wallet Processing
- Server-Side Processing
Development and Debugging
For development purposes, admin users can access payment gateway debug information:Debug information is only available to users with
manage_options
capability and should never be used in production applications.Payment Method Data Structures
Understanding how to structure payment data for different gateway types:Credit Card Gateways
For tokenized card payments (Stripe, Authorize.Net, Square):Digital Wallets
For wallet-based payments (PayPal, Apple Pay, Google Pay):Server-Side Methods
For methods processed entirely server-side:Payment Method Configuration
Gateway Credential Alignment
Critical: Frontend SDK credentials must match WooCommerce gateway settings:Stripe
Frontend:
Stripe('pk_live_abc123')
WooCommerce: Same publishable key in Stripe settingsPayPal
Frontend:
client: {live: 'client_id_123'}
WooCommerce: Same client ID in PayPal settingsAuthorize.Net
Frontend:
clientKey: 'public_key_123'
WooCommerce: Same public client key in Auth.Net settingsSquare
Frontend:
applicationId: 'app_id_123'
WooCommerce: Same application ID in Square settingsCheckout Request Structure
Complete Checkout Example
Gateway-Specific Payment Data
- Stripe
- PayPal
- Square
Common Integration Patterns
Pattern 1: Simple Server-Side Payment
For payment methods that don’t require client-side processing:Pattern 2: Token-Based Payment
For gateways that tokenize payment methods:Pattern 3: Multi-Step Payment Flow
For complex payment flows with multiple client-server interactions:Error Handling
Common Error Types
Validation Errors
Missing required fields, invalid data formats
Payment Errors
Declined cards, insufficient funds, gateway issues
Network Errors
Timeout, connection issues, server errors
Business Logic Errors
Out of stock, invalid shipping, cart changes
Error Response Structure
Error Handling Implementation
Best Practices
Security
Client-Side Security
Client-Side Security
- Never expose secret keys in client-side code
- Use HTTPS for all API requests
- Validate all input data on both client and server
- Implement proper CORS policies
- Use Content Security Policy (CSP) headers
PCI Compliance
PCI Compliance
- Never store credit card numbers
- Use gateway tokenization for card storage
- Implement proper data encryption
- Follow PCI DSS requirements
- Regular security audits and testing
Performance
Client-Side Performance
Client-Side Performance
- Load payment SDKs asynchronously
- Cache payment contexts when possible
- Minimize API calls during checkout
- Implement proper loading states
- Use request timeouts
Server-Side Performance
Server-Side Performance
- Cache payment method configurations
- Use database transactions for checkout
- Implement proper error logging
- Monitor gateway response times
- Set up proper webhook handling
User Experience
Form Design
Form Design
- Use progressive disclosure for complex forms
- Implement real-time validation
- Provide clear error messages
- Support keyboard navigation
- Test with screen readers
Payment Flow
Payment Flow
- Show clear progress indicators
- Handle payment redirects gracefully
- Provide payment method options
- Support guest and registered users
- Implement proper success/failure flows
Testing Strategies
Development Testing
Integration Testing
- Test with gateway sandbox/test environments
- Verify webhook endpoint functionality
- Test error scenarios and edge cases
- Validate payment data structure
- Test across different browsers and devices
Production Monitoring
- Monitor checkout success/failure rates
- Track gateway response times
- Log payment errors for analysis
- Set up alerts for critical failures
- Monitor webhook delivery status
Remember that payment processing involves sensitive financial data. Always prioritize security, follow industry best practices, and thoroughly test your implementation before going live.