Skip to content
This repository was archived by the owner on Sep 8, 2021. It is now read-only.

Change name according to Netlify Commerce guidelines. #2

Merged
merged 1 commit into from
Sep 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
# Gocommerce JS Client
# Netlify Commerce JS

This is a JS library for the [Gocommerce](https://github.com/netlify/gocommerce) API.
This is a JS client library for [Netlify Commerce](https://github.com/netlify/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

```js
import Gocommerce from `gocommerce-js`;
import NetlifyCommerce from `netlify-commerce-js`;

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

gocommerce.addToCart({
commerce.addToCart({
path: '/producs/book-1/',
quantity: 2,
meta: {
Expand All @@ -23,7 +23,7 @@ gocommerce.addToCart({
}
}).then((lineItem) => console.log(lineItem));

console.log(gocommerce.getCart());
console.log(commerce.getCart());
/*
{
items: [{
Expand All @@ -43,9 +43,9 @@ console.log(gocommerce.getCart());
}
*/

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

gocommerce.order({
commerce.order({
email: "matt@netlify.com",
shipping_address: {
first_name: "Matt",
Expand All @@ -59,40 +59,40 @@ gocommerce.order({
}
/* You can optionally specify billing_address as well */
}).then(({cart, order}) => {
return gocommerce.payment({
return commerce.payment({
// Get a token from Stripes button or a custom integration
"stripe_token": TOKEN_FROM_STRIPE_CC_FORM,
// The gocommerce API will verify that the amount and order ID match
// 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!")
});

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

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

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

You can use `gocommerce` together with [authlify](https://github.com/netlify/authlify) to let users log in and claim view order history.
You can use Netlify Commerce JS together with [authlify](https://github.com/netlify/authlify) to let users log in and claim view order history.

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

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

gocommerce.orderHistory().then((orders) => {
commerce.orderHistory().then((orders) => {
console.log(orders);
});
});
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "gocommerce-js",
"name": "netlify-commerce-js",
"version": "1.1.1",
"description": "Gocommerce API client for JavaScript",
"description": "Netlify Commerce API client for JavaScript",
"main": "lib/index.js",
"scripts": {
"compile": "babel --presets es2015 --plugins syntax-object-rest-spread,transform-object-rest-spread -d lib/ src/",
"prepublish": "npm run compile",
"babelify": "browserify src/index.js -t [ babelify --presets [ es2015 ] --plugins syntax-object-rest-spread,transform-object-rest-spread ] | uglifyjs > browser/gocommerce.js"
"babelify": "browserify src/index.js -t [ babelify --presets [ es2015 ] --plugins syntax-object-rest-spread,transform-object-rest-spread ] | uglifyjs > browser/commerce.js"
},
"author": "Mathias Biilmann Christensen",
"license": "MIT",
Expand Down
14 changes: 7 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import API from "./api";

const HTTPRegexp = /^http:\/\//;
const cartKey = "gocommerce.shopping-cart";
const cartKey = "netlify.commerce.shopping-cart";
const vatnumbers = {};

function checkRole(user, role) {
Expand Down Expand Up @@ -43,19 +43,19 @@ function centsToAmount(cents) {
return `${(Math.round(cents) / 100).toFixed(2)}`;
}

export default class Gocommerce {
export default class NetlifyCommerce {
constructor(options) {
if (!options.APIUrl) {
throw("You must specify an APIUrl of your Gocommerce instance");
throw("You must specify an APIUrl of your Netlify Commerce instance");
}
if (options.APIUrl.match(HTTPRegexp)) {
console.log('Warning:\n\nDO NOT USE HTTP IN PRODUCTION FOR GOCOMMERCE EVER!\GOCOMMERCE REQUIRES HTTPS to work securely.')
console.log('Warning:\n\nDO NOT USE HTTP IN PRODUCTION FOR NETLIFY COMMERCE EVER!\NETLIFY COMMERCE REQUIRES HTTPS to work securely.')
}

this.api = new API(options.APIUrl);
this.currency = options.currency || "USD";
this.billing_country = options.country;
this.settings_path = "/gocommerce/settings.json";
this.settings_path = "/netlify-commerce/settings.json";
this.settings_refresh_period = options.settingsRefreshPeriod || (10 * 60 * 1000);
this.loadCart();
}
Expand All @@ -73,7 +73,7 @@ export default class Gocommerce {
return response.text().then((html) => {
const doc = document.implementation.createHTMLDocument("product");
doc.documentElement.innerHTML = html;
const product = JSON.parse(doc.getElementById("gocommerce-product").innerHTML);
const product = JSON.parse(doc.getElementById("netlify-commerce-product").innerHTML);
const {sku, title, prices, description, type, vat} = product;
if (sku && title && prices) {
if (this.line_items[sku]) {
Expand Down Expand Up @@ -292,5 +292,5 @@ export default class Gocommerce {
}

if (typeof window !== "undefined") {
window.Gocommerce = Gocommerce
window.NetlifyCommerce = NetlifyCommerce
}