gpt4 book ai didi

ruby-on-rails - 如果我有 Stripe 卡 token ,我该如何检索客户 ID?

转载 作者:行者123 更新时间:2023-12-02 01:53:41 24 4
gpt4 key购买 nike

我的应用使用设计来创建新用户,并将其与 Stripe 集成。

当我创建一个新客户时,我会返回(作为响应)一个 token ,它是卡的 token 。有没有办法使用该 token 检索客户的 ID?

我有一个 related question这显示了我当前的实现。我提出这个问题是因为我现在明白问题是我在创建新客户期间没有检索/存储客户的 ID。

任何提示都是有值(value)的。谢谢。

我的 JS( CoffeeScript )

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

user =
setupForm: ->
$('#new_user').submit ->
$('input[type=submit]').attr('disabled', true)
if $('#card_number').length
user.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, user.handleStripeResponse)

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

我不在 Controller 中做任何事情。我只在我的 user.rb 中使用它:

 after_create :create_a_customer

def create_a_customer
token = self.stripe_card_token
customer = Stripe::Customer.create(
:card => token,
:plan => 1,
:email => self.email
)
end

最佳答案

我们的 JS 看起来是一样的,所以我想我会发布我的表单和 Controller 操作以及我的模型,它对用户进行一些处理和保存。如果有任何帮助,请告诉我!抱歉,我似乎找不到确切的错误,但我知道,对我来说,有一个工作示例来查看总是有帮助的!

表格--

<%= simple_form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => {:class => 'form card_form' }) do |f| %>

<%= f.email_field :email, :required => true, :class => "deviseFormInput", :placeholder => "EMAIL" %><br />
<i>PASSWORD MINIMUM OF 6 CHARACTERS</i>
<%= f.password_field :password, :required => true, :class => "deviseFormInput", :placeholder => "PASSWORD" %>
<%= f.password_field :password_confirmation, :required => true, :class => "deviseFormInput", :placeholder => "PASSWORD CONFIRMATION" %>
<div class="field">
<%= f.input :meatless, as: :boolean, :class => "deviseFormInput", :label => "I AM A VEGETARIAN" %>
</div>
<%= f.input :comments, as: :text, :placeholder => "ALLERGIES OR INTOLERANCES", :label => false, input_html: { class: 'deviseFormInput', style: 'width: 280px; height: 100px;' } %>

<% if @user.stripe_customer_token %>
<p>Credit card acceptance is pending.</p>
<% else %>
<div class="field">
<%= text_field_tag :card_number, nil, name: nil, :class => "deviseFormInput", :placeholder => "CREDIT CARD NUMBER" %>
</div>
<div class="field">
<%= text_field_tag :card_code, nil, name: nil, :class => "deviseFormInput", :placeholder => "CARD SECURITY CODE (CVV)" %>
</div>
<div class="field">
<%= label_tag :card_month, "CARD EXPIRATION" %>
<br>
<div class="presser presserDeep">
<%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month"} %>
</div>
<div class="presser">
<%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+10}, {name: nil, id: "card_year"}%>
</div>
</div>
<% end %>
<%= f.hidden_field :stripe_card_token %>
<%= f.button :submit, 'SIGN UP', :class => 'deviseBtn' %>

<% end %>

Controller --

def create
shopping_cart_id = session[:shopping_cart_id]
@shopping_cart = session[:shopping_cart_id] ? ShoppingCart.find(shopping_cart_id) : ShoppingCart.create
session[:shopping_cart_id] = @shopping_cart.id
@user = User.new(params[:user])
if @user.save_with_payment
sign_in @user
redirect_to root_path, :notice => "Thank you for signing up!"
else
render :new
end
end

MODEL(用于 save_with_payment 方法)--

def save_with_payment
if valid?
customer = Stripe::Customer.create(description: email, card: stripe_card_token)
self.stripe_customer_token = customer.id
self.last_4_digits = customer.cards.data.first["last4"]
save!
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

关于ruby-on-rails - 如果我有 Stripe 卡 token ,我该如何检索客户 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21457911/

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