【问题标题】:React-Native, Stripe card element not displaying for live API keyReact-Native,条纹卡元素未显示实时 API 密钥
【发布时间】:2023-03-02 22:40:02
【问题描述】:

在我的 react-native 项目中,我使用了 react-native-webview 和 stripe js (https://js.stripe.com/v3)

我正在使用条纹 js 创建卡片元素来收集卡片详细信息

var elements = stripe.elements()
var card = elements.create('card');
card.mount('#card-element');

当用户按下提交按钮时,我正在调用 stripe.confirmPaymentIntent() 函数完成支付。

但是当我使用 PRODUCTION PUBLIC_KEY 卡元素未显示时,我遇到了问题,因为测试 PUBLIC_KEY 卡元素正确显示。

有什么办法吗?

我的项目详情是

  • “反应”:“16.9.0”
  • “react-native”:“0.61.5”
  • “react-native-webview”:“10.3.2”

xcode 版本 - 12.1 IOS 版本 - 14.1

function stripeCheckoutRedirectHTML(PUBLIC_KEY) {
  return `<!DOCTYPE html>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<html>
<body>
  <!-- Load Stripe.js on your website. -->
  <script src="https://js.stripe.com/v3"></script>
  <h1 id="error-message"></h1>
  <form method="post" id="payment-form">
    <div class="form-row">
      <label for="card-element">
        Credit or debit card
      </label>
      <div id="card-element">
        <!-- A Stripe Element will be inserted here. -->
      </div>
    </div>
    <button>Submit Payment</button>
  </form>
  <script>
    var stripe = Stripe(${PUBLIC_KEY});
    var elements = stripe.elements();
    var card = elements.create('card');
    card.mount('#card-element');
  </script>
</body>

</html>`;
}
export default function Payment({route, navigation}) {
  return (
    <View style={styles.container}>
      <View style={styles.body} showsVerticalScrollIndicator={false}>
        <WebView
          originWhitelist={['*']}
          source={{
            html: stripeCheckoutRedirectHTML(Config.PUBLIC_KEY),
          }}
        />
      </View>
    </View>
  );
}

【问题讨论】:

    标签: javascript html react-native stripe-payments react-native-webview


    【解决方案1】:

    Stripe.js 要求您在生产环境中通过 HTTPS 运行您的网站。为了使测试更容易,当您使用测试 API 密钥时,此限制会被取消,这就是它之前为您工作的原因。默认情况下,React Native 将通过about:blank 提供任何原始 HTML。因此,当您从测试切换到实时可发布密钥时,Stripe 会抛出一个错误,指出实时 Stripe.js 集成必须使用 HTTPS。有两种方法可以解决此问题:

    • 您可以通过 HTTPS 托管您的网站,并在实例化组件时将其作为源 URI 传递,而不是向 WebView 组件提供原始 HTML。
    • 您可以提供指向通过 https 服务的站点的 baseURL 选项。 React Native 将用这个 URL 替换 about:blank 并且原始 HTML 中的任何非绝对链接都将与那个 URL 相关。例如:
    export default function Payment({route, navigation}) {
      return (
        <View style={styles.container}>
          <View style={styles.body} showsVerticalScrollIndicator={false}>
            <WebView
              originWhitelist={['*']}
              source={{
                html: stripeCheckoutRedirectHTML(Config.PUBLIC_KEY),
                baseUrl: 'https://example.com',
              }}
            />
          </View>
        </View>
      );
    }
    

    【讨论】:

      猜你喜欢
      • 2021-10-20
      • 2019-01-06
      • 2021-01-11
      • 2021-03-03
      • 2022-06-16
      • 1970-01-01
      • 1970-01-01
      • 2017-03-27
      • 2020-04-23
      相关资源
      最近更新 更多