1. Initialize Session
In this section, we will look into how a session can be initialized
1.1 Backend: Call Session API
To create a session , we will have to call the /create-session
endpoint by passing the amount
, currency
and description
of the transaction.
This process needs to happen on the backend since it needs to be authenticated using your access_token
. This token can be misused if visible on the frontend, and hence needs to be restricted to the backend.
Ignoring the security aspect, you will not be allowed the make this call from the backend due to cross-origin-resource-sharing
restrictions on the browser.
Below is an example of how the session can be created. The example is a nodejs express api and axios.
const app = express();
const BASE_URL = process.env.BASE_URL;
const TOKEN = process.env.SS_PAY_TOKEN;
//...
app.get("/checkout-session", async (request, response) => {
//Your internal process of obtaining the order amount, currency etc
const order = create_order_from_cart();
const { data } = await axios.post(
BASE_URL + "payments/create-session",
{
amount: order.amount,
currency: order.currency,
description: order.description,
},
{
headers: {
"X-SS-EXTERNAL-TOKEN": TOKEN,
},
}
);
//...
return response.json({ data });
});
//...
1.2 Frontend: Create Session
Next, we will be using the api created on the backend, on our frontend, preferably the checkout page. Below is the sample code that achieves this.
const checkout = ({reference_number, checkout_payload}) => {
//... will be done in the next step
};
Axios.get(`/api/payments/checkout-session/`).then((res) => {
checkout(res.data);
});
In the next page, we will look into initiating the payment via hosted checkout