2014-01-29 93 views
4

我終於弄清楚如何使用此tutorial實施Stripes月結算。更新Stripe的客戶信用卡信息

到目前爲止,用戶可以創建&使用Stripe刪除其訂閱。但是我無法調用API中的哈希來讓用戶更改他的信用卡信息。

問題:如何使用我創建的方法0123d來調用/傳遞文檔中的子參數以更新信用卡字段?

這是我的代碼評論:

控制器

class SubscriptionsController < ApplicationController 

    def update 
    @subscription = current_user.subscription 

    ### I use the method update_card to update the customer's card 
    @subscription.update_card 

    if @subscription.update_card 
     redirect_to edit_subscription_path, :success => 'Updated Card.' 
    else 
     flash.alert = 'Unable to update card.' 
     render :edit 
    end 
    end 

end 

模型

class Subscription < ActiveRecord::Base 
    attr_accessible :plan_id, :user_id, :email, :stripe_customer_token, :last_4_digits, 
       :card_token, :card_name, :exp_month, :exp_year, :stripe_card_token 

    attr_accessor :stripe_card_token 

    belongs_to :plan 
    belongs_to :user 

    ### How can this work with my edit form. Would I need to store each person's new card? 
    def token 
    token = Stripe::Token.create(
     :card => { 
     :number => "4242424242424242", 
     :exp_month => 1, 
     :exp_year => 2015, 
     :cvc => "314" 
     }, 
    ) 
    end 

    def update_card 
    customer = Stripe::Customer.retrieve(stripe_customer_token) 
    customer.email = "[email protected]" 
    customer.description = "Unlimited" 
    customer.card = token.id 
    customer.save 
    rescue Stripe::StripeError => e 
    logger.error "Stripe Error: " + e.message 
    errors.add :base, "#{e.message}." 
    false 
    end 

這裏是條紋的文檔,示例響應

#<Stripe::Customer id=cus_3OWtLd5pirU4te 0x00000a> JSON: { 
    "object": "customer", 
    "created": 1390946171, 
    "id": "cus_3OWtLd5pirU4te", 
    "livemode": false, 
    "description": "Customer for [email protected]", 
    "email": null, 
    "delinquent": false, 
    "metadata": { 
    }, 
    "subscription": null, 
    "discount": null, 
    "account_balance": 0, 
    "currency": "usd", 

    ###I need to be able to update the data inside the customers card 
    "cards": { 
    "object": "list", 
    "count": 1, 
    "url": "/v1/customers/cus_3OWtLd5pirU4te/cards", 
    "data": [ 
     { 
     "id": "card_103OWt2eZvKYlo2CgyXLaqcd", 
     "object": "card", 
     "last4": "4242", 
     "type": "Visa", 
     "exp_month": 9, 
     "exp_year": 2015, 
     "fingerprint": "Xt5EWLLDS7FJjR1c", 
     "customer": "cus_3OWtLd5pirU4te", 
     "country": "US", 
     "name": "Brendan Lynch", 
     "address_line1": null, 
     "address_line2": null, 
     "address_city": null, 
     "address_state": null, 
     "address_zip": null, 
     "address_country": null, 
     "cvc_check": "pass", 
     "address_line1_check": null, 
     "address_zip_check": null 
     } 
    ] 
    }, 
    "default_card": "card_103OWt2eZvKYlo2CgyXLaqcd" 
} 

參考:Stripe Documentation for Ruby

js.coffee

jQuery -> 
    Stripe.setPublishableKey($('meta[name="stripe-key"]').attr('content')) 
    subscription.setupForm() 

subscription = 
    setupForm: -> 
    $('#new_subscription').submit -> 
     $('input[type=submit]').attr('disabled', true) 
     if $('#card_number').length 
     subscription.processCard() 
     false 
     else 
     true 

    processCard: -> 
    card = 
     {number: $('#card_number').val(), 
     cvc: $('#card_code').val(), 
     expMonth: $('#card_month').val(), 
     expYear: $('#card_year').val()} 

    Stripe.createToken(card, subscription.handleStripeResponse) 

    handleStripeResponse: (status, response) -> 
    if status == 200 
     $('#subscription_stripe_card_token').val(response.id) 
     $('#new_subscription')[0].submit() 
    else 
     $('#stripe_error').text(response.error.message) 
     $('input[type=submit]').attr('disabled', false) 

日誌

Started PUT "/subscriptions/5" for 127.0.0.1 at 2014-01-30 01:53:35 -0800 
Processing by SubscriptionsController#update as HTML 

    Parameters: {"utf8"=>"✓", "authenticity_token"=>"ehyUD17CbH7MMOe98s+Kqkh+wGghntWkG4OpoqbnQaA=", 
"subscription"=>{"plan_id"=>"1", "user_id"=>"1", "stripe_card_token"=>""}, 
"commit"=>"Update My Credit Card", "id"=>"5"} 

User Load (1.0ms) SELECT "users".* FROM "users" 
WHERE "users"."remember_token" = 'xEeu1X6IK9gUAsna9b6Mow' 
LIMIT 1 
Subscription Load (0.5ms) SELECT "subscriptions".* 
FROM "subscriptions" WHERE "subscriptions"."user_id" = 1 LIMIT 1 

Stripe Error: You passed an empty string for 'card'. We assume empty values 
are an attempt to unset a parameter; however 'card' cannot be unset. You 
should remove 'card' from your request or supply a non-empty value 
+1

你有什麼問題嗎? – Raptor

+0

如何使用Ive創建的update_card方法更新信用卡字段? –

回答

7

要麼你可以創建一個新卡,並刪除舊的,這將使新卡default_card爲客戶或您也可以通過更新客戶並傳遞此處所述的卡細節(檢查卡片字段及其子參數)來實現此目的https://stripe.com/docs/api#update_customer

編輯:

做這樣的

require "stripe" 
Stripe.api_key = "sk_test_BQokikJOvBiI2HlWgH4olfQ2" 

token = Stripe::Token.create(
    :card => { 
    :number => "4242424242424242", 
    :exp_month => 1, 
    :exp_year => 2015, 
    :cvc => "314" 
    }, 
) 
cu = Stripe::Customer.retrieve("cus_3Ok02kebTNsPDJ") 
cu.card = token.id 
cu.save 
+0

我不知道這是否有效。 update_card方法似乎只更新年份,月份,名稱,地址,但不是實際的信用卡號碼。 –

+1

用正確的解決方案編輯了答案。 – abhas

+0

我明白我可以通過更新客戶來解決問題。我遇到的麻煩是調用/傳遞子參數。你能提供一個關於如何做到這一點的例子嗎?我試圖做customer.cards.data = ....但我不認爲我做得正確,因爲它不起作用。 –