#nikivi
1 messages · Page 1 of 1 (latest)
I found https://stripe.com/docs/billing/subscriptions/cancel but its unclear how to test it
hi! which specific part is unclear?
case "checkout.session.completed":
// @ts-ignore
const checkoutSessionCompleted = event.data.object
if (checkoutSessionCompleted.status === "complete") {
const email = checkoutSessionCompleted.metadata.userEmail.trim()
const subscription = await stripe.subscriptions.retrieve(
checkoutSessionCompleted.subscription,
)
const endDateInUnix = subscription.current_period_end
const query = `
mutation InternalUpdateMemberUntilOfUser($email: String!, $memberUntilDateInUnixTime: Int!) {
internalUpdateMemberUntilOfUser(email: $email, memberUntilDateInUnixTime: $memberUntilDateInUnixTime)
}
`
const variables = {
email: email,
memberUntilDateInUnixTime: endDateInUnix,
}
// TODO: check for errors, show in ui if error happens
await fetch(c.env.GRAFBASE_API_URL!, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${c.env.INTERNAL_SECRET!}`,
},
body: JSON.stringify({
query,
variables,
}),
})
return c.json({ success: `memberUntil value is updated` })
}
break
so i have this code in my web hook right now
this gets called when user completes payment inside stripe checkout
i currently update a field in my database
i assume when user completes this
they become subscribed
and will be billed by stripe for 6$ in 30 days
looking at this
if i get it right
i can create a button in ui that says cancel subscription
and it will make a call to above and it will stop subscription?
'sub_49ty4767H20z6a',
but i don't get this
can i not send email?
it will set the subscription to cancel at the end of it's current period.
that is the ID of the Subscription object. In the code you shared earlier it's subscription.id and you can store that in your database.
oh so im forced to store it
no, the API takes the ID of the object that is being updated.
all right will store it then
you can do https://stripe.com/docs/api/customers/list#list_customers-email + https://stripe.com/docs/api/subscriptions/list#list_subscriptions-customer alternatively if you wish, but storing the Stripe object IDs associated with the user in your database usually is easier
ok
i just made code for it
after running it i get this
how can i check that subscription was indeed cancelled
in stripe dashboard or through some other way
i dont see it here
cancel event
you can look at the value of subscription.cancel_at in the response(should be the timestamp the subscription will cancel at).
If you look at the Subscription object in the dashboard it will say somewhere that it's going to cancel.
note you have not cancelled the subscription, for now it's still active. You've just updated it to say that you want it to cancel at the end of the current billing period.
thats object i get after
cancelling
const subscription = await stripe.subscriptions.update(
// @ts-ignore
stripeSubscriptionObjectId?.stripeSubscriptionObjectId,
{
cancel_at_period_end: false
}
)
should that false be true
i guess
export default async function cancelStripeResolver(
root: any,
args: {},
context: Context
) {
const hankoId = await hankoIdFromToken(context)
if (hankoId) {
try {
const stripeSubscriptionObjectId = await cancelStripe(hankoId)
const subscription = await stripe.subscriptions.update(
// @ts-ignore
stripeSubscriptionObjectId?.stripeSubscriptionObjectId,
{
cancel_at_period_end: false
}
)
console.log(subscription, "subscription")
return "ok"
} catch (error) {
throw new GraphQLError(JSON.stringify(error))
}
}
}
essentiall this code first does const stripeSubscriptionObjectId = await cancelStripe(hankoId)
export async function cancelStripe(hankoId: string) {
const stripeSubscriptionObjectId = await e
.select(e.User, (u) => ({
filter: e.op(u.hankoId, "=", hankoId),
stripeSubscriptionObjectId: true
}))
.run(client)
await e
.update(e.User, (u) => ({
filter: e.op(u.hankoId, "=", hankoId),
set: {
stripeSubscriptionObjectId: null
}
}))
.run(client)
return stripeSubscriptionObjectId[0]
}
this will get the stripeSubscriptionObjectId id i store in db
i think i should not do an update to stripeSubscriptionObjectId there
but thats besides the point
const subscription = await stripe.subscriptions.update(
// @ts-ignore
stripeSubscriptionObjectId?.stripeSubscriptionObjectId,
{
cancel_at_period_end: false
}
)
i then given that subscription.id do above
i thought above will issue command to stop user subscription at end of date
thats part i want to check
it seems it did nothing
why are you updating cancel_at_period_end to false ?
if you want to cancel the subscription you would set it to true.
Your screenshot here is from the section of the doc for "Stop a pending cancellation" , to be clear. So I think you're a bit confused and need to slow down a bit and be clear on what you're trying to do.
ah i see
ok trying with true now
{
"id": "sub_1O4Ldx4soP2HpBfd33IBxjup",
"object": "subscription",
"application": null,
"application_fee_percent": null,
"automatic_tax": { "enabled": false },
"billing_cycle_anchor": 1698057956,
"billing_thresholds": null,
"cancel_at": 1700736356,
ok i get back this now
i guess thats unix time stamp
when it will cancel at
say user stops subsciption and time ends
then in 1 month time
they go through stripe checkout with same email and card etc.
it will be diff subscription.id i imagine?