Home  >  Article  >  Backend Development  >  Problem retrieving session data on checkout.session.completed Stripe-Go

Problem retrieving session data on checkout.session.completed Stripe-Go

WBOY
WBOYforward
2024-02-05 21:21:03442browse

在 checkout.session.completed Stripe-Go 上检索会话数据时出现问题

Question content

My goal is to get the items ordered by the customer after they complete the checkout session (using Stripe's pre-built checkout) and Save it to my MongoDB database. The problem I'm having is that when I try to retrieve the CheckoutSession LineItems, they are always zero. Sorry if the question seems poorly worded, I'm new here.

This is a snippet of the webhook endpoint, I tried to omit all irrelevant code including error handling.

payload, err := c.GetRawData()
event, err := webhook.ConstructEventWithOptions(payload, c.Request.Header.Get("Stripe-Signature"), webhookSecret, webhook.ConstructEventOptions{IgnoreAPIVersionMismatch: true})

switch (event.Type) {
case "checkout.session.completed":
    var session stripe.CheckoutSession
    err := json.Unmarshal(event.Data.Raw, &session)

    if session.LineItems == nil {
        // I always get here
    }

expect: I want to be able to get an array of the items they purchased, with data like Stripe PriceID, quantity, etc...

result: LineItems are always zeros, even though the order always contains items, but when I try to print the CheckoutSession itself, more zeros appear.

My diagnosis: I can't help but wonder if this has something to do with the API version mismatch, you can see that when I constructed the event I added the option to ignore the API version mismatch. What I'm running into is that StripeCLI is apparently using an older version of the Stripe API (I should note that I downloaded the latest official version from the official GitHub today and am using version 1.17.2). The CLI suggested I add this flag. I also find it odd that the latest version on pkg.go.dev stripe-go is listed as v70.15.0 being incompatible and released on April 14, 2020? As a result, running go get -u github.com/stripe/stripe-go will get this version. However, if I visit the stripe-go GitHub Repo, it shows that the latest version is v75.6.0 and suggests adding Go like this: go get -u github.com/stripe/stripe-go/v75. As you read this, you might be thinking “Wow, this guy is so stupid, he’s never heard of LTS”? But I thought I might mention this because it's the only thing I could think of.

Notice: I also want to mention that I do have some issues with their documentation and they say to do this:

FC4E38232407D7D61E722790E7D8A7F7

To get line items, but error occurred: Unresolved reference 'Get'


Correct Answer


Like this question As the answer mentioned, line_items is "extensible" and is not included by default. This means they are not included in the object when passed in the webhook event.

You must make a separate request to any of the following parties:

  • Retrieve the Checkout Session object and explicitly expand line_items, or
  • RetrievallistLineItems

Additionally, stripe-go, stripe-dotnet and stripe-java are all pinned to a specific API version (because they are strongly typed and every API change may break deserialization). You should ensure that you create the webhook endpoint using the exact same API version that you use in your code.

Stripe CLI can only send/forward webhooks in your Stripe account's default API version or the latest version: https://www.php.cn/link/4379cf00e1a95a97a33dac10ce454ca4. You can view your account's default API version here: https://www.php.cn/link/689041c2baed0f6d91050495d632d6e0.

About the error Unresolved reference 'Get', it is almost impossible to tell what happened based on the information currently provided. This may be an IDE or project settings issue. You can try the answers listed in this question: GoLand (JetBrains) shows error message "Unresolved reference". But the code compiles and runs

The above is the detailed content of Problem retrieving session data on checkout.session.completed Stripe-Go. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete
Previous article:Golang websocketNext article:Golang websocket