use jsonResponse & refund back
This commit is contained in:
		@ -291,12 +291,12 @@ func (app *application) CreateAuthToken(w http.ResponseWriter, r *http.Request)
 | 
				
			|||||||
	// send response
 | 
						// send response
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var payload struct {
 | 
						var payload struct {
 | 
				
			||||||
		Error   bool          `json:"error"`
 | 
							OK      bool          `json:"ok"`
 | 
				
			||||||
		Message string        `json:"message"`
 | 
							Message string        `json:"message"`
 | 
				
			||||||
		Token   *models.Token `json:"authentication_token"`
 | 
							Token   *models.Token `json:"authentication_token"`
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	payload.Error = false
 | 
						payload.OK = true
 | 
				
			||||||
	payload.Message = fmt.Sprintf("token for %s created", userInput.Email)
 | 
						payload.Message = fmt.Sprintf("token for %s created", userInput.Email)
 | 
				
			||||||
	payload.Token = token
 | 
						payload.Token = token
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -338,11 +338,8 @@ func (app *application) CheckAuthentication(w http.ResponseWriter, r *http.Reque
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// valid user
 | 
						// valid user
 | 
				
			||||||
	var payload struct {
 | 
						var payload jsonResponse
 | 
				
			||||||
		Error   bool   `json:"error"`
 | 
						payload.OK = true
 | 
				
			||||||
		Message string `json:"message"`
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	payload.Error = false
 | 
					 | 
				
			||||||
	payload.Message = fmt.Sprintf("authenticated user %s", user.Email)
 | 
						payload.Message = fmt.Sprintf("authenticated user %s", user.Email)
 | 
				
			||||||
	app.writeJSON(w, http.StatusOK, payload)
 | 
						app.writeJSON(w, http.StatusOK, payload)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -423,12 +420,10 @@ func (app *application) SendPasswordResetEmail(w http.ResponseWriter, r *http.Re
 | 
				
			|||||||
	// verify that email exists
 | 
						// verify that email exists
 | 
				
			||||||
	_, err = app.DB.GetUserByEmail(payload.Email)
 | 
						_, err = app.DB.GetUserByEmail(payload.Email)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		var resp struct {
 | 
							resp := jsonResponse{
 | 
				
			||||||
			Error   bool   `json:"error"`
 | 
								OK:      false,
 | 
				
			||||||
			Message string `json:"message"`
 | 
								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)
 | 
							app.writeJSON(w, http.StatusAccepted, resp)
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@ -460,9 +455,8 @@ func (app *application) SendPasswordResetEmail(w http.ResponseWriter, r *http.Re
 | 
				
			|||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var resp struct {
 | 
						resp := jsonResponse{
 | 
				
			||||||
		Error   bool   `json:"error"`
 | 
							OK: true,
 | 
				
			||||||
		Message string `json:"message"`
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	app.writeJSON(w, http.StatusCreated, resp)
 | 
						app.writeJSON(w, http.StatusCreated, resp)
 | 
				
			||||||
@ -512,12 +506,10 @@ func (app *application) ResetPassword(w http.ResponseWriter, r *http.Request) {
 | 
				
			|||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	var resp struct {
 | 
						resp := jsonResponse{
 | 
				
			||||||
		Error   bool   `json:"error"`
 | 
							OK:      true,
 | 
				
			||||||
		Message string `json:"message"`
 | 
							Message: "Password reset.",
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	resp.Error = false
 | 
					 | 
				
			||||||
	resp.Message = "Password reset."
 | 
					 | 
				
			||||||
	app.writeJSON(w, http.StatusCreated, resp)
 | 
						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)
 | 
						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-sales", app.AllSales)
 | 
				
			||||||
		mux.Post("/all-subscriptions", app.AllSubscriptions)
 | 
							mux.Post("/all-subscriptions", app.AllSubscriptions)
 | 
				
			||||||
		mux.Post("/get-sale/{id}", app.GetSale)
 | 
							mux.Post("/get-sale/{id}", app.GetSale)
 | 
				
			||||||
 | 
							mux.Post("/refund", app.RefundCharge)
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	mux.Post("/api/forgot-password", app.SendPasswordResetEmail)
 | 
						mux.Post("/api/forgot-password", app.SendPasswordResetEmail)
 | 
				
			||||||
	mux.Post("/api/reset-password", app.ResetPassword)
 | 
						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/customer"
 | 
				
			||||||
	"github.com/stripe/stripe-go/v79/paymentintent"
 | 
						"github.com/stripe/stripe-go/v79/paymentintent"
 | 
				
			||||||
	"github.com/stripe/stripe-go/v79/paymentmethod"
 | 
						"github.com/stripe/stripe-go/v79/paymentmethod"
 | 
				
			||||||
 | 
						"github.com/stripe/stripe-go/v79/refund"
 | 
				
			||||||
	"github.com/stripe/stripe-go/v79/subscription"
 | 
						"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
 | 
						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 {
 | 
					func cardErrorMessage(code stripe.ErrorCode) string {
 | 
				
			||||||
	msg := ""
 | 
						msg := ""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -81,7 +81,7 @@ export function checkAuth(api) {
 | 
				
			|||||||
        fetch(api + "/api/is-authenticated", requestOptions)
 | 
					        fetch(api + "/api/is-authenticated", requestOptions)
 | 
				
			||||||
        .then(response => response.json())
 | 
					        .then(response => response.json())
 | 
				
			||||||
            .then(function(data) {
 | 
					            .then(function(data) {
 | 
				
			||||||
                if (data.error === true) {
 | 
					                if (data.ok === false) {
 | 
				
			||||||
                    console.log("not logged in");
 | 
					                    console.log("not logged in");
 | 
				
			||||||
                    location.href = "/login"
 | 
					                    location.href = "/login"
 | 
				
			||||||
                } else {
 | 
					                } else {
 | 
				
			||||||
 | 
				
			|||||||
@ -32,7 +32,7 @@ export function val(api) {
 | 
				
			|||||||
        .then(response => response.json())
 | 
					        .then(response => response.json())
 | 
				
			||||||
        .then(response => {
 | 
					        .then(response => {
 | 
				
			||||||
            console.log(response)
 | 
					            console.log(response)
 | 
				
			||||||
            if (response.error === false) {
 | 
					            if (response.ok === true) {
 | 
				
			||||||
                localStorage.setItem("token", response.authentication_token.token);
 | 
					                localStorage.setItem("token", response.authentication_token.token);
 | 
				
			||||||
                localStorage.setItem("token_expiry", response.authentication_token.expiry);
 | 
					                localStorage.setItem("token_expiry", response.authentication_token.expiry);
 | 
				
			||||||
                showSuccess("login-messages", "Login successful.")
 | 
					                showSuccess("login-messages", "Login successful.")
 | 
				
			||||||
@ -73,7 +73,7 @@ export function forgot(api) {
 | 
				
			|||||||
        .then(response => response.json())
 | 
					        .then(response => response.json())
 | 
				
			||||||
        .then(response => {
 | 
					        .then(response => {
 | 
				
			||||||
            console.log(response)
 | 
					            console.log(response)
 | 
				
			||||||
            if (response.error === false) {
 | 
					            if (response.ok === true) {
 | 
				
			||||||
                showSuccess("forgot-messages", "Password reset email sent")
 | 
					                showSuccess("forgot-messages", "Password reset email sent")
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
                showError("forgot-messages", response.message)
 | 
					                showError("forgot-messages", response.message)
 | 
				
			||||||
@ -115,7 +115,7 @@ export function reset(api, email) {
 | 
				
			|||||||
        .then(response => response.json())
 | 
					        .then(response => response.json())
 | 
				
			||||||
        .then(response => {
 | 
					        .then(response => {
 | 
				
			||||||
            console.log(response)
 | 
					            console.log(response)
 | 
				
			||||||
            if (response.error === false) {
 | 
					            if (response.ok === true) {
 | 
				
			||||||
                showSuccess("reset-messages", "Password reset")
 | 
					                showSuccess("reset-messages", "Password reset")
 | 
				
			||||||
                setTimeout(function () {
 | 
					                setTimeout(function () {
 | 
				
			||||||
                    location.href = "/login"
 | 
					                    location.href = "/login"
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user