diff --git a/cmd/api/handlers-api.go b/cmd/api/handlers-api.go
index 31f6e0e..a0d1e49 100644
--- a/cmd/api/handlers-api.go
+++ b/cmd/api/handlers-api.go
@@ -550,6 +550,7 @@ func (app *application) RefundCharge(w http.ResponseWriter, r *http.Request) {
err := app.readJSON(w, r, &chargeToRefund)
if err != nil {
+ app.errorLog.Println(err)
app.badRequest(w, r, err)
return
}
@@ -564,6 +565,7 @@ func (app *application) RefundCharge(w http.ResponseWriter, r *http.Request) {
err = card.Refund(chargeToRefund.PaymentIntent, chargeToRefund.Amount)
if err != nil {
+ app.errorLog.Println(err)
app.badRequest(w, r, err)
return
}
diff --git a/cmd/web/templates/sale.page.gohtml b/cmd/web/templates/sale.page.gohtml
index 0444f39..65e4b4d 100644
--- a/cmd/web/templates/sale.page.gohtml
+++ b/cmd/web/templates/sale.page.gohtml
@@ -15,12 +15,20 @@
Cancel
-Refund Order
+Refund Order
+
+
+
+
{{end}}
{{define "js"}}
+
{{end}}
diff --git a/static/js/sale.js b/static/js/sale.js
index 7756ece..0f27422 100644
--- a/static/js/sale.js
+++ b/static/js/sale.js
@@ -1,9 +1,9 @@
-import {formatCurrency} from "./common.js"
+import { formatCurrency } from "./common.js"
+
+const id = window.location.pathname.split("/").pop();
+const token = localStorage.getItem("token");
export function showInfo(api) {
- let token = localStorage.getItem("token");
- let id = window.location.pathname.split("/").pop();
-
const requestOptions = {
method: 'post',
headers: {
@@ -23,6 +23,45 @@ 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;
}
});
}
+
+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);
+ });
+ }
+ });
+}