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.
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.
Copy
import express from "express";import path from "path";import bodyParser from "body-parser";import ModemPay from "modem-pay";const modem = new ModemPay( "sk_test_516aa853e1574295ed778eadc82b0a7128b18b628e4059f82e77041adf8692cb");const app = express();app.use(bodyParser.urlencoded({ extended: true }));app.use(express.static(path.join(__dirname, "public")));const YOUR_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"));