npm

react-stripe-hooks

1.4.0 • Public • Published

React Stripe Hooks

Introduction

This tiny package is a wrapper around react-stripe-elements enabling you to use Stripe as a hook without the boilerplate.

Installation

npm i react-stripe-hooks

Example

import React, { useState } from 'react'
import { StripeProvider, useStripe } from 'react-stripe-hooks'
import { CardElement } from 'react-stripe-elements'

function PaymentForm() {
    return (
        <StripeProvider apiKey="yourstripeapikey">
            <Form />
        </StripeProvider>
    )
}

function Form() {
    const { stripe, elements } = useStripe()

    const [formState, setFormState] = useState({
        name: '',
        email: '',
    })

    const handleSubmit = event => {
        event.preventDefault()

        const paymentMethod = {
            payment_method: {
                card: elements.getElement('card'),
                billing_details: {
                    name: 'Jenny Rosen',
                },
            },
            payment_method_data: {
                billing_details: {
                    name: formState.name,
                    email: formState.email,
                }
            }
        }

        stripe.handleCardPayment(clientSecret, paymentMethod).then(result => {
            if (result.error) {
                console.error(result.error.message)
            } else {
                console.log(result.paymentIntent.payment_method)
            }
        });
    }

    return (
        <form onSubmit={event => handleSubmit(event)}>
            {/* Name input */}
            {/* Email input */}

            <CardElement />
        </form>
    )
}

Readme

Keywords

none

Package Sidebar

Install

npm i react-stripe-hooks

Weekly Downloads

0

Version

1.4.0

License

MIT

Unpacked Size

1.27 MB

Total Files

22

Last publish

Collaborators

  • anglesoft