package main import ( "fmt" "log" "os" "io/ioutil" "encoding/json" "net/http" "strings" "strconv" "github.com/stripe/stripe-go" "github.com/stripe/stripe-go/checkout/session" "github.com/stripe/stripe-go/webhook" ) func getStripeKey() string { return "sk_test_GJbXPD0IAFNvvGpNEpaeDfhl" } func getstripesessionHandler(response http.ResponseWriter, request *http.Request) { name := getUserName(request) if name != "" { charge_data, ok := request.URL.Query()["charge_data"] if !ok || len(charge_data[0]) < 1 { log.Println("Url Param 'charge_data' is missing") fmt.Fprintf(response, "{ \"rc\": 1, \"msg\": \"charge_data must be specified in URL\" }") return } scales := strings.Split(charge_data[0],",") var abo_years = 0 var items []string for _,scale := range scales { items = strings.Split(scale,":") if (len(items) == 2) { abo_count, err := strconv.Atoi(items[1]) if err == nil { abo_years += abo_count } } } abo_amount := int64(abo_years * 2400) stripe.Key = getStripeKey() params := &stripe.CheckoutSessionParams{ PaymentMethodTypes: stripe.StringSlice([]string{ "card", }), ClientReferenceID: stripe.String(charge_data[0]), CustomerEmail: stripe.String(name), Locale: stripe.String("de"), LineItems: []*stripe.CheckoutSessionLineItemParams{ &stripe.CheckoutSessionLineItemParams{ Name: stripe.String("Jahresabos mini-beieli.ch"), Description: stripe.String("Blablabla Description"), Amount: stripe.Int64(abo_amount), Currency: stripe.String(string(stripe.CurrencyCHF)), Quantity: stripe.Int64(1), }, }, SuccessURL: stripe.String("https://mini-beieli.ch/payment_received.html"), CancelURL: stripe.String("https://mini-beieli.ch/payment_cancelled.html"), } session, err := session.New(params) if err != nil { fmt.Fprintf(response,"{ \"rc\": 5, \"stripesessionid\": \"%s\" }\n","ERROR") } else { fmt.Fprintf(response,"{ \"rc\": 0, \"stripesessionid\": \"%s\" }\n",session.ID ) } } else { fmt.Fprintf(response, "{ \"rc\": 6, \"msg\": \"Only available for logged in users\" }") } } func stripeWebhookHandler(response http.ResponseWriter, request *http.Request) { body, err := ioutil.ReadAll(request.Body) if err != nil { fmt.Fprintf(os.Stderr, "Error reading request body: %v\n", err) response.WriteHeader(http.StatusServiceUnavailable) return } // Pass the request body & Stripe-Signature header to ConstructEvent, along with the webhook signing key // You can find your endpoint's secret in your webhook settings endpointSecret := "whsec_b1OdRuu9aK6zXt6M1EQRxZ4lhl3rrVtN"; event, err := webhook.ConstructEvent(body, request.Header.Get("Stripe-Signature"), endpointSecret) if err != nil { fmt.Fprintf(os.Stderr, "Error verifying webhook signature: %v\n", err) response.WriteHeader(http.StatusBadRequest) // Return a 400 error on a bad signature return } // Handle the checkout.session.completed event if event.Type == "checkout.session.completed" { 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) response.WriteHeader(http.StatusBadRequest) return } // Fulfill the purchase... //handleCheckoutSession(session) scales := strings.Split(session.ClientReferenceID,",") var items []string for _,scale := range scales { items = strings.Split(scale,":") if (len(items) == 2) { abo_count, err := strconv.Atoi(items[1]) if err == nil { _,_ = prolongActivation(items[0],abo_count) } } } log.Println("handleCheckoutSession "+session.ID) log.Println("handleCheckoutSession "+session.ClientReferenceID) } response.WriteHeader(http.StatusOK) }