Search code examples
gogo-ginstripe-payments

Go version Stripe Checkout tutorial can't be compiled, undefined: session


I'm trying a Stripe Checkout tutorial with golang and gin. But the following code can't be build due to the error as undefined: session.

package main

import (
    "github.com/gin-gonic/gin"
    "net/http"
    stripe "github.com/stripe/stripe-go/v71"
)

func main(){
    r := gin.Default()

    r.GET("/buy/:id", func(c *gin.Context) {
        stripe.Key = "sk_test_MyKey............"

        params := &stripe.CheckoutSessionParams{
            PaymentMethodTypes: stripe.StringSlice([]string{
                    "card",
                    "ideal",
            }),
            LineItems: []*stripe.CheckoutSessionLineItemParams{
                    &stripe.CheckoutSessionLineItemParams{
                            PriceData: &stripe.CheckoutSessionLineItemPriceDataParams{
                                Currency: stripe.String("stripe.String(stripe.CurrencyEUR)"),
                                ProductData: &stripe.CheckoutSessionLineItemPriceDataProductDataParams{
                                    Name: stripe.String("T-shirt"),
                                },
                                UnitAmount: stripe.Int64(2000),
                            },
                            Quantity: stripe.Int64(1),
                    },
            },
            Mode: stripe.String("payment"),
            SuccessURL: stripe.String("https://example.com/success?session_id={CHECKOUT_SESSION_ID}"),
            CancelURL: stripe.String("https://example.com/cancel"),
        }
        s, err := session.New(params)
        if err != nil {
            log.Println(err)
        } else {
            c.JSON(http.StatusOK, gin.H{
                "SessionID": s.id,
            })  
        }
    r.Run(":8080")
}

Does anyone knows why session is undefined? Thank you!


Solution

  • You need to import the package containing session: Go package docs

    import "github.com/stripe/stripe-go/v71/checkout/session"
    

    They do link to it from the Create a Checkout Session section, but they forgot the include in the code snippets.