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

86 lines
3.6 KiB
JavaScript
Raw Normal View History

2024-08-22 12:33:36 +00:00
import { formatCurrency, showError, showSuccess } 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 12:33:36 +00:00
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");
}
2024-08-22 07:56:14 +00:00
}
2024-08-21 21:43:16 +00:00
});
}
2024-08-22 12:05:46 +00:00
2024-08-22 19:34:58 +00:00
export function refund(api, isRefund) {
2024-08-22 12:05:46 +00:00
Swal.fire({
title: "Are you sure?",
text: "You won't be able to undo this!",
icon: "warning",
showCancelButton: true,
confirmButtonColor: "#3085d6",
cancelButtonColor: "#d33",
2024-08-22 19:34:58 +00:00
confirmButtonText: isRefund === 1 ? "Refund" : "Cancel subscription"
2024-08-22 12:05:46 +00:00
}).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),
};
2024-08-22 19:34:58 +00:00
fetch(api, requestOptions)
2024-08-22 12:05:46 +00:00
.then(response => response.json())
.then(function (data) {
console.log(data);
2024-08-22 12:33:36 +00:00
if (!data.ok) {
2024-08-22 19:34:58 +00:00
showError("messages", data.message)
2024-08-22 12:33:36 +00:00
} else {
2024-08-22 19:34:58 +00:00
showSuccess("messages", isRefund === 1 ? "Refunded" : "Subscription cancelled")
2024-08-22 12:33:36 +00:00
document.getElementById("refund-btn").classList.add("d-none");
document.getElementById("refunded").classList.remove("d-none");
document.getElementById("charged").classList.add("d-none");
}
2024-08-22 12:05:46 +00:00
});
}
});
}