In this section, we'll learn how to manage subscriptions directly from the Stripe dashboard and create a serverless function.
If a user has subscribed to a plan, they can manage their subscription status (cancel/refund, etc) directly from the Stripe dashboard.
To check for the active subscription in the Stripe dashboard, we need to navigate to the below page as shown below.
From the Stripe Subscription page, click any active subscription from the respective user.
After that, you can manage the subscription for the respective user by clicking the action
button shown below.
A modal appears where we can select a bunch of options. For demonstration purposes, we can cancel the subscription by selecting End of the current period
option. See the below image for your reference.
In the image below, we can view the subscription status after canceling the subscription at the end of the period:
We have performed an activity for the cancelation of a subscription from the Stripe Dashboard. The Stripe store our activity in the form of logs. In the Stripe logs, we have the information of API endpoint request and post request body as shown below.
We can use the information (e.g. Status, Method, API endpoint, etc) from the Stripe logs to perform the respective activity by sending an HTTP request programmatically.
Rather than searching the Stripe API documentation, we can look for the Stripe log and check what HTTP request we need to send to perform the respective activity in Stripe.
For example,
For the Stripe API reference, you can visit the below page.
To update the user's subscription, we can use the stripe.subscriptions.update
function of the Stripe API as below.
As shown in the above image, we have highlighted the the API endpoint and Post request body that we can use in the Nodejs function to send the HTTP request.
We need to create a serverless function to update the user subscription programmatically.
You can access the full source code for this section at the below Github repository.
To create a serverless function, we can create a file at the below location:
src/api/cancelSubscription.js
In the cancelSubscription.js
, we can write the code as below:
import Axios from "axios"
import validator from "validator"
import jwt from "jsonwebtoken"
import stripeAPI from "stripe"
import { sanityRequest, sanityUpdate } from "../lib/sanity/sanityActions"
import { formatDate } from "../lib/formatDate"
import { unix_timestamp_data } from "../lib/unix_timestamp_data"
export default async function handler(req, res) {
const stripe = new stripeAPI(String(process.env.GATSBY_STRIPE_secret_ID), {
apiVersion: "2020-08-27",
})
try {
const token =
req?.body?.token || req?.query?.token || req?.headers["x-access-token"]
if (!token) {
return res.status(403).send("A token is required for authentication")
}
const subID = req.body.subID || req.query.subID
const actionReq = req.body.actionReq || req.query.actionReq
const decoded = jwt.verify(token, String(process.env.jwt))
if (!validator.isEmail(decoded.email)) {
throw {
status: 400,
message: "Bad Token",
}
}
let action = {}
if (actionReq == "dont_cancel") {
action = {
cancel_at: "",
}
} else {
action = {
cancel_at_period_end: true,
}
}
let subscription = await stripe.subscriptions.update(subID, action)
await sanityUpdate(subID, {
status: subscription.status,
cancel_at_period_end: subscription.cancel_at_period_end,
canceled_at: formatDate(unix_timestamp_data(subscription.canceled_at)),
cancel_at: formatDate(unix_timestamp_data(subscription.cancel_at)),
livemode: subscription.livemode,
})
res.status(200).json({
status: subscription.status,
cancel_at: subscription.cancel_at,
livemode: subscription.livemode,
message: "success",
})
} catch (error) {
const status = error.response?.status || error.statusCode || 500
const message = error.response?.data?.message || error.message
res.status(status).json({
message: error.expose ? message : `Faulty ${req.baseUrl}: ${message}`,
})
}
}
In the cancelSubscription
function, we perform two main actions.
To test out the function, we can send the request below.
In the above request, we generate the token
of the user by sending the request to the endpoint - http://localhost:8000/api/login
.