#ninjajinja_code

1 messages ยท Page 1 of 1 (latest)

civic raftBOT
#

๐Ÿ‘‹ 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/1296702823286964277

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

stark sable
manic cypress
#

sure Please give me a sec Thanks

#

req_8ROYJGC8I3daV4 HI @stark sable Please find request id

stark sable
manic cypress
#

if its ok can I share scrren shot of my logs?

stark sable
#

sure

manic cypress
#

please find the screenshot above

stark sable
#

can you paste the full stacktrace of the error, it's cut off midway

manic cypress
#

sure

stark sable
#

what's at index.ts line 80?

manic cypress
#
      `createSubscriptionHandler() - unexpected error: ${JSON.stringify(error)}`
    );
    if (error instanceof ServiceError) {
      switch (error.data.code) {
        case "STRIPE_CUSTOMER_NOT_FOUND":
        case "STRIPE_SUBSCRIPTION_ERROR":
        case "NFT_NOT_REGISTERED":
        case "SUBSCRIPTION_ALREADY_EXISTS":
        case "STRIPE_SUBSCRIPTION_CREATION_FAILED":
          throw new HttpResponseError(error.data.code, error.message);
        default:
          throw new HttpResponseError(
            "internal-server-error",
            "Unexpected error while handling the createSubscription request"
          );
      }
    }
stark sable
#

the subscription creation is clearly fine, you'll should step through your code after that to see where the issue is actually occurring

manic cypress
#

ok I think I found something when iam trying to pull latest_invocie.paymentintentid ists null beacuse the invocie seems to be null

#

any idea why is that?

stark sable
#

btw subscription creation is a promise, how come you're not waiting for it to complete before returning?

manic cypress
#

i wrapped it in fucntion

stark sable
manic cypress
#
  stripeCustomerId: string,
  priceId: string,
  nfts: number[],
  dbSubscriptionId: string,
  dbUserId: string
) {
  const now = new Date();
  const currentQuarterEnd = endOfQuarter(now);
  console.log(Math.floor(currentQuarterEnd.getTime() / 1000));

  return stripe.subscriptions.create({
    customer: stripeCustomerId,
    items: [{ price: priceId, quantity: nfts.length }],
    payment_settings: {
      payment_method_types: ["card", "paypal"],
      save_default_payment_method: "on_subscription",
    },
    payment_behavior: "default_incomplete",
    expand: ["latest_invoice.payment_intent", "schedule"],
    metadata: {
      userId: dbUserId,
      subscriptionId: dbSubscriptionId,
      nfts: JSON.stringify(nfts),
    },
    proration_behavior: "none",
    collection_method: "charge_automatically",
    billing_cycle_anchor: Math.floor(currentQuarterEnd.getTime() / 1000),
  });
}```
#
      customer.stripeCustomerId,
      product.priceId,
      nfts,
      dbSubscription.id,
      dbSubscription.userId
    );```
usage
stark sable
#

what's the actual line of code where you try to access latest_invocie.paymentintentid -> there's clearly typos here

manic cypress
stark sable
#

because you set

"proration_behavior": "none",
 "billing_cycle_anchor": "1735669799",

There's nothing to be charged on the first invoice. There's a pending_setup_intent which you would use to collect the payment method details instead

manic cypress
#

buisness has decided is the first payment would consider for complete quarter

#

and then rest can follow the quarter in sync with yearly quarters how shoud i approach it

stark sable
#

i don't understand what you mean by the first payment would consider for complete quarter, can you illustrate with an example as to what's the ideal situation?

manic cypress
#

sure
a user comes in and buys 1 sub in 4th quarter of 2024 on OCT 21 our subscription period is 3 months now we want that current period should end with end of quarter we have some rewards to pay though they would register after the quarter has started we would pay them rewards for whole quarter and then the upcoming payments should follow the yearly quarter cycle i.e jan-mar .... so on

stark sable
#

what do you expect them to pay for the subscription when they start it on oct 21? should it be a prorated amount?

manic cypress
#

complete amount

#

we dont want any prorations as we would be paying them based on complete payment

stark sable
#

gimme a while to test something out

manic cypress
#

sure Thank you

stark sable
#

okay, i think i got something that works, typing it out right now...

#

At a high level,

  1. create a subscription as per normal
  2. After the subscription is successful, create a subscription schedule for the existing subscription and set the billing_cycle_anchor to start at the end of the quarter with no prorations

You need to update the below example with your own Price id

const testClock = await stripe.testHelpers.testClocks.create({
      frozen_time: Math.floor(+new Date() / 1000),
    });
  
    const customer = await stripe.customers.create({
      description: 'test clock customer',
      email : 'test@example.com',
      test_clock: testClock.id,
      payment_method: 'pm_card_amex_threeDSecureNotSupported',
      invoice_settings : {
        default_payment_method : 'pm_card_amex_threeDSecureNotSupported'
      }
    });
  
    const subscription = await stripe.subscriptions.create({
      customer: customer.id,
      items: [
        {price:'price_xxx'} // USD 3 months
      ],
    });
  
    console.log(subscription);
  
    const schedule = await stripe.subscriptionSchedules.create({
      from_subscription: subscription.id,
    });
    
  const subscriptionSchedule = await stripe.subscriptionSchedules.update(
      schedule.id, {
        phases:[
          {
            start_date : schedule.phases[0].start_date,
            end_date : 1735689600,
            items: [{
              price: 'price_xxx'
            }],
            proration_behavior: "none",
          },
          {
            billing_cycle_anchor : "phase_start",
            items: [{
              price: 'price_xxx'
            }],
            proration_behavior: "none",
          }
        ]
      }
      );
  
      console.log(subscriptionSchedule);
#

test it and see if it fits what you're looking for

manic cypress
#

thank you so much will try