Git Product home page Git Product logo

use-stripe-cart's Introduction

use-stripe-cart

A Shopping Cart State and Logic for Stripe in React

NPM JavaScript Style Guide

All Contributors

Installation

npm install --save use-stripe-cart

or

yarn add use-stripe-cart

Usage

At the root level of your app, wrap your Root app in the <CartProvider />.

Remember to add your public Stripe key, in this example it's added in in the environment process.env.REACT_APP_STRIPE_API_PUBLIC.

/** @jsx jsx */
import { jsx } from 'theme-ui';
import ReactDOM from 'react-dom';
import { Elements, ElementsConsumer } from '@stripe/react-stripe-js';
import { loadStripe } from '@stripe/stripe-js';
import { CartProvider } from 'use-stripe-cart';
import './index.css';
import App from './App';

const stripePromise = loadStripe(process.env.REACT_APP_STRIPE_API_PUBLIC);

ReactDOM.render(
  <Elements stripe={stripePromise}>
    <ElementsConsumer>
      {({ stripe }) => (
        <CartProvider
          stripe={stripe}
          billingAddressCollection={false}
          successUrl={'https://stripe.com'}
          cancelUrl={'https://twitter.com/dayhaysoos'}
          currency={'USD'}
        >
          <App />
        </CartProvider>
      )}
    </ElementsConsumer>
  </Elements>,
  document.getElementById('root')
);

To add an item to the cart, use addItem()

/**@jsx jsx */
import { jsx, Box, Image, Button, Flex } from 'theme-ui';
import { useStripeCart } from 'use-stripe-cart';
import { toCurrency } from '../util';

/**
 * PRODUCT DATA COMING FROM PROPS
const fakeData = [
  {
    name: 'Bananas',
    sku: 'sku_GBJ2Ep8246qeeT',
    price: 400,
    image: 'https://www.fillmurray.com/300/300',
    currency: 'USD',
  },
  {
    name: 'Tangerines',
    sku: 'sku_GBJ2WWfMaGNC2Z',
    price: 100,
    image: 'https://www.fillmurray.com/300/300',
    currency: 'USD',
  },
];
*/

const Product = (product) => {
  const { addItem } = useStripeCart();
  const { name, sku, price, image, currency } = product;
  return (
    <Flex
      sx={{
        flexDirection: 'column',
        justifyContent: 'center',
        alignItems: 'center',
      }}
    >
      <Image src={image} />
      <Box>
        <p>{name}</p>
        <p>{toCurrency({ price: price, currency })}</p>
      </Box>
      <Button onClick={() => addItem({ ...product })} backgroundColor={'black'}>
        Add To Cart
      </Button>
    </Flex>
  );
};

For displaying what's actually in the cart, refer to the CartDisplay component: https://github.com/dayhaysoos/use-stripe-cart/blob/master/example/src/components/cart-display.js

API

cartDetails: Object

Cart details is an object with skus of the items in the cart as keys and details of the items as the value, for example:

{
  sku_GBJ2Ep8246qeeT: {
    name: 'Bananas';
    sku: 'sku_GBJ2Ep8246qeeT';
    price: 400;
    image: 'https://www.fillmurray.com/300/300';
    currency: 'USD';
    quantity: 1;
    formattedPrice: '$4.00';
  }
}

License

MIT © dayhaysoos


This hook is created using create-react-hook.

Contributors ✨

Thanks goes to these wonderful people (emoji key):


Kevin Cunningham

⚠️ 💻

Ian Jones

⚠️

Christopher Brown

⚠️ 💻

Nick DeJesus

💻 ⚠️

Shodipo Ayomide

📖

This project follows the all-contributors specification. Contributions of any kind welcome!

use-stripe-cart's People

Contributors

dayhaysoos avatar developerayo avatar allcontributors[bot] avatar doingandlearning avatar mellson avatar theianjones avatar

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.