7.7 KiB
Square Payment Integration Setup Guide
This guide walks you through setting up Square payment processing for the SASA Membership Portal.
Overview
The application supports two payment methods:
- Square Payments: Credit/debit card payments processed through Square's Web Payments SDK
- Cash Payments: Manual payments recorded by administrators
Prerequisites
- A Square Developer account
- Access to the Square Developer Dashboard
- Square Sandbox credentials for testing (included with all Square accounts)
Setup Steps
1. Create a Square Developer Account
- Go to Square Developer Portal
- Sign up for a free developer account or log in with your existing Square account
- Navigate to the Applications Dashboard
2. Create or Select an Application
- In the Applications Dashboard, create a new application or select an existing one
- Name your application (e.g., "SASA Membership Portal")
- Click "Save"
3. Get Your Credentials
For Sandbox (Testing):
- In your application, go to the Sandbox tab
- Copy the following credentials:
- Sandbox Access Token: Found under "Sandbox Access Token"
- Sandbox Application ID: Found under "Sandbox Application ID"
- Sandbox Location ID: Click on "Locations" to find your sandbox location ID
For Production (Live Payments):
- In your application, go to the Production tab
- Copy the following credentials:
- Production Access Token: Found under "Production Access Token"
- Production Application ID: Found under "Production Application ID"
- Production Location ID: Click on "Locations" to find your production location ID
4. Configure Environment Variables
Update your .env file with the Square credentials:
# For Sandbox (Testing)
SQUARE_ACCESS_TOKEN=EAAAl... # Your Sandbox Access Token
SQUARE_ENVIRONMENT=sandbox
SQUARE_LOCATION_ID=LXXX... # Your Sandbox Location ID
SQUARE_APPLICATION_ID=sandbox-sq0idb-... # Your Sandbox Application ID
# For Production (Live Payments)
SQUARE_ACCESS_TOKEN=EAAAl... # Your Production Access Token
SQUARE_ENVIRONMENT=production
SQUARE_LOCATION_ID=LXXX... # Your Production Location ID
SQUARE_APPLICATION_ID=sq0idp-... # Your Production Application ID
5. Restart the Application
After updating the environment variables, restart your Docker containers:
docker-compose down
docker-compose up -d --build
Testing with Sandbox
Square provides test card numbers for sandbox testing:
Test Card Numbers
| Card Number | Result |
|---|---|
| 4111 1111 1111 1111 | Successful payment |
| 4000 0000 0000 0002 | Card declined - Insufficient funds |
| 4000 0000 0000 0010 | Card declined - CVV failure |
| 5105 1051 0510 5100 | Successful payment (Mastercard) |
Test CVV: Any 3-digit number (e.g., 111)
Test Expiration: Any future date (e.g., 12/25)
Test Postal Code: Any valid postal code (e.g., 12345)
Testing the Payment Flow
- Log in to the membership portal
- Navigate to membership setup
- Select a membership tier
- Choose "Credit/Debit Card" as payment method
- Use one of the test card numbers above
- Complete the payment
In sandbox mode, no real money is charged.
Features Implemented
Backend (backend/app/)
-
services/square_service.py: Core Square integration service- Payment processing
- Payment retrieval
- Refund processing
- Customer creation
-
api/v1/payments.py: Payment endpointsGET /api/v1/payments/config/square: Get Square configuration for frontendPOST /api/v1/payments/square/process: Process Square paymentPOST /api/v1/payments/square/refund: Refund a payment (admin only)
-
schemas/schemas.py: Payment schemasSquarePaymentRequest: Request schema for processing paymentsSquarePaymentResponse: Response schema for payment resultsSquareRefundRequest: Request schema for refunds
Frontend (frontend/src/)
-
components/SquarePayment.tsx: Square Web Payments SDK integration- Card input form
- Token generation
- Payment submission
- Error handling
-
components/MembershipSetup.tsx: Updated membership flow- Payment method selection
- Integration with SquarePayment component
- Cash payment option
-
index.html: Square Web Payments SDK script tag
Payment Flow
- User selects membership tier → Creates pending membership in database
- User chooses payment method:
- Square: Square payment form loads
- Cash: Simple confirmation dialog
- Square Payment:
- User enters card details
- Card is tokenized by Square SDK (PCI-compliant)
- Token sent to backend
- Backend processes payment with Square API
- Payment record created in database
- Membership activated
- Confirmation email sent
- Cash Payment:
- Payment record created with "pending" status
- Membership remains "pending"
- Admin must manually approve
Security Features
- PCI Compliance: Card data never touches your server (handled by Square SDK)
- Tokenization: Card details are converted to secure tokens
- Idempotency: Prevents duplicate payments
- Environment Separation: Clear sandbox/production separation
- Authorization: Payment endpoints require authentication
- Admin Controls: Refunds require admin privileges
Currency
The system is configured for GBP (British Pounds). To change the currency:
- Update
square_service.pyline 56: Change'currency': 'GBP' - Update frontend display symbols in
MembershipSetup.tsxandSquarePayment.tsx
Troubleshooting
"Failed to load payment configuration"
- Check that
SQUARE_APPLICATION_IDis set in.env - Verify the backend is running and accessible
- Check browser console for CORS errors
"Payment processing failed"
- Verify
SQUARE_ACCESS_TOKENis valid - Check
SQUARE_LOCATION_IDmatches your account - Ensure
SQUARE_ENVIRONMENTmatches your token type (sandbox/production) - Check backend logs for detailed error messages
"Card tokenization failed"
- Ensure Square SDK loaded (check Network tab in browser DevTools)
- Verify
SQUARE_APPLICATION_IDmatches the environment - Check that test card numbers are valid for sandbox
Database errors
- Ensure
squareuppackage is installed:pip install squareup==43.2.0.20251016 - Restart backend container after updating requirements.txt
Going Live with Production
Before accepting real payments:
- Get approved by Square: Complete verification in Square Dashboard
- Update credentials: Switch to production credentials in
.env - Change environment: Set
SQUARE_ENVIRONMENT=production - Update SDK URL: In
frontend/index.html, change:<!-- Change from sandbox --> <script src="https://sandbox.web.squarecdn.com/v1/square.js"></script> <!-- To production --> <script src="https://web.squarecdn.com/v1/square.js"></script> - Test thoroughly: Test the complete flow with real cards (you can refund these)
- Monitor: Watch for errors in logs and Square Dashboard
Support
- Square Documentation: https://developer.squareup.com/docs/web-payments/overview
- Square Support: https://squareup.com/help/contact
- API Reference: https://developer.squareup.com/reference/square