@stripe/stripe-js#StripeElements TypeScript Examples

The following examples show how to use @stripe/stripe-js#StripeElements. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BillingPage.tsx    From clearflask with Apache License 2.0 5 votes vote down vote up
async onPaymentSubmit(elements: StripeElements, stripe: Stripe): Promise<boolean> {
    trackingBlock(() => {
      ReactGA.event({
        category: 'billing',
        action: this.props.accountBilling?.payment ? 'click-payment-update-submit' : 'click-payment-add-submit',
        label: this.props.accountBilling?.plan.basePlanId,
        value: this.props.accountBilling?.plan.pricing?.basePrice,
      });
    });

    this.setState({ isSubmitting: true, stripePaymentError: undefined });

    const cardNumberElement = elements.getElement(CardNumberElement);
    if (cardNumberElement === null) {
      this.setState({
        stripePaymentError: 'Payment processor not initialized yet',
        isSubmitting: false,
      });
      return false;
    }

    const tokenResult = await stripe.createToken(cardNumberElement);
    if (!tokenResult.token) {
      this.setState({
        stripePaymentError: tokenResult.error
          ? `${tokenResult.error.message} (${tokenResult.error.code || tokenResult.error.decline_code || tokenResult.error.type})`
          : 'Payment processor failed for unknown reason',
        isSubmitting: false,
      });
      return false;
    }

    const dispatcher = await ServerAdmin.get().dispatchAdmin();
    try {
      await dispatcher.accountUpdateAdmin({
        accountUpdateAdmin: {
          paymentToken: {
            type: 'killbill-stripe',
            token: tokenResult.token.id,
          },
          renewAutomatically: true,
        },
      });
    } catch (er) {
      this.setState({
        isSubmitting: false,
        stripePaymentError: 'Failed to add payment',
      });
      return false;
    }

    try {
      await dispatcher.accountBillingAdmin({});
    } catch (er) {
      this.setState({
        isSubmitting: false,
        stripePaymentError: 'Failed to add payment',
      });
      return false;
    }

    this.setState({ isSubmitting: false, showAddPayment: undefined });
    return true;
  }