YummyPastry

Get Costco Receipts

Nov 23rd, 2025
2,203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. async function listGasReceipts(startDate, endDate) {
  2. return await new Promise(function (resolve, reject) {
  3. var xhr = new XMLHttpRequest();
  4. xhr.responseType = 'json';
  5. xhr.open('POST', 'https://ecom-api.costco.com/ebusiness/order/v1/orders/graphql');
  6. xhr.setRequestHeader('Content-Type', 'application/json-patch+json');
  7. xhr.setRequestHeader('Costco.Env', 'ecom');
  8. xhr.setRequestHeader('Costco.Service', 'restOrders');
  9. xhr.setRequestHeader('Costco-X-Wcs-Clientid', localStorage.getItem('clientID'));
  10. xhr.setRequestHeader('Client-Identifier', '481b1aec-aa3b-454b-b81b-48187e28f205');
  11. xhr.setRequestHeader('Costco-X-Authorization', 'Bearer ' + localStorage.getItem('idToken'));
  12.  
  13. const gasReceiptsQuery = {
  14. "query": `
  15. query receiptsWithCounts($startDate: String!, $endDate: String!) {
  16. receiptsWithCounts(startDate: $startDate, endDate: $endDate) {
  17. receipts {
  18. warehouseName
  19. receiptType
  20. documentType
  21. transactionDateTime
  22. transactionDate
  23. companyNumber
  24. warehouseNumber
  25. operatorNumber
  26. warehouseShortName
  27. registerNumber
  28. transactionNumber
  29. transactionType
  30. transactionBarcode
  31. total
  32. warehouseAddress1
  33. warehouseAddress2
  34. warehouseCity
  35. warehouseState
  36. warehouseCountry
  37. warehousePostalCode
  38. totalItemCount
  39. subTotal
  40. taxes
  41. invoiceNumber
  42. sequenceNumber
  43. itemArray {
  44. itemNumber
  45. itemDescription01
  46. frenchItemDescription1
  47. itemDescription02
  48. frenchItemDescription2
  49. itemIdentifier
  50. itemDepartmentNumber
  51. unit
  52. amount
  53. taxFlag
  54. merchantID
  55. entryMethod
  56. transDepartmentNumber
  57. fuelUnitQuantity
  58. fuelGradeCode
  59. itemUnitPriceAmount
  60. fuelUomCode
  61. fuelUomDescription
  62. fuelUomDescriptionFr
  63. fuelGradeDescription
  64. fuelGradeDescriptionFr
  65. }
  66. tenderArray {
  67. tenderTypeCode
  68. tenderSubTypeCode
  69. tenderDescription
  70. amountTender
  71. displayAccountNumber
  72. sequenceNumber
  73. approvalNumber
  74. responseCode
  75. tenderTypeName
  76. transactionID
  77. merchantID
  78. entryMethod
  79. tenderAcctTxnNumber
  80. tenderAuthorizationCode
  81. tenderTypeNameFr
  82. tenderEntryMethodDescription
  83. walletType
  84. walletId
  85. storedValueBucket
  86. }
  87. subTaxes {
  88. tax1
  89. tax2
  90. tax3
  91. tax4
  92. aTaxPercent
  93. aTaxLegend
  94. aTaxAmount
  95. bTaxPercent
  96. bTaxLegend
  97. bTaxAmount
  98. cTaxPercent
  99. cTaxLegend
  100. cTaxAmount
  101. dTaxAmount
  102. }
  103. instantSavings
  104. membershipNumber
  105. }
  106. }
  107. }`.replace(/\s+/g,' '),
  108. "variables": {
  109. "startDate": startDate,
  110. "endDate": endDate
  111. }
  112. };
  113.  
  114. xhr.onload = function() {
  115. if (xhr.status === 200) {
  116. resolve(xhr.response.data.receiptsWithCounts.receipts);
  117. } else {
  118. reject(xhr.status);
  119. }
  120. };
  121.  
  122. xhr.onerror = function() {
  123. reject('Network Error');
  124. };
  125.  
  126. xhr.send(JSON.stringify(gasReceiptsQuery));
  127. });
  128. }
  129.  
  130. async function downloadGasReceipts() {
  131. var startDateStr = '01/01/2020';
  132. var endDate = new Date();
  133. var endDateStr = endDate.toLocaleDateString('en-US', {
  134. year: "numeric",
  135. month: "2-digit",
  136. day: "2-digit"
  137. });
  138.  
  139. var receipts = await listGasReceipts(startDateStr, endDateStr);
  140. console.log(`Got ${receipts.length} gas receipts, saving.`);
  141.  
  142. var a = document.createElement('a');
  143. a.download = `costco-gas-${endDate.toISOString()}.json`;
  144. a.href = window.URL.createObjectURL(new Blob([JSON.stringify(receipts, null, 2)], {type: 'text/plain'}));
  145. a.target = '_blank';
  146. document.body.appendChild(a);
  147. a.click();
  148. }
  149.  
  150. await downloadGasReceipts();
Advertisement
Add Comment
Please, Sign In to add comment