繁体   English   中英

Stripe的secret key和publishable key如何使用?

[英]How to use Stripe's secret key and publishable key?

我想使用 https 库使用 https 调用连接到 Stripe API。

var https = require('https');

我已经得到了秘密密钥和可发布密钥并将其放入 object 中:

var stripe = {
  secret_key: 'secret_key_given_in_the_dashboard',
  publishable_key: 'publishable_key_given_in_the_dashboard'
}

我现在正在创建 requestDetail object:

var requestDetails = {
    'protocol'  : 'https:',
    'hostname'  : 'api.stripe.com',
    'method'    : 'POST', //WHICH OF POST GET PUT DELETE SHOULD I USE?
    'path'      : '???????????????????????',// WHICH ENDPOINT SHOULD I USE?
    'auth'      : '???????????????????????',// SHOULD I USE THE SECRET AND PUBLISHABLE KEY HERE?
    'headers'   : {
      'Content-Type'  : 'application/x-www-form-urlencoded',
      'Content-Length' : Buffer.byteLength(stringPayload)
    }
  };

我计划在使用 https 的调用中使用 requestDetails object:

var req = https.request(requestDetails, function(res){
      // Grab the status of the sent request
      var status = res.statusCode;
      //Callback successfully if the request went through
      if(status == 200 || status == 201) {
        callback(false);
      } else {
        callback('Status code returned was ' + status);
      }
    });

我应该在哪里以及如何使用密钥和可发布密钥来调用条带 API? 哪个端点? 哪种方法(POST、GET、PUT 或 DELETE)?

我想最终创建一个订单并通过 STRIPE api 付款。但是现在只要通过 api 条带的任何经过身份验证的调用都可以,因为我需要一个有效的示例格式....我不太确定在哪里添加秘密密钥和可发布密钥....

您应该安装官方stripe包(来源: https : //github.com/stripe/stripe-node ),需要该包并使用您的密钥(来自 github 文档的示例)对其进行身份验证:

const stripe = require('stripe')('your_stripe_secret_key');

stripe.customers.create({
  email: 'customer@example.com',
})
.then(customer => console.log(customer.id))
.catch(error => console.error(error));

该包是为您发出 API 请求的抽象。

更多文档: https : //stripe.com/docs/api? lang =node

但是,如果您想直接将https用于 Stripe API 请求(不推荐),您可以查看使用 cURL 的文档和示例,因为它显示了每个示例的端点。

https://stripe.com/docs/api/authentication?lang=curl

尝试使用 fetch, 'Authorization': 'Bearer ' + sk

我基于customer_id检索客户的工作示例:

const url = `https://api.stripe.com/v1/customers/${stripe_customer_id}`;
return await fetch(url, {
  method: "get",
  headers: {
    "Content-Type": "application/json",
    'Authorization': 'Bearer ' + sk,
  }
})
  .then(function(response) {
    return response.json();
  })
  .then(function(response) {
    // console.log(response);
    return response;
  });
};

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM