#nikivi

1 messages · Page 1 of 1 (latest)

toxic depotBOT
spark rover
ornate yarrow
#

hi! which specific part is unclear?

spark rover
#
    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?

ornate yarrow
#

it will set the subscription to cancel at the end of it's current period.

ornate yarrow
spark rover
#

oh so im forced to store it

ornate yarrow
spark rover
#

all right will store it then

ornate yarrow
spark rover
#

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

ornate yarrow
#

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.

spark rover
#

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 thought above will issue command to stop user subscription at end of date

#

thats part i want to check

#

it seems it did nothing

ornate yarrow
#

why are you updating cancel_at_period_end to false ?

#

if you want to cancel the subscription you would set it to true.

ornate yarrow
# spark rover looking at this

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.

spark rover
#

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

spark rover
#

say user stops subsciption and time ends

#

then in 1 month time

#

they go through stripe checkout with same email and card etc.