Bogdan Pop
Bogdan Pop

Reputation: 438

Parse Server Cloud Code Node.js compatibility

I am trying to develop an ecommerce iOS application.

I was wondering if it is possible to create this using Parse Server + Stripe. I would need server-side code to create customers, charge customers, etc.

Could I get similar functions like this in my Cloud Code?

// Using Express (http://expressjs.com/)
app.get('/customer', function(request, response) {
  var customerId = '...'; // Load the Stripe Customer ID for your logged in user
  stripe.customers.retrieve(customerId, function(err, customer) {
    if (err) {
      response.status(402).send('Error retrieving customer.');
    } else {
      response.json(customer);
    }
  })
});

Upvotes: 0

Views: 648

Answers (1)

mogile_oli
mogile_oli

Reputation: 2188

You can use the stripe node.js module in your parse server.

First you'll need to install the module using

npm install stripe

or adding it to you package.js file

...
"dependencies": {
    "express": "^4.13.4",
    "parse-server": "^2.2.19",
    "stripe": "^4.11.0",
...

Then, in your cloud/main.js file, you can write a function that your iOS app can call

Parse.Cloud.define("yourCloudFunctionName", function(request, response){
    // You can retreive the user info from your request.params
    var user = request.params.user;

    // Call your stripe package using your API key
    var stripe = require('stripe')(' your stripe API key ');

    var email = request.params.email;
    // Maybe you want to create a customer using the parse email?
    stripe.customers.create(
    { email: email },
      function(err, customer) {
      err; // null if no error occurred 
      customer; // the created customer object 
      // You'll need to return something to the iOS code...
      if(err) return err;
      else return customer;
  }
);

On the iOS side, you can call the function this way:

[PFCloud callFunctionInBackground:@"yourCloudFunctionName"
         withParameters:@{@"parameterKey": @"parameterValue"}
         block:^(NSArray *results, NSError *error) {
  if (!error) {
     // this is where you handle the results and change the UI.
  }
}];

You'll want to send some user info into @"parameterKey": @"parameterValue"

More info on the stripe node module here.

I hope that helps.

Upvotes: 1

Related Questions