mini-beieli-web/stripe.go

86 lines
3.2 KiB
Go

package main
import (
"fmt"
"log"
"strconv"
"net/http"
"github.com/stripe/stripe-go"
"github.com/stripe/stripe-go/checkout/session"
)
func getstripesessionHandler(response http.ResponseWriter, request *http.Request) {
name := getUserName(request)
if name != "" {
charge_name, ok := request.URL.Query()["charge_name"]
if !ok || len(charge_name[0]) < 1 {
log.Println("Url Param 'charge_name' is missing")
fmt.Fprintf(response, "{ \"rc\": 1, \"msg\": \"charge_name must be specified in URL\" }")
return
}
charge_description, ok := request.URL.Query()["charge_description"]
if !ok || len(charge_description[0]) < 1 {
log.Println("Url Param 'charge_description' is missing")
fmt.Fprintf(response, "{ \"rc\": 2, \"msg\": \"charge_description must be specified in URL\" }")
return
}
charge_quantity, ok := request.URL.Query()["charge_quantity"]
if !ok || len(charge_quantity[0]) < 1 {
log.Println("Url Param 'charge_quantity' is missing")
fmt.Fprintf(response, "{ \"rc\": 3, \"msg\": \"charge_quantity must be specified in URL\" }")
return
}
charge_quantity_int64, err := strconv.Atoi(charge_quantity[0])
if err == nil {
fmt.Println(charge_quantity_int64)
} else {
fmt.Println(charge_quantity[0], "is not an integer.")
}
charge_amount_rappen, ok := request.URL.Query()["charge_amount_rappen"]
if !ok || len(charge_amount_rappen[0]) < 1 {
log.Println("Url Param 'charge_amount_rappen' is missing")
fmt.Fprintf(response, "{ \"rc\": 4, \"msg\": \"charge_amount_rappen must be specified in URL\" }")
return
}
charge_amount_rappen_int64, err := strconv.Atoi(charge_amount_rappen[0])
if err == nil {
fmt.Println(charge_amount_rappen_int64,)
} else {
fmt.Println(charge_amount_rappen[0], "is not an integer.")
}
stripe.Key = "sk_test_GJbXPD0IAFNvvGpNEpaeDfhl"
params := &stripe.CheckoutSessionParams{
PaymentMethodTypes: stripe.StringSlice([]string{
"card",
}),
LineItems: []*stripe.CheckoutSessionLineItemParams{
&stripe.CheckoutSessionLineItemParams{
Name: stripe.String(charge_name[0]),
Description: stripe.String(charge_description[0]),
Amount: stripe.Int64(int64(charge_amount_rappen_int64)),
Currency: stripe.String(string(stripe.CurrencyCHF)),
Quantity: stripe.Int64(int64(charge_quantity_int64)),
},
},
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\" }")
}
}