Stripe Checkout Beta:避免为每次付款创建新客户 [英] Stripe Checkout Beta: Avoid creating new customer for every payment

查看:83
本文介绍了Stripe Checkout Beta:避免为每次付款创建新客户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Stripe Checkout Beta 构建 Rails 应用程序.为此,我在服务器上创建了一个 Stripe::Checkout::Session,在前端使用 redirectToCheckout,然后等待 webhook 以 类型进入code>checkout_beta.session_succeeded 告诉我付款已完成.这工作正常,但是:

I'm building a Rails application using Stripe Checkout Beta. For this, I'm creating a Stripe::Checkout::Session on the server, use redirectToCheckout on the frontend and then wait for the webhook coming in with the type checkout_beta.session_succeeded telling me that the payment was fulfilled. This works fine, but:

对于每笔付款,Stripe 都会创建一个客户 - 这不是我想要的.如果现有客户再次付款,Stripe 不应创建新客户,新付款应与现有客户相关联.

For every payment, Stripe creates a new customer - which is not what I want. If an existing customer pays a second time, Stripe should not create a new customer, the new payment should be linked to the existing customer.

如何在重定向到 Checkout 之前设置客户 ID?

How can I set the customer id before I redirect to Checkout?

这是我的 Ruby 控制器 代码:

Here is my Ruby controller code:

# app/controllers/payments_controller.rb

def new
  @session = Stripe::Checkout::Session.create(
    success_url: "https://shop.example.test/success",
    cancel_url: "https://shop.example.test/cancelled",
    payment_method_types: ["card"],
    line_items: cart_items.map do |cart_item|
      {
        name: cart_item.name,
        quantity: 1,
        amount: cart_item.name.price.cents,
        currency: 'eur'
      }
    end,
    customer_email: current_user.email
  )
end

HTML 代码(使用 Slim):

The HTML code (using Slim):

# app/views/payments/new.html.slim

= content_tag :div, nil, data: { \
    controller: 'stripe',
    stripe_public_key: ENV['STRIPE_PUBLIC_KEY'],
    stripe_session_id: @session.id }

JavaScript 代码(使用 Stimulus.js):

The JavaScript code (using Stimulus.js):

// app/javascripts/controllers/stripe_controller.js

import { Controller } from 'stimulus'

export default class extends Controller {
  connect() {
    this.stripe = window.Stripe(this.data.get('public-key'), {
      betas: ['checkout_beta_4']
    })

    this.stripe.redirectToCheckout({
      paymentIntentId: this.data.get('session-id')
    }).then((result) => {
      console.log(result)
    })
  }
}

Ruby webhook 代码(使用 gem stripe_event):

The Ruby webhook code (using the gem stripe_event):

# config/initializer/stripe.rb

Stripe.api_key = Rails.configuration.x.stripe.private_key
Stripe.api_version = '2019-02-19; checkout_sessions_beta=v4'
StripeEvent.signing_secret = Rails.configuration.x.stripe.webhook_secret

class RecordSessions
  def call(event)
    session = event.data.object

    Payment.create!(
      stripe_charge_token: session.payment_intent
    )
  end
end

StripeEvent.configure do |events|
  events.subscribe 'checkout_beta.session_succeeded', RecordSessions.new
end

推荐答案

Stripe::Checkout::Session.create 的 API 允许添加一个 customer 键对 Stripe 客户 ID 的引用.这样,您的现有客户就会收到付款.

The API of Stripe::Checkout::Session.create allows to add a customer key which references to a Stripe customer id. This way the payment is added to your existing customer.

您可以指定一个现有的 Customer 对象供 Checkout 使用一次性付款或订阅.如果你这样做,任何条纹Checkout 创建的对象与该客户相关联.

You may specify an existing Customer object for Checkout to use when making one-time payments or subscriptions. If you do so, any Stripe objects that Checkout creates are associated with that Customer.

session = Stripe::Checkout::Session.create(
  customer: 'cus_123',
  payment_method_types: ['card'],
  line_items: [{
    name: 'T-shirt',
    description: 'Comfortable cotton t-shirt',
    images: ['https://example.com/t-shirt.png'],
    amount: 500,
    currency: 'eur',
    quantity: 1,
  }],
  success_url: 'https://example.com/success',
  cancel_url: 'https://example.com/cancel',
)

参见:https://stripe.com/docs/payments/checkout/server#using-existing-customers

这篇关于Stripe Checkout Beta:避免为每次付款创建新客户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