How would you convert the following code into Parse REST http request?
curl https://api.stripe.com/v1/charges \
-u {PLATFORM_SECRET_KEY}: \
-H "Stripe-Account: {CONNECTED_STRIPE_ACCOUNT_ID}" \
-d amount=1000 \
-d currency=aud \
-d source={TOKEN}
I've attempted the following but am receiving 401 authorization errors:
Parse.Cloud.define("payMerchantDirect", function(request, response){
Parse.Cloud.httpRequest({
method: "POST",
url: "https://" + {PLATFORM_SECRET_KEY} + ':@' + "api.stripe.com/v1" + "/charges/",
headers: {
"Stripe-Account": request.params.{CONNECTED_STRIPE_ACCOUNT_ID}
},
body: {
'amount': 1000,
'currency': "aud",
'source': request.params.{TOKEN}
},
success: function(httpResponse) {
response.success(httpResponse.text);
},
error: function(httpResponse) {
response.error('Request failed with response code ' + httpResponse.status);
}
});
});
I've triple checked the Stripe keys and IDs used, but alas still not working. Is it correct to place the -u cURL variable in the url?
Cheers, Eric
Solved it.
Another issue was that by adding parameters to the URL resulted in 404 errors.
The solution to this question (Parse.com create stripe card token in cloud code (main.js)) helped with my question.
Basically you call the -u and -H cURL parameters in the httpRequest 'headers'. Making sure you add the 'Bearer' prefix to the {PLATFORM_SECRET_KEY}.
Parse.Cloud.define("payMerchantDirect", function(request, response){
Parse.Cloud.httpRequest({
method: "POST",
url: "https://api.stripe.com/v1/charges",
headers : {
'Authorization' : 'Bearer {PLATFORM_SECRET_KEY}',
'Stripe-Account' : request.params.{CONNECTED_STRIPE_ACCOUNT_ID}
},
body: {
'amount': request.params.amount,
'currency': "aud",
'source': request.params.sharedCustomerToken
},
success: function(httpResponse) {
response.success(httpResponse.data.id);
},
error: function(httpResponse) {
response.error('Request failed with response code ' + httpResponse.status);
}
});
});