#davidgsiot
1 messages ยท Page 1 of 1 (latest)
Hello! I'm not sure why that would be... can you give me the session.ID you're trying to retrieve?
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)
}
As far as I know that should work. What version of the Go library are you using?
"github.com/stripe/stripe-go/webhook"```
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. ๐
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.
Is it making the request to fetch the session? If so, can you give me the request ID so I can take a look at the request? Here's how you can find a request ID: https://support.stripe.com/questions/finding-the-id-for-an-api-request
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.
line_items is null? Hmm....
Right?? So much weirdness.
Yep, investigating, hang on...
Ah, okay, so it looks like the expand info you're trying to specify isn't making it to Stripe: https://dashboard.stripe.com/test/logs/req_rfo5kRJvobdy8i
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...
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.
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.
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.
I'm using this example: https://stripe.com/docs/payments/checkout/fulfill-orders
๐ 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?
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
Can you show me what you have in your imports?
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
stripe "github.com/stripe/stripe-go/v76"
"github.com/stripe/stripe-go/webhook"
)```
Ah, can you add "github.com/stripe/stripe-go/v76/checkout/session" to your imports as well
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.
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
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```
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
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.
Yeah I'm definitely gonna flag to the team to get that changed
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
That's set further up in the code, right? stripe.Key= sk_123
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. ๐
Gotcha - yeah i'll have someone on our take a look at the go samples end to end and clean it up