Reputation: 15
I'm trying to get Stripe working with the new promises support.
Using checkout, I get the token and send it to the server:
Meteor.call('submit_charge', res.id, fee, name, reg, function (err, res) {
console.log(err, res);
});
The server method is defined as:
submit_charge: function(tok, amt, name, reg) {
var Stripe = StripeAPI('privatekey');
console.log('Submitting charge for ' + name);
Stripe.charges.create({
amount: amt,
currency: "usd",
card: tok,
description: "Payment - " + name,
metadata: {
'reg': reg
},
}).then(function(charge) {
console.log('Charge: ' + charge.id);
return charge.id;
}, function(err) {
console.log('Error: ' + err);
return 0;
});
}
I can call the method and it executes, but doesn't return anything. The console.log(err, res)
in the Meteor.call
returns undefined for both.
The charge processes... and the console.logs show the charge ID from Stripe, so it doesn't seem to be an async issue.
Am I missing something incredibly basic here?
Thanks for the help!
Upvotes: 1
Views: 601
Reputation: 75975
You have to use synchronous javascript:
submit_charge: function(tok, amt, name, reg) {
var Stripe = StripeAPI('privatekey');
console.log('Submitting charge for ' + name);
var createCharge = Meteor._wrapAsync(Stripe.charges.create.bind(Stripe.charges));
try {
var result = createCharge({
amount: amt,
currency: "usd",
card: tok,
description: "Payment - " + name,
metadata: {
'reg': reg
});
return result;
}
catch(e) {
//Error
console.log(e);
}
}
Basically you're trying to return data from within a callback. You need to return
data to your meteor method and not the function in the then
.
Using Meteor._wrapAsync
uses Fibers to wait until the transaction is complete then returns the value or throws an error (hence the try/catch) to get the error.
Upvotes: 2