diff --git a/example/checkout/main.go b/example/checkout/main.go index 0595433..08186d2 100644 --- a/example/checkout/main.go +++ b/example/checkout/main.go @@ -1,50 +1,85 @@ package main import ( - "context" + "embed" + "fmt" + "html/template" "log" + "net/http" + "os" + "strconv" + + gonanoid "github.com/matoous/go-nanoid/v2" "github.com/sumup/sumup-go" "github.com/sumup/sumup-go/checkouts" ) +var ( + //go:embed templates + templatesFs embed.FS + templates *template.Template +) + +func init() { + templates = template.Must(template.ParseFS(templatesFs, "templates/*.html")) +} + func main() { - ctx := context.Background() + merchantCode := os.Getenv("SUMUP_MERCHANT_CODE") client := sumup.NewClient() - checkout, err := client.Checkouts.Create(ctx, checkouts.CreateCheckoutBody{ - Amount: 123, - CheckoutReference: "TX000001", - Currency: "EUR", - MerchantCode: "MK0001", + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + templates.ExecuteTemplate(w, "index.html", nil) + }) + http.HandleFunc("/create-checkout", func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + http.Redirect(w, r, "/", http.StatusSeeOther) + return + } + + rawAmount := r.FormValue("amount") + amount, err := strconv.ParseFloat(rawAmount, 64) + if err != nil { + http.Error(w, fmt.Sprintf("Invalid amount %q: %v.", rawAmount, err), http.StatusBadRequest) + return + } + + checkoutReference := gonanoid.Must() + description := "Test Payment" + checkout, err := client.Checkouts.Create(r.Context(), checkouts.CreateCheckoutBody{ + Amount: amount, + CheckoutReference: checkoutReference, + Currency: "EUR", + MerchantCode: merchantCode, + Description: &description, + }) + if err != nil { + http.Error(w, fmt.Sprintf("Failed to create checkout: %v.", err), http.StatusInternalServerError) + return + } + + data := struct { + CheckoutID string + }{ + CheckoutID: *checkout.Id, + } + + templates.ExecuteTemplate(w, "checkout.html", data) }) - if err != nil { - log.Printf("[ERROR] create checkout: %v", err) - return - } - - log.Printf("[INFO] checkout created: id=%q, amount=%v, currency=%q", *checkout.Id, *checkout.Amount, string(*checkout.Currency)) - - checkoutSuccess, err := client.Checkouts.Process(ctx, *checkout.Id, checkouts.ProcessCheckoutBody{ - Card: &checkouts.Card{ - Cvv: "123", - ExpiryMonth: "12", - ExpiryYear: "2023", - Name: "Boaty McBoatface", - Number: "4200000000000042", - }, - PaymentType: checkouts.ProcessCheckoutBodyPaymentTypeCard, + http.HandleFunc("/payment-result", func(w http.ResponseWriter, r *http.Request) { + status := r.URL.Query().Get("status") + message := r.URL.Query().Get("message") + + templates.ExecuteTemplate(w, "result.html", struct { + Status string + Message string + }{ + Status: status, + Message: message, + }) }) - if err != nil { - log.Printf("[ERROR] process checkout: %v", err) - return - } - - if accepted, ok := checkoutSuccess.AsCheckoutSuccess(); ok { - log.Printf("[INFO] checkout success: id=%q, transaction_id=%q", *accepted.Id, *(*accepted.Transactions)[0].Id) - } - - if accepted, ok := checkoutSuccess.AsCheckoutAccepted(); ok { - log.Printf("[INFO] checkout accepted: redirect_to=%q", *accepted.NextStep.RedirectUrl) - } + + fmt.Println("Server started at http://localhost:8080") + log.Fatal(http.ListenAndServe(":8080", nil)) } diff --git a/example/checkout/templates/checkout.html b/example/checkout/templates/checkout.html new file mode 100644 index 0000000..f21b2d8 --- /dev/null +++ b/example/checkout/templates/checkout.html @@ -0,0 +1,27 @@ + + + + + Complete Payment + + + + +

Complete Your Payment

+
+ + + + \ No newline at end of file diff --git a/example/checkout/templates/index.html b/example/checkout/templates/index.html new file mode 100644 index 0000000..b865714 --- /dev/null +++ b/example/checkout/templates/index.html @@ -0,0 +1,16 @@ + + + + + SumUp Payment + + + +

Enter Amount

+
+ + +
+ + + \ No newline at end of file diff --git a/example/checkout/templates/result.html b/example/checkout/templates/result.html new file mode 100644 index 0000000..899c409 --- /dev/null +++ b/example/checkout/templates/result.html @@ -0,0 +1,14 @@ + + + + + Payment Result + + + +

Payment {{.Status}}

+

{{.Message}}

+ Make another payment + + + \ No newline at end of file diff --git a/example/go.mod b/example/go.mod index 8b9ec49..294dc16 100644 --- a/example/go.mod +++ b/example/go.mod @@ -6,4 +6,7 @@ replace github.com/sumup/sumup-go => ../ require github.com/sumup/sumup-go v0.0.0-20230919081147-7283f347e1b5 -require golang.org/x/oauth2 v0.26.0 // indirect +require ( + github.com/matoous/go-nanoid/v2 v2.1.0 // indirect + golang.org/x/oauth2 v0.26.0 // indirect +) diff --git a/example/go.sum b/example/go.sum index 43a2433..65040b0 100644 --- a/example/go.sum +++ b/example/go.sum @@ -1,4 +1,6 @@ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/matoous/go-nanoid/v2 v2.1.0 h1:P64+dmq21hhWdtvZfEAofnvJULaRR1Yib0+PnU669bE= +github.com/matoous/go-nanoid/v2 v2.1.0/go.mod h1:KlbGNQ+FhrUNIHUxZdL63t7tl4LaPkZNpUULS8H4uVM= golang.org/x/oauth2 v0.26.0 h1:afQXWNNaeC4nvZ0Ed9XvCCzXM6UHJG7iCg0W4fPqSBE= golang.org/x/oauth2 v0.26.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=