2

So I am just about finished with integrating Stripe Connect with Parse Cloud Code and a Django web app.

Currently, Parse hasn't implemented the Stripe module method to generate tokens given an access token and a customer id. So I need to do this myself.

I run the cURL command that Stripe API provides you to see an example response, here it is,

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

So I get a response and everything is going good. But I am now trying to mimic this behavior within Parse.Cloud.httpRequest.

Here is my attempt to generate the command:

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

The response returns 'Creating token with stripe failed. Error: [object Object]' the message comes from:

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

My issue is generally generating the httpRequest. Anyone have any ideas on how to create the correct httpRequest?

jsetting32
  • 1,632
  • 2
  • 20
  • 45

3 Answers3

1

A more typical form is to return the promise created by the http request.

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

There may be some other issue having to do with the web service's requirements for a well-formed call, but this at least will make the call and return a promise for the result.

danh
  • 62,181
  • 10
  • 95
  • 136
1

So I solved the issue and wish I could have looked back at the posted answers before coming up with this. But hey! it works :)...

So here is how I generate the 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.');
});

And the retrieveToken method is:

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

I added the access token as a header but apparently, this way works (adding it before the stripe address). Not sure how secure it is and would love feedback! Thats the last thing i need is being sued for sending sensitive data insecurely or whatever.

jsetting32
  • 1,632
  • 2
  • 20
  • 45
0

You could use your curl command with the -v option to find out exactly how it sends out the request. It seems that it uses HTTP basic authentication.

So you need to create an Authorization header, with base64 encoding, something like this in your code:

header : { "Authorization" : "Basic "+btoa(accessToken+":") }
Balint Domokos
  • 1,021
  • 8
  • 12