#keystoenchancia_code

1 messages ยท Page 1 of 1 (latest)

unborn frostBOT
#

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

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

Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.

solid sundial
#

here is my full code:

#

def get_next_august_1st():
current_year = datetime.now().year
if datetime.now().month < 8:
return datetime(current_year, 8, 1)
return datetime(current_year + 1, 8, 1)

@app.route('/webhook', methods=['POST'])
def stripe_webhook():
payload = request.get_data(as_text=True)
sig_header = request.headers.get('Stripe-Signature')

print(f"Received Stripe-Signature: {sig_header}")
print(f"Recieved payload: {payload}")

if not sig_header:
    print("Error: Missing Stripe-Signature header")
    return jsonify(success=False, message="Missing Stripe-Signature"), 400
#

try:
event = stripe.Webhook.construct_event(
payload, sig_header, endpoint_secret
)

    if event['type'] == 'checkout.session.completed':
        session = event['data']['object']
        
        if 'subscription' in session:
            subscription_id = session['subscription']
            
            next_august_1st = int(get_next_august_1st().timestamp())
            
            subscription = stripe.Subscription.retrieve(subscription_id)

            subscription_schedule = stripe.SubscriptionSchedule.create(
            customer=subscription['customer'],
            start_date=subscription['start_date'],  # Use the subscription's start date
            phases=[
                {
                    "items": [
                        {
                            "price": subscription['items']['data'][0]['price']['id'],  # Link to the price
                            "quantity": 1,
                        }
                    ],
                    "invoice_settings": {"days_until_due": 0},
                },
                {
                    "items": [
                        {
                            "price": subscription['items']['data'][0]['price']['id'],
                            "quantity": 1,
                        }
                    ],
                    "start_date": next_august_1st,
                }
            ]
        )

        # Attach the subscription schedule to the subscription
        stripe.Subscription.update(
            subscription['id'],
            schedule=subscription_schedule.id,
        )
#

print(f"Subscription {subscription_id} billing cycle anchor updated to {next_august_1st}")
else:
print("No subscription found in session. Ignoring event.")

    return jsonify(success=True)

except ValueError as e:
    print(f"Error in processing payload: {str(e)}")
    return jsonify(success=False), 400

except stripe.error.SignatureVerificationError as e:
    print(f"Error verifying signature: {str(e)}")
    return jsonify(success=False), 400
except Exception as e:
    error_trace = traceback.format_exc()
    print(f"Unexpected error: {error_trace}")
    return jsonify({"success": False, "message": "Internal server error", "error": str(e)}), 500

if name == 'main':
app.run(debug=True)

#

i am getting an error from Stripe: Received unknown parameter: phases[1][start_date]

late atlas
#

๐Ÿ‘‹
Currently, you can't pay upfront a Subscription before starting

solid sundial
#

so i can't configure a new schedule frrom the existing subscription, and add a 2nd phase that starts on the next Aug 1

#

or at least that's what i'm tryign to do

late atlas
#

And what happens in phase 1 ?

solid sundial
#

So they pay and the subscription is created, and then the schedule is attached so that the next august 1st they pay again

#

honestly, this is my first time doing anything like this - i was using billing_cycle_anchor to try and achieve this but it wasn't working and i was advised to try this

unborn frostBOT
violet mica
#

Hi, taking over as my teammate needs to step away. Let me catch up.

solid sundial
#

hey thank you for picking this up, so i'm tryign to pass it via a webhook listening for checkout.session.complete

violet mica
#

What are you trying to 'pass it via a webhook'?

solid sundial
#

ignore me, i needed to scroll further down

#

something like this world work:

#

Stripe::SubscriptionSchedule.update( schedule.id, { phases: [ { items: [ { price: schedule.phases[0].items[0].price, quantity: schedule.phases[0].items[0].quantity, }, ], start_date: schedule.phases[0].start_date, end_date: next_august_1st, }, { items: [ { price: schedule.phases[0].items[0].price, quantity: 1, }, ], iterations: 1, start_date: next_august_1st }, ], }, )

violet mica
solid sundial
#

ok, will give it a go, thanks for the help