-
-
Notifications
You must be signed in to change notification settings - Fork 4
Payment System Setup
This guide covers the complete setup of the Source-License payment processing system with Stripe and PayPal webhooks for production deployment.
The payment system consists of:
- Payment Processors - Handle Stripe and PayPal API interactions
- Webhook Handlers - Process payment events automatically
- Payment Logger - Comprehensive logging and monitoring
- Subscription Management - Automated license renewals
- Security Layer - Signature verification and fraud prevention
# Payment Gateway Settings
# Stripe
STRIPE_PUBLISHABLE_KEY=pk_live_your_stripe_publishable_key
STRIPE_SECRET_KEY=sk_live_your_stripe_secret_key
STRIPE_WEBHOOK_SECRET=whsec_your_webhook_secret
# PayPal
PAYPAL_CLIENT_ID=your_paypal_client_id
PAYPAL_CLIENT_SECRET=your_paypal_client_secret
PAYPAL_ENVIRONMENT=production # or sandbox for testing
PAYPAL_WEBHOOK_ID=your_paypal_webhook_id
# Application Settings
APP_ENV=production
APP_HOST=yourdomain.com
HTTPS=true
# Logging & Monitoring
LOG_FORMAT=json
LOG_LEVEL=info
MONITORING_WEBHOOK_URL=https://your-monitoring-service.com/webhook-
Login to Stripe Dashboard
- Go to Developers → Webhooks
- Click "Add endpoint"
-
Endpoint Configuration
- URL:
https://yourdomain.com/webhooks/stripe - Events to send:
charge.succeededcharge.failedcharge.refundedcustomer.subscription.createdcustomer.subscription.deletedcustomer.subscription.updatedinvoice.payment_succeededinvoice.payment_failed
- URL:
-
Security
- Copy the webhook signing secret
- Set as
STRIPE_WEBHOOK_SECRETenvironment variable
-
Login to PayPal Developer Console
- Go to Applications → Create App
- Enable webhook features
-
Webhook Configuration
- URL:
https://yourdomain.com/webhooks/paypal - Events to send:
PAYMENT.SALE.COMPLETEDPAYMENT.SALE.DENIEDPAYMENT.SALE.REFUNDEDBILLING.SUBSCRIPTION.CREATEDBILLING.SUBSCRIPTION.CANCELLEDBILLING.SUBSCRIPTION.SUSPENDEDBILLING.SUBSCRIPTION.ACTIVATED
- URL:
-
Security
- Copy the webhook ID
- Set as
PAYPAL_WEBHOOK_IDenvironment variable
The system requires the following tables with these key fields:
-- Required fields for webhook integration
- payment_intent_id (Stripe payment intent)
- transaction_id (PayPal transaction)
- idempotency_key (duplicate prevention)
- payment_method ('stripe' or 'paypal')-- Required fields for automatic management
- customer_email (for webhook matching)
- status ('active', 'suspended', 'revoked')
- expires_at (automatic extension)-- Required fields for recurring payments
- external_subscription_id (Stripe/PayPal subscription ID)
- status ('active', 'canceled', 'past_due')
- current_period_start, current_period_end
- auto_renew (boolean)
- last_payment_at-
Stripe:
charge.succeeded -
PayPal:
PAYMENT.SALE.COMPLETED - Action: Activate/extend license, update subscription status
-
Stripe:
charge.failed -
PayPal:
PAYMENT.SALE.DENIED - Action: Send notification, enter grace period (subscriptions)
-
Stripe:
charge.refunded -
PayPal:
PAYMENT.SALE.REFUNDED - Action: Revoke license, cancel subscription
- Creation: Activate license, set up billing cycle
- Cancellation: Revoke license immediately
- Suspension: Suspend license (temporary)
-
Signature Verification
- All webhooks verified with provider signatures
- Invalid signatures rejected with 400 status
-
Duplicate Prevention
- Idempotency keys prevent duplicate processing
- Database constraints prevent race conditions
-
Comprehensive Logging
- All events logged with structured data
- Security events sent to monitoring systems
- Payment statistics tracked automatically
# Make the test script executable
chmod +x test_webhooks.rb
# Run tests against local development server
./test_webhooks.rb
# Run tests against production (be careful!)
./test_webhooks.rb --url https://yourdomain.com# Install Stripe CLI
stripe listen --forward-to localhost:4567/webhooks/stripe
# Trigger test events
stripe trigger charge.succeeded
stripe trigger customer.subscription.createdUse PayPal's webhook simulator in the developer console to send test events to your endpoint.
-
logs/payments.log- Payment events -
logs/webhooks.log- Webhook processing -
logs/licenses.log- License lifecycle events -
logs/security.log- Security events
The system can send critical events to external monitoring services:
# Set monitoring webhook URL
MONITORING_WEBHOOK_URL=https://your-service.com/webhook
# Events automatically sent:
# - Payment failures
# - Webhook signature failures
# - Duplicate payment attempts
# - Subscription cancellationsMonitor these endpoints:
-
/health- Basic application health -
/ready- Comprehensive readiness check -
/webhooks/health- Webhook system status
- Environment variables configured
- Database schema updated
- Webhook endpoints configured in payment providers
- SSL certificates installed
- Monitoring systems configured
- Test webhook endpoints with providers
- Verify signature verification works
- Test complete payment flows
- Monitor logs for errors
- Set up alerting for failed webhooks
- Webhook endpoints only accessible via HTTPS
- Rate limiting configured for webhook endpoints
- Database backups scheduled
- Monitoring alerts configured
- Log retention policies set
- Check URL accessibility from internet
- Verify SSL certificate validity
- Check webhook configuration in provider dashboard
- Review application logs for errors
- Verify webhook secret is correct
- Check clock synchronization
- Ensure raw payload is used for verification
- Review headers being sent
- Check license lookup logic in webhook handlers
- Verify customer email matching
- Review database constraints
- Check transaction rollbacks in logs
- Monitor webhook response times (< 10 seconds)
- Check database query performance
- Review logging overhead
- Consider webhook queuing for high volume
- Monitor webhook success rates
- Review security logs weekly
- Update payment processor SDKs
- Test webhook endpoints monthly
- Track webhook response times
- Monitor payment success rates
- Review license activation metrics
- Analyze subscription churn rates
# Create payment intent
result = PaymentProcessor.create_payment_intent(order, 'stripe')
# Process payment after customer confirmation
result = PaymentProcessor.process_payment(order, 'stripe', {
payment_method_id: params[:payment_method_id],
idempotency_key: "order_#{order.id}_#{Time.now.to_i}"
})# Create PayPal order
result = PaymentProcessor.create_payment_intent(order, 'paypal')
# Redirect customer to approval_url
redirect result[:approval_url]
# Capture payment after approval
result = PaymentProcessor.process_payment(order, 'paypal', {
order_id: params[:paypal_order_id]
})This comprehensive setup ensures your Source-License payment system is production-ready with robust webhook processing, security, and monitoring capabilities.