new version with new stripe functionality

This commit is contained in:
Joerg Lehmann 2023-02-03 17:25:51 +01:00
parent 97d2bb2593
commit 6364bedb29
1 changed files with 35 additions and 2 deletions

View File

@ -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),