将cURL转换为Parse.Cloud.httpRequest

时间:2015-03-18 06:39:18

标签: javascript parse-platform cloud-code

所以我刚刚完成了将Stripe Connect与Parse Cloud Code和Django Web应用程序集成。

目前,Parse尚未实施Stripe模块方法,以在给定访问令牌和客户ID的情况下生成令牌。所以我需要自己做。

我运行了Stripe API为您提供的cURL命令,以查看示例响应,此处为

curl https://api.stripe.com/v1/tokens \
   -u theaccesstoken: \
   -d customer=customersid

所以我得到了回复,一切都很顺利。但我现在正试图在Parse.Cloud.httpRequest中模仿这种行为。

这是我尝试生成命令:

var retrieveToken = function(url, accessToken, customerId) {
    var promise = new Parse.Promise();
    Parse.Cloud.httpRequest({
        method: 'POST',
        header : {'access_token' : accessToken},
        url: url, 
        body : {'customer':customerId},
        success: function(httpResponse) {
            promise.resolve(httpResponse);
        },
        error: function(httpResponse) {
            promise.reject(httpResponse);
        }
    });
    return promise;
}

响应返回'创建带有条带的令牌失败。错误:[object Object]'消息来自:

return retrieveToken(tokenURL, accessToken, customerId).then(null, function(error) {
    console.log('Creating token with stripe failed. Error: ' + error);
    return Parse.Promise.error('An error has occurred. Your credit card was not charged.');
});

我的问题通常是生成httpRequest。任何人对如何创建正确的httpRequest有任何想法?

3 个答案:

答案 0 :(得分:1)

更典型的形式是返回http请求创建的承诺。

var retrieveToken = function(url, accessToken, customerId) {
    var params = { method: 'POST',
                   header : {'access_token' : accessToken},
                   url: url,
                   body : {'customer':customerId} };
    // return the promise that is created (and fulfilled) by the httpRequest
    return Parse.Cloud.httpRequest(params);
}

return retrieveToken(tokenURL, accessToken, customerId).then(function(result) {
    console.log('success ' + JSON.stringify(result));
}, function(error) {
    console.log('Creating token with stripe failed. Error: ' + error.message);
    return Parse.Promise.error('An error has occurred. Your credit card was not charged.');
});

可能还有一些其他问题与Web服务对格式良好的调用的要求有关,但这至少会调用并返回结果的承诺。

答案 1 :(得分:1)

所以我解决了这个问题,并希望在提出这个问题之前我可以回顾一下已发布的答案。但是嘿!它有效:)......

以下是我如何生成httpRequest:

var customerId = currentUser.get('stripeCustomerId');
var accessToken = vendor.get('stripeAccessToken');
var tokenURL = 'https://'+accessToken+':@api.stripe.com/v1/tokens';
return retrieveToken(tokenURL, customerId).then(null, function(error) {
    console.log('Creating token with stripe failed. Error: ' + error);
    return Parse.Promise.error('An error has occurred. Your credit card was not charged.');
});

retrieveToken方法是:

var retrieveToken = function(url, customerId) {
    var promise = new Parse.Promise();
    Parse.Cloud.httpRequest({
        method: 'POST',
        url: url, 
        header: 'content-type: application/json',
        body: {'customer' : customerId},
        success: function(httpResponse) {
            promise.resolve(httpResponse);
        },
        error: function(error) {
            promise.error(error);
        }
    });
    return promise;
}

我添加了访问令牌作为标题,但显然,这种方式有效(在条带地址之前添加它)。不确定它有多安全,并且会喜欢反馈!这就是我需要的最后一件事就是因不安全或其他原因而被起诉敏感数据。

答案 2 :(得分:0)

您可以将curl命令与-v选项一起使用,以确切了解它是如何发出请求的。它似乎使用HTTP基本身份验证。

因此,您需要在代码中使用base64编码创建一个Authorization标头:

header : { "Authorization" : "Basic "+btoa(accessToken+":") }