From 059638f3a70bb45545079d8cb9c16bb34afa60f0 Mon Sep 17 00:00:00 2001 From: vinchent Date: Thu, 22 Aug 2024 14:33:36 +0200 Subject: [PATCH] finish refund --- cmd/api/handlers-api.go | 11 +++++++ cmd/web/templates/all-sales.page.gohtml | 1 + cmd/web/templates/sale.page.gohtml | 41 ++++++++++++++----------- internal/models/models.go | 13 ++++++++ static/js/all-sales.js | 38 +++++++++++++---------- static/js/sale.js | 20 +++++++++++- 6 files changed, 89 insertions(+), 35 deletions(-) diff --git a/cmd/api/handlers-api.go b/cmd/api/handlers-api.go index a0d1e49..f812bc4 100644 --- a/cmd/api/handlers-api.go +++ b/cmd/api/handlers-api.go @@ -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" diff --git a/cmd/web/templates/all-sales.page.gohtml b/cmd/web/templates/all-sales.page.gohtml index 05009fc..db60084 100644 --- a/cmd/web/templates/all-sales.page.gohtml +++ b/cmd/web/templates/all-sales.page.gohtml @@ -12,6 +12,7 @@ All Sales Customer Product Amount + Status diff --git a/cmd/web/templates/sale.page.gohtml b/cmd/web/templates/sale.page.gohtml index 65e4b4d..741f0a5 100644 --- a/cmd/web/templates/sale.page.gohtml +++ b/cmd/web/templates/sale.page.gohtml @@ -1,28 +1,33 @@ -{{template "base" .}} - -{{define "title"}} -{{index .StringMap "title"}} -{{end}} -{{define "content"}} +{{ template "base" . }} +{{ define "title" }} +{{ index .StringMap "title" }} +{{ end }} +{{ define "content" }}

Sale

+Refunded +Charged
+
- Order No:
- Customer:
- Product:
- Quantity:
- Total Sale:
+ Order No: +
+ Customer: +
+ Product: +
+ Quantity: +
+ Total Sale: +

-Cancel -Refund Order +Cancel +Refund Order - -{{end}} - -{{define "js"}} +{{ end }} +{{ define "js" }} -{{end}} +{{ end }} diff --git a/internal/models/models.go b/internal/models/models.go index 235b3c2..51875e1 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -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 +} diff --git a/static/js/all-sales.js b/static/js/all-sales.js index 3fa23aa..bb3be29 100644 --- a/static/js/all-sales.js +++ b/static/js/all-sales.js @@ -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 = `Order ${i.id}`; + newCell.innerHTML = `Order ${i.id}`; - 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 = `Refunded` + } else { + newCell.innerHTML = `Charged` + } + }); } else { let newRow = tbody.insertRow(); let newCell = newRow.insertCell(); diff --git a/static/js/sale.js b/static/js/sale.js index 0f27422..d6cb434 100644 --- a/static/js/sale.js +++ b/static/js/sale.js @@ -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"); + } }); } });