Rails / Stripe – 多笔付款

我正在使用Rails和Stripe构建一个事件应用来处理付款。 我已经在我的预订页面使用了javascript,以便允许用户预订并支付多个空格而不是一次只支付一个空格。 您可以在此处看到预订页面 –

预订页面视图

但是,在我的Stripe仪表板上,只有一个空间付款 –

条纹付款

如何解决此问题,以便在预订视图中显示全额付款?

这是我的控制器和视图代码 –

bookings_controller.rb

class BookingsController  @event.number_of_spaces flash[:warning] = "Sorry, this event is fully booked." raise ActiveRecord::Rollback, "event is fully booked" end end if @booking.save # CHARGE THE USER WHO'S BOOKED # #{} == puts a variable into a string Stripe::Charge.create(amount: @event.price_pennies, currency: "gbp", card: @booking.stripe_token, description: "Booking number #{@booking.id}") flash[:success] = "Your place on our event has been booked" redirect_to event_path(@event) else flash[:error] = "Payment unsuccessful" render "new" end if @event.is_free? @booking.save! flash[:success] = "Your place on our event has been booked" redirect_to event_path(@event) end end private def booking_params params.require(:booking).permit(:stripe_token, :quantity) end end 

booking.new.html.erb

 

Confirm Your Booking

Confirm number of spaces you wish to book here:

Total Amount £<span class="total" data-unit-cost="">0

/
$('.calculate-total input').on('keyup', calculateBookingPrice); function calculateBookingPrice() { var unitCost = parseFloat($('.calculate-total .total').data('unit-cost')), numSpaces = parseInt($('.calculate-total .num-spaces').val()), total = (numSpaces * unitCost).toFixed(2); if (isNaN(total)) { total = 0; } $('.calculate-total span.total').text(total); } $(document).ready(calculateBookingPrice) Stripe.setPublishableKey(''); var stripeResponseHandler = function(status, response) { var $form = $('#new_booking'); if (response.error) { // Show the errors on the form $form.find('.payment-errors').text(response.error.message); $form.find('input[type=submit]').prop('disabled', false); } else { // token contains id, last4, and card type var token = response.id; // Insert the token into the form so it gets submitted to the server $form.append($('').val(token)); // and submit $form.get(0).submit(); } }; // jQuery(function($) { - changed to the line below $(document).on("ready page:load", function () { $('#new_booking').submit(function(event) { var $form = $(this); // Disable the submit button to prevent repeated clicks $form.find('input[type=submit]').prop('disabled', true); Stripe.card.createToken($form, stripeResponseHandler); // Prevent the form from submitting with the default action return false; }); });

我需要在booking_params中添加“价格”吗? 那里已经有’数量’了吗?

我是否需要在预订表中添加“total_amount”列? 或者它与我在控制器中的条纹动作和金额有关?

任何帮助表示赞赏。

您需要创建Stripe.order并传递items: [] ,您可以在其中传递quantity 。 在你的情况下,它将是一个项目。

有关更多信息,请查看Stripe API

更新

这里一步一步指导:

  1. 登录Stripe帐户,创建product并将其active 。 这将是您的预订。 保存。
  2. 当您在产品内部时,您将看到Inventory 。 查找+ Add SKU 。 点击它。
  3. 你会看到像这样的弹出窗口 在此处输入图像描述
  4. 填写你需要的任何东西。 在你的情况下currencypriceinventory ,也许imageID将自动生成。 按Add SKU
  5. 现在您将获得此product SKU 。 正如您可能已经猜到的那样,您可以为同一product许多SKU, price可能会有所不同。
  6. 复制新创建的SKU id
  7. 现在您可以像这样创建Stripe::Order

     response = Stripe :: Order.create(
      货币:美元',
      项目:[
         {
          类型:'sku',
           parent:'sku_9AJ4OfKbdRuoGi',#SKU id
          数量:2
         }
       ]
      客户:无,如果需要,#替换客户ID
      电子邮件:'michael.thompson@example.com'
     )
    

这就是您的response样子:

 # JSON: { "id": "or_18ryyuBq3wgBfJrxa44Jzm3T", "object": "order", "amount": 19998, "amount_returned": null, "application": null, "application_fee": null, "charge": null, "created": 1473465868, "currency": "usd", "customer": null, "email": "michael.thompson@example.com", "items": [ {"object":"order_item","amount":19998,"currency":"usd","description":"booking","parent":"sku_9AJ4OfKbdRuoGi","quantity":2,"type":"sku"}, {"object":"order_item","amount":0,"currency":"usd","description":"Taxes (included)","parent":null,"quantity":null,"type":"tax"}, {"object":"order_item","amount":0,"currency":"usd","description":"Free shipping","parent":"ship_free-shipping","quantity":null,"type":"shipping"} ], "livemode": false, "metadata": {}, "returns": {"object":"list","data":[],"has_more":false,"total_count":0,"url":"/v1/order_returns?order=or_18ryyuBq3wgBfJrxa44Jzm3T"}, "selected_shipping_method": "ship_free-shipping", "shipping": null, "shipping_methods": [ {"id":"ship_free-shipping","amount":0,"currency":"usd","delivery_estimate":null,"description":"Free shipping"} ], "status": "created", "status_transitions": null, "updated": 1473465868 } 

在检查任何API调用或webhook通知的响应时,使用Order对象的status属性来validationOrder的当前状态并做出相应的响应。

  • 首次请求订单时,生成的订单对象的初始status为已created
  • 付款后, status更改为已paid 。 您现在应该履行订单。
  • 完成订单后,更新Order对象以将status更改为已fulfilled 。 只有处于paid状态的订单才能被标记为已fulfilled
  • 如果客户在完成订单之前(通过您的网站)取消订单,请将status更新为canceled ,退款将退还。
  • 如果订单已经完成并且客户退回所购买的商品,则更新statusreturned ,这将退还付款。

阅读订单指南了解更多详情。

现在您应该在Stripe's dashboard看到新订单

希望这可以帮助。