This comprehensive guide covers the complete TradeSafe payment gateway integration for Kibbledrop, configured for South African Rands (ZAR) and subscription-based recurring payments.
- OAuth 2.0 Authentication: Secure client credentials flow
- Trade Creation: Complete buyer/seller token and transaction creation
- ZAR Currency: All payments processed in South African Rands
- Subscription Payments: Initial payment with recurring billing setup
- Webhook Handling: Real-time transaction state updates
- Sandbox & Production: Easy environment switching
- Demo Interface: Interactive React component for testing
Kibbledrop uses a "Pay Now, Recurring Later" model:
- Initial Payment: Customer pays immediately for first subscription delivery via TradeSafe
- Subscription Setup: System creates recurring billing schedule in background
- Future Payments: Automatic recurring payments for subsequent deliveries
- Currency: All transactions in South African Rands (ZAR)
Customer Places Subscription → Pay First Delivery (ZAR) → Setup Recurring → Automated Future Payments
lib/
├── tradesafe-graphql-client.ts # Main GraphQL client with OAuth
├── tradesafe-config.ts # Configuration utilities
app/api/tradesafe/
├── token/route.ts # OAuth token endpoint
├── trade/route.ts # Trade creation endpoint
├── callback/route.ts # Webhook callback handler
├── test/route.ts # API testing endpoint
└── mock-trade/route.ts # Mock trade creation
components/
├── tradesafe-demo.tsx # Demo React component
└── trade-button.tsx # Trade action button
app/demo/tradesafe/
└── page.tsx # Demo page
# TradeSafe OAuth 2.0 Credentials
TRADESAFE_CLIENT_ID="your_client_id"
TRADESAFE_CLIENT_SECRET="your_client_secret"
TRADESAFE_ENVIRONMENT="sandbox" # or "production"
TRADESAFE_WEBHOOK_SECRET="your_webhook_secret"
# TradeSafe API URLs
TRADESAFE_AUTH_URL="https://auth.tradesafe.co.za"
TRADESAFE_GRAPHQL_URL="https://api-developer.tradesafe.dev/graphql" # sandbox
# Production: https://api.tradesafe.co.za/graphql-
Development (.env.local):
- Use sandbox environment
- Test credentials provided by TradeSafe
- Mock webhook endpoints for local testing
-
Production (.env.production):
- Use production environment
- Live credentials from TradeSafe dashboard
- Real webhook endpoints
-
Obtain TradeSafe credentials:
- Register at TradeSafe Developer Portal
- Create application and get OAuth credentials
- Set up webhook endpoints
-
Configure environment variables:
cp .env.example .env.local # Edit .env.local with your credentials
The integration includes a Trade model in Prisma:
model Trade {
id String @id @default(cuid())
tradeId String? @unique
title String
description String?
value Float
feeAllocation String @default("BUYER")
industryId String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// Parties
buyerUserId String?
sellerUserId String?
// State tracking
state String?
// Relations
buyerUser User? @relation("BuyerTrades", fields: [buyerUserId], references: [id])
sellerUser User? @relation("SellerTrades", fields: [sellerUserId], references: [id])
}// Automatic token refresh and management
const client = new TradeSafeGraphQLClient();
const token = await client.getAccessToken();const trade = await client.createTrade({
title: "Pet Food Purchase",
description: "Premium kibble order",
value: 299.99,
feeAllocation: "BUYER",
});// Automatic webhook verification and processing
export async function POST(request: Request) {
const webhook = await verifyWebhook(request);
// Process trade state changes
}- Use sandbox environment
- Test with demo interface:
/demo/tradesafe - Monitor API responses:
/api/tradesafe/test
- Switch to production environment
- Update webhook URLs
- Test with real transactions
- All credentials stored as environment variables
- Webhook signatures verified automatically
- OAuth tokens managed securely
- No sensitive data in client-side code
POST /api/tradesafe/token- OAuth token managementPOST /api/tradesafe/trade- Create new tradePOST /api/tradesafe/callback- Webhook handlerGET /api/tradesafe/test- API testing
<TradeSafeDemo />- Interactive demo component<TradeButton />- Trade action button
- OAuth failures: Check credentials and environment URLs
- Webhook timeouts: Verify endpoint accessibility
- Trade creation errors: Validate required fields
- Database sync issues: Run Prisma migrations
- API test endpoint:
/api/tradesafe/test - Demo interface:
/demo/tradesafe - Database inspection: Prisma Studio
For TradeSafe-specific issues: