udemy-go-web-2/static/js/sale.js

68 lines
2.5 KiB
JavaScript
Raw Normal View History

2024-08-22 12:05:46 +00:00
import { formatCurrency } from "./common.js"
2024-08-22 08:01:54 +00:00
2024-08-22 12:05:46 +00:00
const id = window.location.pathname.split("/").pop();
const token = localStorage.getItem("token");
2024-08-21 21:43:16 +00:00
2024-08-22 12:05:46 +00:00
export function showInfo(api) {
2024-08-21 21:43:16 +00:00
const requestOptions = {
method: 'post',
headers: {
'Accept': `application/json`,
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + token,
},
};
fetch(api + "/api/admin/get-sale/" + id, requestOptions)
.then(response => response.json())
.then(function (data) {
console.log(data);
2024-08-22 07:56:14 +00:00
if (data) {
document.getElementById("order-no").innerHTML = data.id
document.getElementById("customer").innerHTML = data.customer.first_name + " " + data.customer.last_name
document.getElementById("product").innerHTML = data.widget.name
document.getElementById("quantity").innerHTML = data.quantity
document.getElementById("amount").innerHTML = formatCurrency(data.transaction.amount)
2024-08-22 12:05:46 +00:00
document.getElementById("pi").value = data.transaction.payment_intent;
document.getElementById("charge-amount").value = data.transaction.amount;
document.getElementById("currency").value = data.transaction.currency;
2024-08-22 07:56:14 +00:00
}
2024-08-21 21:43:16 +00:00
});
}
2024-08-22 12:05:46 +00:00
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);
});
}
});
}