简体   繁体   中英

How to charge a stripe card in meteor

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.

The setup is very basic. I also have jquery installed.

Template:

<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);

But your chargeCard() method expects a string:

check(stripeToken, String);

So you need to either pass only the token id :

Meteor.call('chargeCard', token.id);

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,
            ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM