GithubHelp home page GithubHelp logo

netlify / gocommerce-js Goto Github PK

View Code? Open in Web Editor NEW
131.0 22.0 30.0 1017 KB

A gocommerce client library

Home Page: https://www.npmjs.com/package/gocommerce-js

License: MIT License

JavaScript 100.00%

gocommerce-js's Introduction

GoCommerce JS

This is a JS client library for GoCommerce API.

IMPORTANT: This requires at least version 1.8.0 of the GoCommerce backend (since v5.0.0).

It handles orders and payments. Integrates with Stripe for payments and will support international pricing and VAT verification.

Usage

import GoCommerce from "gocommerce-js";

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

commerce.addToCart({
	path: "/products/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.updateCart("netlify-mug-01", 3); // Set to 0 to remove

commerce.order({
  email: "[email protected]",
  shipping_address: {
    name: "Matt Biilmann",
    company: "netlify", // Optional
    address1: "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
    "provider": "stripe",
    "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 GoCommerce JS together with GoTrue to let users log in and claim view order history.

goTrue.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);
  });
});

gocommerce-js's People

Contributors

bcomnes avatar biilmann avatar brycekahle avatar calavera avatar enstyled avatar mraerino avatar pukhalski avatar rkusa avatar roseannam avatar rybit avatar wolfgang42 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gocommerce-js's Issues

Tax calculation disregards discounts [CRITICAL]

- Do you want to request a feature or report a bug?
Bug.

- What is the current behavior?
The total tax stays the same even if a discount code is applied
(while it actually should be based to the final price, including the
discount)

- What is the expected behavior?
Tax calculation should be based to the final price, including the
discount

Add per line item discount to cart object

Now only the total discount is calculated but sometimes it is handy to show a per item discount, since there could be a fixed amount discount for one item and a percentage for all because of a coupon used.

Use ESDoc tags to document API client

The usage in the README is great for basic things, and for getting started, but there's a lot more in the API client that could be used. Currently (afaik) those methods are undocumented. However, I would suggest adding ESDoc tags to allow IDEs such as Visual Studio Code to offer documentation for required parameters and a quick description on each method when you're writing your code. ESDoc tags is a great way to improve understanding when you're writing code for me, and I think this library should totally use it (manually reading code to see what it does and how to use it is boring).

Don`t have any functions to remove item from cart?

Well,

I`m using the gocommerce in an static gen e-commerce with Hugo. But this library is with lack of functions like remove item from cart.

That`s anyway to update the cart removing some item from there?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.