forked from amer2004/SweetHome
Edit payment with haram
This commit is contained in:
125
app/utils/api.js
125
app/utils/api.js
@ -1,8 +1,7 @@
|
||||
import AuthService from "../services/AuthService";
|
||||
const API_BASE =
|
||||
process.env.NEXT_PUBLIC_API_URL || "https://45.93.137.91.nip.io/api";
|
||||
const REPORT_API_BASE =
|
||||
process.env.NEXT_PUBLIC_API_URL || "http://45.93.137.91/api";
|
||||
|
||||
const API_BASE = process.env.NEXT_PUBLIC_API_URL || "https://45.93.137.91.nip.io/api";
|
||||
const REPORT_API_BASE = process.env.NEXT_PUBLIC_REPORT_API_URL || "http://45.93.137.91/api";
|
||||
|
||||
function isFormData(value) {
|
||||
return typeof FormData !== "undefined" && value instanceof FormData;
|
||||
@ -65,33 +64,42 @@ async function apiFetch(endpoint, options = {}) {
|
||||
|
||||
const url = `${API_BASE}${endpoint}`;
|
||||
|
||||
const res = await fetch(url, {
|
||||
...options,
|
||||
headers,
|
||||
body:
|
||||
hasBody && !bodyIsFormData && typeof options.body !== "string"
|
||||
? JSON.stringify(options.body)
|
||||
: options.body,
|
||||
});
|
||||
|
||||
assertNotBlocked(res);
|
||||
|
||||
if (!res.ok && res.status !== 206) {
|
||||
const text = await res.text().catch(() => "");
|
||||
throw new Error(`API ${res.status}: ${text || res.statusText}`);
|
||||
}
|
||||
|
||||
const text = await res.text();
|
||||
if (!text) return null;
|
||||
|
||||
try {
|
||||
const json = JSON.parse(text);
|
||||
if (json && typeof json === "object" && "data" in json) {
|
||||
return json.data;
|
||||
const res = await fetch(url, {
|
||||
...options,
|
||||
headers,
|
||||
body:
|
||||
hasBody && !bodyIsFormData && typeof options.body !== "string"
|
||||
? JSON.stringify(options.body)
|
||||
: options.body,
|
||||
});
|
||||
|
||||
assertNotBlocked(res);
|
||||
|
||||
if (!res.ok && res.status !== 206) {
|
||||
const text = await res.text().catch(() => "");
|
||||
throw new Error(`API ${res.status}: ${text || res.statusText}`);
|
||||
}
|
||||
return json;
|
||||
} catch {
|
||||
return text;
|
||||
|
||||
const text = await res.text();
|
||||
if (!text) return null;
|
||||
|
||||
try {
|
||||
const json = JSON.parse(text);
|
||||
if (json && typeof json === "object" && "data" in json) {
|
||||
return json.data;
|
||||
}
|
||||
return json;
|
||||
} catch {
|
||||
return text;
|
||||
}
|
||||
} catch (error) {
|
||||
if (error instanceof TypeError && error.message === 'Failed to fetch') {
|
||||
throw new Error(
|
||||
`تعذر الاتصال بالخادم. تحقق من اتصالك بالإنترنت أو حاول مرة أخرى لاحقاً. (${API_BASE}${endpoint})`,
|
||||
);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
@ -110,11 +118,16 @@ async function authFetch(endpoint, body, token = null) {
|
||||
headers["Authorization"] = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
const res = await fetch(`${API_BASE}${endpoint}`, {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: bodyIsFormData ? body : JSON.stringify(body),
|
||||
});
|
||||
let res;
|
||||
try {
|
||||
res = await fetch(`${API_BASE}${endpoint}`, {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: bodyIsFormData ? body : JSON.stringify(body),
|
||||
});
|
||||
} catch (err) {
|
||||
return { status: 0, data: null, ok: false, message: err.message };
|
||||
}
|
||||
|
||||
assertNotBlocked(res);
|
||||
|
||||
@ -142,13 +155,18 @@ async function authFetch(endpoint, body, token = null) {
|
||||
}
|
||||
|
||||
async function reportFetch(endpoint, body) {
|
||||
const res = await fetch(buildApiUrl(REPORT_API_BASE, endpoint), {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
let res;
|
||||
try {
|
||||
res = await fetch(buildApiUrl(REPORT_API_BASE, endpoint), {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
} catch (err) {
|
||||
return { status: 0, data: null, ok: false, message: err.message };
|
||||
}
|
||||
|
||||
assertNotBlocked(res);
|
||||
|
||||
@ -392,13 +410,18 @@ export async function uploadPicture(file) {
|
||||
|
||||
const token = AuthService.getToken();
|
||||
|
||||
const res = await fetch(`${API_BASE}/Files/UploadPicture`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
...(token && { Authorization: `Bearer ${token}` }),
|
||||
},
|
||||
body: formData,
|
||||
});
|
||||
let res;
|
||||
try {
|
||||
res = await fetch(`${API_BASE}/Files/UploadPicture`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
...(token && { Authorization: `Bearer ${token}` }),
|
||||
},
|
||||
body: formData,
|
||||
});
|
||||
} catch (err) {
|
||||
throw new Error(`تعذر الاتصال بالخادم: ${err.message}`);
|
||||
}
|
||||
|
||||
assertNotBlocked(res);
|
||||
|
||||
@ -604,7 +627,6 @@ export async function adminConfirmDeposit(
|
||||
comment = null,
|
||||
) {
|
||||
const token = AuthService.getToken();
|
||||
const endpoint = `${API_BASE}/Reservations/AdminConfirmDeposit/admin-confirm-deposit`;
|
||||
const normalizedComment =
|
||||
typeof comment === "string" && comment.trim() ? comment.trim() : null;
|
||||
const payload = {
|
||||
@ -613,7 +635,7 @@ export async function adminConfirmDeposit(
|
||||
comment: normalizedComment,
|
||||
};
|
||||
|
||||
const res = await fetch(endpoint, {
|
||||
const res = await fetch(buildApiUrl(API_BASE, "/Reservations/AdminConfirmDeposit/admin-confirm-deposit"), {
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
@ -672,6 +694,9 @@ export async function ownerConfirmReservation(id) {
|
||||
|
||||
// ─── Payments ───
|
||||
|
||||
export async function getMyTransaction() {
|
||||
return apiFetch("/Customer/GetMyTransaction");
|
||||
}
|
||||
|
||||
export async function payDeposit(data) {
|
||||
const formData = new FormData();
|
||||
|
||||
Reference in New Issue
Block a user