【问题标题】:How to use Stripe's secret key and publishable key?如何使用 Stripe 的密钥和可发布密钥?
【发布时间】:2020-05-06 07:16:21
【问题描述】:

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

var https = require('https');

我已经得到了密钥和可发布密钥,并将其放入一个对象中:

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

我现在正在创建 requestDetail 对象:

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 对象:

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 进行任何经过身份验证的调用都可以,因为我需要一个可以工作的示例格式...... 我不太确定在哪里添加密钥和可发布密钥....

【问题讨论】:

  • 秘密密钥将在服务器端使用,可发布密钥将由客户端用于调用任何条带 API,例如生成支付令牌等。
  • 如何使用?有使用 https 的例子吗?

标签: javascript node.js rest https stripe-payments


【解决方案1】:

您应该安装官方的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

【讨论】:

  • 您好,我将其作为一项作业,我认为我不允许使用库...我必须使用 https.... curl 示例没有显示在哪里将键放在标题中,什么键?
  • @preston 你需要使用条带库,
猜你喜欢
  • 2014-03-19
  • 1970-01-01
  • 2018-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-05
  • 2017-03-01
  • 1970-01-01
相关资源
最近更新 更多