Payment sessions simplify the payment process by redirecting the customer to a secure payment page hosted by Modem. You create a payment session on your backend, specifying details like the amount and currency. The customer completes the payment, and you verify the transaction via webhook or API. This method is ideal for a simple, secure checkout experience.
Steps:
Create a payment session on your backend.
Redirect the customer to Modem’s secure payment page.
Confirm the payment status via webhook or API.
importexpressfrom"express";importpathfrom"path";importModemfrom"../index";const modem =newModem("sk_test_b9596116930a926d33bd51ced40766e61d0c53c521f0cd28c70253e158ee4865");const app =express();app.use(express.json());app.use(express.static(path.join(__dirname,"public")));constYOUR_DOMAIN="http://localhost:7024";app.post("/create-checkout-session",async(req, res)=>{const session =await modempay.sessions.create({amount:2000,title:"Stubborn Attachments",redirect_url:`${YOUR_DOMAIN}/success.html`,cancel_url:`${YOUR_DOMAIN}/cancel.html`,}); res.redirect(303, session.payment_link);});app.post('/verify',async(req, res)=>{const payload = req.body;const signature = req.headers["x-modem-signature"];const secret = process.env.MODEM_WEBHOOK_SECRET;try{const event = modempay.webhooks.composeEventDetails(payload, signature, secret);// Process the event res.status(200).send("Webhook processed!");}catch(error){ res.status(400).send("Invalid signature!");}})app.listen(7024,()=>console.log("Running on port 7024"));
A payment intent gives you more control over the payment process. You create the payment intent on your backend, collect payment details directly from the customer, and confirm the payment. Afterward, you verify the transaction status via webhooks or API.
Steps:
Create a payment intent on your backend.
Collect customer payment details.
Confirm the payment and verify the status via webhook or API.
importexpressfrom"express";importpathfrom"path";importbodyParserfrom"body-parser";importModemfrom"../index";const modem =newModem("sk_test_b9596116930a926d33bd51ced40766e61d0c53c521f0cd28c70253e158ee4865");const app =express();app.use(bodyParser.urlencoded({extended:true}));app.use(express.static(path.join(__dirname,"public")));constYOUR_DOMAIN="http://localhost:7024";app.post("/create-checkout-session",async(req, res)=>{const intent =await modempay.paymentIntents.create({...req.body,return_url:`${YOUR_DOMAIN}/success.html`,cancel_url:`${YOUR_DOMAIN}/cancel.html`,}); res.redirect(303, intent.link);});app.post('/verify',async(req, res)=>{const payload = req.body;const signature = req.headers["x-modem-signature"];const secret = process.env.MODEM_WEBHOOK_SECRET;try{const event = modempay.webhooks.composeEventDetails(payload, signature, secret);// Process the event res.status(200).send("Webhook processed!");}catch(error){ res.status(400).send("Invalid signature!");}})app.listen(7024,()=>console.log("Running on port 7024"));