From d271e23861cabbfa4c3d88399328a20d9d60cdb8 Mon Sep 17 00:00:00 2001 From: vinchent Date: Thu, 22 Aug 2024 13:38:51 +0200 Subject: [PATCH] use jsonResponse & refund back --- cmd/api/handlers-api.go | 67 +++++++++++++++++++++++++++++------------ cmd/api/routes-api.go | 1 + internal/cards/cards.go | 17 +++++++++++ static/js/common.js | 2 +- static/js/login.js | 6 ++-- 5 files changed, 69 insertions(+), 24 deletions(-) diff --git a/cmd/api/handlers-api.go b/cmd/api/handlers-api.go index e899e7d..31f6e0e 100644 --- a/cmd/api/handlers-api.go +++ b/cmd/api/handlers-api.go @@ -291,12 +291,12 @@ func (app *application) CreateAuthToken(w http.ResponseWriter, r *http.Request) // send response var payload struct { - Error bool `json:"error"` + OK bool `json:"ok"` Message string `json:"message"` Token *models.Token `json:"authentication_token"` } - payload.Error = false + payload.OK = true payload.Message = fmt.Sprintf("token for %s created", userInput.Email) payload.Token = token @@ -338,11 +338,8 @@ func (app *application) CheckAuthentication(w http.ResponseWriter, r *http.Reque } // valid user - var payload struct { - Error bool `json:"error"` - Message string `json:"message"` - } - payload.Error = false + var payload jsonResponse + payload.OK = true payload.Message = fmt.Sprintf("authenticated user %s", user.Email) app.writeJSON(w, http.StatusOK, payload) } @@ -423,12 +420,10 @@ func (app *application) SendPasswordResetEmail(w http.ResponseWriter, r *http.Re // verify that email exists _, err = app.DB.GetUserByEmail(payload.Email) if err != nil { - var resp struct { - Error bool `json:"error"` - Message string `json:"message"` + resp := jsonResponse{ + OK: false, + Message: "No matching email found on our system", } - resp.Error = true - resp.Message = "No matching email found on our system" app.writeJSON(w, http.StatusAccepted, resp) return } @@ -460,9 +455,8 @@ func (app *application) SendPasswordResetEmail(w http.ResponseWriter, r *http.Re return } - var resp struct { - Error bool `json:"error"` - Message string `json:"message"` + resp := jsonResponse{ + OK: true, } app.writeJSON(w, http.StatusCreated, resp) @@ -512,12 +506,10 @@ func (app *application) ResetPassword(w http.ResponseWriter, r *http.Request) { return } - var resp struct { - Error bool `json:"error"` - Message string `json:"message"` + resp := jsonResponse{ + OK: true, + Message: "Password reset.", } - resp.Error = false - resp.Message = "Password reset." app.writeJSON(w, http.StatusCreated, resp) } @@ -547,3 +539,38 @@ func (app *application) GetSale(w http.ResponseWriter, r *http.Request) { } app.writeJSON(w, http.StatusOK, order) } + +func (app *application) RefundCharge(w http.ResponseWriter, r *http.Request) { + var chargeToRefund struct { + ID int `json:"id"` + PaymentIntent string `json:"pi"` + Amount int `json:"amount"` + Currency string `json:"currency"` + } + + err := app.readJSON(w, r, &chargeToRefund) + if err != nil { + app.badRequest(w, r, err) + return + } + + // validate + + card := cards.Card{ + Secret: app.config.stripe.secret, + Key: app.config.stripe.key, + Currency: chargeToRefund.Currency, + } + + err = card.Refund(chargeToRefund.PaymentIntent, chargeToRefund.Amount) + if err != nil { + app.badRequest(w, r, err) + return + } + + var resp jsonResponse + resp.OK = true + resp.Message = "Charge refunded" + + app.writeJSON(w, http.StatusOK, resp) +} diff --git a/cmd/api/routes-api.go b/cmd/api/routes-api.go index 537f0a1..6416be2 100644 --- a/cmd/api/routes-api.go +++ b/cmd/api/routes-api.go @@ -35,6 +35,7 @@ func (app *application) routes() http.Handler { mux.Post("/all-sales", app.AllSales) mux.Post("/all-subscriptions", app.AllSubscriptions) mux.Post("/get-sale/{id}", app.GetSale) + mux.Post("/refund", app.RefundCharge) }) mux.Post("/api/forgot-password", app.SendPasswordResetEmail) mux.Post("/api/reset-password", app.ResetPassword) diff --git a/internal/cards/cards.go b/internal/cards/cards.go index 412314b..8023a6b 100644 --- a/internal/cards/cards.go +++ b/internal/cards/cards.go @@ -5,6 +5,7 @@ import ( "github.com/stripe/stripe-go/v79/customer" "github.com/stripe/stripe-go/v79/paymentintent" "github.com/stripe/stripe-go/v79/paymentmethod" + "github.com/stripe/stripe-go/v79/refund" "github.com/stripe/stripe-go/v79/subscription" ) @@ -118,6 +119,22 @@ func (c *Card) CreateCustomer(pm, email string) (*stripe.Customer, string, error return cust, "", nil } +func (c *Card) Refund(pi string, amount int) error { + stripe.Key = c.Secret + amountToRefund := int64(amount) + + refundParams := &stripe.RefundParams{ + Amount: &amountToRefund, + PaymentIntent: &pi, + } + + _, err := refund.New(refundParams) + if err != nil { + return err + } + return nil +} + func cardErrorMessage(code stripe.ErrorCode) string { msg := "" diff --git a/static/js/common.js b/static/js/common.js index 47c80af..fc56913 100644 --- a/static/js/common.js +++ b/static/js/common.js @@ -81,7 +81,7 @@ export function checkAuth(api) { fetch(api + "/api/is-authenticated", requestOptions) .then(response => response.json()) .then(function(data) { - if (data.error === true) { + if (data.ok === false) { console.log("not logged in"); location.href = "/login" } else { diff --git a/static/js/login.js b/static/js/login.js index 2a4b67d..47006ee 100644 --- a/static/js/login.js +++ b/static/js/login.js @@ -32,7 +32,7 @@ export function val(api) { .then(response => response.json()) .then(response => { console.log(response) - if (response.error === false) { + if (response.ok === true) { localStorage.setItem("token", response.authentication_token.token); localStorage.setItem("token_expiry", response.authentication_token.expiry); showSuccess("login-messages", "Login successful.") @@ -73,7 +73,7 @@ export function forgot(api) { .then(response => response.json()) .then(response => { console.log(response) - if (response.error === false) { + if (response.ok === true) { showSuccess("forgot-messages", "Password reset email sent") } else { showError("forgot-messages", response.message) @@ -115,7 +115,7 @@ export function reset(api, email) { .then(response => response.json()) .then(response => { console.log(response) - if (response.error === false) { + if (response.ok === true) { showSuccess("reset-messages", "Password reset") setTimeout(function () { location.href = "/login"