如何使用带有导轨的自定义形式的条纹v2进行充电?

时间:2016-06-20 16:01:49

标签: ruby-on-rails stripe-payments

我一直在

  

ChargesController中的Stripe :: InvalidRequestError #create   客户cus_8fqEmSUxnGEO6g没有ID为tok_18OUXYLM1r6OYriq1WcKNB79的链接源。

我不确定是什么导致了这一点。

这是我的表格:

    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(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();
   }
};

这是我的jQuery:

memory_file = BytesIO()
with zipfile.ZipFile(memory_file, 'w') as zf:
    zi = zipfile.ZipInfo('your_filename.txt')
    zi.date_time = time.localtime(time.time())[:6]
    zi.compress_type = zipfile.ZIP_DEFLATED
    zf.writestr(zi, 'your file content goes here')

memory_file.seek(0)

1 个答案:

答案 0 :(得分:3)

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

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

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

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