我一直在尝试将 Stripe 的托管帐户实施到我的云代码功能中。到目前为止,我设法使它工作,但现在我遇到了一个我似乎无法解决的问题。
归结为:使用'content_type': 'application/x-www-form-urlencoded'
我如何发送 JSON?
无法将 content_type 更改为,application/JSON
因为 Stripe 需要一个 form-urlencoded。我试图对 JSON 进行字符串化,但当我这样做时,Stripe 也会抱怨。它需要一个“哈希”,我假设它是一个 JSON。
是否可以对 JSON 进行 url 编码,以便我可以在将 content_type 设置为 form-urlencoded 的同时发送它?
我当前的代码不起作用,因为 Parse 说:
未捕获的错误:无法对对象进行编码
var secret_key = stripeKeys.stripeSecretKey;
var cardTokenId = "tok_...";
var country = "BE";
var currency = "EUR";
var email = "test@test.org";
var firstName = "test";
var lastName = "tester";
var dobDay = 1;
var dobMonth = 1;
var dobYear = 1950;
var addressCity = "City";
var addressCountry = "Country";
var addressLine = "Address line";
var addressZIP = "ZIP";
var addressProvince = "Province";
var createAccountPromise = function()
{
var params =
{
url: "https://api.stripe.com/v1/accounts",
method: "POST",
headers:
{
"Authorization": "Basic " + new Buffer(secret_key + ":").toString("base64"),
"content_type": "application/x-www-form-urlencoded"
},
body:
{
"country": country,
"default_currency": currency,
"email": email,
"managed": true,
"legal_entity":
{
"first_name": firstName,
"last_name": lastName,
"type": "individual",
"dob":
{
"day": dobDay,
"month": dobMonth,
"year": dobYear
},
"personal_address":
{
"city": addressCity,
"country": addressCountry,
"line1": addressLine,
"postal_code": addressZIP,
"state": addressProvince
}
},
"external_account": cardTokenId
}
};
return Parse.Cloud.httpRequest(params);
}
createAccountPromise()
.then(function(result)
{
console.log("SUCCESS: " + result.text);
response.success("Account Created");
},
function(errorReason)
{
console.log("ERROR: " + errorReason.text);
response.error("Account NOT Created because: " + errorReason.text);
});