添加 JSON 到 Parse.Cloud.httpRequest "application/x-www-form-urlencoded"

Add JSON to Parse.Cloud.httpRequest "application/x-www-form-urlencoded"

我一直在尝试将 Stripe 的托管帐户实施到我的云代码功能中。 到目前为止我设法让它工作,但现在我 运行 遇到了一个我似乎无法解决的问题。

归结为: 使用 'content_type': 'application/x-www-form-urlencoded' 如何发送 JSON?

无法将 content_type 更改为 application/JSON,因为 Stripe 需要编码形式 url。 我试图对 JSON 进行字符串化,但当我这样做时,Stripe 也会抱怨。它需要 'hash',我假设是 JSON。

是否可以 url 编码一个 JSON 以便我可以发送它同时仍然将 content_type 设置为 form-urlencoded?

我当前的代码不起作用,因为 Parse 说:

Uncaught Error: Can't form encode an Object

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);
    });

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

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

或者

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

问题来自于 application/x-www-form-urlencoded Content-Type 导致 "flat" key=value 属性列表,而您传递的是分层 object 具有多个级别。 JSON 知道如何编码这样的 object,application/x-www-form-urlencoded 不知道(有几种不同的方法可以做到这一点,使用点符号、括号等)。

你应该做的是 "flatten" 你的 JSON object 这样它只有一层,并使用 "expanded" 名称作为键。 IE。而不是让 legal_entity 包含 first_name,直接设置 legal_entity[first_name](以匹配 Stripe 使用的格式)。

因此,您的 body 将是:

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
}

当然,如果您有更复杂的 object,可以在代码中 "flatten" 而不是手动完成。

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