use jsonResponse & refund back
This commit is contained in:
parent
479183ea13
commit
d271e23861
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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 := ""
|
||||
|
||||
|
@ -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 {
|
||||
|
@ -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"
|
||||
|
Loading…
Reference in New Issue
Block a user