2014-09-03 45 views
0

我試圖將Stripe的用戶current_period_end保存到數據庫。當我創建新的訂閱時,該值將保存爲0,而不是其訂閱的實際日期。當前期末值保存爲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 
+0

'@ subscription.save_with_payment'但你的模型有'save_with_stripe_payment'? – ChrisBarthol 2014-09-03 13:57:55

+0

@ChrisBarthol我向模型添加了代碼,以便部分對您有意義。檢查更新。訂閱的保存工作正常,這是當前期末我有麻煩保存到數據庫。 – 2014-09-03 14:05:08

回答

1

這可能是您收到的數據類型以及如何將其存儲在數據庫中的問題。根據你所提供的信息,我預計你會得到錯誤的字段類型。

你的代碼是正確的,所以我會仔細檢查一下,看看你的字段是一個整數。

0

當地帶創建了一個新的客戶返回張貼在這個問題底部的對象。

要訪問你需要做的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" 
} 
+0

不幸的是,在您的代碼中,該值在'cancellation_date'的數據庫中仍然爲'0'。 – 2014-09-03 14:57:55

+0

我會建議使用調試器,看看您是否能夠看到客戶對象的外觀以及如何分離出current_preiod_end。 – ChrisBarthol 2014-09-03 15:01:44

0

這僅僅是一個在黑暗中拍攝(因爲我不知道你的cancellation_date字段的格式,或者你正在使用的數據庫),但是你確定你正確地處理返回的時間戳嗎?

如果CANCELLATION_DATE是日期時間字段,我相信你必須做一些事情,如:

​​

如果這是沒有問題的話,我會用控制檯和/或服務器的輸出,以確保current_period_end當您撥打Stripe::Customer.create(與其他人所建議的一樣)時,它會返回您期望的內容。你真的需要確定什麼時候「切換到0」正在發生的事情,所以我會檢查(按照這個順序):

  1. 當您創建客戶從條紋響應
  2. customer.subscriptions.first.current_period_end
  3. self.cancellation_date的賦值後,但節省

之前,也有可能是被打數據庫之前,這個領域搞亂驗證或before_save過濾器。

這就是我的想法,現在:)

希望這有助於!

相關問題