编程语言
首页 > 编程语言> > javascript-将JSON添加到Parse.Cloud.httpRequest“ application / x-www-form-urlencoded”

javascript-将JSON添加到Parse.Cloud.httpRequest“ application / x-www-form-urlencoded”

作者:互联网

我一直在尝试在我的云代码功能中实现Stripe的托管帐户.
到目前为止,我设法使其正常工作,但现在遇到了一个似乎无法解决的问题.

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

由于Stripe需要采用表单编码,因此无法将content_type更改为application / JSON.
我试图对JSON进行字符串化,但是当我这样做时,Stripe也抱怨.它期望一个“哈希”,我假设它是一个JSON.

是否可以对JSON进行URL编码,以便我可以在将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);
    });

解决方法:

问题来自以下事实:当您传递具有多个级别的分层对象时,application / x-www-form-urlencoded Content-Type会导致属性的“扁平”键=值列表. JSON知道如何对这样的对象进行编码,而application / x-www-form-urlencoded则不这样做(有几种不同的方法,包括点符号,方括号等).

您应该做的是“拉平” JSON对象,使其只有一个级别,然后使用“扩展”名称作为键.即与其设置包含first_name的legal_entity,不如直接设置legal_entity [first_name](以匹配Stripe使用的格式).

因此,您的身体将是:

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.

标签:parse-platform,stripe-connect,json,javascript
来源: https://codeday.me/bug/20191119/2034287.html