refund front 1

This commit is contained in:
vinchent 2024-08-22 14:05:46 +02:00
parent d271e23861
commit 7635a9826f
3 changed files with 55 additions and 6 deletions

View File

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

View File

@ -15,12 +15,20 @@
</div>
<hr>
<a href='{{index .StringMap "cancel"}}' class="btn btn-info">Cancel</a>
<a href="#!" class="btn btn-warning">Refund Order</a>
<a id="refund-btn" href="#!" class="btn btn-warning">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"}}
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script type="module">
import {showInfo} from "/static/js/sale.js"
import {showInfo, refund} from "/static/js/sale.js"
showInfo({{.API}});
document.getElementById("refund-btn").addEventListener("click", function(event) {
refund({{.API}});
});
</script>
{{end}}

View File

@ -1,9 +1,9 @@
import { formatCurrency } from "./common.js"
export function showInfo(api) {
let token = localStorage.getItem("token");
let id = window.location.pathname.split("/").pop();
const id = window.location.pathname.split("/").pop();
const token = localStorage.getItem("token");
export function showInfo(api) {
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);
});
}
});
}