如何在流星中充电条纹卡 [英] How to charge a stripe card in meteor
问题描述
在尝试用Meteor为卡充值时有温暖的时光.我得到的错误是:Exception while invoking method 'chargeCard' Error: Match error: Expected string, got object
.我确实在输入电子邮件和卡号的地方看到了模态,但是在按下付款按钮后,在终端中我收到了错误消息.
Having a warm time trying to charge a card in Meteor. The error I get is: Exception while invoking method 'chargeCard' Error: Match error: Expected string, got object
. I do get the modal where I typed in the email and card number but after pressing the pay button, in terminal I get the error message.
如何正确调用收费功能?我找不到任何与我的实现方式非常匹配的教程.
How to call the charge function properly? I cant find any tutorial that matches closely the way I implement it.
该设置非常基础.我还安装了jQuery.
The setup is very basic. I also have jquery installed.
模板:
<template name="hello">
<form id="myForm">
<input type="text" id="amount" name="amount"/>
<input type="hidden" id="stripeToken" name="stripeToken"/>
<input type="hidden" id="stripeEmail" name="stripeEmail"/>
</form>
<hr>
<button id="customButton">Pay</button>
</template>
js:
if (Meteor.isClient) {
Template.hello.helpers({
});
Template.hello.events({
'click button': function (e) {
e.preventDefault();
var handler = StripeCheckout.configure({
key: 'pk_test_rand',
token: function(token) {
$("#stripeToken").val(token.id);
$("#stripeEmail").val(token.email);
$("#myForm").submit();
Meteor.call('chargeCard', token); // this seem not right?
}
});
// Showing the pop up Stripe dialog
var amount = $("#amount").val() *100;
// Open Checkout with further options
handler.open({
name: 'Demo Site',
description: '2 widgets ($20.00)',
amount: amount
});
// Close Checkout on page navigation
$(window).on('popstate', function() {
handler.close();
});
}
});
Meteor.startup(function(){
$.getScript('https://checkout.stripe.com/checkout.js', function(){
// script has loaded
});
});
}
if (Meteor.isServer) {
Meteor.methods({
'chargeCard': function(stripeToken) {
check(stripeToken, String);
var Stripe = StripeAPI('sk_test_rand');
Stripe.charges.create({
source: stripeToken,
amount: 5000, // this is equivalent to $50
currency: 'usd'
}, function(err, charge) {
console.log(err, charge);
});
}
});
}
推荐答案
似乎您正在传递整个令牌对象:
It seems you're passing the whole token object:
Meteor.call('chargeCard', token);
但是您的chargeCard()
方法需要一个字符串:
But your chargeCard()
method expects a string:
check(stripeToken, String);
因此您只需要传递令牌 id :
So you need to either pass only the token id:
Meteor.call('chargeCard', token.id);
或更改您的chargeCard()
方法以期望并使用整个令牌对象:
or change your chargeCard()
method to expect and use the whole token object:
Meteor.methods({
'chargeCard': function(stripeToken) {
check(stripeToken, Object);
var Stripe = StripeAPI('sk_test_rand');
Stripe.charges.create({
source: stripeToken.id,
...
这篇关于如何在流星中充电条纹卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!