use jsonResponse & refund back

This commit is contained in:
2024-08-22 13:38:51 +02:00
parent 479183ea13
commit d271e23861
5 changed files with 69 additions and 24 deletions

View File

@ -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)
}

View File

@ -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)