62 lines
1.8 KiB
Go
62 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"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),
|
|
}
|
|
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\" }")
|
|
}
|
|
}
|