#davidgsiot

1 messages ยท Page 1 of 1 (latest)

half pendantBOT
velvet fable
#

Hello! I'm not sure why that would be... can you give me the session.ID you're trying to retrieve?

keen isle
#

It's the session I get back after a successful charge.

    var session stripe.CheckoutSession
    err = json.Unmarshal(event.Data.Raw, &session)
    if err != nil {
      fmt.Fprintf(os.Stderr, "Error parsing webhook JSON: %v\n", err)
      w.WriteHeader(http.StatusBadRequest)
      return
    }
    params := &stripe.CheckoutSessionParams{}
    params.AddExpand("line_items")

    // Retrieve the session. If you require line items in the response, you may include them by expanding line_items.
    sessionWithLineItems, _ := session.Get(session.ID, params)
    lineItems := session.LineItems
    // Fulfill the purchase...
    FulfillOrder(lineItems)
  }
velvet fable
#

As far as I know that should work. What version of the Go library are you using?

keen isle
#
    "github.com/stripe/stripe-go/webhook"```
velvet fable
#

Can you try adding the session.ID as part of params instead of as a separate argument?

#

Wait, no.

#

That won't work.

#

What you're doing should work... how odd. ๐Ÿ˜…

keen isle
#

Right? I'm looking at teh source for the Go library, and

// Get returns the details of a checkout session.
func Get(id string, params *stripe.CheckoutSessionParams) (*stripe.CheckoutSession, error) {
    return getC().Get(id, params)
}

// Get returns the details of a checkout session.
func (c Client) Get(id string, params *stripe.CheckoutSessionParams) (*stripe.CheckoutSession, error) {
    path := stripe.FormatURLPath("/v1/checkout/sessions/%s", id)
    session := &stripe.CheckoutSession{}
    err := c.B.Call(http.MethodGet, path, c.Key, params, session)
    return session, err
}

Are right there.

velvet fable
keen isle
#

In trying to solve it, I added :

var session stripe.CheckoutSession
    err = json.Unmarshal(event.Data.Raw, &session)
    if err != nil {
      fmt.Fprintf(os.Stderr, "Error parsing webhook JSON: %v\n", err)
      w.WriteHeader(http.StatusBadRequest)
      return
    }
    pretty, err := json.MarshalIndent(session, "", "    ")
    fmt.Println("Session: ", string(pretty))

And I get an enormous JSON:

But nowhere in there is the Product ID.

velvet fable
#

line_items is null? Hmm....

keen isle
#

Right?? So much weirdness.

velvet fable
#

Yep, investigating, hang on...

#

line_items on a Checkout Session aren't included by default and need to be expanded to be included in the response. Let me look at some of your other requests...

half pendantBOT
velvet fable
#

Actually, I'm not seeing any GET requests from your Go code for a Checkout Session...

#

So it seems like the Go library isn't even hitting the API before something goes wrong.

keen isle
#

So the payment initiation is coming from a Stripe checkout page: https://checkout.stripe.com/c/pay/cs_test_b1jVFOtOfKWQG8859GI7glaBg3eAr00cMsQcECy6xCgh9o60TRhZnvQHG....

#

The Go client is just getting the callbacks from a successful payment.

velvet fable
#

The problem seems to be that session.Get(session.ID, params) is not making the API request, and instead throwing an error. I just don't understand why.

keen isle
twin swift
#

๐Ÿ‘‹ I'm taking a look as well - expansion is working for me, so just trying to figure out what's different between my code and yours

#

OH! In your code why do you have
lineItems := session.LineItems

shouldn't it be lineItems := sessionWithLineItems.LineItems?

keen isle
#

That would be after the session.Get(...) but the go compiler says that there is no method Get on the stripe.CheckoutSession object.

#

session.Get undefined (type "github.com/stripe/stripe-go/v76".CheckoutSession has no field or method

twin swift
#

Can you show me what you have in your imports?

keen isle
#
import (
    "encoding/json"
    "fmt"
    "io"
    "net/http"
    "os"

    stripe "github.com/stripe/stripe-go/v76"
    "github.com/stripe/stripe-go/webhook"
)```
twin swift
#

Ah, can you add "github.com/stripe/stripe-go/v76/checkout/session" to your imports as well

keen isle
#

If I do that, the compiler complains that it's imported and not used.

#

But the github.com/stripe/stripe-go/v76 import covers anything below that in the hierarchy, so it should already be imported.

#

And unless /checkout/session is explicitly exported, it shouldn't be possible to import it separately.

twin swift
#

Sorry let me clarify - the issue here specifically is with var session stripe.CheckoutSession. If you remove that line and import "github.com/stripe/stripe-go/v76/checkout/session" then I believe it should work

keen isle
#

Oh for the love of all that's holy ... I need the 'session', but it can't be called that.

#
var mySession stripe.CheckoutSession
    err = json.Unmarshal(event.Data.Raw, &mySession)
    if err != nil {
      fmt.Fprintf(os.Stderr, "Error parsing webhook JSON: %v\n", err)
      w.WriteHeader(http.StatusBadRequest)
      return
    }
        // pretty, err := json.MarshalIndent(session, "", "    ")
        // fmt.Println("Session: ", string(pretty))
    params := &stripe.CheckoutSessionParams{}
    params.AddExpand("line_items")

    // Retrieve the session. If you require line items in the response, you may include them by expanding line_items.
    sessionWithLineItems, _ := session.Get(mySession.ID, params)
        lineItems := sessionWithLineItems.LineItems```
twin swift
#

Yes! You were missing the correct import and hitting a naming collision because the pacakge is called session but you also had a variable with the same name

keen isle
#

The sample code is wrong and needs to be changed. the stripe.CheckoutSession object is required because I need the ID from it, but that variable has to be called something other than session or the compiler gets confused.

twin swift
#

Yeah I'm definitely gonna flag to the team to get that changed

keen isle
#

Thank you both for your diligent help on this!

#

That sample code needs a lot more work ๐Ÿ™‚ It's more than just that variable naming. The session.Get() call needs headers set with the stripe secret key

twin swift
#

That's set further up in the code, right? stripe.Key= sk_123

keen isle
#

It is, but it never sets the header value. So if you don't comment that line out, the compiler fails because it is declared and never used. So I have to figure out how to set the header for the Get() call. ๐Ÿ™‚

twin swift
#

Gotcha - yeah i'll have someone on our take a look at the go samples end to end and clean it up