#evan_api

1 messages ยท Page 1 of 1 (latest)

tawdry vergeBOT
#

๐Ÿ‘‹ Welcome to your new thread!

โฒ๏ธ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.

โฑ๏ธ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.

๐Ÿ”— This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1298197968268296194

๐Ÿ“ Have more to share? Add more details, code, screenshots, videos, etc. below.

dire solstice
#

This is also how am performing the upgrade:

     updatedSubscription = await stripeGatewayClient.subscriptions.update(
      providerSubscription.id,
      {
        items: [
          {
            id: providerSubscription.items.data[0].id,
            price: newProviderPlan.id,
          },
        ],
        metadata: {
          subscriptionPlanId: paymentObj.newSubscriptionPlan.id,
        },
        proration_behavior: 'create_prorations',
        payment_behavior: 'default_incomplete',
        billing_cycle_anchor: 'now',
        payment_settings: {
          save_default_payment_method: 'on_subscription',
        },
        expand: ['latest_invoice.payment_intent'],
      }
    )
#

The amount_due or amount_subtotal I get from the retrieve upcoming invoice is 3999 AED, but when I check the invoice on the dashbaord, the invoice is 4001 AED

#

in_1QCd8HD9sYh65PYtv0eZst8S

barren wadi
#

Hi, let me help you with this.

#

What's the payload you receive from the upcoming Invoice endpoint?

dire solstice
#

Am using the total to show to the customer, which is 3999, but if I subscribe the invoice amount will be 4001, which is few cents different

barren wadi
#

Are you using a specific proration_date?

dire solstice
#

these are my settings when doing the upgrade:

     updatedSubscription = await stripeGatewayClient.subscriptions.update(
      providerSubscription.id,
      {
        items: [
          {
            id: providerSubscription.items.data[0].id,
            price: newProviderPlan.id,
          },
        ],
        metadata: {
          subscriptionPlanId: paymentObj.newSubscriptionPlan.id,
        },
        proration_behavior: 'create_prorations',
        payment_behavior: 'default_incomplete',
        billing_cycle_anchor: 'now',
        payment_settings: {
          save_default_payment_method: 'on_subscription',
        },
        expand: ['latest_invoice.payment_intent'],
      }
    )

#

am not specifying prorate_date here

#

but I am doing that when I preview the proration amount

#
  return stripeGatewayClient.invoices.retrieveUpcoming({
    customer: stripeCustomerObj.stripeCustomerId,
    subscription: providerSubscription.id,
    subscription_details: {
      items: newItem,
      proration_date: proration_date,
      proration_behavior: 'always_invoice',
    },
  })

barren wadi
#

What's the proration_date in the second call?

#

It mainly defines how the proration is calculated, and unless it's exactly the same as on the actual update, the total might differ slightly

dire solstice
#

const proration_date = Math.floor(Date.now() / 1000)

#

this should be the time of the current timestamp shown in seconds

barren wadi
#

Yes, but Date.now() is not the same when you make the 2 calls. That's why the proration amount is different.

dire solstice
#

Okay so you think that the proration amount from the first call (for preview), should be passed to the endpoint when users actually confirms and upgrades?

#

And is there away I can preview the proration amount but not calculated per second, but to be calculated per hour, or day?

#

So that slight mismatch does not happen..

barren wadi
tawdry vergeBOT
dire solstice
#

But if I set it in the future would not that mean that the prorated amount will be different than now? Meaning users will have to pay even more?

short elm
#

๐Ÿ‘‹ taking over for my colleague. Let me catch up.

#

if you use the same timestamp in the preview API and in the subscription update you'll get the same proration amount

dire solstice
#

Yeah I got that

#

Thanks for that

#

I will try this out

#

thanks for the clarification

short elm
#

let me know if you need any more help

dire solstice
#

sure I am testing this flow now

#

I followed these steps

#

then I used "subscription_proration_date": 1729590818,

#

And passed it when updating the subscription..

tawdry vergeBOT
dire solstice
#

