条带式开机自检操作的正文格式



我直接使用 REST API 访问条纹 API(不使用库),但令人惊讶的是,我找不到有关适当正文格式发布数据的文档。

Stripe 是否需要 JSON 或表单编码对?

您需要发布原始数据,例如键值对。(无 json)

例如键 1=值 1&键 2=值 2

确保在标题中包含以下内容Content-Type = application/x-www-form-urlencoded

这是 Nodejs 的 curl 代码示例我正在处理类似的问题

如您所知,我们无法为"post"方法发送JSON。它必须是 URL 编码的

这是条纹提供的示例https://stripe.com/docs/api/checkout/sessions/create

curl https://api.stripe.com/v1/checkout/sessions 
>   -u stripe_key_here: 
>   -d success_url="https://example.com/success" 
>   -d cancel_url="https://example.com/cancel" 
>   -d "payment_method_types[]=card" 
>   -d "line_items[][name]=T-shirt" 
>   -d "line_items[][description]=Comfortable cotton t-shirt" 
>   -d "line_items[][amount]=1500" 
>   -d "line_items[][currency]=usd" 
>   -d "line_items[][quantity]=2"

-u 表示我们在标头中提供的授权
-d 表示网址的正文

节点 JS 中的示例代码


const fetch = require("node-fetch");
const stripeKey = process.env.STRIPE_KEY;
async function getCheckoutID() {
  try {
    const endpoint = "https://api.stripe.com/v1/checkout/sessions";
    const query = objToQuery({
      success_url: "https://example.com/success",
      cancel_url: "https://example.com/cancel",
      "payment_method_types[]": "card",
      "line_items[][name]": "T-shirt",
      "line_items[][description]": "Comfortable cotton t-shirt",
      "line_items[][amount]": 1500,
      "line_items[][quantity]": 1,
      "line_items[][currency]": "usd"
    });
    // enpoint => "https://www.domin.com/api"
    // query => "?key=value&key1=value1"
    const URL = `${endpoint}${query}`;
    const fetchOpts = {
      method: "post",
      headers: {
        "Authorization": `Bearer ${stripeKey}`,
        "Content-Type": "application/x-www-form-urlencoded"
      }
    }
    const checkout = await getJSON(URL, fetchOpts);
    console.log("CHECKOUT OBJECT : " , checkout);
    return checkout;
  }
  catch(e) {
      console.error(e);
   }
}

// hepler functions
// instead of using fetch
// getJSON will make it readable code
async function getJSON(url, options) {
  const http = await fetch(url, options);
  if (!http.ok) {
    console.log(http);
    throw new Error("ERROR STATUS IS NOT OK :");
  }
  return http.json();
}
// convert JS object to url query
// {key:"value" , key1: "value1"} to "key=value&key1=value1"
function objToQuery(obj) {
  const query = Object.keys(obj).map( k => `${k}=${obj[k]}`).join("&");
  return query.length > 0 ? `?${query}` : "";
}

表单编码对

cURL 的文档提供了很好的示例。它们只是通过命令行上的 cURL 通过-d开关提供表单编码的键/值对。只要确保你使用你的测试密钥,这样你就不会搞砸任何实时数据。然后你可以玩任何你想要的。

返回的数据是 JSON

以下是使用测试卡的 cURL 费用示例:

curl https://api.stripe.com/v1/charges -u your_test_key: -d card[number]=4000056655665556 -d card[exp_month]=11 -d card[exp_year]=2020 -d amount=100 -d currency=eur -d description="Stripe cUrl charge test"

使用条带测试密钥交换your_test_key。请记住在密钥后保留 : 以免被要求输入密码。

最新更新