当前期间结束值保存为0而不是实际值

时间:2014-09-03 13:47:10

标签: ruby-on-rails stripe-payments

我正在尝试将Stripe的用户current_period_end保存到数据库中。当我创建新订阅时,该值将保存为0,而不是订阅结束的实际日期。

我有什么遗失的吗?

它应该保存为:

"current_period_end": 1441292360

Subscription.rb:

      def save_with_stripe_payment
        customer = Stripe::Customer.create(description: email, plan: plan_id, card: stripe_card_token)
        self.stripe_customer_token = customer.id
        self.cancellation_date = customer.subscriptions.first.current_period_end
        save!
      rescue Stripe::InvalidRequestError => e
        logger.error "Stripe error while creating customer: #{e.message}"
        errors.add :base, "There was a problem with your credit card."
        false
      end

3 个答案:

答案 0 :(得分:1)

这可能是您收到的数据类型以及如何将其存储在数据库中的问题。根据您提供的信息,我预计您的字段类型错误。

您的代码是正确的,所以我会仔细检查您的字段是否为整数。

答案 1 :(得分:0)

当Strip创建新客户时,它会返回此问题底部发布的对象。

要访问current_period_end,您需要执行customer.subscriptions.data.first["current_period_end"]

  {
  "object": "customer",
  "created": 1409190039,
  "id": "cus_4fdAW5ftNQow1a",
  "livemode": false,
  "description": "new_paying_customer",
  "email": null,
  "delinquent": false,
  "metadata": {},
  "subscriptions": {
    "object": "list",
    "total_count": 1,
    "has_more": false,
    "url": "/v1/customers/cus_4fdAW5ftNQow1a/subscriptions",
    "data": [
      {
        "id": "sub_4fdAS9IlSOFfiv",
        "plan": {
          "interval": "month",
          "name": "Basic Plan",
          "created": 1409178429,
          "amount": 1200,
          "currency": "usd",
          "id": "basic",
          "object": "plan",
          "livemode": false,
          "interval_count": 1,
          "trial_period_days": null,
          "metadata": {},
          "statement_description": null
        },
        "object": "subscription",
        "start": 1409190039,
        "status": "active",
        "customer": "cus_4fdAW5ftNQow1a",
        "cancel_at_period_end": false,
        "current_period_start": 1409190039,
        "current_period_end": 1411868439,
        "ended_at": null,
        "trial_start": null,
        "trial_end": null,
        "canceled_at": null,
        "quantity": 1,
        "application_fee_percent": null,
        "discount": null,
        "metadata": {}
      }
    ]
  },
  "discount": null,
  "account_balance": 0,
  "currency": "usd",
  "cards": {
    "object": "list",
    "total_count": 1,
    "has_more": false,
    "url": "/v1/customers/cus_4fdAW5ftNQow1a/cards",
    "data": [
      {
        "id": "card_14WHtz4rPA98z9GRTW1QMenU",
        "object": "card",
        "last4": "4242",
        "brand": "Visa",
        "funding": "unknown",
        "exp_month": 1,
        "exp_year": 2015,
        "fingerprint": "0qYXzA0d9EZtsgQ6",
        "customer": "cus_4fdAW5ftNQow1a"
      }
    ]
  },
  "default_card": "card_14WHtz4rPA98z9GRTW1QMenU"
}

答案 2 :(得分:0)

这只是一个黑暗的镜头(因为我不知道你的cancellation_date字段或你正在使用的数据库的格式),但你确定你正确处理返回的时间戳吗?

如果cancellation_date是DateTime字段,我相信您必须执行以下操作:

cpe = customer.subscriptions.first.current_period_end
self.cancellation_date = Time.at(cpe).to_datetime

如果问题不是那么我就会使用控制台和/或服务器输出来确保current_period_end在您拨打Stripe::Customer.create时返回您期望的内容(像其他人一样建议)。你真的需要确定"何时切换到0"正在发生,所以我要检查(按此顺序):

  1. 创建客户时Stripe的响应
  2. customer.subscriptions.first.current_period_end
  3. 的值
  4. 分配后但保存前self.cancellation_date的值
  5. 在点击数据库之前,可能还有验证或before_save过滤器弄乱了这个字段。

    这就是我现在的所有想法:)

    希望这有帮助!