as follows:

     updatedSubscription = await stripeGatewayClient.subscriptions.update(
      providerSubscription.id,
      {
        items: [
          {
            id: providerSubscription.items.data[0].id,
            price: newProviderPlan.id,
          },
        ],
        metadata: {
          subscriptionPlanId: paymentObj.newSubscriptionPlan.id,
        },
        proration_behavior: 'create_prorations',
        payment_behavior: 'default_incomplete',
        billing_cycle_anchor: 'now',
        proration_date: paymentObj?.proration_date || undefined,
        payment_settings: {
          save_default_payment_method: 'on_subscription',
        },
        expand: ['latest_invoice.payment_intent'],
      }
    )

#

paymentObj?.proration_date is basically 1729590818

barren creek
#

Hey! Taking over for my colleague. Let me catch up.

dire solstice
#

this is the invoice that was created

#

I am expecting the invoice to have a total of 3999, but it is showing 4001

tawdry vergeBOT
barren creek
dire solstice
#

Am not using a custom formula

  const newItem = [
    {
      id: providerSubscription.items.data[0].id,
      price: stripePlanId,
    },
  ]
  const proration_date = Math.floor(Date.now() / 1000)
  console.log("๐Ÿš€ ~ getProrateAmount ~ proration_date:", proration_date)
  return stripeGatewayClient.invoices.retrieveUpcoming({
    customer: stripeCustomerObj.stripeCustomerId,
    subscription: providerSubscription.id,
    subscription_details: {
      items: newItem,
      proration_date: proration_date,
      proration_behavior: 'always_invoice',
    },
  })
barren creek
#

That invoice has 4001:

  • Zendy: 3400-1(unused time) = -3499
  • Zendy 7500 : 7500
    => 7500- -3499 = 4001
#

What is not expected in the calculation above ?

dire solstice
#

Am using retrieveUpcoming invoice to get the preview of the price if the customer wants to upgrade

#

yeah above is correct

#

but when gettiing the upconing invoice to show the user the price they will pay

#

I can see that in the total it is written 3499

#

and that's the value that it is previewed to the user

#
   ...... 
    "subscription_proration_date": 1729590818,
    "subtotal": 3999,
    "subtotal_excluding_tax": 3999,
    "tax": null,
    "test_clock": null,
    "total": 3999,
    "total_discount_amounts": [],
    "total_excluding_tax": 3999,
    "total_tax_amounts": [],
    "transfer_data": null,
    "webhooks_delivered_at": null
#

Above was the result from retrieveUpcoming endpoint

#

so a mismatch is happenning between what the user is seeing, and how much they are paying

barren creek
#

It depends what params you send with the upcoming invoice

dire solstice
#

this is how am performing the endpoint

  const newItem = [
    {
      id: providerSubscription.items.data[0].id,
      price: stripePlanId,
    },
  ]
  const proration_date = Math.floor(Date.now() / 1000)
  console.log("๐Ÿš€ ~ getProrateAmount ~ proration_date:", proration_date)
  return stripeGatewayClient.invoices.retrieveUpcoming({
    customer: stripeCustomerObj.stripeCustomerId,
    subscription: providerSubscription.id,
    subscription_details: {
      items: newItem,
      proration_date: proration_date,
      proration_behavior: 'always_invoice',
    },
  })

barren creek
#

You need to provide the exact timestamp when the customer would like to make the update

#

so that you have the same amount at the end

dire solstice
#

yeah I did that above

barren creek
#

And what is the difference ?

#

There must be a difference in the timestamp

#

or could you please make another test and share with me the response payload you got from Stripe.

dire solstice
#

if I now subtract the next plan price which is 7500 - (amount of first line item) which is 3499 I get 4001 which is correct

#

For the demo purpose, to show the final deducted price for the user before they upgrade, am using the total field that comes from same response which says 3999, but am interested in showing what the user will pay at the moment which is 4001, the calculation I did previously

#

My question would be, what field I should be using to show the user the amount they are paying when they upgrade( from retrieveUpcoming invoice), should I do the calculation manually to show the price, or use the total ? which in my case mismatches for now. but when doing the calculation as per your formula above, getting numbers from the retireve upcoming invoice, it is correct 4001

tawdry vergeBOT
rustic abyss
#

I think it's because you're passing billing_cycle_anchor: 'now', in your subscription update call

#

and not in the preview call, so the proration amonts will differ slightly based on the time difference between them