我正在为我的客户开发一个 WordPress 网站。 他想要3种付款方式
第三次订阅2个月后如何取消/停止?
最好的选择是使用SubscriptionSchedules。它们允许控制订阅的多个“阶段”,例如迭代次数(在您的情况下为 2),或者在阶段结束时执行的操作(例如切换到新价格)。
您可以通过 API 创建一个订阅计划,其中价格
price_123
有一个阶段,并且您可以将 iterations
参数 设置为 2,以便续订第二个月。您还可以将 end_behavior
参数 设置为 cancel
以指示基础订阅应在第二个月末自动取消。
另一种方法是简单地创建一个价格订阅
price_123
并监听 invoice.created
事件。在创建新发票的第二个月,您可以使用更新订阅 API 并将 cancel_at_period_end
参数 设置为 true
以指示您希望订阅在第二个月末停止。
花了相当多的时间试图为我的类似案例解决这个问题。在这里分享我的代码,以防它适用于其他人。
我试图将定期订阅更新为仅计费三次(为期三个月的训练营)。通过 Zapier 执行此操作。
import requests
# Input data from subscription event in Zapier
subscription_id = input_data['subscription_id']
price_id = input_data['price_id']
customer_id = input_data['customer_id']
api_key = "YOUR_API_KEY_HERE" # Replace with your actual Stripe API key
def update_subscription_schedule(subscription_id, price_id, customer_id):
"""
Updates the subscription schedule for a given subscription.
Args:
subscription_id (str): The ID of the subscription to update.
price_id (str): The ID of the price. Needed for the update for some reason
customer_id (str): The ID of the customer
Returns:
dict: A dictionary containing the status code, a message, and other details about the update operation.
"""
url = "https://api.stripe.com/v1/subscription_schedules"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/x-www-form-urlencoded",
}
payload = {
"from_subscription": subscription_id,
}
try:
# Create a new subscription schedule from the existing subscription
response = requests.post(url, headers=headers, data=payload)
response.raise_for_status()
subscription_schedule = response.json()
subscription_schedule_id = subscription_schedule["id"]
current_phase_start_date = subscription_schedule["current_phase"]["start_date"]
subscription_coupon = subscription_schedule["phases"][0].get("coupon", None)
# Update the newly created subscription schedule
update_url = f"https://api.stripe.com/v1/subscription_schedules/{subscription_schedule_id}"
update_payload = {
"end_behavior": "cancel",
"phases[0][items][0][price]": price_id,
"phases[0][items][0][quantity]": 1,
"phases[0][coupon]": subscription_coupon,
"phases[0][iterations]": 3, # Subscription will be billed 3 times (2 more after the initial billing)
"phases[0][start_date]": current_phase_start_date,
}
update_response = requests.post(update_url, headers=headers, data=update_payload)
update_response.raise_for_status()
output = {
"status_code": update_response.status_code,
"message": "Subscription Schedule updated successfully.",
"subscription_schedule_id": subscription_schedule_id,
"current_phase_start_date": current_phase_start_date,
"subscription_coupon": subscription_coupon,
}
except requests.exceptions.RequestException as e:
output = {"status_code": e.response.status_code, "message": f"Error: {str(e)}"}
return output
# Output for Zapier
output = update_subscription_schedule(subscription_id, price_id, customer_id)
有没有其他方法可以在不使用 Zapier 的情况下做到这一点?我遇到了很多 Zaps 不执行或出错的问题,我需要一种 99% 可靠的方式来管理订阅。如果我有一个按月定价的产品,我该如何设置,例如,每当有人购买(订阅)该产品时,它就会在 3 个月后取消?