Skip to main content

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โ€‹

  1. A Square account (free to create at squareup.com)
  2. Square Application credentials
  3. Square Location ID

Getting Your Square Credentialsโ€‹

Step 1: Create a Square Developer Accountโ€‹

  1. Go to developer.squareup.com
  2. Sign in with your Square account or create a new one
  3. Navigate to the Developer Dashboard

Step 2: Create an Applicationโ€‹

  1. Click "+ Create App"
  2. Give your app a name (e.g., "Gully Store")
  3. Click "Create Application"

Step 3: Get Your Application IDโ€‹

  1. Open your newly created application

  2. Navigate to "Credentials" in the left sidebar

  3. You'll see two environments:

    • Sandbox (for testing)
    • Production (for live payments)
  4. 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โ€‹

  1. In the Square Developer Dashboard, go to "Locations"
  2. Copy the Location ID for the location where you want to process payments
  3. 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:

  1. Check Console Logs:

    • Open DevTools (F12 or Cmd+Option+I)
    • Look for "Google Pay initialized successfully" or error messages
  2. 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
  3. See Detailed Troubleshooting:

    • Refer to GOOGLE_PAY_TROUBLESHOOTING.md for complete guide
    • Common fix: Sign into Chrome and add payment method to Google Pay

Sandbox Mode (Testing)โ€‹

Square provides test card numbers for sandbox testing:

Card BrandCard NumberCVVExpiry
Visa4111 1111 1111 1111AnyAny future
Mastercard5105 1051 0510 5100AnyAny future
Amex3400 000000 00009AnyAny future
Discover6011 0000 0000 0004AnyAny future

Note: Always use sandbox credentials during development!

Test Payment Flowโ€‹

  1. Set NEXT_PUBLIC_PAYMENT_PROVIDER=square
  2. Use sandbox credentials
  3. Go to /shop and add items to cart
  4. Proceed to checkout
  5. Use a test card number
  6. Complete the payment
  7. 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:โ€‹

  1. Frontend loads Square Web Payments SDK
  2. User enters card information
  3. SDK tokenizes card (PCI-compliant)
  4. Frontend sends token to /api/payments/submit
  5. Backend processes payment with Square API
  6. Backend stores transaction in database
  7. Frontend redirects to success page

Intuit Payment Flow:โ€‹

  1. Frontend loads Intuit Embedded Payments SDK
  2. User selects payment method
  3. SDK tokenizes payment information
  4. Frontend sends token to /api/payments/submit
  5. Backend processes payment with Intuit API
  6. Backend stores transaction in database
  7. 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 payment
  • POST /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.local to version control
  • โœ… Keep SQUARE_ACCESS_TOKEN secret (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_ENVIRONMENT to production
  • 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_ID environment 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_TOKEN is set on the server

Webhooks (Optional)โ€‹

Square can send webhooks for payment events:

Setup Webhooksโ€‹

  1. Go to Square Dashboard โ†’ Webhooks
  2. Create a webhook endpoint: https://yourdomain.com/api/webhooks/square
  3. Subscribe to events:
    • payment.created
    • payment.updated
    • refund.created
  4. Copy the webhook signature key
  5. 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โ€‹

FeatureSquareIntuit
Credit/Debit Cardsโœ…โœ…
ACH/Bank TransferโŒโœ…
PayPalโŒโœ…
VenmoโŒโœ…
Apple Payโœ…โœ…
Google Payโœ…โŒ
Gift Cardsโœ…โŒ
Invoicingโœ…โœ…
Recurring Paymentsโœ…โœ…
Customer Profilesโœ…โœ…
POS Integrationโœ…โŒ
QuickBooks IntegrationโŒโœ…
Transaction Fees2.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):

  1. Set up new provider credentials
  2. Change NEXT_PUBLIC_PAYMENT_PROVIDER environment variable
  3. Test in sandbox/staging environment
  4. Deploy to production
  5. Monitor initial transactions

Note: Existing payment records remain unchanged. The provider only affects new transactions.

Next Stepsโ€‹

  1. โœ… Set up Square account
  2. โœ… Get credentials
  3. โœ… Configure environment variables
  4. โœ… Test in sandbox mode
  5. โœ… Switch to production when ready

Need help? Check the Getting Started Guide or create an issue in the repository.