react-stripe-elements icon indicating copy to clipboard operation
react-stripe-elements copied to clipboard

Split Form using *Elements and createPaymentMethod (lack of examples in documentation)

Open nicholas-mandelbaum opened this issue 6 years ago • 8 comments

I can't figure out how to use createPaymentMethod with split form (separate elements) It works fine with the single cardElement, but I don't understand the documentation, and I'm unable to replicate the example (I'm using NextJS).

As in the documentation, I have Checkoutform.js

async getPaymentMethod() {
    const { stripe } = this.props;
    const cardElement = this.props.elements.getElement('card');
    const { paymentMethod } = await stripe.createPaymentMethod({
      type: 'card',
      card: cardElement
    });
...

SplitFieldForm.js

import React, { Component } from 'react';
import {
  CardNumberElement,
  CardExpiryElement,
  CardCvcElement
} from 'react-stripe-elements';

class SplitFieldsForm extends Component {
  render() {
    return (
      <>
        <CardNumberElement id="card_number" />
        <CardExpiryElement id="card_expiry" />
        <CardCvcElement id="card_cvc" />
      </>
    );
  }
}

export default SplitFieldsForm;

I have tried various ways to 'getElement' separately, but just can't figure out a way to make it work. Is there a similar example that I could follow or more explicit documentation ? Thanks.

nicholas-mandelbaum avatar Dec 23 '19 22:12 nicholas-mandelbaum

As Stripe.js can pull in other elements, you just have to give the first one, in this case, the cardNumber. Documentation could mention it, it would save perhaps some time to other users.

async getPaymentMethod() {
    const { stripe } = this.props;
    const cardElement = this.props.elements.getElement('cardNumber');
    const { paymentMethod } = await stripe.createPaymentMethod({
      type: 'card',
      card: cardElement
    });

nicholas-mandelbaum avatar Dec 23 '19 23:12 nicholas-mandelbaum

This should be added into the docs for sure.

dMcCaffree avatar Apr 15 '20 19:04 dMcCaffree

This defo should be explained, wasted hours on this issue

redsquare avatar May 27 '20 09:05 redsquare

Thanks for this ! I was also trying to figure this out. As we stand today the docs does not seems to be updated yet. The new Git repo sample works this way, but its not explicit that the others data (CVC, Expiry) are automatically pulled along with the number behind the scene

Haja-andri avatar Jun 03 '20 13:06 Haja-andri

This defo should be explained, wasted hours on this issue

My support 🙌🏻

petrsuhin-al avatar Aug 05 '20 21:08 petrsuhin-al

+1 on this defo need to be explained on the documentation

jepoy16 avatar Aug 26 '20 17:08 jepoy16

Why hasn't this been documented yet? Principle of Least Astonishment

TheChuckster avatar Aug 27 '20 03:08 TheChuckster

Hi all, sounds like we reached a resolution here. We'll definitely consider this as a docs improvement to make.

I will close the issue for now -- people will still be able to find it, but the project has moved to React Stripe.js. I'll make sure this is tracked internally as a docs improvement task.

sashko-stripe avatar Mar 26 '21 18:03 sashko-stripe