58 lines
1.3 KiB
Go
58 lines
1.3 KiB
Go
package application
|
|
|
|
import (
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
|
|
"github.com/gorilla/mux"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
func (h *Handler) CreditWalletHTTPHandler() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
userid := vars["userid"]
|
|
|
|
if r.Method != "POST" {
|
|
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
r.ParseForm()
|
|
|
|
amountStr := r.FormValue("amount")
|
|
paymentMethod := r.FormValue("payment_method")
|
|
description := r.FormValue("description")
|
|
|
|
amount, err := strconv.ParseFloat(amountStr, 64)
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("could not read amount")
|
|
http.Error(w, "Bad Request", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if paymentMethod == "" {
|
|
paymentMethod = "Paiement en espèce (MMS)"
|
|
}
|
|
|
|
err = h.applicationHandler.CreditWallet(r.Context(), userid, amount, paymentMethod, description)
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("could not credit wallet")
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
refererURL, err := url.Parse(r.Referer())
|
|
if err != nil {
|
|
http.Redirect(w, r, r.Referer(), http.StatusFound)
|
|
return
|
|
}
|
|
|
|
query := refererURL.Query()
|
|
query.Set("tab", "wallet")
|
|
refererURL.RawQuery = query.Encode()
|
|
|
|
http.Redirect(w, r, refererURL.String(), http.StatusFound)
|
|
}
|
|
} |