Items can officially be posted to eBay

This commit is contained in:
2024-03-26 15:58:54 -05:00
parent 7646ee0fc8
commit 04a3dba994
3 changed files with 84 additions and 53 deletions

View File

@@ -6,11 +6,13 @@ export const createAndListItem = async (req, res) => {
sku,
marketplaceId,
format,
listingDuration,
availableQuantity,
categoryId,
condition,
listingDescription,
pricingSummary,
mpn,
merchantLocationKey,
quantityLimitPerBuyer,
product, // Extract the entire product object
@@ -20,7 +22,7 @@ export const createAndListItem = async (req, res) => {
const token = await fetchEbayUserToken(req, res); // Get the eBay user token
// only need these if payloads is there?
let dynamicAspects = {};
for (const [key, value] of Object.entries(product.aspects)) {
dynamicAspects[key] = Array.isArray(value) ? value : [value];
@@ -31,19 +33,30 @@ export const createAndListItem = async (req, res) => {
const inventoryItemPayload = {
sku,
condition,
quantity: availableQuantity,
product: {
title: product.title.substring(0, 80), // Utilizing product title
brand: product.brand, // Using brand from the product object
mpn: mpn,
description: product.description, // Using product description
imageUrls: product.imageUrls, // Using all imageUrls from product
aspects: dynamicAspects
},
availability: {
shipToLocationAvailability: {
quantity: availableQuantity,
},
},
// Package weight and dimensions, adjusted to the new structure
availability: {
shipToLocationAvailability: {
quantity: availableQuantity,
availabilityDistributions: [ // Adding availability distributions
{
availabilityType: "IN_STOCK", // Assuming items are in stock for pickup
fulfillmentTime: { // Fulfillment time for shipping
unit: "DAY",
value: 1 // Adjust based on your shipping fulfillment time
},
merchantLocationKey: merchantLocationKey, // Specify the location for shipping availability
quantity: availableQuantity // Quantity available for shipping from the specified location
}
]
},
},
packageWeightAndSize: {
weight: {
value: parseFloat(packageWeightAndSize.weight.value),
@@ -74,22 +87,28 @@ export const createAndListItem = async (req, res) => {
// Optionally, you can parse the response body if eBay's API returns useful information in it
const itemResponseData = await createItemResponse;
console.log(itemResponseData);
// 2. Create the offer for the inventory item
const offerPayload = {
sku,
marketplaceId,
format,
listing: {
categoryId,
listingDescription,
merchantLocationKey,
format, // This is equivalent to format: format,
categoryId, // Moved to top-level, outside of the 'listing' object
listingDescription, // Moved to top-level, outside of the 'listing' object
merchantLocationKey, // Moved to top-level, outside of the 'listing' object
pricingSummary: {
price: pricingSummary.price,
},
pricingSummary,
quantityLimitPerBuyer,
availableQuantity,
listingDuration,
listingPolicies: { // Include the listing policies with their respective IDs
fulfillmentPolicyId: "246576176016",
returnPolicyId: "246576188016",
paymentPolicyId: "246576154016",
},
// Additional fields from the sample payload can be included here as needed
};
const offerResponse = await fetch(
`https://api.ebay.com/sell/inventory/v1/offer`,
{
@@ -103,6 +122,7 @@ export const createAndListItem = async (req, res) => {
}
);
if (!offerResponse.ok) {
// Handle error in creating the offer
const errorBody = await offerResponse.text();
@@ -113,18 +133,23 @@ export const createAndListItem = async (req, res) => {
const offerData = await offerResponse.json();
const offerId = offerData.offerId;
console.log(offerId)
// 3. Publish the offer to convert it into a live listing
await fetch(
`https://api.ebay.com/sell/inventory/v1/offer/${offerId}/publish/`,
const publishOfferResponse = await fetch(
`https://api.ebay.com/sell/inventory/v1/offer/${offerId}/publish/`, // Ensure the offerId is correctly inserted
{
method: "POST",
headers: {
Authorization: `Bearer ${token}`,
Authorization: `Bearer ${token}`, // Ensure your token is valid
"Content-Type": "application/json",
},
}
);
const responseBody = await publishOfferResponse.json(); // Assuming the response will be in JSON format
console.log(responseBody);
res.json({ success: true, message: "Item listed successfully" });
} catch (error) {
@@ -133,7 +158,7 @@ export const createAndListItem = async (req, res) => {
success: false,
message: "Failed to create and list item on eBay",
error: error.message,
});
})
}
};
@@ -203,3 +228,38 @@ export const getAllInventory = async (req, res) => {
}
};
export const getOffers = async (req, res) => {
const sku = "LEMUG2022212234120245"; // Assuming this is your SKU
const offerId = 418806088016
try {
const token = await fetchEbayUserToken(req, res); // Assuming fetchEbayUserToken handles token retrieval internally
const response = await fetch('https://api.ebay.com/commerce/taxonomy/v1/get_default_category_tree_id?marketplace_id=EBAY_US', {
method: 'GET',
headers: {
"Content-Type": "application/json",
"Content-Language": "en-US",
"Authorization": `Bearer ${token}`,
},
});
console.log(response)
const offers = await response.json(); // Parsing the JSON body of the response
res.json(offers); // Sending the offers back to the client
} catch (error) {
console.error('Error fetching offers:', error);
res.status(500).json({
error: 'Internal Server Error',
message: error.message,
});
}
};
// fulfill policy = 246576176016
// return policy = 246576188016
// payment policy = 246576154016