Payment Integration
JCV Fitness uses MercadoPago as the primary payment provider for subscription payments in Colombian Pesos (COP). The integration is handled by a Cloudflare Worker that creates payment preferences and processes webhooks.Architecture Overview
Cloudflare Worker Implementation
Worker Configuration
File:cloudflare-worker/wrangler.toml
Worker Secrets
CORS Configuration
Allowed Origins:Payment Flow
1. Preference Creation
Endpoint:POST / (or POST /create-preference)
Request Body:
2. Webhook Processing
Endpoint:POST /webhook or POST /api/webhooks/mercadopago
MercadoPago Webhook Payload:
- Log Receipt
- Validate Notification Type
- Check for Duplicates (Idempotency)
- Fetch Payment Details
- Verify Payment Status
- Find User
- Determine Plan Type
- Check Existing Subscription (Idempotency)
- Create Subscription
- Update Profile
- Create Audit Log
- Update Webhook Log
- Return Success
Error Handling
Error Types
HTTP Status Codes
Worker returns:200 OK: Webhook processed successfully OR permanently failed (no retry needed)500 Internal Server Error: Temporary failure, MercadoPago should retry
Plan Pricing Configuration
Current Pricing (Colombian Pesos):| Plan | Price (COP) | Duration | Worker Amount |
|---|---|---|---|
| Básico | $49,900 | 40 days | 49900 |
| Pro | $89,900 | 40 days | 89900 |
| Premium | $149,900 | 40 days | 149900 |
- Update
PLAN_CONFIGin worker:
- Update frontend pricing:
- Deploy worker:
Testing
Test Mode (Sandbox)
Setup:- Get test credentials from MercadoPago Developer Dashboard
- Set
MP_ACCESS_TOKENto test token - Use staging worker:
mercadopago-jcv-staging.fagal142010.workers.dev
Manual Webhook Testing
Trigger webhook manually:Verify Webhook Logs
Query Supabase:Monitoring
Webhook Logs Dashboard
Key Metrics:- Total webhooks received
- Success rate
- Average processing time
- Error breakdown by type
- Duplicate webhook rate
Cloudflare Worker Logs
Access logs:- Go to Cloudflare Dashboard
- Workers & Pages > mercadopago-jcv
- Logs tab
Security Considerations
Webhook Verification
Current: Worker validates origin and fetches payment details from MercadoPago API Future: Implement signature verificationSecrets Management
Never commit:- MercadoPago access tokens
- Supabase service role keys
- Any API credentials
Troubleshooting
Webhook Not Received
-
Check MercadoPago notification URL:
- Should be:
https://mercadopago-jcv.fagal142010.workers.dev/webhook - Verify in payment preference creation
- Should be:
-
Check Worker logs:
- Look for incoming requests
- Check for CORS errors
-
Manually trigger webhook:
- Use MercadoPago Simulator or manual curl
Payment Approved but Subscription Not Created
-
Check webhook_logs table:
-
Check error_message:
- User not found: Email mismatch?
- Supabase error: RLS policy issue?
-
Verify user exists:
Duplicate Subscriptions
Prevention: Worker checkspayment_reference before creating subscription
If it happens:
Related Documentation
- System Architecture - Overall system design
- Database Schema - Subscription tables
- Deployment Guide - Worker deployment