简体   繁体   English

条带多个计划订阅api调用的格式数组

[英]Format array for Stripe multiple plan subscription api call

I'm trying to send multiple plan subscription request to the Stripe API. 我正在尝试向Stripe API发送多个计划订阅请求。 However I have an array like this 但是我有这样的数组

0: {"plan":"plan_EaDE7UnYYcicOj","quantity":"2"}
1: {"plan":"plan_EbOzfXj7R9hcdz","quantity":"2"}

I want to convert it so that it is in the format that Stripe wants it which is like this 我想将其转换为Stripe想要的格式,如下所示

curl https://api.stripe.com/v1/subscriptions \
  -u sk_test_XXXXXXXXXXXXXXXXXX: \
  -d customer=cus_4fdAW5ftNQow1a \
  -d items[0][plan]=plan_CBXbz9i7AIOTzr \
  -d items[0][quantity]=2
  -d items[1][plan]=plan_IFuCu48Snc02bc \
  -d items[1][quantity]=2

I'm using the fetch method like below 我正在使用如下的获取方法

const response = await fetch("https://api.stripe.com/v1/subscriptions", {
    method: 'post',
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
      "Authorization": "Bearer " + apiKey
    },
    body: encodeBody(customerId, items) //problem here
  });

I have been trying to encode the array into a proper format but everytime I get [Object Object] and the customer id is joined together due to encode.concat 我一直在尝试将数组编码为正确的格式,但是每次我得到[Object Object]时,由于encode.concat,客户ID便合并在一起

    function encodeBody(customerId, items){
      let encoded = "";

      for (let [k, v] of Object.entries(items)) {
      encoded = encoded.concat(k,"=", encodeURI(v), "&");
      }
      encoded = encoded.concat("customer", encodeURI(customerId));
      return encoded;
    }

I'm new to using the encoding function so I don't understand what to do. 我是使用编码功能的新手,所以我不知道该怎么做。

You want your query string's items to be in this format: 您希望查询字符串的items采用以下格式:

items[0][plan]=plan_CBXbz9i7AIOTzr&items[1][plan]=plan_IFuCu48Snc02bc&items[1][quantity]=2

To do that, you want to iterate over both the plans and the properties on each plan . 为此,您要遍历plans和每个plan上的属性。 You can achieve this with this implementation of encodeBody() (uses ES6 since I see you're already using const above): 您可以通过实现encodeBody()来实现此encodeBody()使用ES6,因为我看到您已经在上面使用const ):

function encodeBody(customerId, items) {
  let encoded = '';

  for (let i = 0; i < items.length; i++) {
    const item = items[i];

    for (pair of Object.entries(item)) {
      console.log(pair);
      encoded += `items[${i}][${pair[0]}]=${pair[1]}&`;
    }
  }

  encoded += `customer=${customerId}`;

  return encoded;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在多个计划条带订阅中更新单个计划-WixCode - Update a single plan in a multiple plan stripe subscription - WixCode 如何从 javascript 中的创建订阅 API 调用中检索 Stripe 订阅 ID - How to retrieve the Stripe subscription id from the create subscription API call in javascript 使用Stripe Parse API检索订阅 - Retrieve subscription with Stripe Parse api 通过API添加Stripe订阅 - Adding a Stripe subscription through API 如何使用Stripe JS PHP进行可变数量的递归捐赠计划订阅 - How to do a variable amount recurent donation plan subscription with Stripe JS PHP 如何为多个计划呈现 Paypal 订阅的多个按钮? - How to render multiple button for Paypal subscription for more then one plan? 在 Google Apps 脚本中使用 Stripe API 订阅失败 - Subscription failed using Stripe API in Google Apps Script 使用 Node.js 时,Stripe API 调用会产生多个订阅 - Stripe API call is producing multiple subscriptions when using Node.js 用于Wordpress循环生成的多种表单的Stripe API - Stripe API for multiple forms generated by wordpress loop 如何以固定格式在单个数组中安排多个api响应 - how to arrange multiple api response in single array with in fixed format
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM