gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:38:46 25 4
gpt4 key购买 nike

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

最佳答案

您可以在解析服务器中使用 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"

有关 strip Node 模块的更多信息 here .

希望对您有所帮助。

关于javascript - 解析服务器云代码 Node.js 兼容性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39987991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com