Square Payments Integration Guide
This guide explains how to set up and use Square payments in your Gully Store application.
Overviewโ
The application now supports both Intuit and Square payment providers. You can switch between them using a feature flag without changing any code.
Prerequisitesโ
- A Square account (free to create at squareup.com)
- Square Application credentials
- Square Location ID
Getting Your Square Credentialsโ
Step 1: Create a Square Developer Accountโ
- Go to developer.squareup.com
- Sign in with your Square account or create a new one
- Navigate to the Developer Dashboard
Step 2: Create an Applicationโ
- Click "+ Create App"
- Give your app a name (e.g., "Gully Store")
- Click "Create Application"
Step 3: Get Your Application IDโ
-
Open your newly created application
-
Navigate to "Credentials" in the left sidebar
-
You'll see two environments:
- Sandbox (for testing)
- Production (for live payments)
-
Copy the following credentials:
- Application ID (same for both sandbox and production)
- Access Token (different for sandbox and production)
Step 4: Get Your Location IDโ
- In the Square Developer Dashboard, go to "Locations"
- Copy the Location ID for the location where you want to process payments
- You can also get this via the API:
curl https://connect.squareupsandbox.com/v2/locations \
-H 'Square-Version: 2024-01-18' \
-H 'Authorization: Bearer YOUR_SANDBOX_ACCESS_TOKEN' \
-H 'Content-Type: application/json'
Configurationโ
Environment Variablesโ
Add these environment variables to your .env.local file:
# Payment Provider Configuration
# Set to "square" to use Square payments, or "intuit" for Intuit payments
NEXT_PUBLIC_PAYMENT_PROVIDER=square
# Square Configuration (Public - used in browser)
NEXT_PUBLIC_SQUARE_APPLICATION_ID=your_square_application_id
NEXT_PUBLIC_SQUARE_LOCATION_ID=your_square_location_id
NEXT_PUBLIC_SQUARE_ENVIRONMENT=sandbox # or "production"
# Square Configuration (Private - server-side only)
SQUARE_ACCESS_TOKEN=your_square_access_token
SQUARE_ENVIRONMENT=sandbox # or "production"
SQUARE_LOCATION_ID=your_square_location_id
SQUARE_WEBHOOK_SIGNATURE_KEY=your_webhook_signature_key # Optional, for webhooks
Example Configuration for Sandboxโ
# Use Square for payments
NEXT_PUBLIC_PAYMENT_PROVIDER=square
# Public configuration
NEXT_PUBLIC_SQUARE_APPLICATION_ID=sandbox-sq0idb-XXXXXXXXXXXXXX
NEXT_PUBLIC_SQUARE_LOCATION_ID=LXXXXXXXXXXXXXX
NEXT_PUBLIC_SQUARE_ENVIRONMENT=sandbox
# Private configuration
SQUARE_ACCESS_TOKEN=EAAAXXXXXXXXXXXXXXXXXXXXXXXXXX
SQUARE_ENVIRONMENT=sandbox
SQUARE_LOCATION_ID=LXXXXXXXXXXXXXX
Example Configuration for Productionโ
# Use Square for payments
NEXT_PUBLIC_PAYMENT_PROVIDER=square
# Public configuration
NEXT_PUBLIC_SQUARE_APPLICATION_ID=sq0idp-XXXXXXXXXXXXXX
NEXT_PUBLIC_SQUARE_LOCATION_ID=LXXXXXXXXXXXXXX
NEXT_PUBLIC_SQUARE_ENVIRONMENT=production
# Private configuration
SQUARE_ACCESS_TOKEN=EAAAXXXXXXXXXXXXXXXXXXXXXXXXXX
SQUARE_ENVIRONMENT=production
SQUARE_LOCATION_ID=LXXXXXXXXXXXXXX
Switching Between Payment Providersโ
To switch between Intuit and Square, simply change the NEXT_PUBLIC_PAYMENT_PROVIDER environment variable:
Use Square Paymentsโ
NEXT_PUBLIC_PAYMENT_PROVIDER=square
Use Intuit Paymentsโ
NEXT_PUBLIC_PAYMENT_PROVIDER=intuit
That's it! The application will automatically use the correct payment form and API endpoints.
Payment Featuresโ
Square Supports:โ
- โ Credit/Debit Card Payments
- โ Apple Pay
- โ Google Pay
- โ Gift Cards
- โ Secure card tokenization
- โ PCI compliance (Square handles card data)
Intuit Supports:โ
- โ Credit/Debit Card Payments
- โ ACH/Bank Transfers
- โ PayPal
- โ Venmo
- โ Apple Pay
Testingโ
Quick Google Pay Checkโ
If Google Pay is not showing up on Chrome:
-
Check Console Logs:
- Open DevTools (F12 or Cmd+Option+I)
- Look for "Google Pay initialized successfully" or error messages
-
Verify Requirements:
- Using Chrome (version 61+)
- Signed into Chrome with Google account
- At least one payment method at pay.google.com
- Google Pay enabled in Square Dashboard
- Site on HTTPS or localhost
-
See Detailed Troubleshooting:
- Refer to
GOOGLE_PAY_TROUBLESHOOTING.mdfor complete guide - Common fix: Sign into Chrome and add payment method to Google Pay
- Refer to
Sandbox Mode (Testing)โ
Square provides test card numbers for sandbox testing:
| Card Brand | Card Number | CVV | Expiry |
|---|---|---|---|
| Visa | 4111 1111 1111 1111 | Any | Any future |
| Mastercard | 5105 1051 0510 5100 | Any | Any future |
| Amex | 3400 000000 00009 | Any | Any future |
| Discover | 6011 0000 0000 0004 | Any | Any future |
Note: Always use sandbox credentials during development!
Test Payment Flowโ
- Set
NEXT_PUBLIC_PAYMENT_PROVIDER=square - Use sandbox credentials
- Go to
/shopand add items to cart - Proceed to checkout
- Use a test card number
- Complete the payment
- Verify in Square Dashboard โ Payments (Sandbox mode)
Architectureโ
File Structureโ
src/
โโโ utils/
โ โโโ squarePayments.ts # Square API integration
โ โโโ intuitPayments.ts # Intuit API integration
โ โโโ paymentProvider.ts # Abstraction layer
โ โโโ featureFlags.ts # Feature flag configuration
โโโ app/
โ โโโ checkout/
โ โ โโโ CheckoutForm.tsx # Router component
โ โ โโโ SquareCheckoutForm.tsx # Square-specific form
โ โ โโโ IntuitCheckoutForm.tsx # Intuit-specific form
โ โโโ api/
โ โโโ payments/
โ โโโ submit/
โ โ โโโ route.ts # Unified payment submission
โ โโโ square/
โ โโโ initiate/
โ โ โโโ route.ts # Square initiation
โ โโโ submit/
โ โโโ route.ts # Square submission
Payment Flowโ
Square Payment Flow:โ
- Frontend loads Square Web Payments SDK
- User enters card information
- SDK tokenizes card (PCI-compliant)
- Frontend sends token to
/api/payments/submit - Backend processes payment with Square API
- Backend stores transaction in database
- Frontend redirects to success page
Intuit Payment Flow:โ
- Frontend loads Intuit Embedded Payments SDK
- User selects payment method
- SDK tokenizes payment information
- Frontend sends token to
/api/payments/submit - Backend processes payment with Intuit API
- Backend stores transaction in database
- Frontend redirects to success page
API Endpointsโ
Common Endpointsโ
POST /api/payments/submit- Process payment (supports both providers)
Square-Specific Endpointsโ
POST /api/payments/square/initiate- Initialize Square paymentPOST /api/payments/square/submit- Submit Square payment
Intuit-Specific Endpointsโ
POST /api/payments/paypal/initiate- Initialize PayPal payment
Database Schemaโ
Payments are stored in the Payment model:
model Payment {
id String @id @default(cuid())
transactionId String @unique
amount Float
currency String @default("USD")
paymentMethod String // e.g., "square_card", "paypal", "intuit_card"
status String // e.g., "completed", "pending", "failed"
productName String?
metadata Json?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
Security Best Practicesโ
Environment Variablesโ
- โ
NEVER commit
.env.localto version control - โ
Keep
SQUARE_ACCESS_TOKENsecret (server-side only) - โ Use different credentials for sandbox and production
- โ Rotate access tokens regularly
- โ Use environment-specific variables in deployment
PCI Complianceโ
- โ NEVER store raw card numbers
- โ Use Square's tokenization (they handle PCI compliance)
- โ Always use HTTPS in production
- โ Validate payments server-side
Production Checklistโ
Before going live:
- Change
NEXT_PUBLIC_SQUARE_ENVIRONMENTtoproduction - Use production Square credentials
- Test end-to-end payment flow
- Verify webhook configuration (if using webhooks)
- Enable HTTPS
- Set up error monitoring
- Configure proper CORS settings
- Review Square Dashboard settings
Troubleshootingโ
"Payment configuration is missing"โ
Problem: Square credentials not found
Solution:
- Verify environment variables are set correctly
- Restart your development server
- Check that variable names start with
NEXT_PUBLIC_for client-side access
"Payment tokenization failed"โ
Problem: Card validation failed
Solution:
- In sandbox: Use test card numbers
- Check card number, expiry, and CVV are valid
- Ensure card form is fully loaded before submission
"Location ID not configured"โ
Problem: Missing or invalid location ID
Solution:
- Get your location ID from Square Dashboard
- Set
NEXT_PUBLIC_SQUARE_LOCATION_IDenvironment variable - Restart your server
"Access token invalid"โ
Problem: Wrong or expired access token
Solution:
- Verify you're using the correct access token for your environment
- Regenerate access token in Square Dashboard if needed
- Ensure
SQUARE_ACCESS_TOKENis set on the server
Webhooks (Optional)โ
Square can send webhooks for payment events:
Setup Webhooksโ
- Go to Square Dashboard โ Webhooks
- Create a webhook endpoint:
https://yourdomain.com/api/webhooks/square - Subscribe to events:
payment.createdpayment.updatedrefund.created
- Copy the webhook signature key
- Add to
.env.local:
SQUARE_WEBHOOK_SIGNATURE_KEY=your_webhook_signature_key
Webhook Verificationโ
The squarePayments.ts utility includes a verifySquareWebhook() function for signature verification.
Support and Resourcesโ
Square Resourcesโ
Communityโ
Comparison: Square vs Intuitโ
| Feature | Square | Intuit |
|---|---|---|
| Credit/Debit Cards | โ | โ |
| ACH/Bank Transfer | โ | โ |
| PayPal | โ | โ |
| Venmo | โ | โ |
| Apple Pay | โ | โ |
| Google Pay | โ | โ |
| Gift Cards | โ | โ |
| Invoicing | โ | โ |
| Recurring Payments | โ | โ |
| Customer Profiles | โ | โ |
| POS Integration | โ | โ |
| QuickBooks Integration | โ | โ |
| Transaction Fees | 2.9% + 30ยข | 2.9% + 25ยข |
| Monthly Fee | $0 | $0 |
Choose Square if you:
- Want POS integration
- Need gift card support
- Prefer Google Pay
- Have physical locations
Choose Intuit if you:
- Use QuickBooks
- Need ACH payments
- Want PayPal/Venmo support
- Need comprehensive accounting integration
Migration Between Providersโ
To migrate from Intuit to Square (or vice versa):
- Set up new provider credentials
- Change
NEXT_PUBLIC_PAYMENT_PROVIDERenvironment variable - Test in sandbox/staging environment
- Deploy to production
- Monitor initial transactions
Note: Existing payment records remain unchanged. The provider only affects new transactions.
Next Stepsโ
- โ Set up Square account
- โ Get credentials
- โ Configure environment variables
- โ Test in sandbox mode
- โ Switch to production when ready
Need help? Check the Getting Started Guide or create an issue in the repository.