Official Square payment integration for Vendure e-commerce platform
Process real payments through Square with full PCI compliance, automatic tokenization, and support for authorization, settlement, and refunds.
- π³ Full Payment Lifecycle - Authorization, settlement, and refunds
- π PCI Compliant - Card data never touches your server
- π Multi-Currency Support - All Square-supported currencies
- π§ͺ Sandbox Testing - Complete test environment with Square test cards
- π Transaction Metadata - Full Square transaction details stored
- π Idempotent Operations - Prevents duplicate charges
- β‘ TypeScript - Full type safety with TypeScript definitions
- π‘οΈ Error Handling - Comprehensive error states and messages
npm install vendure-plugin-square squarePeer Dependencies:
@vendure/core: ^3.0.0square: ^43.0.0
- Create a Square Developer account at https://developer.squareup.com
- Create a new application
- Get your credentials:
- Application ID (for Web Payments SDK)
- Access Token (for backend API)
- Location ID (from your Square locations)
Add to your vendure-config.ts:
import { SquarePlugin, squarePaymentHandler } from 'vendure-plugin-square';
export const config: VendureConfig = {
// ... other config
paymentOptions: {
paymentMethodHandlers: [squarePaymentHandler],
},
plugins: [
SquarePlugin.init({
accessToken: process.env.SQUARE_ACCESS_TOKEN!,
environment: process.env.SQUARE_ENVIRONMENT as 'sandbox' | 'production',
locationId: process.env.SQUARE_LOCATION_ID!,
}),
// ... other plugins
],
};Add to your .env:
# Square Configuration
SQUARE_ACCESS_TOKEN=your_square_access_token
SQUARE_ENVIRONMENT=sandbox # or 'production'
SQUARE_LOCATION_ID=your_location_id- Login to Vendure Admin UI
- Go to Settings β Payment methods
- Click "Create new payment method"
- Configure:
- Code:
square-payment - Handler: Select "Square Payment"
- Enabled: ON
- Code:
- Save
Add the Square SDK to your storefront:
import { useEffect, useState } from 'react';
// Load Square SDK
useEffect(() => {
const script = document.createElement('script');
script.src = 'https://sandbox.web.squarecdn.com/v1/square.js'; // or production URL
script.async = true;
document.body.appendChild(script);
}, []);const payments = Square.payments(
process.env.NEXT_PUBLIC_SQUARE_APPLICATION_ID,
process.env.NEXT_PUBLIC_SQUARE_LOCATION_ID
);
const card = await payments.card();
await card.attach('#card-container');
// Tokenize card when submitting
const result = await card.tokenize();
const token = result.token;import { addPaymentToOrder } from '@vendure/core';
const paymentResult = await addPaymentToOrder({
method: 'square-payment',
metadata: {
sourceId: token, // Square payment token
},
});By default, payments are authorized but not captured:
- Customer submits payment
- Square authorizes payment (reserves funds)
- Order state: PaymentAuthorized
- Admin settles payment in Vendure Admin
- Square captures funds
- Order state: PaymentSettled
Benefits:
- Verify inventory before capturing
- Cancel without refunding
- Better fraud protection
To automatically capture payments, modify the handler:
// In square-payment-handler.ts, line ~92
autocomplete: true, // Change from false to trueUse Square's test card numbers:
| Card Number | Scenario |
|---|---|
4111 1111 1111 1111 |
Successful charge |
4000 0000 0000 0002 |
Card declined |
4000 0000 0000 0341 |
Insufficient funds |
Test Card Details:
- CVV: Any 3 digits (e.g.,
111) - Expiration: Any future date (e.g.,
12/25) - ZIP Code: Any 5 digits (e.g.,
90210)
More test values: https://developer.squareup.com/docs/devtools/sandbox/payments
Initialize the plugin with Square credentials.
Parameters:
| Option | Type | Description |
|---|---|---|
accessToken |
string |
Square API access token (required) |
environment |
'sandbox' | 'production' |
Square environment (required) |
locationId |
string |
Square location ID (required) |
Example:
SquarePlugin.init({
accessToken: process.env.SQUARE_ACCESS_TOKEN!,
environment: 'sandbox',
locationId: process.env.SQUARE_LOCATION_ID!,
})Payment method handler with code 'square-payment'.
Methods:
- createPayment: Authorizes payment with Square
- settlePayment: Captures authorized payment
- createRefund: Processes full or partial refunds
- β Card data handled entirely by Square
- β Single-use payment tokens
- β No sensitive data stored on your server
- β HTTPS required for production
- Store access tokens in environment variables
- Never commit credentials to version control
- Use sandbox for development/testing
- Enable HTTPS on production domains
- Regularly rotate access tokens
Solution: Create payment method in Vendure Admin with handler code 'square-payment'
Solution: Ensure Square Web Payments SDK script is loaded before initializing payment form
Solution: Card tokenization failed - check card details or Square SDK initialization
Solution: Verify Square access token and environment (sandbox vs production)
Contributions are welcome! Please feel free to submit a Pull Request.
git clone https://github.com/Dylanmurzello/vendure-plugin-square.git
cd vendure-plugin-square
npm install
npm run buildMIT License - see LICENSE file for details
Built with β€οΈ for the Vendure community
Special Thanks:
- Vendure team for the amazing e-commerce framework
- Square for their robust payment APIs
- The open-source community
Questions or issues? Open an issue on GitHub
Want to contribute? PRs are always welcome! π