|
| 1 | +// Tiny server to test Paddle checkout flow in SANDBOX environment only |
| 2 | +// Usage: PADDLE_CLIENT_TOKEN=test_xxx PADDLE_PRICE_ID=pri_xxx node server.js |
| 3 | +// Then open http://localhost:3333 |
| 4 | + |
| 5 | +import { readFileSync } from 'fs'; |
| 6 | +import { createServer } from 'http'; |
| 7 | +import { fileURLToPath } from 'url'; |
| 8 | +import { dirname, join } from 'path'; |
| 9 | + |
| 10 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 11 | + |
| 12 | +const PORT = process.env.PORT || 3333; |
| 13 | +const PADDLE_CLIENT_TOKEN = process.env.PADDLE_CLIENT_TOKEN; |
| 14 | +const PADDLE_PRICE_ID = process.env.PADDLE_PRICE_ID; |
| 15 | + |
| 16 | +// Validate inputs |
| 17 | +if (!PADDLE_CLIENT_TOKEN || !PADDLE_PRICE_ID) { |
| 18 | + console.error(` |
| 19 | +╭──────────────────────────────────────────────────────────╮ |
| 20 | +│ Missing required environment variables │ |
| 21 | +├──────────────────────────────────────────────────────────┤ |
| 22 | +│ │ |
| 23 | +│ PADDLE_CLIENT_TOKEN │ |
| 24 | +│ → Paddle Sandbox > Developer Tools > Authentication │ |
| 25 | +│ → Client-side tokens tab > Create new token │ |
| 26 | +│ → Must start with "test_" for sandbox │ |
| 27 | +│ │ |
| 28 | +│ PADDLE_PRICE_ID │ |
| 29 | +│ → Paddle Sandbox > Catalog > Products > Your product │ |
| 30 | +│ → Click on a price > Copy the "pri_xxx" ID │ |
| 31 | +│ │ |
| 32 | +├──────────────────────────────────────────────────────────┤ |
| 33 | +│ Example: │ |
| 34 | +│ PADDLE_CLIENT_TOKEN=test_abc PADDLE_PRICE_ID=pri_01xxx │ |
| 35 | +│ pnpm test:checkout │ |
| 36 | +╰──────────────────────────────────────────────────────────╯ |
| 37 | +`); |
| 38 | + process.exit(1); |
| 39 | +} |
| 40 | + |
| 41 | +// Enforce sandbox-only |
| 42 | +if (!PADDLE_CLIENT_TOKEN.startsWith('test_')) { |
| 43 | + console.error(` |
| 44 | +╭──────────────────────────────────────────────────────────╮ |
| 45 | +│ ERROR: This tester only works with sandbox tokens │ |
| 46 | +├──────────────────────────────────────────────────────────┤ |
| 47 | +│ │ |
| 48 | +│ Your token "${PADDLE_CLIENT_TOKEN.slice(0, 10)}..." does not start with "test_" |
| 49 | +│ │ |
| 50 | +│ Get a sandbox token from: │ |
| 51 | +│ https://sandbox-vendors.paddle.com/authentication-v2 │ |
| 52 | +│ │ |
| 53 | +╰──────────────────────────────────────────────────────────╯ |
| 54 | +`); |
| 55 | + process.exit(1); |
| 56 | +} |
| 57 | + |
| 58 | +const html = readFileSync(join(__dirname, 'checkout.html'), 'utf-8'); |
| 59 | + |
| 60 | +// Always use sandbox environment |
| 61 | +const configScript = `<script> |
| 62 | + window.PADDLE_CONFIG = { |
| 63 | + environment: 'sandbox', |
| 64 | + clientToken: '${PADDLE_CLIENT_TOKEN}', |
| 65 | + priceId: '${PADDLE_PRICE_ID}' |
| 66 | + }; |
| 67 | +</script>`; |
| 68 | + |
| 69 | +const injectedHtml = html.replace('</head>', `${configScript}\n</head>`); |
| 70 | + |
| 71 | +const server = createServer((req, res) => { |
| 72 | + res.writeHead(200, { 'Content-Type': 'text/html' }); |
| 73 | + res.end(injectedHtml); |
| 74 | +}); |
| 75 | + |
| 76 | +server.listen(PORT, () => { |
| 77 | + console.log(` |
| 78 | +╭───────────────────────────────────────────────────────────────────╮ |
| 79 | +│ Paddle Checkout Test Server (SANDBOX ONLY) │ |
| 80 | +├───────────────────────────────────────────────────────────────────┤ |
| 81 | +│ Environment: sandbox │ |
| 82 | +│ Token: ${PADDLE_CLIENT_TOKEN.slice(0, 20).padEnd(46)} │ |
| 83 | +│ Price ID: ${PADDLE_PRICE_ID.padEnd(29)} │ |
| 84 | +│ │ |
| 85 | +│ ➜ Open: http://localhost:${String(PORT).padEnd(26)} │ |
| 86 | +│ │ |
| 87 | +│ Test card: 4000 0566 5566 5556 │ |
| 88 | +│ Expiry: Any future date (e.g., 12/30) │ |
| 89 | +│ CVC: 100 │ |
| 90 | +├───────────────────────────────────────────────────────────────────┤ |
| 91 | +│ ⚠️ If you see "Something went wrong", you need to: │ |
| 92 | +│ │ |
| 93 | +│ 1. Go to: https://sandbox-vendors.paddle.com/checkout-settings │ |
| 94 | +│ 2. Set a "Default payment link" (can be localhost) │ |
| 95 | +│ 3. Save and try again │ |
| 96 | +╰───────────────────────────────────────────────────────────────────╯ |
| 97 | +`); |
| 98 | +}); |
0 commit comments