3.6.1 • Published 9 years ago

netlify-commerce-js v3.6.1

Weekly downloads
-
License
MIT
Repository
-
Last release
9 years ago

Netlify Commerce JS

This is a JS client library for Netlify Commerce API.

It lets you signup and authenticate users and is a building block for constructing the UI for signups, password recovery, login and logout.

Usage

import NetlifyCommerce from `netlify-commerce-js`;

const commerce = new NetlifyCommerce({
  APIUrl: "https://commerce.netlify.com"
});

commerce.addToCart({
	path: '/producs/book-1/',
	quantity: 2,
	meta: {
    // You can add anything in metadata and use it in your checkout ui
		photo: "/images/mugs/netlig-01.png"
	}
}).then((lineItem) => console.log(lineItem));

console.log(commerce.getCart());
/*
{
  items: [{
  	title: "Netlify Mug",
  	sku: "netlify-mug-01",
  	description: "A mug with a netlify sticker!",
  	price: {amount: "49.00", "currency": "USD", cents: 4900},
    tax: {amount: "0.00", currency: "USD", cents: 0},
  	quantity: 2,
  	metadata: {
  		photo: "/images/mugs/netlig-01.png" // You can add anything in metadata
  	}
  }],
  subtotal: {amount: "98.00", "currency": "USD", cents: 9800},
  taxes: {amount: "0.00", "currency": "USD", cents: 0},
  total: {amount: "98.00", "currency": "USD", cents: 9800}
}
*/

commerce.updateCard("netlify-mug-01", 3); // Set to 0 to remove

commerce.order({
  email: "matt@netlify.com",
  shipping_address: {
    first_name: "Matt",
    last_name: "Biilmann",
    company: "netlify", // Optional
    address: "610 22nd Street",
    city: "San Francisco",
    state: "CA",
    country: "USA",
    zip: "94107"
  }
  /* You can optionally specify billing_address as well */
}).then(({cart, order}) => {
  return commerce.payment({
    // Get a token from Stripes button or a custom integration
    "stripe_token": TOKEN_FROM_STRIPE_CC_FORM,
    // The commerce API will verify that the amount and order ID match
    "amount": cart.total.cents,
    "order_id": order.id,
  })
}).then((transaction) => {
  console.log("Order confirmed!")
});

commerce.clearCart(); // Will be called automatically after a successful order

You can change country (for VAT calculations) or currency at any time:

commerce.setCountry("USA");
commerce.setCurrency("USD");

You can use Netlify Commerce JS together with authlify to let users log in and claim view order history.

authlify.login(email, password).then((user) => {
  commerce.setUser(user);

  commerce.order({
    email: user.email,
    shipping_address_id: "some-previously-generated-address"
    /* Normal order details */
  });

  commerce.orderHistory().then((orders) => {
    console.log(orders);
  });
});
3.6.1

9 years ago

3.6.0

9 years ago

3.5.2

9 years ago

3.5.1

9 years ago

3.5.0

9 years ago

3.4.2

9 years ago

3.4.1

9 years ago

3.3.0

9 years ago

3.2.2

9 years ago

3.2.1

9 years ago

3.2.0

9 years ago

3.1.0

9 years ago

3.0.0

9 years ago

2.2.0

10 years ago

2.1.0

10 years ago

2.0.0

10 years ago

1.3.1

10 years ago

1.3.0

10 years ago

1.2.0

10 years ago

1.1.4

10 years ago

1.1.3

10 years ago

1.1.1

10 years ago