<commit_before>import { Meteor } from 'meteor/meteor';
import { HTTP } from 'meteor/http';

import bugsnag from '../../bugsnag/server/bugsnag';

const shopifyOrderApi = {
  getPendingOrders() {
    let orders = [];
    const serviceUrl = Meteor.settings.private.shopifyServiceUrl;
    const apiKey = process.env.SHOPIFY_API_KEY;
    const apiPass = <PASSWORD>.SHOPIFY_API_PASS;
    const response = HTTP.get(
      `${serviceUrl}/orders.json`,
      {
        auth: `${apiKey}:${apiPass}`,
        query: 'financial_status=pending&fields=id,total_price,customer',
      }
    );
    if (response && response.data) {
      orders = response.data.orders;
    }
    return orders;
  },

  markOrderAsPaid(orderId, totalPrice) {
    if (orderId && totalPrice) {
      const serviceUrl = Meteor.settings.private.shopifyServiceUrl;
      const apiKey = process.env.SHOPIFY_API_KEY;
      const apiPass = process.env.SHOPIFY_API_PASS;

      const response = HTTP.call(
        'POST',
        `${serviceUrl}/orders/${orderId}/transactions.json`,
        {
          auth: `${apiKey}:${apiPass}`,
          data: {
            transaction: {
              kind: 'capture',
              status: 'success',
              amount: totalPrice,
            },
          },
        }
      );

      if (response.statusCode === 200) {
        HTTP.call(
          'PUT',
          `${serviceUrl}/orders/${orderId}.json`,
          {
            auth: `${apiKey}:${apiPass}`,
            data: {
              order: {
                id: orderId,
                financial_status: 'paid',
              },
            },
          }
        );
      } else {
        bugsnag.notify(
          new Error(
            'Unable to add a "capture" transaction to Shopify. This has to be '
            + 'in place before we can set an orders status to paid. Verify '
            + `order ID ${orderId} exists in Shopify.`
          ),
          {
            orderId,
            totalPrice,
          },
        );
      }
    }
  },
};

export default shopifyOrderApi;<commit_msg>Fixed issue with using wrong create 201 HTTP code when deciding to mark order as paid.<commit_after>import { Meteor } from 'meteor/meteor';
import { HTTP } from 'meteor/http';

import bugsnag from '../../bugsnag/server/bugsnag';

const shopifyOrderApi = {
  getPendingOrders() {
    let orders = [];
    const serviceUrl = Meteor.settings.private.shopifyServiceUrl;
    const apiKey = process.env.SHOPIFY_API_KEY;
    const apiPass = <PASSWORD>;
    const response = HTTP.get(
      `${serviceUrl}/orders.json`,
      {
        auth: `${apiKey}:${apiPass}`,
        query: 'financial_status=pending&fields=id,total_price,customer',
      }
    );
    if (response && response.data) {
      orders = response.data.orders;
    }
    return orders;
  },

  markOrderAsPaid(orderId, totalPrice) {
    if (orderId && totalPrice) {
      const serviceUrl = Meteor.settings.private.shopifyServiceUrl;
      const apiKey = process.env.SHOPIFY_API_KEY;
      const apiPass = process.env.SHOPIFY_API_PASS;

      const response = HTTP.call(
        'POST',
        `${serviceUrl}/orders/${orderId}/transactions.json`,
        {
          auth: `${apiKey}:${apiPass}`,
          data: {
            transaction: {
              kind: 'capture',
              status: 'success',
              amount: totalPrice,
            },
          },
        }
      );

      if (response.statusCode === 201) {
        HTTP.call(
          'PUT',
          `${serviceUrl}/orders/${orderId}.json`,
          {
            auth: `${apiKey}:${apiPass}`,
            data: {
              order: {
                id: orderId,
                financial_status: 'paid',
              },
            },
          }
        );
      } else {
        bugsnag.notify(
          new Error(
            'Unable to add a "capture" transaction to Shopify. This has to be '
            + 'in place before we can set an orders status to paid. Verify '
            + `order ID ${orderId} exists in Shopify.`
          ),
          {
            orderId,
            totalPrice,
          },
        );
      }
    }
  },
};

export default shopifyOrderApi;
{'fraction_non_alphanumeric': 0.125298900047824, 'fraction_numerical': 0.002152080344332855, 'mean_word_length': 1.7056921086675292, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4931581', 'n_tokens_mistral': 1231, 'n_tokens_neox': 1222, 'n_words': 260}