Ruby on rails 在条带支付中访问订阅详细信息

Ruby on rails 在条带支付中访问订阅详细信息,ruby-on-rails,ruby,ruby-on-rails-4,stripe-payments,Ruby On Rails,Ruby,Ruby On Rails 4,Stripe Payments,我的Ruby on Rails应用程序中有两个订阅计划。创建订阅后,我使用stripe webhook向客户发送电子邮件。在电子邮件中,我希望存储有关订阅(和计划)详细信息的数据,例如试用结束的时间以及计划名称或价格 def webhook stripe_event = Stripe::Event.retrieve(params[:id]) #retrieving Event ID if stripe_event.type == "customer.subscription.cr

我的Ruby on Rails应用程序中有两个订阅计划。创建订阅后,我使用stripe webhook向客户发送电子邮件。在电子邮件中,我希望存储有关订阅(和计划)详细信息的数据,例如试用结束的时间以及计划名称或价格

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\u 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

这里是指向条带API的链接,指向

如果您是正确的,您可以执行您正在尝试执行的操作。一旦你有了订阅,
subscription.trial\u 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 
您正在保存订阅id本身。您需要执行以下操作:

subscription = customer.subscriptions.first
保存整个订阅。此外,您还可以使用
subscriptions.retrieve
提供检索id(正如您在第二个代码示例中所做的那样)

subscription = customer.subscriptions.first