Skip to content

Instantly share code, notes, and snippets.

@SimonHoiberg
Created June 19, 2020 14:16
Show Gist options
  • Save SimonHoiberg/05ef4ec637194a9b15ae55d7e1fd17c0 to your computer and use it in GitHub Desktop.
Save SimonHoiberg/05ef4ec637194a9b15ae55d7e1fd17c0 to your computer and use it in GitHub Desktop.
export const handler = async (
event: APIGatewayProxyEvent,
context: any,
callback: (err: Error | null, data: any) => void,
) => {
if (!event.body) {
callback(Error('Invalid body'));
return;
}
const body = JSON.parse(event.body) as IBody;
const { subscriptionID, end } = body;
// Note that 'end' can be 'false'.
// We need to strictly check if it's undefined
if (!subscriptionID || end === undefined) {
callback(null, {
statusCode: 400,
body: 'Missing subscription id',
});
return;
}
try {
// Update the subscription
const subscription = await stripe.subscriptions.update(subscriptionID, {
cancel_at_period_end: end,
});
callback(null, {
statusCode: 200,
body: JSON.stringify(subscription),
});
} catch (error) {
callback(error);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment