解析服务器云代码Node.js兼容性

时间:2016-10-11 22:52:34

标签: javascript ios node.js parse-platform server

我正在尝试开发电子商务iOS应用程序。

我想知道是否可以使用Parse Server + Stripe创建它。我需要服务器端代码来创建客户,向客户收费等等。

我可以在我的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);
    }
  })
});

1 个答案:

答案 0 :(得分:1)

您可以在解析服务器中使用stripe node.js模块。

首先,您需要使用

安装模块
npm install stripe

或将其添加到package.js文件

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

然后,在您的cloud / main.js文件中,您可以编写一个iOS应用可以调用的函数

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

在iOS端,你可以这样调用这个函数:

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

您希望将一些用户信息发送到@" parameterKey":@" parameterValue"

有关条带节点模块here的更多信息。

我希望有所帮助。

相关问题