2012-12-29 65 views
0

我的Rails應用成功地使用條紋進行付款,但是當我試圖檢索信用卡的最後4位數字時出現未定義的方法錯誤從成功收費。條紋:當檢索條紋電荷的最後4位時,未定義的方法'last4' - Rails

錯誤:

undefined method `last4' for #<Stripe::Charge:0x007ff2704febc8> 

app/models/order.rb:33:in `stripe_retrieve' 
app/controllers/orders_controller.rb:54:in `block in create' 
app/controllers/orders_controller.rb:52:in `create' 

orders_controller.rb

def create 
if current_user 
    @order = current_user.orders.new(params[:order]) 
else 
    @order = Order.new(params[:order]) 
end 
respond_to do |format| 
    if @order.save_with_payment 
    @order.stripe_retrieve 

    format.html { redirect_to auctions_path, :notice => 'Your payment of $1 has been successfully processed and your credit card has been linked to your account.' } 
    format.json { render json: @order, status: :created, location: @order } 
    format.js 
    else 
    format.html { render action: "new" } 
    format.json { render json: @order.errors, status: :unprocessable_entity } 
    format.js 
    end 
end 
end 

order.rb

def save_with_payment 
if valid? 
    customer = Stripe::Customer.create(description: email, card: stripe_card_token) 
    self.stripe_customer_token = customer.id 
    self.user.update_column(:customer_id, customer.id) 
    save! 

    Stripe::Charge.create(
     :amount => (total * 100).to_i, # in cents 
     :currency => "usd", 
     :customer => customer.id 
) 
end 
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 

def stripe_retrieve 
charge = Stripe::Charge.retrieve("ch_10U9oojbTJN535") 
self.last_4_digits = charge.last4 
self.user.update_column(:last_4_digits, charge.last4) 
save! 
end 

這裏的條紋文檔,顯示如何檢索費,你可以看到「 last4'是正確的,那麼爲什麼它會出現未定義的方法?

https://stripe.com/docs/api?lang=ruby#retrieve_charge

回答

3

響應將返回其本身所具有的LAST4卡。所以卡是它自己的物體。

charge.card.last4 

這裏的文檔:

#<Stripe::Charge id=ch_0ZHWhWO0DKQ9tX 0x00000a> JSON: { 
    "card": { 
    "type": "Visa", 
    "address_line1_check": null, 
    "address_city": null, 
    "country": "US", 
    "exp_month": 3, 
    "address_zip": null, 
    "exp_year": 2015, 
    "address_state": null, 
    "object": "card", 
    "address_country": null, 
    "cvc_check": "unchecked", 
    "address_line1": null, 
    "name": null, 
    "last4": "1111", 
+0

優秀感謝隊友 – railsy