finish refund

This commit is contained in:
vinchent 2024-08-22 14:33:36 +02:00
parent 7635a9826f
commit 059638f3a7
6 changed files with 89 additions and 35 deletions

View File

@ -570,6 +570,17 @@ func (app *application) RefundCharge(w http.ResponseWriter, r *http.Request) {
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"

View File

@ -12,6 +12,7 @@ All Sales
<th>Customer</th>
<th>Product</th>
<th>Amount</th>
<th>Status</th>
</tr>
</thead>
<tbody>

View File

@ -1,28 +1,33 @@
{{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">Refund Order</a>
<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"}}
{{ end }}
{{ define "js" }}
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script type="module">
import {showInfo, refund} from "/static/js/sale.js"
@ -31,4 +36,4 @@
refund({{.API}});
});
</script>
{{end}}
{{ end }}

View File

@ -413,3 +413,16 @@ 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
}

View File

@ -1,4 +1,4 @@
import {formatCurrency} from "./common.js"
import { formatCurrency } from "./common.js"
export function showTable(api) {
let token = localStorage.getItem("token");
@ -17,26 +17,32 @@ 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();

View File

@ -1,4 +1,4 @@
import { formatCurrency } from "./common.js"
import { formatCurrency, showError, showSuccess } from "./common.js"
const id = window.location.pathname.split("/").pop();
const token = localStorage.getItem("token");
@ -26,6 +26,15 @@ export function showInfo(api) {
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");
}
}
});
}
@ -61,6 +70,15 @@ export function refund(api) {
.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");
}
});
}
});