我的Ruby on Rails应用程序中有两个订阅计划。我在创建订阅时使用条带webhook向客户发送电子邮件。在电子邮件中,我想存储有关订阅(和计划)详细信息的数据,例如当trial_end和计划名称或价格。
def webhook
stripe_event = Stripe::Event.retrieve(params[:id]) #retrieving Event ID
if stripe_event.type == "customer.subscription.created" #checks if retrieved Event type subscription is created
stripe_customer_token = stripe_event.data.object.customer # Get Customer ID
customer = Stripe::Customer.retrieve(stripe_customer_token) #here I'm able to retrieve Customer data e.g. customer.email
subscription = customer.subscriptions.first.id #according to documentation I need to retrieve Subscription by supplying its ID. I can retrieve Subscription, but don't understand how to retrieve its data, like: subscription.trial_end
UserMailer.customer_subscription_created(customer.email).deliver #this works well
UserMailer.customer_subscription_created(subscription.trial_end).deliver #this does not work
end
end
我已检索到我的客户的订阅。当我检索客户时,我可以访问我的客户数据,例如:customer.email我假设我可以在检索Subscription:subscription.trial_end时执行相同操作,但这会给我一个错误。如何访问订阅数据?
此外,当我更改订阅计划时,我这样做并且有效:
def change_user_plan(customer_id, subscription_id)
customer = Stripe::Customer.retrieve("#{customer_id}")
subscription = customer.subscriptions.retrieve("#{subscription_id}")
subscription.plan = 2
subscription.save
end
的Stripe API的链接
答案 0 :(得分:0)
你是对的,你可以做你想做的事。订阅完成后,subscription.trial_end
即可使用。我刚测试过它:
2.1.6 :013 > customer = Stripe::Customer.retrieve("#{customer_id}")
=> #<Stripe::Customer:0x3fcd1ed0a630 id=...> JSON: { ... }
2.1.6 :014 > subscription = customer.subscriptions.retrieve("#{subscription_id}")
=> #<Stripe::Subscription:0x3fcd1ecae574 id=...> JSON: { ... }
2.1.6 :015 > subscription.trial_end
=> 1438387199
问题在于这一行:
subscription = customer.subscriptions.first.id
您正在保存订阅ID本身。你需要这样做:
subscription = customer.subscriptions.first
保存整个订阅。此外,您可以使用subscriptions.retrieve
提供用于检索的ID(正如您在第二个代码示例中所做的那样)。