#chunkyboi_api

1 messages ยท Page 1 of 1 (latest)

dim hareBOT
#

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

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

summer canyon
#

Hi there! also wanted to say hi to an ex-coworker (I used to work at Stripe haha)

hoary slate
#

hello! if you want to DM me their name i can pass it along ๐Ÿ™‚

summer canyon
#

hey! I meant just to you haha

#

sorry about the confusion

#

but yea anyway I have this problem - happy to hop on a call as well

hoary slate
#

ooooo haha hello!

summer canyon
#

my @ was @andycai

#

but yea we have this issue on our hand, was wondering if you could help us with it

hoary slate
#

ok, so basically you just need a free subscription forever?

summer canyon
#

yes

#

and to be clear, for the $0 subscription, we do not need the customer to go through a checkout flow. The ideal solution is actually we'd create a Stripe customer object, a subscription object

#

invoices are not even required as long as we can listen to some objects of the subscription being renewed (refreshed), I just mentioned invoice because I know we can listen to invoice.paid event

hoary slate
#

you should be able to configure that but it's been a while since i've done it... you can create a $0 price, attach it to subscriptions, and then there's a way to only collect payment methods if required. what surface are you using for these? checkout, creating subscriptions directly, etc?

summer canyon
#

so for this specific use case, I've already created a product with a $0 price

#

just not sure where the PM requirement configuration lives

#

this is my account id: acct_1S8u28EsEIWx7LWK

hoary slate
#

if you have a subscription with $0 prices then the invoices should automatically pay when the subscription period renews... have you already tried testing this? if so can you share an example subscription ID? i can also spin up a test real quick

summer canyon
#

I actually have not tested it locall yet

#

but I was able to create a sub here to the free tier: sub_1SAHndEsEIWx7LWKNLiJADvy

#

but this was through dashboard, and no payment method was required

#

but there was a payment method on file so maybe that's not the best example

#

let me try creating a new subscription without a pm

hoary slate
#

ok yes, i just created a customer with no default payment method, created a subscription with a $0 price, and advanced it with test clocks, and it looks like the invoices are automatically paying as expected

summer canyon
#

oh wait that's awesome!

hoary slate
#

so this should just work

summer canyon
#

is it possible to accomplish this programmatically

#

feel free to send over the code

hoary slate
#

yep! create_test_clock_customer is just a wrapper function that creates a customer, attaches a test clock, and gives them a default payment method (or none if you pass an empty string like i did here)

    customer = create_test_clock_customer(payment_method="")
    print(customer.id)
    subscription = client.subscriptions.create(params={
        "customer": customer.id,
        "items": [{"price": "price_1SAaOnLFIO5qXQOWeWbXruJv"}],
    })
    print(subscription.id)
    advance_clock(customer.test_clock, increment=1 * 60 * 60 * 24 * 31) # 31 days
    advance_clock(customer.test_clock, increment=1 * 60 * 60 * 24 * 31) # 31 days
    advance_clock(customer.test_clock, increment=1 * 60 * 60 * 24 * 31) # 31 days
#
    payment_method="pm_card_visa",
    email="testy-test@test.com",
    start_time=int(datetime.now().timestamp()),
    cust_params={},
):
    """
    Create customer with assigned test clock

    Args:
        stripe (Stripe): Instantiated stripe client library
        payment_method (str, optional): Payment method to attach to customer. Defaults to 'pm_card_visa'.
        email (str, optional): Email associated with customer record. Defaults to "testy-test@example.com".

    Returns:
        stripe.Customer: The customer object created
    """
    clock = create_test_clock(start_time=start_time)
    kwargs = {
        "email": email,
        "description": "Test clock customer",
        "name": "Clocky McClockerton",
        "test_clock": clock.id,
    }
    if cust_params:
        kwargs.update(cust_params)
    if payment_method:
        kwargs["payment_method"] = payment_method
        kwargs["invoice_settings"] = {"default_payment_method": payment_method}

    tc_customer = stripe.Customer.create(**kwargs)
    return tc_customer```
summer canyon
#

what we want is ideally:

  1. when the customer logs into our platform, we create a stripe customer object
  2. we attach the newly created customer object to the product
  3. The subscription is created and a $0 invoice is gnerated per month (the invoice part is optional)
hoary slate
#
    """
    Advance a test clock by a specified increment

    Args:
        - stripe (Stripe): Instantiated client library
        - clock_id (str): ID of test clock to advance
        - increment (int, optional): Increment in seconds to advance the clock's frozen time. Defaults to 864000 (10 days in seconds).
        - timedelta (datetime.timedelta, optional): Timedelta object to use for incrementing the clock. Defaults to False (not using).
    """
    if timedelta:
        increment = int(timedelta.total_seconds())

    clock = stripe.test_helpers.TestClock.retrieve(clock_id)
    new_frozen_time = clock.frozen_time + increment
    clock = stripe.test_helpers.TestClock.advance(
        clock_id, frozen_time=new_frozen_time
    )
    clock_finished = False
    print(f"Advancing {clock.id} to {clock.frozen_time}...")
    while not clock_finished:
        time.sleep(1)
        clock = stripe.test_helpers.TestClock.retrieve(clock_id)
        if clock.status == "ready":
            clock_finished = True
        elif clock.status == "internal_failure":
            raise Exception(f"Clock {clock_id} failed to advance.")```
summer canyon
#

oh wow that's awesome!

#

haha I know about advance_clock, p sure my ex-manager built it

#

ok this is awesome

#

so I just do payment_method = "" and it should be able to create a customer without a PM

hoary slate
summer canyon
#

oh shit hehehe

#

sorry I meant the test clock backend haha

hoary slate
#

or rather a coworker wrote it and i rewrote it pretty significantly

#

oooooooooooo

summer canyon
#

oh wait

hoary slate
#

yep makes sense

summer canyon
#

ok anyways if you work with Marko btw say hi to him for me

#

but this is great

#

does this subscription generate an invoice?

#

and more qs:
in my use case, which events should I be listening to?

hoary slate
#

it does generate an invoice!

#

invoice.payment_succeeded should be good enough

summer canyon
#

my action is that I want to perform an internal topup of credits in our system

#

ok awesome

#

and that'll also send for the $0 sub as well right

#

this is the events I'm listening to right now

#

lmk if there's any discrepency

hoary slate
#

that looks good to me!

#

idk if you have looked at this page yet but it covers pretty much every possible event and gives suggestions on how to handle it

summer canyon
#

yeap! also thank you very much

#

if I have further questions should I use the built-in workflows

#

or can I just hit you up directly?

hoary slate
#

built in workflows! we take turns responding to questions in the server so there is a very good chance i won't be around when you have another question

summer canyon
#

another question, when we create those $0 subscriptions and the invoices are created, will emails be sent to the customers?

#

is that configurable by any chance

hoary slate
#

the short answer is yes but there are a lot of email options available, this page does a good job of covering them all

Configure and send invoicing emails to your customers.

summer canyon
#

but the default is to not send emails right

#

ideally we don't send any emails that we don't configure ourselves

#

and for subscription upgrades, I'm assuming we should use the update subscription endpoint?

hoary slate
hoary slate
summer canyon
#

Is it possible to have checkout session handle the entire subscription upgrade flow instead of us?

hoary slate
#

for that you'd want to look at the customer portal which allows you to create a session that allows users to upgrade subscriptions, cancel them, update payment methds etc (you can configure which of these options you want enabled)
https://docs.stripe.com/customer-management

summer canyon
#

oh dude this is awesome!

#

thank you very much, I really appreciate it hehe

dim hareBOT