From 7635a9826fd74825e817a539c8ecb03c6cb60fdf Mon Sep 17 00:00:00 2001 From: vinchent Date: Thu, 22 Aug 2024 14:05:46 +0200 Subject: [PATCH] refund front 1 --- cmd/api/handlers-api.go | 2 ++ cmd/web/templates/sale.page.gohtml | 12 ++++++-- static/js/sale.js | 47 +++++++++++++++++++++++++++--- 3 files changed, 55 insertions(+), 6 deletions(-) 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); + }); + } + }); +}