MONEI / Shopify-api-node

Node Shopify connector sponsored by MONEI
https://monei.com/shopify-payment-gateway/
MIT License
947 stars 278 forks source link

Error using fulfillment.createV2 ERR_NON_2XX_3XX_RESPONSE #618

Closed cbetech closed 1 year ago

cbetech commented 1 year ago

Hi, I'have been using the fulfillment.create for a while and eventually started giving me this error:

HTTPError: Response code 404 (Not Found)
    at Request.<anonymous> (/Users/..../node_modules/got/dist/source/as-promise/index.js:118:42)
    at processTicksAndRejections (node:internal/process/task_queues:93:5) {
  code: 'ERR_NON_2XX_3XX_RESPONSE',
  timings: {
    start: 1688757339392,
    socket: 1688757339396,
    lookup: 1688757339408,
    connect: xxx,
    secureConnect: xxx,
    upload: xx,
    response: xx,
    end: xx,
    error: undefined,
    abort: undefined,
    phases: {
      wait: 4,
      dns: 12,
      tcp: 74,
      tls: 89,
      request: 0,
      firstByte: 670,
      download: 16,
      total: 865
    }
  }
}

I tried using createV2 but the error still the same

I'm using node version 15.2.1 Shopify-api-node version latest

This is my code, I'm testing with a order with only one product


var shopify = require('shopify-api-node');

var shop = new shopify({
  shopName: merchant.shopName,
  apiKey:  merchant.apiKey,
  password: merchant.pass,
  apiVersion: '2023-07'
});

var body = {
    location_id: data.location_id,
    tracking_number: data.tracking_number,
    tracking_company: data.tracking_company,
    notify_customer: true,
    order_id: data.order_id,
    line_items_by_fulfillment_order: [{
        fulfillment_order_id: data.order_id,
                fulfillment_order_line_items: [{
                id: data.item[0].id,
                quantity: 1
                 }]
    }]
};

shop.fulfillment.createV2(body).then(function(response){
    console.log(response)
}).catch(function(error){
    console.error(error)    
})
tayyab48 commented 1 year ago

any update on this issue ? I am facing the same problem.

gabrielpaivadev commented 1 year ago

This problem happens here too

tayyab48 commented 1 year ago

there is one mistake in the request:

you should firstly get the fulfillment_order_id then use it into create request.

const fulfillment_order = await shopify.order.fulfillmentOrders('Order ID goes here');

const _fulfillment_order_id = fulfillment_order[0].id;

var body = { location_id: data.location_id, tracking_number: data.tracking_number, tracking_company: data.tracking_company, notify_customer: true, order_id: data.order_id, line_items_by_fulfillment_order: [{ fulfillment_order_id: _fulfillment_order_id fulfillment_order_line_items: fulfillment_order[0].line_items || [] }] };

await shopify.fulfillment.createV2(body);

ikudosi commented 1 year ago

Here's what I have as a param for fulfillments.createV2 for API version 2023-01:

interface FulfillmentOrderTrackingInfo {
  number?: string | number
  url?: string | null
  company?: string | null
}

interface FulfillmentOrderLineItem {
  id: number
  quantity: number
}

interface FulfillmentOrderLineItemsByFulfillmentOrder {
  fulfillment_order_id: number
  fulfillment_order_line_items: FulfillmentOrderLineItem[]
}

interface FulfillmentOrder {
  message: string | null
  notify_customer: boolean
  tracking_info: FulfillmentOrderTrackingInfo
  line_items_by_fulfillment_order: FulfillmentOrderLineItemsByFulfillmentOrder[]
}

// Example
const data: FulfillmentOrder  = {}

await shopifyService.fulfillment.createV2(data)
genuinemx commented 1 year ago

Im still with the same error code: 'ERR_NON_2XX_3XX_RESPONSE',

const fullfilment = await currentShopify.connection.fulfillment.get(order_id)
const fullfillmentId = fullfilment[0].id

    const objetoMapeado = {
        id: fullfilment[0].line_items[0].id,
        quantity: fullfilment[0].line_items[0].quantity
    };

    var body = {
        notify_customer: true,
        tracking_info: { number: "999999999", company: "ACS Courier" },
        line_items_by_fulfillment_order: [
            {
                fulfillment_order_id: fullfillmentId,
                fulfillment_order_line_items: objetoMapeado || [],
            },
        ],
    };
    await currentShopify.connection.fulfillment.insert(body).then(function (response) {
        res.json({ success: response.status })
    }).catch(function (error) {
        logger.error(error.statusMessage)

    })

i have tried with objetoMapeado and also with fullfilment[0].line_items. I had also test with location_id and order_id on the body but still the same error.

this is my insert function:

insert: function(body) {
return shopify.fulfillment.createV2(body);
},
ikudosi commented 1 year ago

@genuinemx Not sure but there may be some syntax / code issues with the snippet you provided.

Can you try to do the following to see if it works? I think your body object is valid so lets try to isolate.

const response = await shopify.fulfillment.createV2(body);
genuinemx commented 1 year ago
fulfillment: {
            insert: async function(body) {
                const response = await shopify.fulfillment.createV2(body);
                return response
            },
        }

I tried as you say, still the same error @ikudosi

ikudosi commented 1 year ago

@genuinemx Can you verify you have the proper scopes enabled for this app to do fulfillments?

Also can you paste the full error? Only the code was provided.

genuinemx commented 1 year ago

@ikudosi the problem was that is not fullfilment[0].id in the cases when a product was fullfilled and then canceled, you need to grab the last line of the line_items where the status is Opened.

Now it works fine! Thanks

cbetech commented 1 year ago

I've found the problem, my main problem was that I was testing with an order that i've been fulfilling and unfulfilling many times, so I was trying this

const fulfillment_order = await shopify.order.fulfillmentOrders('Order ID goes here');
const fulfillment_order_id = fulfillment_order[0].id;

instead, you need to get the fulfillment_order_id from the last element of the array returned from the order.fulfillmentOrders(id) function (the one with the status 'open')

const fulfillment_order = await shopify.order.fulfillmentOrders('Order ID goes here');
const fulfillment_order_id = fulfillment_order[fulfillment_order.length - 1].id;
const fulfillment_order_items = fulfillment_order[fulfillment_order.length - 1].line_items;

and then I used this code

var body = {
   location_id: 'location id goes here',
   order_id: 'order id goes here',
   notify_customer: true,
   tracking_info: { number: "999999999", company: "DHL Express" },
   line_items_by_fulfillment_order: [
    {
        fulfillment_order_id: fulfillment_order_id,
        fulfillment_order_line_items: fulfillment_order_items  || []
    }
   ]
};

const response = await shopify.fulfillment.createV2(body);