Compare commits
3 Commits
479183ea13
...
059638f3a7
Author | SHA1 | Date | |
---|---|---|---|
059638f3a7 | |||
7635a9826f | |||
d271e23861 |
@ -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,51 @@ 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.errorLog.Println(err)
|
||||||
|
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.errorLog.Println(err)
|
||||||
|
app.badRequest(w, r, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// update status in DB
|
||||||
|
err = app.DB.UpdateOrderStatus(chargeToRefund.ID, 2)
|
||||||
|
if err != nil {
|
||||||
|
app.badRequest(
|
||||||
|
w,
|
||||||
|
r,
|
||||||
|
errors.New("the charge was refunded, but the database could not be updated"),
|
||||||
|
)
|
||||||
|
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)
|
||||||
|
@ -12,6 +12,7 @@ All Sales
|
|||||||
<th>Customer</th>
|
<th>Customer</th>
|
||||||
<th>Product</th>
|
<th>Product</th>
|
||||||
<th>Amount</th>
|
<th>Amount</th>
|
||||||
|
<th>Status</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
|
@ -1,26 +1,39 @@
|
|||||||
{{template "base" .}}
|
{{ template "base" . }}
|
||||||
|
{{ define "title" }}
|
||||||
{{define "title"}}
|
{{ index .StringMap "title" }}
|
||||||
{{index .StringMap "title"}}
|
{{ end }}
|
||||||
{{end}}
|
{{ define "content" }}
|
||||||
{{define "content"}}
|
|
||||||
<h2 class="mt-5">Sale</h2>
|
<h2 class="mt-5">Sale</h2>
|
||||||
|
<span id="refunded" class="badge bg-danger d-none">Refunded</span>
|
||||||
|
<span id="charged" class="badge bg-success d-none">Charged</span>
|
||||||
<hr>
|
<hr>
|
||||||
|
<div class="alert alert-danger text-center d-none" id="messages"></div>
|
||||||
<div>
|
<div>
|
||||||
<strong>Order No:</strong> <span id="order-no"></span><br>
|
<strong>Order No:</strong> <span id="order-no"></span>
|
||||||
<strong>Customer:</strong> <span id="customer"></span><br>
|
<br>
|
||||||
<strong>Product:</strong> <span id="product"></span><br>
|
<strong>Customer:</strong> <span id="customer"></span>
|
||||||
<strong>Quantity:</strong> <span id="quantity"></span><br>
|
<br>
|
||||||
<strong>Total Sale:</strong> <span id="amount"></span><br>
|
<strong>Product:</strong> <span id="product"></span>
|
||||||
|
<br>
|
||||||
|
<strong>Quantity:</strong> <span id="quantity"></span>
|
||||||
|
<br>
|
||||||
|
<strong>Total Sale:</strong> <span id="amount"></span>
|
||||||
|
<br>
|
||||||
</div>
|
</div>
|
||||||
<hr>
|
<hr>
|
||||||
<a href='{{index .StringMap "cancel"}}' class="btn btn-info">Cancel</a>
|
<a href='{{ index .StringMap "cancel" }}' class="btn btn-info">Cancel</a>
|
||||||
<a href="#!" class="btn btn-warning">Refund Order</a>
|
<a id="refund-btn" href="#!" class="btn btn-warning d-none">Refund Order</a>
|
||||||
{{end}}
|
<input type="hidden" id="pi" value="">
|
||||||
|
<input type="hidden" id="charge-amount" value="">
|
||||||
{{define "js"}}
|
<input type="hidden" id="currency" value="">
|
||||||
|
{{ end }}
|
||||||
|
{{ define "js" }}
|
||||||
|
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import {showInfo} from "/static/js/sale.js"
|
import {showInfo, refund} from "/static/js/sale.js"
|
||||||
showInfo({{.API}});
|
showInfo({{.API}});
|
||||||
|
document.getElementById("refund-btn").addEventListener("click", function(event) {
|
||||||
|
refund({{.API}});
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
{{end}}
|
{{ end }}
|
||||||
|
@ -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 := ""
|
||||||
|
|
||||||
|
@ -413,3 +413,16 @@ func (m *DBModel) GetOrderByID(ID int) (Order, error) {
|
|||||||
}
|
}
|
||||||
return o, nil
|
return o, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *DBModel) UpdateOrderStatus(id, statusID int) error {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
stmt := `UPDATE orders SET status_id = ? where id = ?`
|
||||||
|
|
||||||
|
_, err := m.DB.ExecContext(ctx, stmt, statusID, id)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import {formatCurrency} from "./common.js"
|
import { formatCurrency } from "./common.js"
|
||||||
|
|
||||||
export function showTable(api) {
|
export function showTable(api) {
|
||||||
let token = localStorage.getItem("token");
|
let token = localStorage.getItem("token");
|
||||||
@ -36,6 +36,12 @@ export function showTable(api) {
|
|||||||
item = document.createTextNode(cur);
|
item = document.createTextNode(cur);
|
||||||
newCell.appendChild(item)
|
newCell.appendChild(item)
|
||||||
|
|
||||||
|
newCell = newRow.insertCell();
|
||||||
|
if (i.status_id != 1) {
|
||||||
|
newCell.innerHTML = `<span class="badge bg-danger">Refunded</span>`
|
||||||
|
} else {
|
||||||
|
newCell.innerHTML = `<span class="badge bg-success">Charged</span>`
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
let newRow = tbody.insertRow();
|
let newRow = tbody.insertRow();
|
||||||
|
@ -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"
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
import {formatCurrency} from "./common.js"
|
import { formatCurrency, showError, showSuccess } from "./common.js"
|
||||||
|
|
||||||
|
const id = window.location.pathname.split("/").pop();
|
||||||
|
const token = localStorage.getItem("token");
|
||||||
|
|
||||||
export function showInfo(api) {
|
export function showInfo(api) {
|
||||||
let token = localStorage.getItem("token");
|
|
||||||
let id = window.location.pathname.split("/").pop();
|
|
||||||
|
|
||||||
const requestOptions = {
|
const requestOptions = {
|
||||||
method: 'post',
|
method: 'post',
|
||||||
headers: {
|
headers: {
|
||||||
@ -23,6 +23,63 @@ export function showInfo(api) {
|
|||||||
document.getElementById("product").innerHTML = data.widget.name
|
document.getElementById("product").innerHTML = data.widget.name
|
||||||
document.getElementById("quantity").innerHTML = data.quantity
|
document.getElementById("quantity").innerHTML = data.quantity
|
||||||
document.getElementById("amount").innerHTML = formatCurrency(data.transaction.amount)
|
document.getElementById("amount").innerHTML = formatCurrency(data.transaction.amount)
|
||||||
|
document.getElementById("pi").value = data.transaction.payment_intent;
|
||||||
|
document.getElementById("charge-amount").value = data.transaction.amount;
|
||||||
|
document.getElementById("currency").value = data.transaction.currency;
|
||||||
|
if (data.status_id === 1) {
|
||||||
|
document.getElementById("refund-btn").classList.remove("d-none");
|
||||||
|
document.getElementById("refunded").classList.add("d-none");
|
||||||
|
document.getElementById("charged").classList.remove("d-none");
|
||||||
|
} else {
|
||||||
|
document.getElementById("refund-btn").classList.add("d-none");
|
||||||
|
document.getElementById("refunded").classList.remove("d-none");
|
||||||
|
document.getElementById("charged").classList.add("d-none");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export function refund(api) {
|
||||||
|
Swal.fire({
|
||||||
|
title: "Are you sure?",
|
||||||
|
text: "You won't be able to undo this!",
|
||||||
|
icon: "warning",
|
||||||
|
showCancelButton: true,
|
||||||
|
confirmButtonColor: "#3085d6",
|
||||||
|
cancelButtonColor: "#d33",
|
||||||
|
confirmButtonText: "Refund"
|
||||||
|
}).then((result) => {
|
||||||
|
if (result.isConfirmed) {
|
||||||
|
let payload = {
|
||||||
|
pi: document.getElementById("pi").value,
|
||||||
|
currency: document.getElementById("currency").value,
|
||||||
|
amount: parseInt(document.getElementById("charge-amount").value, 10),
|
||||||
|
id: parseInt(id, 10),
|
||||||
|
};
|
||||||
|
const requestOptions = {
|
||||||
|
method: 'post',
|
||||||
|
headers: {
|
||||||
|
'Accept': `application/json`,
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Authorization': 'Bearer ' + token,
|
||||||
|
},
|
||||||
|
body: JSON.stringify(payload),
|
||||||
|
};
|
||||||
|
|
||||||
|
fetch(api + "/api/admin/refund", requestOptions)
|
||||||
|
.then(response => response.json())
|
||||||
|
.then(function (data) {
|
||||||
|
console.log(data);
|
||||||
|
if (!data.ok) {
|
||||||
|
showError(data.message)
|
||||||
|
|
||||||
|
} else {
|
||||||
|
showSuccess("messages", "Refunded!")
|
||||||
|
document.getElementById("refund-btn").classList.add("d-none");
|
||||||
|
document.getElementById("refunded").classList.remove("d-none");
|
||||||
|
document.getElementById("charged").classList.add("d-none");
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user