You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Stripe React Native SDK替换tipsi-stripe废弃Token创建方法求助

从tipsi-stripe迁移到Stripe React Native SDK:替代createTokenWithCard和createTokenWithBankAccount

Stripe已弃用旧版Token API(对应tipsi-stripe的createTokenWithCard/createTokenWithBankAccount),当前标准方案是使用Payment Method API,通过Stripe React Native SDK的createPaymentMethod方法生成Payment Method ID,再传递给后端处理。以下是具体替代方案和代码调整:

1. 替代卡片Token生成(原createTokenWithCard)

使用createPaymentMethod创建卡片类型的Payment Method,同时保留借记卡校验逻辑。

2. 替代银行账户Token生成(原createTokenWithBankAccount)

针对欧元区IBAN账户,创建sepa_debit类型的Payment Method,替代原银行账户Token。

完整调整后的代码

import { Stripe } from '@stripe/stripe-react-native';

// ... 你的其他代码逻辑

if (type === 'CARD') {
  const valid = cardData.valid;
  if (!valid) return;
  const cardValues = cardData.values;
  const expParts = cardValues.expiry.split('/');

  const paymentMethodParams = {
    type: 'Card',
    card: {
      cvc: cardValues.cvc,
      expMonth: Number.parseInt(expParts[0], 10),
      expYear: Number.parseInt(expParts[1], 10),
      number: cardValues.number.split(' ').join(''),
    },
    billing_details: {
      email: ctx.user.email,
      phone: ctx.user.phoneNumber || '',
    },
  };

  try {
    const { paymentMethod, error } = await Stripe.createPaymentMethod(paymentMethodParams);
    if (error) {
      setIsRequestLoading(false);
      setError('Card validation failed');
      setMessage(error.message);
      popupRef.current.open();
      return;
    }

    if (paymentMethod.card?.funding !== 'debit') {
      setIsRequestLoading(false);
      setError('Sorry');
      setMessage('We can only accept debit cards');
      popupRef.current.open();
      return;
    }

    cardMethod = paymentMethod.id;
  } catch (e) {
    setIsRequestLoading(false);
    setError('Something went wrong');
    setMessage(e.message);
    popupRef.current.open();
    return;
  }
} else {
  const paymentMethodParams = {
    type: 'SepaDebit',
    sepa_debit: {
      iban: IBAN.replace(/\s/g, ''),
    },
    billing_details: {
      email: ctx.user.email,
      phone: ctx.user.phoneNumber || '',
      address: {
        country: values.country === 'Ireland' ? 'IE' : 'GB',
      },
    },
    currency: 'eur',
  };

  try {
    const { paymentMethod, error } = await Stripe.createPaymentMethod(paymentMethodParams);
    if (error) {
      setIsRequestLoading(false);
      setError('Bank account validation failed');
      setMessage(error.message);
      popupRef.current.open();
      return;
    }

    bankMethod = paymentMethod.id;
  } catch (e) {
    setIsRequestLoading(false);
    setError('Something went wrong');
    setMessage(e.message);
    popupRef.current.open();
    return;
  }
}

const account = {
  ...values,
  cardID: cardMethod ? cardMethod : undefined,
  bankDetails: bankMethod ? bankMethod : undefined,
  email: ctx.user.email,
  phone: ctx.user.phoneNumber || '',
};

try {
  const res = await createAccount({
    variables: {
      account,
    },
  });

  if (res.data && res.data.id) {
    handleUpdate();
  } else {
    setWithReport(true);
    setIsRequestLoading(false);
    setError('Something went wrong.');
    popupRef.current.open();
  }
} catch (error) {
  setIsRequestLoading(false);
  const e = extractGraphQLError(error);
  setError('Something went wrong.');
  setMessage(e);
  if (e === DEFAULT_ERROR) setWithReport(true);
  popupRef.current.open();
}

后端关键调整

前端传递的不再是Token ID,而是Payment Method ID,后端需对应调整:

  • 卡片场景:若用于用户收款,调用PaymentMethod.attach绑定到Stripe Customer;若用于向用户打款,调用Customer.createExternalAccount将Payment Method转为外部账户。
  • 银行账户场景:将SEPA Debit Payment Method附加到目标Stripe账户(如Connected Account或Customer),用于后续Payout操作。

确保你的Stripe React Native SDK为最新版本,避免兼容性问题。

内容的提问来源于stack exchange,提问作者aisha sharma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 20:48:09