简体   繁体   中英

Stripe payment with node js

My stack: Frontend: react, react-stripe module Backend: nodejs with Express 4.x

I've been following the examples on https://stripe.com/docs/charges

Heres my frontend code which gets triggered after the user clicks "Pay":

  onToken(token){
    console.log("Token " , token);
    $.post('/apicall', token, function(result){
      console.log(result);
    })
  }

And here's my server code that receives the data and should handle the test payment:

router.post('/payment/barf-complete', function(req, response, next){
  console.log(req.body);
  var stripeToken = req.body;

  var charge = stripe.charges.create({
    amount: 1990, // amount in cents, again
    currency: "eur",
    source: stripeToken,
    description: "Example charge"
    }, function(err, charge) {
    if (err && err.type === 'StripeCardError') {
      // The card has been declined
      console.log(err);
    }
  });
});

I had to change var stripeToken = request.body.stripeToken; into var stripeToken = request.body; because the first variable declaration kept being undefined.

Now the issue is that I keep getting the following error on my server right after the payment got done in the frontend:

rawType: 'card_error',
code: 'invalid_number',
param: 'number',
message: 'The card object must have a value for \'number\'.',

I have never used a payment system before and the documentation doesn't provide any useful information for me - :/

Any ideas what I might have missed?

EDIT Heres a console.log of request.body:

{ id: 'tok_123456789',
  object: 'token',
  'card[id]': 'card_123456789',
  'card[object]': 'card',
  'card[address_city]': '',
  'card[address_country]': '',
  'card[address_line1]': '',
  'card[address_line1_check]': '',
  'card[address_line2]': '',
  'card[address_state]': '',
  'card[address_zip]': '',
  'card[address_zip_check]': '',
  'card[brand]': 'Visa',
  'card[country]': 'US',
  'card[cvc_check]': 'pass',
  'card[dynamic_last4]': '',
  'card[exp_month]': '1',
  'card[exp_year]': '2017',
  'card[funding]': 'credit',
  'card[last4]': '4242',
  'card[name]': 'noa@mail.com',
  'card[tokenization_method]': '',
  client_ip: '12334567',
  created: '1457075159',
  email: 'noa@mail.com',
  livemode: 'false',
  type: 'card',
  used: 'false' }

req.body是整个令牌对象,但是Stripe只需要令牌对象的id ,因此将source设置为req.body.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