【问题标题】:Receiving Invalid request error from stripe while creating payment intent using Flutter使用 Flutter 创建支付意图时从条带接收无效请求错误
【发布时间】:2021-02-13 23:24:51
【问题描述】:

我正在将 Stripe 集成到我的 Flutter 应用程序中,想要使用提及 here 的 http POST 请求创建支付意图。但我不断收到这个错误。尝试了很多东西!

{
  "error": {
    "code": "parameter_unknown",
    "doc_url": "https://stripe.com/docs/error-codes/parameter-unknown",
    "message": "Received unknown parameter: {\"amount\":2,\"currency\":\"usd\"}",
    "param": "{\"amount\":2,\"currency\":\"usd\"}",
    "type": "invalid_request_error"
  }
}

我的api函数是这样的:

Future<Map<String, dynamic>> createPaymentIntent(int amount) async {
    try {
      var url = "https://api.stripe.com/v1/payment_intents";

      var body = json.encode({
        "amount": amount,
        "currency": 'usd'
      });

      var headers = {
        'Content-type': 'application/x-www-form-urlencoded',
        'Accept': 'application/json',
        'authorization': 'Bearer '+stripeSecret
      };

      await http.post(url, body: json.encode(body), headers: headers, encoding: Encoding.getByName("utf-8")).then((response) {
        if(response.statusCode == 200){
          print(response.body);
          return json.decode(response.body);
        }
        else{
          return response.reasonPhrase;
        }
      });

    } catch (e) {
      return e.message;
    }
  }

注意'Content-type': 'application/x-www-form-urlencoded' 是必需的。

【问题讨论】:

    标签: flutter dart stripe-payments payment


    【解决方案1】:

    您正在向 Stripe API 发送 JSON 编码的正文,但 Stripe API 需要 form-encoded request body

    我们的 API 具有可预测的面向资源的 URL,接受 form-encoded 请求正文,返回 JSON 编码响应,并使用标准 HTTP 响应代码、身份验证和动词。

    【讨论】:

    猜你喜欢
    • 2014-10-18
    • 1970-01-01
    • 2021-11-07
    • 2016-09-17
    • 1970-01-01
    • 1970-01-01
    • 2019-12-30
    • 2021-01-21
    • 2020-03-31
    相关资源
    最近更新 更多