package main import ( "fmt" "log" "os" "io/ioutil" "encoding/json" "net/http" "strings" "strconv" "github.com/stripe/stripe-go" "github.com/stripe/stripe-go/paymentintent" ) func getStripeKey() string { return "sk_test_GJbXPD0IAFNvvGpNEpaeDfhl" } func getstripepaymentintentHandler(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.PaymentIntentParams{ Amount: stripe.Int64(abo_amount), Currency: stripe.String(string(stripe.CurrencyCHF)), ReceiptEmail: stripe.String(name), } params.AddMetadata("charge_data", charge_data[0]) paymentintent, err := paymentintent.New(params) if err != nil { fmt.Fprintf(response,"{ \"rc\": 5, \"stripeclientsecret\": \"%s\" }\n",err) } else { fmt.Fprintf(response,"{ \"rc\": 0, \"stripeclientsecret\": \"%s\" }\n",paymentintent.ClientSecret) } } else { fmt.Fprintf(response, "{ \"rc\": 6, \"msg\": \"Only available for logged in users\" }") } } func stripeWebhookHandler(w http.ResponseWriter, req *http.Request) { const MaxBodyBytes = int64(65536) req.Body = http.MaxBytesReader(w, req.Body, MaxBodyBytes) payload, err := ioutil.ReadAll(req.Body) if err != nil { fmt.Fprintf(os.Stderr, "Error reading request body: %v\n", err) w.WriteHeader(http.StatusServiceUnavailable) return } event := stripe.Event{} if err := json.Unmarshal(payload, &event); err != nil { fmt.Fprintf(os.Stderr, "Failed to parse webhook body json: %v\n", err.Error()) w.WriteHeader(http.StatusBadRequest) return } // Unmarshal the event data into an appropriate struct depending on its Type switch event.Type { case "payment_intent.succeeded": var paymentIntent stripe.PaymentIntent err := json.Unmarshal(event.Data.Raw, &paymentIntent) if err != nil { fmt.Fprintf(os.Stderr, "Error parsing webhook JSON: %v\n", err) w.WriteHeader(http.StatusBadRequest) return } fmt.Printf("PaymentIntent was successful (charge_data: %s, amount: %d)!\n", paymentIntent.Metadata["charge_data"], paymentIntent.Amount) // ... handle other event types default: fmt.Fprintf(os.Stderr, "Unexpected event type: %s\n", event.Type) w.WriteHeader(http.StatusBadRequest) return } w.WriteHeader(http.StatusOK) }