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 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 var resp jsonResponse
resp.OK = true resp.OK = true
resp.Message = "Charge refunded" resp.Message = "Charge refunded"

View File

@ -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>

View File

@ -1,28 +1,33 @@
{{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 id="refund-btn" href="#!" class="btn btn-warning">Refund Order</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="pi" value="">
<input type="hidden" id="charge-amount" value=""> <input type="hidden" id="charge-amount" value="">
<input type="hidden" id="currency" value=""> <input type="hidden" id="currency" value="">
{{ end }}
{{end}} {{ define "js" }}
{{define "js"}}
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script> <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script type="module"> <script type="module">
import {showInfo, refund} from "/static/js/sale.js" import {showInfo, refund} from "/static/js/sale.js"
@ -31,4 +36,4 @@
refund({{.API}}); refund({{.API}});
}); });
</script> </script>
{{end}} {{ end }}

View File

@ -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
}

View File

@ -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");
@ -17,26 +17,32 @@ export function showTable(api) {
.then(response => response.json()) .then(response => response.json())
.then(function (data) { .then(function (data) {
if (data) { if (data) {
data.forEach(function (i) { data.forEach(function (i) {
let newRow = tbody.insertRow(); let newRow = tbody.insertRow();
let newCell = newRow.insertCell(); 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(); newCell = newRow.insertCell();
let item = document.createTextNode(i.customer.last_name + ", " + i.customer.first_name); let item = document.createTextNode(i.customer.last_name + ", " + i.customer.first_name);
newCell.appendChild(item) newCell.appendChild(item)
newCell = newRow.insertCell(); newCell = newRow.insertCell();
item = document.createTextNode(i.widget.name); item = document.createTextNode(i.widget.name);
newCell.appendChild(item) newCell.appendChild(item)
let cur = formatCurrency(i.transaction.amount) let cur = formatCurrency(i.transaction.amount)
newCell = newRow.insertCell(); newCell = newRow.insertCell();
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();
let newCell = newRow.insertCell(); 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 id = window.location.pathname.split("/").pop();
const token = localStorage.getItem("token"); const token = localStorage.getItem("token");
@ -26,6 +26,15 @@ export function showInfo(api) {
document.getElementById("pi").value = data.transaction.payment_intent; document.getElementById("pi").value = data.transaction.payment_intent;
document.getElementById("charge-amount").value = data.transaction.amount; document.getElementById("charge-amount").value = data.transaction.amount;
document.getElementById("currency").value = data.transaction.currency; 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(response => response.json())
.then(function (data) { .then(function (data) {
console.log(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");
}
}); });
} }
}); });