MONEI / Shopify-api-node

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

Updating order #166

Closed corbinu closed 6 years ago

corbinu commented 6 years ago

I am currently transitioning to this module. It works great other than when I attempt to put and order update (specifically alter the order notes). The transaction succeeds but the order is not actually updated on Shopify's end.

I tried to find some reason for this in the codebase and came up empty. I can create a reproduction but would mean creating a private Shopify app and would need someway to securely let one of you maintainers have the credentials to install in a test store.

Thanks!

lpinca commented 6 years ago

Does it work if you don't use this library? I mean, are the notes updated if you use curl or https://insomnia.rest/ or another HTTP client?

If it works we should see what's different in the request and fix it accordingly.

corbinu commented 6 years ago

Yes it always worked when I was using the shopify-node module

corbinu commented 6 years ago

This is what I was using before: https://www.npmjs.com/package/shopify-node

I have replaced all uses of it except for updating the order notes

lpinca commented 6 years ago

I don't have a test shop and an app to debug this atm so I can only suggest you to inspect the request made by both modules and see where they differ. You can use a debugging proxy like Fiddler for example.

corbinu commented 6 years ago

Ok will do

olivej commented 6 years ago

I can confirm that I am having same problem. I cannot update any fields on Draft Orders or Order objects (haven't tried any others).

lpinca commented 6 years ago

I can't reproduce. Here is my test case using a private app.

(async function () {
  const { note } = await shopify.order.update(orderId, {
    id: orderId,
    note: 'This is a note'
  });

  console.log(note);
})().catch((err) => console.log(err.response.body));

The order note is updated correctly. Changes are reflected in the admin dashboard.

corbinu commented 6 years ago

@lpinca Sorry haven't had a chance to look at this yet been swamped all weekend. I should have been more clear it is note_attributes not note that I am trying to update.

lpinca commented 6 years ago

Ok, I'll try again when I get a chance.

corbinu commented 6 years ago

Thanks so much maybe I am just making some silly mistake but everything else dropped right in so is driving me crazy.

lpinca commented 6 years ago

@corbinu I still can't reproduce. Here is the code

const Shopify = require('shopify-api-node');
const { deepStrictEqual } = require('assert');

const shopify = new Shopify({
  shopName: 'redacted',
  apiKey: 'redacted',
  password: 'redacted'
});

function getNoteAttributes(orderId) {
  return shopify.order.get(orderId).then((order) => order.note_attributes);
}

(async function () {
  const id = 161875656716;
  const note_attributes = [];

  deepStrictEqual(await getNoteAttributes(id), note_attributes);

  note_attributes.push({ name: 'color', value: 'red' });
  await shopify.order.update(id, { id, note_attributes });
  deepStrictEqual(await getNoteAttributes(id), note_attributes);

  note_attributes[0].value = 'green'
  await shopify.order.update(id, { id, note_attributes });
  deepStrictEqual(await getNoteAttributes(id), note_attributes);

  note_attributes.length = 0;
  await shopify.order.update(id, { id, note_attributes });
  deepStrictEqual(await getNoteAttributes(id), note_attributes);
})().catch((err) => {
  console.error(err.response && err.response.body || err.stack);
});
salhotra commented 6 years ago

I can confirm the same issue. I am using google cloud functions for the callback on the 'create order' webhook in shopify. Everything was working correctly for the past 2 months. I made a change 2 days ago and re deployed my cloud function. Then all of a sudden order tags didn't get updated in the shopify dashboard. In the response I received 200 and the response object in the then() had the correct tags in the order object when I printed it in the console. Then I removed this package and used shopify-node-api. I made the necessary config changes for that package it worked. Then I checked my firebase console and found 19 requests on my cloud function from shopify. So I looked at my code and I had forgot to send res status 200 to shopify after order update. I fixed this and the same problem emerged. This is the code from the other package. (very similar to this package).

Shopify.put(/admin/orders/${orderId}.json, data, (err, order) => { if (err) { console.log(err); response.sendStatus(400); } if (get(order, 'order.tags.length') !== 0) { response.sendStatus(200); } });

Same functionality with this package:

shopify.order.update(orderId, data) .then((orders) => { console.log(orders); response.sendStatus(200); }) .catch(err => console.error(err));

If we don't send 200 to shopify, it calls this function around 20 times and one of those times the tags are updated. (This of course is not a solution)

lpinca commented 6 years ago

It doesn't make much sense. Your source of truth is the API. If the data in the dashboard isn't updated it may be a caching/front-end/dashboard issue. If you get the order after the update and the updated data it's there, the changes are applied.

salhotra commented 6 years ago

@lpinca I agree that this doesn't look like a problem with this package. But if it is indeed a "caching/front-end/dashboard issue", then why does it work using postman or cUrl is what I am wondering.

lpinca commented 6 years ago

From my tests it also works with this lib, see https://github.com/MONEI/Shopify-api-node/issues/166#issuecomment-363222994.

psema4 commented 6 years ago

I started seeing something similar with the Products API this morning as well - the request would PUT without error and without updating my products.

After reading this issue and digging through the library's source code without gaining new insight, I added a console.log(util.inspect(options)) just before the return statement in Shopify.request() [index.js @ line 121] and found the source of my trouble.

Turns out I had some code that was unexpectedly adding an extra layer around my product:

{ product: { product: { title: 'foo' } } }

lpinca commented 6 years ago

@psema4 are you always ending your promise chain with catch? Shopify returns a 422 status code if submitted data is invalid and the promise is rejected. If it fails silently there is a bug.

corbinu commented 6 years ago

Not exactly sure what did that fixed it but seems to be working now after a lot of fiddling sorry for taking up your time. Thanks so much for taking a look.

lpinca commented 6 years ago

No problem, closing then.

Harshpanday commented 1 year ago

Hey @lpinca , I recently started using this library and I am facing a similar issue. I am trying to update the fulfillment_status of my order, but for some reason it is not getting updated. Here is my code

const Shopify = require('shopify-api-node');
        const shopify = new Shopify({
            shopName: 'XXX.myshopify.com',
            apiKey: 'XXXX',
            password: 'XXXX',
          });

          const { orderId, fulfillmentStatus } = req.body;
          try {
            // Update the fulfillment status of the order
            const updatedOrder = await shopify.order.update(orderId, {
                id : orderId,
                fulfillment_status: "Fulfilled",
            });
            console.log(updatedOrder)
        }catch(e){
            console.log(e)
        }

In console, the order getting printed is the correct order but the fulfillment status is still null. I would be grateful if you tell me what am I doing wrong.