简体   繁体   中英

Convert Stripe cURL code to Parse.Cloud.httpRequests

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM