gpt4 book ai didi

ruby-on-rails - 使用 Coffeescript 更新 Stripe 信用卡

转载 作者:太空宇宙 更新时间:2023-11-03 18:19:00 27 4
gpt4 key购买 nike

我终于想出了如何使用这个 tutorial 实现 Stripes Monthly Billing

目前我的#new_subscription 表单和编辑表单的提交按钮创建信用卡数据并将其发送到 Stripe。

但由于某种原因,我在更新信用卡时一直收到此错误

can't convert Symbol into String
app/controllers/subscriptions_controller.rb:38:in `update'

问题:这个错误是由于 coffeescript 引起的,还是我模型中的 update_card 方法导致了这个问题?

编辑表格

<%= form_for @subscription, :html => { :id => "update_card" } do |f| %>
<%= render 'shared/error_messages', object: f.object %>

<%= f.hidden_field :plan_id %>
<%= f.hidden_field :user_id %>
<%= f.hidden_field :stripe_card_token %>

<h4>Change Credit Card</h4>

<div class="field">
<%= label_tag :card_number, "Credit Card Number" %>
<%= text_field_tag :card_number, nil, name: nil %>
</div>
<div class="field">
<%= label_tag :card_code, "Security Code on Card (CVV)" %>
<%= text_field_tag :card_code, nil, name: nil %>
</div>

<div class="field">
<%= label_tag :card_month, "Card Expiration" %>
<%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month"} %>
<%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year"} %>
</div>

<%= f.submit "Change Credit Card", :class => "btn btn-primary" %>

<% end %>

JS.咖啡

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

###That part is working great in the coffee script.

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)

###This is the Coffee for my Edit Form..
changecard =
setupForm: ->
$('#update_card').submit ->
$('input[type=submit]').attr('disabled', true)
if $('#card_number').length
changecard.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, changecard.handleStripeResponse)

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

型号

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

def update_card(attrs)
customer = Stripe::Customer.retrieve(stripe_customer_token)
customer.email = "myemail@gmail.com"
customer.description = "Subscription"

###This is being used for the credit card data
customer.card = attrs[:stripe_card_token]
customer.save

rescue Stripe::StripeError => e
logger.error "Stripe Error: " + e.message
errors.add :base, "#{e.message}."
false
end

Controller

class SubscriptionsController < ApplicationController

def update
@subscription = current_user.subscription

### I use the method update_card to update the customer's card
@subscription.update_card(params[:subscription])

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

引用:Stripe Reference for Ruby

日志

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

Parameters: {"utf8"=>"✓", "authenticity_token"=>"ehyUD17CbH7MMOe98s+Kqkh+wGghntWkG4OpoqbnQaA=",
"subscription"=>{"plan_id"=>"1", "user_id"=>"1",
"stripe_card_token"=>"tok_103PJj26f8kAprmwvAtoZFRq"}, "id"=>"5"}

User Load (0.8ms) 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

Redirected to
Completed 500 Internal Server Error in 1283ms

TypeError (can't convert Symbol into String):
app/controllers/subscriptions_controller.rb:38:in `update'

最佳答案

您正在使用 params 作为参数调用 update_card:

@subscription.update_card(params)

update_card 期望 :stripe_card_tokenparams 中:

def update_card(params)
#...
customer.card = params[:stripe_card_token]

但是没有params[:stripe_card_token],token在params[:subscription][:stripe_card_token]中:

Parameters: {
...
"subscription"=>{ ... "stripe_card_token"=>"tok_103PCo26f8kAprmwHCPjZ6T5"},
}

params 结构也与您的表单结构相匹配:

<%= form_for @subscription, :html => { :id => "update_card" } do |f| %>
...
<%= f.hidden_field :stripe_card_token %>

form_for @subscription 将设置表单以使用 subscription[X] 字段 X 以便 hidden_​​field 将在 HTML 中包含 name="subscription[stripe_card_token]",并在 Controller 中转换为 params[:subscription][:stripe_card_token]

也许你的意思是:

@subscription.update_card(params[:subscription])

您还对 @subscription.update_card 进行了无参数调用。您的整个 Controller 方法可能看起来更像这样:

def update
@subscription = current_user.subscription
if @subscription.update_card(params[:subscription])
redirect_to edit_subscription_path, :success => 'Updated Card.'
else
flash.alert = 'Unable to update card.'
render :edit
end
end

关于ruby-on-rails - 使用 Coffeescript 更新 Stripe 信用卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21450993/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com