Wrapping Stripe 在 Meteor 的 Fibers 中创建客户回调 [英] Wrapping Stripe create customer callbacks in Fibers in Meteor
问题描述
我在创建新客户时无法让 Stripe.js 正常工作.这是他们教程中的 Node.js 代码:
I'm having trouble getting Stripe.js to work when creating a new customer. Here is their Node.js code in their tutorial:
// Set your secret key: remember to change this to your live secret key in production
// See your keys here https://dashboard.stripe.com/account/apikeys
var stripe = require("stripe")("sk_test_9999999999999999999999");
// (Assuming you're using express - expressjs.com)
// Get the credit card details submitted by the form
var stripeToken = request.body.stripeToken;
stripe.customers.create({
source: stripeToken,
description: 'payinguser@example.com'
}).then(function(customer) {
return stripe.charges.create({
amount: 1000, // amount in cents, again
currency: "usd",
customer: customer.id
});
}).then(function(charge) {
saveStripeCustomerId(user, charge.customer);
});
这是我的尝试.我将所有回调都封装在 Meteor.bindEnvironment
中,因为异步回调需要在纤程中运行.我在服务器控制台中收到错误消息:
This is my attempt. I wrapped all the callbacks in Meteor.bindEnvironment
because async callbacks need to run in a fiber. I get an error in the server console:
Exception while invoking method 'submitOrder' Error: Stripe: Unknown arguments (function (/* arguments */) {
有人能指出我用纤维包裹它的正确方向吗?还是使用 Meteor.wrapAsync?
Can anyone point me in the right direction for wrapping this in fibers? Or utilizing Meteor.wrapAsync?
var createStripeCustomer = function(ShoppingCartObject){
check(ShoppingCartObject, Object);
var stripe = Stripe("sk_test_9999999999999999");
// (Assuming you're using express - expressjs.com)
// Get the credit card details submitted by the form
var stripeToken = ShoppingCartObject.charge.token;
stripe.customers.create(
{
source: stripeToken,
description: ShoppingCartObject._id,
email: ShoppingCartObject.customerInfo.agentEmail,
},
Meteor.bindEnvironment(function(customer){
return stripe.charges.create({
amount: ShoppingCartObject.totalPrice, // amount in cents, again
currency: "usd",
customer: customer.id
});
}),
Meteor.bindEnvironment(function(charge){
ShoppingCartObject.charge.customer = charge.customer;
submitOrder(ShoppingCartObject);
})
);
};
var submitOrder = function(ShoppingCartObject){
check(ShoppingCartObject, Object);
var data = _.omit(ShoppingCartObject, '_id');
var setHash = { $set: data };
ShoppingCarts.update({_id: ShoppingCartObject._id}, setHash);
};
推荐答案
这是对我有用的方法的简化版本.我基本上为每个 Stripe 调用创建了一个返回 Future 的函数.
Here is a simplified version of an approach that has worked for me. I basically create a function for each Stripe call that returns a Future.
// Server
var Future = Npm.require('fibers/future');
function createCustomer(token){
var future = new Future;
Stripe.customers.create({
card: token.id,
email: token.email
}, function(error, result){
if (error){
future.return(error);
} else {
future.return(result);
}
});
return future.wait();
}
Meteor.methods({
purchase: function(token){
check(token: Object);
try {
var customer = createCustomer(token);
} catch(error) {
// error handle
}
// create charge, etc. repeating same pattern
}
});
这篇关于Wrapping Stripe 在 Meteor 的 Fibers 中创建客户回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!