Compare commits
No commits in common. "059638f3a70bb45545079d8cb9c16bb34afa60f0" and "479183ea13b72bba7e678780987c43e74d3541a3" have entirely different histories.
059638f3a7
...
479183ea13
@ -291,12 +291,12 @@ func (app *application) CreateAuthToken(w http.ResponseWriter, r *http.Request)
|
||||
// send response
|
||||
|
||||
var payload struct {
|
||||
OK bool `json:"ok"`
|
||||
Error bool `json:"error"`
|
||||
Message string `json:"message"`
|
||||
Token *models.Token `json:"authentication_token"`
|
||||
}
|
||||
|
||||
payload.OK = true
|
||||
payload.Error = false
|
||||
payload.Message = fmt.Sprintf("token for %s created", userInput.Email)
|
||||
payload.Token = token
|
||||
|
||||
@ -338,8 +338,11 @@ func (app *application) CheckAuthentication(w http.ResponseWriter, r *http.Reque
|
||||
}
|
||||
|
||||
// valid user
|
||||
var payload jsonResponse
|
||||
payload.OK = true
|
||||
var payload struct {
|
||||
Error bool `json:"error"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
payload.Error = false
|
||||
payload.Message = fmt.Sprintf("authenticated user %s", user.Email)
|
||||
app.writeJSON(w, http.StatusOK, payload)
|
||||
}
|
||||
@ -420,10 +423,12 @@ func (app *application) SendPasswordResetEmail(w http.ResponseWriter, r *http.Re
|
||||
// verify that email exists
|
||||
_, err = app.DB.GetUserByEmail(payload.Email)
|
||||
if err != nil {
|
||||
resp := jsonResponse{
|
||||
OK: false,
|
||||
Message: "No matching email found on our system",
|
||||
var resp struct {
|
||||
Error bool `json:"error"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
resp.Error = true
|
||||
resp.Message = "No matching email found on our system"
|
||||
app.writeJSON(w, http.StatusAccepted, resp)
|
||||
return
|
||||
}
|
||||
@ -455,8 +460,9 @@ func (app *application) SendPasswordResetEmail(w http.ResponseWriter, r *http.Re
|
||||
return
|
||||
}
|
||||
|
||||
resp := jsonResponse{
|
||||
OK: true,
|
||||
var resp struct {
|
||||
Error bool `json:"error"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
app.writeJSON(w, http.StatusCreated, resp)
|
||||
@ -506,10 +512,12 @@ func (app *application) ResetPassword(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
resp := jsonResponse{
|
||||
OK: true,
|
||||
Message: "Password reset.",
|
||||
var resp struct {
|
||||
Error bool `json:"error"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
resp.Error = false
|
||||
resp.Message = "Password reset."
|
||||
app.writeJSON(w, http.StatusCreated, resp)
|
||||
}
|
||||
|
||||
@ -539,51 +547,3 @@ 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.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,7 +35,6 @@ 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)
|
||||
|
@ -12,7 +12,6 @@ All Sales
|
||||
<th>Customer</th>
|
||||
<th>Product</th>
|
||||
<th>Amount</th>
|
||||
<th>Status</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
|
@ -1,39 +1,26 @@
|
||||
{{ template "base" . }}
|
||||
{{ define "title" }}
|
||||
{{ index .StringMap "title" }}
|
||||
{{ end }}
|
||||
{{ define "content" }}
|
||||
{{template "base" .}}
|
||||
|
||||
{{define "title"}}
|
||||
{{index .StringMap "title"}}
|
||||
{{end}}
|
||||
{{define "content"}}
|
||||
<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>
|
||||
<div class="alert alert-danger text-center d-none" id="messages"></div>
|
||||
<div>
|
||||
<strong>Order No:</strong> <span id="order-no"></span>
|
||||
<br>
|
||||
<strong>Customer:</strong> <span id="customer"></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>
|
||||
<strong>Order No:</strong> <span id="order-no"></span><br>
|
||||
<strong>Customer:</strong> <span id="customer"></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>
|
||||
<hr>
|
||||
<a href='{{ index .StringMap "cancel" }}' class="btn btn-info">Cancel</a>
|
||||
<a id="refund-btn" href="#!" class="btn btn-warning d-none">Refund Order</a>
|
||||
<input type="hidden" id="pi" value="">
|
||||
<input type="hidden" id="charge-amount" value="">
|
||||
<input type="hidden" id="currency" value="">
|
||||
{{ end }}
|
||||
{{ define "js" }}
|
||||
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
|
||||
<a href='{{index .StringMap "cancel"}}' class="btn btn-info">Cancel</a>
|
||||
<a href="#!" class="btn btn-warning">Refund Order</a>
|
||||
{{end}}
|
||||
|
||||
{{define "js"}}
|
||||
<script type="module">
|
||||
import {showInfo, refund} from "/static/js/sale.js"
|
||||
import {showInfo} from "/static/js/sale.js"
|
||||
showInfo({{.API}});
|
||||
document.getElementById("refund-btn").addEventListener("click", function(event) {
|
||||
refund({{.API}});
|
||||
});
|
||||
</script>
|
||||
{{ end }}
|
||||
{{end}}
|
||||
|
@ -5,7 +5,6 @@ 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"
|
||||
)
|
||||
|
||||
@ -119,22 +118,6 @@ 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 := ""
|
||||
|
||||
|
@ -413,16 +413,3 @@ func (m *DBModel) GetOrderByID(ID int) (Order, error) {
|
||||
}
|
||||
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) {
|
||||
let token = localStorage.getItem("token");
|
||||
@ -17,32 +17,26 @@ export function showTable(api) {
|
||||
.then(response => response.json())
|
||||
.then(function (data) {
|
||||
if (data) {
|
||||
data.forEach(function (i) {
|
||||
let newRow = tbody.insertRow();
|
||||
let newCell = newRow.insertCell();
|
||||
data.forEach(function (i) {
|
||||
let newRow = tbody.insertRow();
|
||||
let newCell = newRow.insertCell();
|
||||
|
||||
newCell.innerHTML = `<a href="/admin/sales/${i.id}">Order ${i.id}</a>`;
|
||||
newCell.innerHTML = `<a href="/admin/sales/${i.id}">Order ${i.id}</a>`;
|
||||
|
||||
newCell = newRow.insertCell();
|
||||
let item = document.createTextNode(i.customer.last_name + ", " + i.customer.first_name);
|
||||
newCell.appendChild(item)
|
||||
newCell = newRow.insertCell();
|
||||
let item = document.createTextNode(i.customer.last_name + ", " + i.customer.first_name);
|
||||
newCell.appendChild(item)
|
||||
|
||||
newCell = newRow.insertCell();
|
||||
item = document.createTextNode(i.widget.name);
|
||||
newCell.appendChild(item)
|
||||
newCell = newRow.insertCell();
|
||||
item = document.createTextNode(i.widget.name);
|
||||
newCell.appendChild(item)
|
||||
|
||||
let cur = formatCurrency(i.transaction.amount)
|
||||
newCell = newRow.insertCell();
|
||||
item = document.createTextNode(cur);
|
||||
newCell.appendChild(item)
|
||||
let cur = formatCurrency(i.transaction.amount)
|
||||
newCell = newRow.insertCell();
|
||||
item = document.createTextNode(cur);
|
||||
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 {
|
||||
let newRow = tbody.insertRow();
|
||||
let newCell = newRow.insertCell();
|
||||
|
@ -81,7 +81,7 @@ export function checkAuth(api) {
|
||||
fetch(api + "/api/is-authenticated", requestOptions)
|
||||
.then(response => response.json())
|
||||
.then(function(data) {
|
||||
if (data.ok === false) {
|
||||
if (data.error === true) {
|
||||
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.ok === true) {
|
||||
if (response.error === false) {
|
||||
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.ok === true) {
|
||||
if (response.error === false) {
|
||||
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.ok === true) {
|
||||
if (response.error === false) {
|
||||
showSuccess("reset-messages", "Password reset")
|
||||
setTimeout(function () {
|
||||
location.href = "/login"
|
||||
|
@ -1,9 +1,9 @@
|
||||
import { formatCurrency, showError, showSuccess } from "./common.js"
|
||||
|
||||
const id = window.location.pathname.split("/").pop();
|
||||
const token = localStorage.getItem("token");
|
||||
import {formatCurrency} from "./common.js"
|
||||
|
||||
export function showInfo(api) {
|
||||
let token = localStorage.getItem("token");
|
||||
let id = window.location.pathname.split("/").pop();
|
||||
|
||||
const requestOptions = {
|
||||
method: 'post',
|
||||
headers: {
|
||||
@ -23,63 +23,6 @@ export function showInfo(api) {
|
||||
document.getElementById("product").innerHTML = data.widget.name
|
||||
document.getElementById("quantity").innerHTML = data.quantity
|
||||
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