udemy-go-web-2/static/js/sale.js
2024-08-22 21:34:58 +02:00

86 lines
3.6 KiB
JavaScript

import { formatCurrency, showError, showSuccess } from "./common.js"
const id = window.location.pathname.split("/").pop();
const token = localStorage.getItem("token");
export function showInfo(api) {
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);
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)
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");
}
}
});
}
export function refund(api, isRefund) {
Swal.fire({
title: "Are you sure?",
text: "You won't be able to undo this!",
icon: "warning",
showCancelButton: true,
confirmButtonColor: "#3085d6",
cancelButtonColor: "#d33",
confirmButtonText: isRefund === 1 ? "Refund" : "Cancel subscription"
}).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, requestOptions)
.then(response => response.json())
.then(function (data) {
console.log(data);
if (!data.ok) {
showError("messages", data.message)
} else {
showSuccess("messages", isRefund === 1 ? "Refunded" : "Subscription cancelled")
document.getElementById("refund-btn").classList.add("d-none");
document.getElementById("refunded").classList.remove("d-none");
document.getElementById("charged").classList.add("d-none");
}
});
}
});
}