Session::create中可以添加自定义的ID,以便在webhook中检索它吗?

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

Can I add a custom Id to Session::create so I can retrieve it in a webhook?

问题

基本上,按照 Stripe 文档,您可以创建一个结帐会话:

$checkout_session = \Stripe\Checkout\Session::create([
    'line_items' => [[
        'price' => $prices->data[0]->id,
        'quantity' => 1,
    ]],
    'mode' => 'subscription',
    'success_url' => $YOUR_DOMAIN . '/success.html?session_id={CHECKOUT_SESSION_ID}',
    'cancel_url' => $YOUR_DOMAIN . '/cancel.html',
]);

用户将被重定向到 Stripe,完成交易,然后重定向回网站,webhook 会触发相关事件。

现在,在 webhook 监听器中,您可以获取有关付款和客户的信息,但我不知道在我的端上是哪个客户。

所以,我可以在 Session::create 中附加一些自定义数据,以便在服务器上的 webhook 监听器中识别哪个用户进行了购买吗?

英文:

Basically, following the stripe docs, you create a checkout session

  $checkout_session = \Stripe\Checkout\Session::create([
    'line_items' => [[
      'price' => $prices->data[0]->id,
      'quantity' => 1,
    ]],
    'mode' => 'subscription',
    'success_url' => $YOUR_DOMAIN . '/success.html?session_id={CHECKOUT_SESSION_ID}',
    'cancel_url' => $YOUR_DOMAIN . '/cancel.html',
  ]);

The user gets redirected to stripe, completes the transaction, gets redirected back to the website, and the events for the webhook fire.

Now in the webhook listener, you can get the info about the payment and customer, but I have no idea who that customer is on my end.

So, can I attach some custom data to the Session::create so I can identify witch user made the purchase in the webhook listener on the server ?

答案1

得分: 1

您可以在创建 Stripe 会话时传递一个 metadata 对象,其中包含您希望添加的所有自定义数据,例如您数据库中的 customerId。当触发 Webhook 时,Stripe 将与此处列出的其他字段一起发布 metadata 对象。

因此,您的代码应该如下所示:

$checkout_session = \Stripe\Checkout\Session::create([
  'line_items' => [[
    'price' => $prices->data[0]->id,
    'quantity' => 1,
  ]],
  'mode' => 'subscription',
  'success_url' => $YOUR_DOMAIN . '/success.html?session_id={CHECKOUT_SESSION_ID}',
  'cancel_url' => $YOUR_DOMAIN . '/cancel.html',
  'metadata' => [
    'customerId' => 'some-value'
  ]
]);

阅读更多

英文:

You can pass a metadata object in your session create call to Stripe with all custom data that you'd like to add for eg, customerId of your database. When the webhook is triggered, stripe will post the metadata object along with other fields listed here.

So your code should look like

$checkout_session = \Stripe\Checkout\Session::create([
'line_items' => [[
  'price' => $prices->data[0]->id,
  'quantity' => 1,
]],
'mode' => 'subscription',
'success_url' => $YOUR_DOMAIN . '/success.html?session_id={CHECKOUT_SESSION_ID}',
'cancel_url' => $YOUR_DOMAIN . '/cancel.html',
'metadata' => [
      'customerId' => 'some-value'
]
]);

Read more

huangapple
  • 本文由 发表于 2023年5月29日 04:00:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76353373.html
匿名

发表评论

匿名网友

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

确定