首页 > 解决方案 > 尝试在生产中进行条带付款时收到错误“错误:请求失败,状态码为 408”

问题描述

我正在尝试使用 stripe.charges.create 函数和库 axios 在我的本机应用程序中进行条带支付。这目前在测试模式下运行良好,因此使用秘密和可发布的测试密钥。但是,在修改密钥以及更改源时,我收到以下错误: [Error: Request failed with status code 408] 。查看条纹文档https://stripe.com/docs/api/errors似乎这可能是由于缺少参数,...' 4xx 范围内的代码表示根据提供的信息失败的错误(例如,省略了必需的参数,收费失败等)。我是否会在如下所示的 stripe.charges.create 函数中丢失客户 ID?条纹文档提到,对于卡来源,客户 ID 是必需的,https://stripe.com/docs/api/charges/create。如果需要,我应该在代码中的哪个位置添加它以进行实时付款?从条带仪表板获取的 API 密钥。

测试 index.js文件

const stripe = require("stripe")("**SK_TEST**");

exports.payWithStripe = functions.https.onRequest((request, response) => {
  stripe.charges
    .create({
      amount: request.body.amount,
      currency: request.body.currency,
      source: "tok_mastercard",
    })
    .then((charge) => {
      response.send(charge);
    })
    .catch((err) => {
      console.log(err);
    });
});

生产 index.js文件

const stripe = require("stripe")("SK_LIVE");

exports.payWithStripe = functions.https.onRequest((request, response) => {
  stripe.charges
    .create({
      amount: request.body.amount,
      currency: request.body.currency,
      source: "request.body.token",
    })
    .then((charge) => {
      response.send(charge);
    })
    .catch((err) => {
      console.log(err);
    });
});

CardScreen.js文件

import React, { PureComponent } from "react";
import { View, Text, StyleSheet } from "react-native";
import stripe from "tipsi-stripe";
import Button from "./components/Button";
import { demoCardFormParameters } from "./demodata/demodata";
import axios from "axios";

stripe.setOptions({ publishableKey: "PK_LIVE" });

export default class CardScreen extends PureComponent {
  static title = "Card Form";

  state = {
    loading: false,
    paymentMethod: null,
  };

  handleCardPayPress = async () => {
    try {
      this.setState({ loading: true, paymentMethod: null });

      const paymentMethod = await stripe.paymentRequestWithCardForm({
        smsAutofillDisabled: true,
      });

      this.setState({ loading: false, paymentMethod });
    } catch (error) {
      this.setState({ loading: false });
    }
  };

  makePayment = async () => {
    this.setState({ loading: true });

    axios({
      method: "POST",
      url: "https://us-central1-appname-90059.cloudfunctions.net/payWithStripe",
      data: {
        amount: 50,
        currency: "gbp",
        token: this.state.paymentMethod,
      },
    })
      .then((response) => {
        console.log(response);
        this.setState({ loading: false });
      })
      .catch((err) => {
        console.log(err);
      });
  };

  render() {
    const { loading, paymentMethod } = this.state;

    return (
      <View style={styles.container}>
        <Text style={styles.header}>Card Form Example</Text>
        <Text style={styles.instruction}>
          Click button to show Card Form dialog.
        </Text>
        <Button
          text="Enter your card and pay"
          loading={loading}
          onPress={this.handleCardPayPress}
        />
        <View style={styles.paymentMethod}>
          {paymentMethod && (
            <>
              <Text style={styles.instruction}>
                Payment Method: {JSON.stringify(paymentMethod.id)}
              </Text>

              <Button
                text="Make Payment"
                loading={loading}
                onPress={this.makePayment}
              />
            </>
          )}
        </View>
      </View>
    );
  }
}

谢谢。

标签: firebasereact-nativeaxiosgoogle-cloud-functionsstripe-payments

解决方案


推荐阅读