Skip to main content

Server Setup

Create a payment using S2S Call

To create a payment intent, send a request to either our sandbox or production endpoint. For detailed information, refer to the API Reference documentation. Upon successful creation, you will receive a client_secret, which must be provided to the SDK to render it properly.
// Example Usage :- Can be Modified
async function createPaymentIntent() {
    /* Add respective env enpoints
     - Sandbox - https://api.test.switch.vaultera.co
     - Prod - https://api.switch.vaultera.co
    */
    const request = {
        "amount": 1000,
        "currency": "ISK"
    }

    const url = process.env.SWITCH_URL || "https://api.test.switch.vaultera.co";
    const apiResponse = await fetch(`${url}/payments`, {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
            Accept: "application/json",
            "api-key": process.env.API_KEY,
        },
        body: JSON.stringify(request),
    });
    const paymentIntent = await apiResponse.json();

    if (paymentIntent.error) {
        console.error("Error - ", paymentIntent.error);
        throw new Error(paymentIntent?.error?.message ?? "Something went wrong.");
    }
    return paymentIntent;
}
To integrate Web SDK, follow Node And React, Node and HTML and Vanilla JS and REST API Integration.
In case your integrating the ExpressCheckout (mentioned later below), instead of creating multiple paymentIntents for the same customer session, you can also use paymentsUpdate API for better analytics.
Note: This is full code for reference
const express = require('express');
const cors = require('cors'); // to allow cross-origin requests
const app = express();
const port = process.env.PORT || 3000;

app.use(cors());
app.use(express.json());

async function createPaymentIntent() {
    /* Add respective env enpoints
     - Sandbox - https://api.test.switch.vaultera.co
     - Prod - https://api.switch.vaultera.co
    */
    const request = {
        "amount": 1000,
        "currency": "ISK"
    }

    const url = process.env.SWITCH_URL || "https://api.test.switch.vaultera.co";
    const apiResponse = await fetch(`${url}/payments`, {
        method: "POST",
        headers: {
            "Content-Type": "application/json",
            Accept: "application/json",
            "api-key": process.env.API_KEY,
        },
        body: JSON.stringify(request),
    });
    const paymentIntent = await apiResponse.json();

    if (paymentIntent.error) {
        console.error("Error - ", paymentIntent.error);
        throw new Error(paymentIntent?.error?.message ?? "Something went wrong.");
    }
    return paymentIntent;
}

app.post('/create-payment', async (req, res) => {
    try {
        const paymentIntent = await createPaymentIntent();
        res.json(paymentIntent);
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
});

app.post('/health', async (req, res) => {
    try {
        res.json({ message: 'Server is running' });
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
});

app.use((err, req, res, next) => {
    console.error(err.stack);
    res.status(500).json({ error: 'Internal server error' });
});

app.listen(port, () => {
    console.log(`Server running on port ${port}`);
});