如何处理单一结账中的多个订阅?如何处理Stripe Webhook事件?

huangapple go评论81阅读模式
英文:

How to handle multiple subscriptions in signle checkout? How to handle stripe webhook event?

问题

以下是翻译好的部分:

"For context I'm implementing the same billing interval subscriptions in one checkout"
"为了解释背景,我正在一个结账中实施相同的计费间隔订阅。"

"As far as I've understood, I've added multiple line_items with their price_ids and quantity and all checkout is working fine."
"据我所了解,我已添加了多个具有其价格ID和数量的line_items,并且所有的结账都正常工作。"

"But I'm not able to handle the webhook events for this kinda multiple subscriptions checkout."
"但我无法处理此类多个订阅结账的Webhook事件。"

"When we do single subscription checkout we get the subscription data in checkout.session.complete event object."
"当我们进行单个订阅结账时,我们在checkout.session.complete事件对象中获取订阅数据。"

"How can we get all the subscriptions data from this object. So that for all subscriptions we can loop through this array and create a record in our DB."
"如何从此对象中获取所有订阅数据,以便我们可以循环遍历此数组并在我们的数据库中创建记录。"


def handle_checkout_session_completed(event)
  puts "checkout.session.completed\n"
  return unless User.exists?(event.data.object.client_reference_id)

  checkout_session = event.data.object
  puts "checkout_session: #{checkout_session}\n"

  session = Stripe::Checkout::Session.retrieve({
    id: checkout_session['id'],
    expand: ['line_items']
  })

  line_items = session.line_items.data
  puts line_items

  if checkout_session.payment_status == 'paid'
    fulfill_order(checkout_session, line_items)
  end
end

def fulfill_order(checkout_session, line_items)
  user = User.find(checkout_session.client_reference_id)
  user.update(stripe_id: checkout_session.customer)

  line_items.each do |line_item|
    product_id = line_item.price.product
    tool_id = determine_tool_id(product_id)

    Subscription.create(
      user: user,
      subscription_id: checkout_session.subscription,
      plan_id: tool_id,
      interval: line_item.price.recurring.interval,
      status: 'active',
      current_period_start: Time.at(checkout_session.subscription.current_period_start).to_datetime,
      current_period_end: Time.at(checkout_session.subscription.current_period_end).to_datetime
    )
  end
end

英文:

For context I'm implementing the same billing interval subscriptions in one checkout

As far as I've understood, I've added multiple line_items with their price_ids and quantity and all checkout is working fine.
But I'm not able to handle the webhook events for this kinda multiple subscriptions checkout.

When we do single subscription checkout we get the subscription data in checkout.session.complete event object.

How can we get the all the subscriptions data from this object. So that for all subscriotions we can loop through this array and create a record in our DB.


def handle_checkout_session_completed(event)
  puts "checkout.session.completed\n"
  return unless User.exists?(event.data.object.client_reference_id)

  checkout_session = event.data.object
  puts "checkout_session: #{checkout_session}\n"

  session = Stripe::Checkout::Session.retrieve({
    id: checkout_session['id'],
    expand: ['line_items']
  })

  line_items = session.line_items.data
  puts line_items

  if checkout_session.payment_status == 'paid'
    fulfill_order(checkout_session, line_items)
  end
end

def fulfill_order(checkout_session, line_items)
  user = User.find(checkout_session.client_reference_id)
  user.update(stripe_id: checkout_session.customer)

  line_items.each do |line_item|
    product_id = line_item.price.product
    tool_id = determine_tool_id(product_id)

    Subscription.create(
      user: user,
      subscription_id: checkout_session.subscription,
      plan_id: tool_id,
      interval: line_item.price.recurring.interval,
      status: 'active',
      current_period_start: Time.at(checkout_session.subscription.current_period_start).to_datetime,
      current_period_end: Time.at(checkout_session.subscription.current_period_end).to_datetime
    )
  end
end

答案1

得分: 0

一个 subscription 模式的 CheckoutSession 只能创建一个订阅,无法使用一个 CheckoutSession 创建多个订阅。

英文:

One subscription mode CheckoutSession can only create one subscription, there's no way to create multiple subscriptions with one CheckoutSession.

答案2

得分: 0

当事件为 'invoice.payment.succeeded' 时,
      输出 "invoice.payment.succeeded\n",
      仅当事件数据对象中存在订阅信息时继续执行。

      使用以下代码检索Stripe订阅信息:`stripe_subscriptions = Stripe::Subscription.retrieve(event.data.object.subscription)`
      获取订阅项目数据:`subscriptions_items = stripe_subscriptions.items.data`
      # 查找与Stripe订阅相关联的所有订阅

      遍历每个订阅项目:
      ```
      subscriptions_items.each do |subscription_item|
        subscription = Subscription.find_by(subscription_item_id: subscription_item.id)
        subscription.update(
          status: stripe_subscriptions.status,
          current_period_start: Time.at(stripe_subscriptions.current_period_start).to_datetime,
          current_period_end: Time.at(stripe_subscriptions.current_period_end).to_datetime,
          interval: subscription_item.price.recurring.interval
        )
      end
      ```
英文:
when 'invoice.payment.succeeded'
      puts "invoice.payment.succeeded\n"
      return unless event.data.object.subscription.present?

      stripe_subscriptions = Stripe::Subscription.retrieve(event.data.object.subscription)
      subscriptions_items = stripe_subscriptions.items.data
      # find all the subscriptions associate with the stripe subscription

      subscriptions_items.each do |subscription_item|
        subscription = Subscription.find_by(subscription_item_id: subscription_item.id)
        subscription.update(
          status: stripe_subscriptions.status,
          current_period_start: Time.at(stripe_subscriptions.current_period_start).to_datetime,
          current_period_end: Time.at(stripe_subscriptions.current_period_end).to_datetime,
          interval: subscription_item.price.recurring.interval
        )
      end

Basically we have to listen for the invoice.payment.succeeded event which returns the object which has included the subscription id. And we can retrieve the subscriptions using following line stripe_subscriptions = Stripe::Subscription.retrieve(event.data.object.subscription)

And we can for_each the each sub-subscription that user has subscribed to and save in our database.

huangapple
  • 本文由 发表于 2023年7月23日 16:27:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76747286.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定