admin管理员组文章数量:1278880
I am creating a membership based site where members pay in full on the date of joining and every August 1 thereafter. However, when I go to change the billing_cycle_anchor, it has an error stating that the billing cycle anchor must be 'now', 'unset' or 'unchanged', despite the documentation stating that the parameter accepts unix timestamps ()
Here is my 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.sessionpleted':
session = event['data']['object']
if 'subscription' in session:
subscription_id = session['subscription']
subscription = stripe.Subscription.retrieve(subscription_id)
next_august_1st = get_next_august_1st()
updated_subscription = stripe.Subscription.modify(
subscription_id,
billing_cycle_anchor=int(next_august_1st.timestamp()),
proration_behavior='none'
)
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)
I am creating a membership based site where members pay in full on the date of joining and every August 1 thereafter. However, when I go to change the billing_cycle_anchor, it has an error stating that the billing cycle anchor must be 'now', 'unset' or 'unchanged', despite the documentation stating that the parameter accepts unix timestamps (https://docs.stripe/billing/subscriptions/billing-cycle?locale=en-GB)
Here is my 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.sessionpleted':
session = event['data']['object']
if 'subscription' in session:
subscription_id = session['subscription']
subscription = stripe.Subscription.retrieve(subscription_id)
next_august_1st = get_next_august_1st()
updated_subscription = stripe.Subscription.modify(
subscription_id,
billing_cycle_anchor=int(next_august_1st.timestamp()),
proration_behavior='none'
)
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)
Share
Improve this question
asked Feb 24 at 11:55
user29777024user29777024
1
1 Answer
Reset to default 0There is no direct way to do what you're looking for unfortunately. You have though multiple workarounds that I'm going to list here:
When creating the Checkout Session in mode
subscription
you can pass 2 items, the subscription price and a one-off price for the first period. You can then setsubscription_data.billing_cycle_anchor
to the 1st of August and passsubscription_data.proration_behavior: 'none'
. This will make your customer pay a full price for the period until August 1st and then start a yearly period from that point. This is more accurate/readable to your customers, since they will be able to see on the Checkout page how they will be invoiced.The other option is to create a subscription schedule instead of modifying the subscription in your webhook endpoint when listening to the
checkout.sessionpleted
event. As explained here you can set the first phase to end before Aug 1st and the billing cycle will then be bound the second phase's start date.
本文标签: How do I change the billingcycleanchor after Stripe checkout for a subscriptionStack Overflow
版权声明:本文标题:How do I change the billing_cycle_anchor after Stripe checkout for a subscription - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741272278a2369525.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论