132 lines
4.8 KiB
Go
132 lines
4.8 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"strconv"
|
|
"os"
|
|
"io/ioutil"
|
|
"encoding/json"
|
|
"net/http"
|
|
"github.com/stripe/stripe-go"
|
|
"github.com/stripe/stripe-go/checkout/session"
|
|
"github.com/stripe/stripe-go/webhook"
|
|
)
|
|
|
|
func getStripeKey() string {
|
|
return "sk_test_GJbXPD0IAFNvvGpNEpaeDfhl"
|
|
}
|
|
|
|
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 = getStripeKey()
|
|
|
|
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\" }")
|
|
}
|
|
}
|
|
|
|
func stripeWebhookHandler(response http.ResponseWriter, request *http.Request) {
|
|
body, err := ioutil.ReadAll(request.Body)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error reading request body: %v\n", err)
|
|
response.WriteHeader(http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
|
|
// Pass the request body & Stripe-Signature header to ConstructEvent, along with the webhook signing key
|
|
// You can find your endpoint's secret in your webhook settings
|
|
endpointSecret := "whsec_b1OdRuu9aK6zXt6M1EQRxZ4lhl3rrVtN";
|
|
event, err := webhook.ConstructEvent(body, request.Header.Get("Stripe-Signature"), endpointSecret)
|
|
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error verifying webhook signature: %v\n", err)
|
|
response.WriteHeader(http.StatusBadRequest) // Return a 400 error on a bad signature
|
|
return
|
|
}
|
|
|
|
// Handle the checkout.session.completed event
|
|
if event.Type == "checkout.session.completed" {
|
|
var session stripe.CheckoutSession
|
|
err := json.Unmarshal(event.Data.Raw, &session)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "Error parsing webhook JSON: %v\n", err)
|
|
response.WriteHeader(http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
// Fulfill the purchase...
|
|
//handleCheckoutSession(session)
|
|
log.Println("handleCheckoutSession "+session.ID)
|
|
}
|
|
|
|
response.WriteHeader(http.StatusOK)
|
|
}
|
|
|