ruby-on-rails - 如何使用带有 rails 的自定义形式的 stripe v2 进行充电?

标签 ruby-on-rails stripe-payments

我不断得到

Stripe::InvalidRequestError in ChargesController#create Customer cus_8fqEmSUxnGEO6g does not have a linked source with ID tok_18OUXYLM1r6OYriq1WcKNB79.

我不确定是什么原因造成的。

这是我的表格:

 <%= form_tag charges_path, id:"payment-form" do %>
    <article>
      <label class="amount">
        <span>Price: <%= number_to_currency @booking.price %></span>
      </label>
    </article>
    <%= hidden_field_tag 'booking_id', @booking.id %>
    <div class="form-group">
      <%= label_tag :card_number, "Credit Card Number" %>
      <%= text_field_tag :card_number, nil, name: nil, data: {stripe: "number"}, value:"4242 4242 4242 4242", class:"form-control" %>
    </div>
    <div class="form-group">
      <%= label_tag :card_code, "Security Code on Card (CVC)" %>
      <%= text_field_tag :card_code, nil, name: nil, data: {stripe: "cvc"}, value:"123", class:"form-control" %>
    </div>
    <div class="form-group">
      <%= label_tag :card_month, "Card Expiration" %>
      <%= text_field_tag :card_month, nil, name: nil, data: {stripe: "exp"}, value:"12 / 17", class:"form-control" %>
    </div>
    <div class="card-errors">
    </div>
    <%= submit_tag 'Submit', class: "button round", id: "pay_now", class:"btn btn-success" %>

  <% end %>

这是我的 Controller :

    class ChargesController < ApplicationController
      before_action :authenticate_user!

      def create
         token = params[:stripeToken]
         # Amount in cents
         @amount = (current_booking.price * 100).to_i

         customer = Stripe::Customer.create(
         :email => params[:stripeEmail]
         )

         charge = Stripe::Charge.create(
         :customer    => customer.id,
         :amount      => @amount,
         :description => 'Content',
         :currency    => 'usd',
         :source      => token
         )

         redirect_to booking_path(current_booking), notice:"Thanks, your  purchase was successful!"
         rescue Stripe::CardError => e
         flash[:error] = e.message
         redirect_to "/bookings/new/?space_listing_id=#{@current_booking.space_listing_id}"
         end

          private

          def current_booking
            @current_booking ||= Booking.find(params[:booking_id])
          end
        end

这是我的 jQuery:

jQuery(function($) {
  $('#payment-form').submit(function(event) {
    var $form = $(this);
// Disable the submit button to prevent repeated clicks
    $form.find('#pay_now').prop('disabled', true);

Stripe.card.createToken($form, stripeResponseHandler);

// Prevent the form from submitting with the default action
return false;
  });
});

function stripeResponseHandler(status, response) {
  var $form = $('#payment-form');

  if (response.error) {
    $("div.card-errors").text(response.error.message);
    $form.find('#pay_now').prop('disabled', false);
  } else {
// response contains id and card, which contains additional card details
     var token = response.id;
// Insert the token into the form so it gets submitted to the server  
    $form.append($('<input type="hidden" name="stripeToken" />').val(token));
// and submit
    $form.get(0).submit();
   }
};

最佳答案

为客户创建费用时,source 参数将代表已链接到该客户对象的卡: https://stripe.com/docs/api/#create_charge-source

在这种情况下,您尝试传递尚未与该客户关联的 token (这显然会失败)。相反,您需要在尝试收费之前使用该源创建或更新客户对象。要解决此问题,您实际上只需将 :source => token 从费用创建尝试移至客户即可:

customer = Stripe::Customer.create(
  :email => params[:stripeEmail],
  :source => token.id
)

Stripe 网站上有一个指南更详细地介绍了这一点: https://stripe.com/docs/charges#saving-credit-card-details-for-later

关于ruby-on-rails - 如何使用带有 rails 的自定义形式的 stripe v2 进行充电?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37926984/

相关文章:

node.js - 如何从 Firebase 的云功能获取 strip 付款费用 'success'?

javascript - 如何将自定义字段添加到 Stripe Checkout 的弹出表单

ruby-on-rails - 模板缺少 ruby​​-on-rails

mysql - 使用 Rails 应用程序中存在于多个数据库中的表

ruby-on-rails - Carrierwave - 添加或删除图像时多张图像上传速度太慢

ruby-on-rails - Ruby url 到 html 链接转换

ruby-on-rails - Stripe token 未附加到 Rails 应用程序的请求正文

python - Stripe API : filter for Payout ID when I have destination ID (bank ID)

ruby-on-rails - 访问 Rake 命名空间中的 Rails 环境配置

javascript - 从 POST 请求获取响应数据后,React JS 和 Axios Render