将 Stripe cURL 代码转换为 Parse.Cloud.httpRequests

Convert Stripe cURL code to Parse.Cloud.httpRequests

如何将以下代码转换为 Parse REST http 请求?

curl https://api.stripe.com/v1/charges \
-u {PLATFORM_SECRET_KEY}: \
-H "Stripe-Account: {CONNECTED_STRIPE_ACCOUNT_ID}" \
-d amount=1000 \
-d currency=aud \
-d source={TOKEN}

我尝试了以下操作,但收到 401 授权错误:

Parse.Cloud.define("payMerchantDirect", function(request, response){
Parse.Cloud.httpRequest({
  method: "POST",
  url: "https://" + {PLATFORM_SECRET_KEY} + ':@' + "api.stripe.com/v1" + "/charges/",
  headers: {
        "Stripe-Account": request.params.{CONNECTED_STRIPE_ACCOUNT_ID}
  },
  body: {
        'amount': 1000,
        'currency': "aud",
        'source': request.params.{TOKEN}
  },
  success: function(httpResponse) {
            response.success(httpResponse.text);
            },
  error: function(httpResponse) {
            response.error('Request failed with response code ' +     httpResponse.status);
            }
  });
});

我已经对使用的 Stripe 密钥和 ID 进行了三次检查,但仍然无法正常工作。将 -u cURL 变量放在 url 中是否正确?

干杯, 埃里克

这是我将 cURL 请求转换为 httpRequest 的示例:

cURl:

curl https://api.stripe.com/v1/transfers \
   -u [secret api key]: \
   -d amount=400 \
   -d destination=[account id]\ 
   -d currency=usd

http请求:

Parse.Cloud.httpRequest
(
    {
        method:"POST",
        url: "https://" + STRIPE_SECRET_KEY + ':@' + STRIPE_API_BASE_URL + "/transfers?currency=usd&amount=" + amountOwedProvider.toString() + "&destination=" + recipient_id
    }
)

这并不性感,使用 header/body 可能有更好的方法,但基本上我设置了 URL,然后是?在所有参数之前,以及参数之间的符号 (&)。

但是,您似乎只是在创建费用。您可以为此使用 Parse 的 Stripe 模块。

此外,对于关联帐户,您应该将该帐户设置为收费目的地,或者很少发生一次性从您的 stripe 帐户转账到他们帐户的情况,而不是使用收费。这是出于税收目的。

解决了。

另一个问题是,将参数添加到 URL 会导致 404 错误。

这个问题的解决方案(Parse.com create stripe card token in cloud code (main.js))对我的问题有帮助。

基本上您在 httpRequest 'headers' 中调用 -u 和 -H cURL 参数。确保将 'Bearer' 前缀添加到 {PLATFORM_SECRET_KEY}.

Parse.Cloud.define("payMerchantDirect", function(request, response){
Parse.Cloud.httpRequest({
  method: "POST",
  url: "https://api.stripe.com/v1/charges",
  headers : {
    'Authorization' : 'Bearer {PLATFORM_SECRET_KEY}',
    'Stripe-Account' : request.params.{CONNECTED_STRIPE_ACCOUNT_ID}
  },
  body: {
        'amount': request.params.amount,
        'currency': "aud",
        'source': request.params.sharedCustomerToken
  },
  success: function(httpResponse) {
            response.success(httpResponse.data.id);
            },
            error: function(httpResponse) {
            response.error('Request failed with response code ' + httpResponse.status);
            }
  });
});