【问题标题】:Access subscription details in Stripe payment在 Stripe 支付中访问订阅详情
【发布时间】:2015-09-15 23:38:32
【问题描述】:

我的 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.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

这里是指向 retrieve Subscription 的 Stripe API 的链接

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 stripe-payments


    【解决方案1】:

    你是对的,你可以做你想做的事。订阅后,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(就像您在第二个代码示例中所做的那样)。

    【讨论】:

      猜你喜欢
      • 2017-09-20
      • 2020-07-18
      • 2012-07-07
      • 2018-01-09
      • 2017-04-06
      • 1970-01-01
      • 2017-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多