Intégration en Node.js
Description et emplacement des fichiers à utiliser
- node.js/luckycart.node.zip : la librairie Node.js
- node.js/lc_plugin_sample.js : un exemple d’utilisation d'ajout de panier
- node.js/lc_update_sample.js : un exemple d’utilisation de mise à jour de panier
Etapes d'intégration
1. Installation de la librairie
Placez et désarchivez la librairie luckycart.node.zip à la racine de votre projet Node.js.
2. Inclusion de la librairie
Modifiez le code de la page de confirmation de commande pour inclure la librairie :
// Include LC library
const Luckycart = require('luckycart');
2. Initialisation de la librairie et envoi des données
Toujours dans le code de la page de confirmation de commande :
// Create a Lucky Cart object with your key and secret
const client = new Luckycart('key', 'secret');
// Create an object of data to send.
// Assuming data from the site are store in customer and cart data.
const data = {
// mandatory data
customerId: customer.getId(),
cartId: cart.getId(),
ttc: cart.getTtc(),
ht: cart.getHt(),
country: customer.getCountry(),
email: customer.getEmail(),
payment: cart.getPayment(),
firstName: customer.getFirstname(),
lastName: customer.getLastname(),
codePromo: cart.getCode(),
currency: cart.getCurrency(),
lang: customer.getLanguage(),
shopId: cart.getShopId(),
optin: customer.getOptin(),
products : [
{ id: cart.products[0].getId(), ttc: cart.products[0].getTtc(),
ht: cart.products[0].getHt(), qty: cart.products[0].getQty(),
brand: cart.products[0].getBrand(), cat: cart.products[0].getCategory()},
{ id: cart.products[1].getId(), ttc: cart.products[1].getTtc(),
ht: cart.products[1].getHt(), qty: cart.products[1].getQty(),
brand: cart.products[1].getBrand(), cat: cart.products[1].getCategory()},
{ id: cart.products[2].getId(), ttc: cart.products[2].getTtc(),
ht: cart.products[2].getHt(), qty: cart.products[2].getQty(),
brand: cart.products[2].getBrand(), cat: cart.products[2].getCategory()},
]
// add here any other specific data
};
client.plugin(data, function(err, result){
// Callback called once data have been sent
if (err) console.log(err);
var templateData= {
script: result.script || "",
...
};
res.render("checkout", templateData);
});
3. Affichage du jeu
Il s’effectue en positionnant les containers suivants sur la page de confirmation de commande :
<body>
...
<!-- Add this div tag anywhere in the order confirmation page to render the game plugin -->
<div id="luckygame">
<!-- Luckycart plugin will be displayed here -->
</div>
...
<!-- Add this at the bottom of the page (in order not to interfere with page loading) -->
<%- script %>
</body>
Mise à jour d'un panier
La mise à jour d'un panier déjà envoyé s'effectue de la même façon que ci-dessus sauf que la méthode update() est utilisée à la place de la méthode plugin() en spécifiant l'id du panier à mettre à jour :
client.update(cart.getId(), data, function(err, res){
// Callback called once data have been sent
if (err) console.log(err);
// Informations on tickets changes
console.log(res.tickets);
});