formspree / formspree-js

The fastest way to hook up a React form
https://formspree.io/javascript
MIT License
63 stars 12 forks source link

Integrating Stripe into Formspree #10

Closed smakosh closed 2 years ago

smakosh commented 2 years ago

What's new?

Demo usage:

Underneath: https://stripe.com/docs/payments/accept-a-payment-synchronously?html-or-react=react

yarn add @formspree/react
import { useForm, FormspreeProvider, CardElement } from '@formspree/react';
// document that this is just passing CardElement through

const useOptions = () => {
  const options = useMemo(
    () => ({
      style: {
        base: {
          color: "#424770",
          letterSpacing: "0.025em",
          fontFamily: "Source Code Pro, monospace",
          "::placeholder": {
            color: "#aab7c4"
          }
        },
        invalid: {
          color: "#9e2146"
        }
      }
    }),
    []
  );

  return options;
};

function SignupForm() {
  const options = useOptions();
  const [state, handleSubmit] = useForm('hashid');

  return (
    <form onSubmit={handleSubmit}>
      <label htmlFor="email">Email</label>
      <input id="email" type="email" name="email" />
      <CardElement options={options} />
      <button disabled={state.submitting}>Pay</button>
    </form>
  )
}

const App = () => (
  <FormspreeProvider stripePK="asdfklajsdf">
    <SignupForm />
  </FormspreeProvider>
)

Use cases:

https://github.com/stripe/stripe-js/issues/43#issuecomment-643840075

Takeaways:

Next steps:

Propositions

smakosh commented 2 years ago

Build is failing because @formspree/core PR needs to be merged first to upgrade the package, so that the new types will be accessible here

smakosh commented 2 years ago

Is this good to be merged?