Интеграция с живым товаром - BigCommerce - PullRequest
0 голосов
/ 24 апреля 2020

Я пытаюсь интегрировать прямой поток продуктов от поставщика непосредственно в мой магазин BigCommerce. Данные поступают в формате XML, поэтому я использовал плагин с именем xml2 js, чтобы преобразовать его в JSON, но по какой-то причине при ведении журнала в консоли он все еще остается в формате XML. Кроме того, я вижу возможность создания продукта здесь https://developer.bigcommerce.com/api-reference/catalog/catalog-api/products/createproduct, могу ли я выполнить массовый импорт с использованием той же конечной точки?

const Connection = require('../connection');
require('dotenv').config();
const request = require('request');

let xml =
    `<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GenarateXML xmlns="link">
      <tokenKey>TOKEN</tokenKey>
      <accessKey>ACCESSKEY</accessKey>
    </GenarateXML>
  </soap:Body>
</soap:Envelope>`

var options = {
    url: 'supplier's website',
    method: 'POST',
    body: xml,
    headers: {
        'Content-Type': 'text/xml;charset=utf-8',
        'Content-Length': xml.length,
        'SOAPAction': "SOAPACTION"
    }
};

let productFeed;

let callback = (error, response, body) => {
    if (!error && response.statusCode == 200) {
        // console.log('Raw result', body);
        var xml2js = require('xml2js');
        var parser = new xml2js.Parser({ explicitArray: false, trim: true });
        parser.parseString(body, (err, result) => {
            console.log(JSON.stringify(result));
            productFeed = JSON.stringify(result);
        });
    };
    // console.log(response.statusMessage);
};
request(options, callback);


//Initialize new API Connection:
var api = new Connection({
    hash: "***",
    token: "***",
    cid: "***",
    host: 'https://api.bigcommerce.com' //The BigCommerce API Host
});

var appRouter = function (app) {

    app.use(function (req, res, next) {
        res.header("Access-Control-Allow-Origin", "*");
        res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
        next();
    });

    app.get("/", function (req, res) {
        res.status(200).send({
            body: `${productFeed}`
        });
    });
};

module.exports = appRouter;
...