简体   繁体   English

stripe.js:调用stripe.charges.create的api调用未返回响应

[英]stripe.js: api call that calls stripe.charges.create isn't returning a response

I'm trying to use stripe.js and return the created charge after saving it on the server. 我正在尝试使用stripe.js并在将其保存到服务器后返回创建的费用。 The stripe charge saves successfully and is saved in my stripe test account, but it seems like the callback isn't working. 条带费用已成功保存,并保存在我的条带测试帐户中,但是回调似乎无法正常工作。 From my code below, the console.log('success') gets called, but the "then" callback isn't called, and the console.log('savedChanges') doesn't get called either. 从下面的代码中,会调用console.log('success'),但不会调用“ then”回调,也不会调用console.log('savedChanges')。 Am I doing something wrong? 难道我做错了什么? I was following https://stripe.com/docs/tutorials/charges for reference. 我正在遵循https://stripe.com/docs/tutorials/charges供参考。

/app/scripts/controllers/stripe.js /app/scripts/controllers/stripe.js

'use strict';

app.controller('StripeCtrl', function ($scope, $location, $http) {
  $scope.charge = {};
  $scope.successfulCharge = null;

  $scope.submitCharge = function(status, response){
    var $form = $('#payment-form');

    if (response.error) {
      $form.find('.payment-errors').text(response.error.message);
      $form.find('button').prop('disabled', false);
    } else {
      var token = response.id;
      var data = {
        amount: ($scope.charge.amount * 100),
        card: token,
        description: "2014 missions donation for " + $scope.charge.missionary,
        metadata: {
          'email': $scope.charge.email,
          'missionary': $scope.charge.missionary
        }
      };

      $http.post('/api/stripe/submitCharge', data).success(function(data, status, headers) {
        console.log('submitCharge success!!!');
        console.log(data);
        $scope.successfulCharge = data;
      });
    }
  }

  $scope.getCharges = function(){
    $http.get('/api/charges').success(function(charges) {
      return charges;
    });
  }
});

/lib/controllers/api.js /lib/controllers/api.js

'use strict';

var mongoose = require('mongoose'),
    config = require('../config/config'),
    stripe = require('stripe')(config.stripe.secret_key),
    Charge = mongoose.model('Charge');

exports.charges = function(req, res) {
  return Charge.find(function (err, charges) {
    if (!err) {
      return res.json(charges);
    } else {
      return res.send(err);
    }
  });
};

exports.publishable_key = function(req, res){
  return res.send(config.stripe.publishable_key);
};

exports.submitCharge = function(req, res){
  var savedCharge;
  var result = stripe.charges.create({
    amount: req.body.amount,
    currency: "usd",
    card: req.body.card,
    description: req.body.description
  }, function(err, charge) {
    if (err) {
      console.log('errors');
      console.log(err);
    } else {
      console.log('success');
    }
  }).then(function(charge){
    savedCharge = Charge.create({
      name: charge.card.name,
      amount: charge.amount,
      email: charge.metadata.email,
      address: charge.card.address_line1 + charge.card.address_line1,
      city: charge.card.address_city,
      state: charge.card.address_city,
      zip: charge.card.address_zip,
      tax_receipt: charge.metadata.tax_receipt,
      missionary: charge.metadata.missionary,
    });
  });

  console.log('savedCharge');
  console.log(savedCharge);
  return res.send(savedCharge);
};

It looks like the Stripe API gives you the ability to use a callback or a promise, but you're using both in your call to charges.create : 看起来Stripe API使您能够使用回调 Promise,但是在charges.create的调用中同时使用了两者:

var result = stripe.charges.create({
 ...data...
}, function (err, charge) { // <-- callback
 ...
}).then(function(charge) { // <-- promise
 ...
})

You need to choose one or the other. 您需要选择一个。 That is, either put the call to Charge.create inside the callback from stripe.charges.create (where your console.log('success') is) or move your error handling to the promise by providing a second argument to the then() statement: a function with an err parameter. 也就是说,可以Charge.create的调用放在stripe.charges.create (其中console.log('success')所在的)的回调中,或者通过为then()提供第二个参数来将错误处理移至promise。 then()语句:具有err参数的函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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