3

我一直在尝试将 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);
    });
4

2 回答 2

3

问题来自这样一个事实,即application/x-www-form-urlencodedContent-Type 导致key=value属性的“平面”列表,而您正在传递具有多个级别的分层对象。JSON 知道如何编码这样的对象,application/x-www-form-urlencoded但不知道(有几种不同的方法可以做到这一点,使用点符号、括号等)。

您应该做的是“扁平化”您的 JSON 对象,使其只有一个级别,并使用“扩展”名称作为键。即直接设置而不是legal_entity包含 a 的 a (以匹配 Stripe 使用的格式)。first_namelegal_entity[first_name]

所以,你的身体会是:

body: 
{   
    "country": country,
    "default_currency": currency,
    "email": email,
    "managed": true,
    "legal_entity[first_name]": firstName,
    "legal_entity[last_name]": lastName,
    "legal_entity[type]": "individual",
    "legal_entity[dob][day]": dobDay,
    "legal_entity[dob][month]": dobMonth,
    "legal_entity[dob][year]": dobYear
    "legal_entity[personal_address][city]": addressCity,
    "legal_entity[personal_address][country]": addressCountry,
    "legal_entity[personal_address][line1]": addressLine,
    "legal_entity[personal_address][postal_code]": addressZIP,
    "legal_entity[personal_address][state]": addressProvince
    "external_account": cardTokenId
}

当然,如果您有更复杂的对象,则可以在代码中“展平”事物,而不是手动进行。

此外,您应该使用Content-Type,而不是content_type

于 2016-01-12T09:52:59.223 回答
0

用于encodeURIComponent将 json 转换为 x-www-form-urlencoded。然后发送请求。

return Parse.Cloud.httpRequest(encodeURIComponent(params));

或者

return Parse.Cloud.httpRequest(encodeURIComponent(JSON.stringify(params)));
于 2016-01-11T11:22:54.280 回答