如何在 Rails 中包含新的 Stripe Checkout?

How to include the new Stripe Checkout in Rails?

这是我之前使用的 Checkout 设置(没有连接)并且运行良好:

home/charges/new.html.erb

<%= form_tag charges_path do %>
  <div id="error_explanation">
    <% if flash[:error].present? %>
      <p><%= flash[:error] %></p>
    <% end %>
  </div>

  <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
   data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
   data-description="bishbashbooked payment"
   data-amount="700"
   data-currency="gbp"
   data-locale="auto">
  </script>
<% end %>

charges_controller.rb

class ChargesController < ApplicationController

 def new
    render 'home/charges/new'
  end

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

charge = Stripe::Charge.create(
  :customer    => customer.id,
  :amount      => 700,
  :description => 'bishbashbooked',
  :currency    => 'gbp',
)

rescue Stripe::CardError => e
    flash[:error] = e.message
    redirect_to new_charge_path
  end

end

首先,我如何将其更改为符合 SCA(使用新的 Checkout),同时将付款发送到卖家的关联账户?我想要网站上的一个按钮,上面写着“立即付款”,然后会将您重定向到新的 Stripe Checkout 页面。付款成功后,我需要它将我重定向到特定的 URL.

我从文档中看到我需要包括这个:

session = Stripe::Checkout::Session.create({
  payment_method_types: ['card'],
  line_items: [{
    name: "Cucumber from Roger's Farm",
    amount: 200,
    currency: 'usd',
    quantity: 10,
  }],
  payment_intent_data: {
    application_fee_amount: 200,
  },
  success_url: 'https://example.com/success',
  cancel_url: 'https://example.com/cancel',
}, {stripe_account:  @group.stripe_user_id })

还有这个:

// Initialize Stripe.js with the same connected account ID used when creating
// the Checkout Session.
var stripe = Stripe('pk_test_xxxxxxxxxxxxxxxxxxxxxxxx', {
  stripeAccount: <%= @group.stripe_user_id %>
});

stripe.redirectToCheckout({
  // Make the id field from the Checkout Session creation API response
  // available to this file, so you can provide it as parameter here
  // instead of the {{CHECKOUT_SESSION_ID}} placeholder.
  sessionId: <%= session.id %>
}).then(function (result) {
  // If `redirectToCheckout` fails due to a browser or network
  // error, display the localized error message to your customer
  // using `result.error.message`.
});

这两位代码似乎就是我所需要的。但是,首先我不知道将这段代码放在哪里,其次如何让它按照我的需要工作。 “立即付款”按钮在哪里?如何使用此功能将客户引导至 Stripe?

我知道这可能被归类为一个模糊的问题,但我真的被这个问题困住了。我很感激任何帮助,因为我已经尝试解决这个问题大约一个星期了,但没有成功。

谢谢,如果您需要更多信息,请告诉我。

要在 Rails 中构建结帐流程,有很多方法都行得通。这是我会做的一种方法:

  1. 在数据库中创建您要销售的产品
  2. 为您想要销售或将产品收集到购物篮或类似物品中的每个产品设置一个按钮
  3. 创建一个将接收产品 ID 或带有产品 ID 的购物篮的 CheckoutController
  4. 在您的 CheckoutContoller 中,(按照您认为合适的方式命名操作),创建一个 Stripe CheckoutSession [0]
  5. 对于每个产品,将其作为订单项添加到 CheckoutSession [1]
  6. 有一个 erb 视图,它接收 Checkout Session 的 ID 并将用户重定向到 Stripe Checkout [2]
  7. 使用重定向 URL,然后返回您的站点并显示某种成功消息 [3]。
  8. 处理任何 post-付款操作/最佳实践

[0] https://stripe.com/docs/payments/checkout/one-time#create-checkout-session [1] https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-line_items [2] https://stripe.com/docs/payments/checkout/one-time#redirect-checkout [3] https://stripe.com/docs/payments/checkout/fulfillment

希望对您有所帮助!