Skip to content

Instantly share code, notes, and snippets.

@neopheus
Created December 8, 2023 16:03
Show Gist options
  • Save neopheus/e4fe5ed6715a3b5a8c2528a5a2ff6577 to your computer and use it in GitHub Desktop.
Save neopheus/e4fe5ed6715a3b5a8c2528a5a2ff6577 to your computer and use it in GitHub Desktop.
Uber Eats Download Invoice
const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
const downloadInvoice = async (orderUUID) => {
try {
console.log(`Downloading invoice for order UUID: ${orderUUID}`);
const invoiceResponse = await fetch('https://www.ubereats.com/_p/api/getInvoiceFilesV1?localeCode=fr', {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'x-csrf-token': 'x' }, // Remplacez 'x' par votre token CSRF réel
body: JSON.stringify({ orderUUID }),
});
const invoiceData = await invoiceResponse.json();
if (invoiceData.status === "success" && invoiceData.data.files) {
invoiceData.data.files.forEach(file => {
const a = document.createElement("a");
a.href = file.downloadURL;
a.download = ""; // Le nom du fichier sera déduit de l'URL
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(a.href);
a.remove();
});
console.log(`Invoice for order ${orderUUID} downloaded.`);
}
await delay(1000); // Attente de 1 seconde entre chaque téléchargement
} catch (error) {
console.error('Error downloading invoice:', error);
}
};
const fetchAllOrders = async (lastWorkflowUUID = '') => {
console.log('Fetching orders...');
const res = await fetch('/api/getPastOrdersV1?localeCode=fr', {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'x-csrf-token': 'x' },
credentials: 'include',
body: JSON.stringify({ lastWorkflowUUID }),
});
const { data } = await res.json();
for (const order of Object.values(data.ordersMap)) {
await downloadInvoice(order.baseEaterOrder?.uuid);
}
console.log('Batch of orders processed.');
if (data.meta?.hasMore) {
const lastOrder = Object.values(data.ordersMap).pop();
await delay(5000); // Attente de 5 secondes avant de récupérer le prochain lot
return fetchAllOrders(lastOrder.baseEaterOrder?.uuid);
} else {
console.log('All orders processed and invoices downloaded.');
}
};
fetchAllOrders();
@neopheus
Copy link
Author

neopheus commented Oct 18, 2024

const delay = ms => new Promise(resolve => setTimeout(resolve, ms));

// Fonction pour demander la génération d'une facture
const requestInvoice = async (orderUUID) => {
  try {
    console.log(`Requesting invoice for order UUID: ${orderUUID}`);
    const requestResponse = await fetch('https://www.ubereats.com/_p/api/requestInvoiceV3?localeCode=fr', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json', 'x-csrf-token': 'x' }, // Remplacez 'x' par votre token CSRF réel
      body: JSON.stringify({ orderUUID }),
    });

    const requestData = await requestResponse.json();

    if (requestData.status === "success") {
      console.log(`Invoice request for order ${orderUUID} was successful.`);
      return true; // Succès de la demande de facture
    } else {
      console.error(`Failed to request invoice for order ${orderUUID}`);
      return false; // Échec de la demande
    }
  } catch (error) {
    console.error('Error requesting invoice:', error);
    return false; // Échec en cas d'erreur
  }
};

// Fonction pour télécharger la facture
const downloadInvoice = async (orderUUID) => {
  try {
    console.log(`Downloading invoice for order UUID: ${orderUUID}`);
    const invoiceResponse = await fetch('https://www.ubereats.com/_p/api/getInvoiceFilesV1?localeCode=fr', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json', 'x-csrf-token': 'x' }, // Remplacez 'x' par votre token CSRF réel
      body: JSON.stringify({ orderUUID }),
    });

    const invoiceData = await invoiceResponse.json();

    if (invoiceData.status === "success" && invoiceData.data.files) {
      invoiceData.data.files.forEach(file => {
        const a = document.createElement("a");
        a.href = file.downloadURL;
        a.download = ""; // Le nom du fichier sera déduit de l'URL
        document.body.appendChild(a);
        a.click();
        window.URL.revokeObjectURL(a.href);
        a.remove();
      });
      console.log(`Invoice for order ${orderUUID} downloaded.`);
    }
    await delay(1000); // Attente de 1 seconde entre chaque téléchargement
  } catch (error) {
    console.error('Error downloading invoice:', error);
  }
};

// Fonction pour récupérer toutes les commandes et demander puis télécharger les factures
const fetchAllOrders = async (lastWorkflowUUID = '') => {
  console.log('Fetching orders...');
  const res = await fetch('/api/getPastOrdersV1?localeCode=fr', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json', 'x-csrf-token': 'x' }, 
    credentials: 'include',
    body: JSON.stringify({ lastWorkflowUUID }),
  });
  const { data } = await res.json();

  for (const order of Object.values(data.ordersMap)) {
    const orderUUID = order.baseEaterOrder?.uuid;

    // D'abord, demandez la facture
    const invoiceRequested = await requestInvoice(orderUUID);
    if (invoiceRequested) {
      // Si la demande a réussi, téléchargez la facture
      await downloadInvoice(orderUUID);
    }
  }

  console.log('Batch of orders processed.');

  if (data.meta?.hasMore) {
    const lastOrder = Object.values(data.ordersMap).pop();
    await delay(5000); // Attente de 5 secondes avant de récupérer le prochain lot
    return fetchAllOrders(lastOrder.baseEaterOrder?.uuid);
  } else {
    console.log('All orders processed and invoices downloaded.');
  }
};

fetchAllOrders();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment