Spring Boot 如何在Stripe事件之间设置唯一ID

jhiyze9q  于 2024-01-06  发布在  Spring
关注(0)|答案(2)|浏览(181)

我在我的 Spring 应用程序中使用Stripe checkout 我的订阅。

  1. public StripeCashoutResponse createSession(Transaction transaction){
  2. Stripe.apiKey = secretKey;
  3. logger.info("init stripe subscription payment");
  4. // The price ID passed from the client
  5. String priceId = passPriceId;
  6. StripeCashoutResponse cashoutResponse = null;
  7. SessionCreateParams params = new SessionCreateParams.Builder()
  8. .setSuccessUrl("https://example.com/success.html?session_id={CHECKOUT_SESSION_ID}")
  9. .setCancelUrl("https://example.com/canceled.html")
  10. .setMode(SessionCreateParams.Mode.SUBSCRIPTION)
  11. .setClientReferenceId(transaction.getTransactionId())
  12. .addLineItem(new SessionCreateParams.LineItem.Builder()
  13. // For metered billing, do not pass quantity
  14. .setQuantity(1L)
  15. .setPrice(priceId)
  16. .build()
  17. )
  18. .build();
  19. try {
  20. Session session = Session.create(params);
  21. StripeResponse response = session.getLastResponse();
  22. //convert to object to get the url
  23. Gson gson = new Gson();
  24. cashoutResponse = gson.fromJson(response.body(), StripeCashoutResponse.class);
  25. cashoutResponse.setPaymentStatus(session.getPaymentStatus());
  26. cashoutResponse.setCode(response.code());
  27. logger.info(cashoutResponse.getUrl());
  28. } catch (StripeException e) {
  29. transaction.setStatus(TransactionStatus.PAYMENT_FAILED);
  30. logger.error("Payment Failed: Unable to process the payment due to an error during the calling of stripe", e);
  31. }
  32. return cashoutResponse;
  33. }

字符串
这个调用的响应给了我一个像cs_test_a1GoqW9TGTJgZnGuplZJ49Y2oxD70jtLKXBrVlQA23C7skzUJd8GlUJZSY这样的ID,我将这个ID保存在数据库中。
一旦订阅被验证,我就会在我的webhooks中收到多个事件:

  1. charge.succeeded
  2. payment_method.attached
  3. customer.created
  4. customer.updated
  5. customer.subscription.created
  6. customer.subscription.updated
  7. payment_intent.succeeded
  8. payment_intent.created
  9. invoice.created
  10. invoice.finalized
  11. invoice.updated
  12. invoice.paid
  13. invoice.payment_succeeded
  14. checkout.session.completed


我们想要处理事件invoice.payment_succeeded,但是这个事件没有我们在 checkout 的第一步中保存的id。
我们在第一步中保存的id只在最后一步checkout.session.completed中出现。
在第一次结账会话期间,我们没有订阅ID。
现在我不知道如何在第一步( checkout )中保存一个id,我可以在我的事件invoice.payment_succeeded中找到它,所以当这个事件由Stripe发送时,我可以正确地处理它。
谢谢

eit6fx6z

eit6fx6z1#

当使用Checkout时,Stripe建议依赖checkout.session.completed事件来实现。他们在文档here中详细记录了这一点。
问题是在Checkout完成付款的过程中,您会收到许多其他事件类型,并且您没有一种简单的方法来知道是否忽略这些事件。我建议您将自定义元数据添加到将由Checkout创建的底层订阅。这是通过传递subscription_data[metadata]参数来完成的。这样做将确保生成的订阅将在其上设置元数据。生成的发票也将在subscription_details[metadata]属性下具有该元数据的副本。
现在使用这种方法,当您获得customer.subscription.*invoice.paid事件时,您可以查找相关的元数据,将其与此订单/付款的内部数据库记录相匹配。
最后,Stripe建议忽略invoice.payment_succeeded,而是依赖invoice.paid。该事件类型是较新的,并修复了一些长期存在的问题,例如在Stripe考虑支付的金额低于最低金额的情况下,触发事件。

pgky5nke

pgky5nke2#

最后,经过与Stripe团队几天的讨论,在结帐过程中不可能有唯一的id。即使您添加元数据,这些元数据也不会在所有事件中可用。

相关问题