0

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

Eric
  • 27
  • 6

2 Answers2

0

Here's an example where I turned a cURL request into httpRequest:

cURl:

curl https://api.stripe.com/v1/transfers \
   -u [secret api key]: \
   -d amount=400 \
   -d destination=[account id]\ 
   -d currency=usd

httpRequest:

Parse.Cloud.httpRequest
(
    {
        method:"POST",
        url: "https://" + STRIPE_SECRET_KEY + ':@' + STRIPE_API_BASE_URL + "/transfers?currency=usd&amount=" + amountOwedProvider.toString() + "&destination=" + recipient_id
    }
)

It's not sexy and there is probably a better way using the header/body, but basically I set up the URL, then a ? before all the parameters, and an ampersand (&) between the parameters.

However, it looks like you're just creating a charge. You could use Parse' Stripe module for that.

Also, with connected accounts, you're supposed to either set the account as a destination for the charge, or have very few occurrences of one time transfers from your stripe account to theirs, rather than using a charge. This is for tax purposes.

Jake T.
  • 4,308
  • 2
  • 20
  • 48
  • Thanks Jake, Yes this is for connected account charges. What I'm trying to accomplish is the first method as described in: [link](https://stripe.com/docs/connect/payments-fees#charging-directly) I have managed to charge a customer and transfer the funds to a destination. However the payments still get handled through the platform account, I would like the connected accounts to manage all that. Hence the -H variable in my example curl code. – Eric Nov 06 '15 at 09:21
0

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);
            }
  });
});
Community
  • 1
  • 1
Eric
  • 27
  • 6