Establish a custom Stripe integration [Code]
The following code illustrates how to integrate Stripe into your custom booking app, using the API at {businessCode}.storeganise.com/api/docs/user/billing/stripe#user_billing_stripe.POST_session
const express = require('express'); const sgApiUrl = 'https://{your_account}.storeganise.com/api'; const app = express(); app.use(require('cookie-parser')()); app.get('/', async (req, res) => { const user = req.cookies.sg && await fetch(`${sgApiUrl}/v1/user`, { headers: { 'authorization': `Bearer ${req.cookies.sg}` }, }).then(r => r.json()); if (req.query.session_id) { await fetch(`${sgApiUrl}/v1/billing/stripe/sources`, { method: 'POST', headers: { 'authorization': `Bearer ${req.cookies.sg}`, 'content-type': 'application/json' }, body: JSON.stringify({ token: req.query.session_id }), }); return res.redirect('/'); } if (!user) { return res.send(`<form method="post" action="/login"><input name="email" type="email"><input name="password" type="password"><button>Submit</button></form>`) } const cards = await fetch(`${sgApiUrl}/v1/billing/stripe/sources`, { headers: { 'authorization': `Bearer ${req.cookies.sg}` }, }).then(r => r.json()); res.send(`<details> <summary>User <em>${user.firstName} ${user.lastName}</em></summary> <pre>${JSON.stringify(user, null, 2)}</pre> </details> <details> <summary>Payment methods ${cards.length}</summary> <pre>${JSON.stringify(cards, null, 2)}</pre> </details> <form method="POST" action="/add"> <button>Add a card</button> </form> `); }); app.post('/add', async (req, res) => { const r = await fetch(`${sgApiUrl}/v1/billing/stripe/session?returnUrl=https://${req.headers.host}`, { method: 'POST', headers: { 'authorization': `Bearer ${req.cookies.sg}` }, }).then(r => r.json()); res.redirect(r.url); }); app.post('/login', express.urlencoded({ extended: false }), async (req, res) => { const r = await fetch(`${sgApiUrl}/v1/auth/token`, { method: 'POST', headers: { 'authorization': `Basic ${Buffer.from(req.body.email + ':' + req.body.password).toString('base64')}` }, }); if (!r.ok) return res.status(400).send(await r.text()); const data = await r.json(); res.cookie('sg', data.accessToken, { httpOnly: true, path: '/', secure: true, maxAge: 1440 * 60, sameSite: 'none', }); res.redirect('/'); }); app.listen(process.env.PORT || 3000);
Live example: https://replit.com/@caub/Storeganise-Stripe#index.js