JavaScript

This sample demonstrates how to work with Profit365 REST API with JavaScript and jQuery library. Sample contains:

// put id & secret generated for yout application here
var _clientId = "";
var _clientSecret = "";

// put your authentication token here
var _authentication = "john.doe@profit365.eu:YourPassword";

// encode string into base64
_authentication = window.btoa(_authentication);

// jQuery ajax request must also have valid authentication headers.
// Use ajaxSetup to append headers in any request

$.ajaxSetup({ beforeSend: function (xhr) { // set your applications id & secret here to request
xhr.setRequestHeader("ClientID", _clientId);
xhr.setRequestHeader("ClientSecret", _clientSecret);
// set authentication to request
xhr.setRequestHeader("Authorization", "basic " + _authentication);
// set Company identifier to request
xhr.setRequestHeader("CompanyID", "your-company-id");
}
});


// function gets sales invoices list from api and send result to callback function
function _getSalesInvoices(page, search, callback) {

var _url = "https://api.profit365.eu/1.6/sales/invoices";

if (page != null && page != "") { _url += "/" + page; }

if (search != null && search != "") { _url += "?search=" + search; }

// create ajax request to get list of invoices
$.ajax({ type: "GET",
url: _url,
contentType: "application/json",
success: function (invoices) { // execute callback with array of invoices
callback(invoices);
}
});
}

// function creates new sales invoice and sent it to api
function _createSalesInvoice(invoice, callback) {
// dont forget to handle beforeSend
$.ajax({ type: "POST",
url: "https://api.profit365.eu/1.6/sales/invoices",
dataType: "json",
data: invoice,
success: function (invoiceFromAPI) { // full invoice data back from API
callback(invoiceFromAPI);
}
});
}

// create new invoice object
var _invoice = {
ordnerID: "your ordner ID",
warehouseID: "your warehouse ID",
dateCreated: "2016-01-01",
// array of rows in invoice
rows: [{ name: "first sold item name",
itemsAmount: 12,
itemsAmount: 24.12
}, { name: "another sold item name",
itemsAmount: 1,
price: 10
}]
};

// append third row to invoice
_invoice.rows[2] = {
name: "third sold item name",
itemsAmount: 1,
price: 9.99
};

// execute function with _invoice, use custom callback function what will alert new invoice id
_createSalesInvoice(_invoice, function(invoiceFromAPI) { alert(invoiceFromAPI); });