【问题标题】:Parse Server Cloud Code Node.js compatibility解析服务器云代码 Node.js 兼容性
【发布时间】:2025-11-21 16:25:03
【问题描述】:

我正在尝试开发一个电子商务 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);
    }
  })
});

【问题讨论】:

  • 您的 iOS 应用程序将使用 API 与您的后端通信 - 无论是 RESTful API,还是像 GraphQL 这样的冒险工具。您可以使用任何您想要的语言构建该 API,但 Node.js 绝对是一个很好的起点。也就是说,如果您不熟悉 Node 或整个 JavaScript,那么您最好从头开始阅读它们。

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


【解决方案1】:

您可以在解析服务器中使用条带 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的更多信息。

希望对你有帮助。

【讨论】:

  • 那么来自 Stripe docs on node.js 的参考实际上是 JavaScript 代码,应该在服务器端?这就是我在阅读有关 node.js 和 JavaScript 后所理解的。
  • 是的。您需要服务器端来执行实际的事务或操作。所以服务端可以是node.js或者PHP等。Node.js是服务端环境,语言是Javascript。