From 6364bedb299d2b5f9660a3063156afa1765928b9 Mon Sep 17 00:00:00 2001 From: Joerg Lehmann Date: Fri, 3 Feb 2023 17:25:51 +0100 Subject: [PATCH] new version with new stripe functionality --- stripe.go | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/stripe.go b/stripe.go index 4a4a826..92eea94 100644 --- a/stripe.go +++ b/stripe.go @@ -3,8 +3,9 @@ package main import ( "encoding/json" "fmt" - "github.com/stripe/stripe-go/v72" - "github.com/stripe/stripe-go/v72/paymentintent" + "github.com/stripe/stripe-go/v74" + "github.com/stripe/stripe-go/v74/customer" + "github.com/stripe/stripe-go/v74/paymentintent" "io/ioutil" "log" "net/http" @@ -21,6 +22,33 @@ func getStripePK() string { return os.Getenv("STRIPE_PK") } +func getCustomerid(name string) string { + // if customer does not already exist, create it... + var cid = "" + params := &stripe.CustomerSearchParams{} + params.Query = *stripe.String("name:'" + name + "'") + customers := customer.Search(params) + for customers.Next() { + fmt.Printf("%s\n", customers.Current().(*stripe.Customer).ID) + cid = customers.Current().(*stripe.Customer).ID + } + + if cid == "" { + // create new customer + paramsc := &stripe.CustomerParams{ + Name: stripe.String(name), + } + + customer, err := customer.New(paramsc) + if err != nil { + log.Println("Error Creating Customer: " + name) + } + cid = customer.ID + } + + return cid +} + func getstripepaymentintentHandler(response http.ResponseWriter, request *http.Request) { name := getUserName(request) if name != "" { @@ -51,7 +79,12 @@ func getstripepaymentintentHandler(response http.ResponseWriter, request *http.R stripe.Key = getStripeKey() + // if customer does not already exist, create it... + customerid := getCustomerid(name) + + // define payment params := &stripe.PaymentIntentParams{ + Customer: stripe.String(customerid), Amount: stripe.Int64(abo_amount), Currency: stripe.String(string(stripe.CurrencyCHF)), ReceiptEmail: stripe.String(name),