Sunday, 23 April 2017

Using stripe's node API with angular 404 error

I'm trying to use Node's stripe library to process cards from a form that's on the Angular frontend. The problem is that I'm getting a Failed to load resource: mydomain.com/stripeAPI (server responded with 404 Not Found). What am I doing wrong?

My form submits to the following function in my angular controller:

vm.myCheckout = function(req, res) {
    storeDataFactory.stripePayment(req).then(function(res){
        if (res.status != 500) {
            console.log('Payment accepted!');
    }).catch(function(error){
        if(res) {console.log(res.status);}
        if(error) {
                console.log("There was an error processing payment.");
    });
};

StoreDataFactory calls the following:

function stripePayment() {
        return $http.post('/stripeAPI').then(complete).catch(failed);
    }

My api/routes/index.js (backend) file lists the route as:

//Stripe Routes
router.route('/stripeAPI').post(ctrlStripe.stripePayment);

Express app.js:

app.use('/api', routes);

stripe.controllers.js

module.exports.stripePayment = function(req, res) {
  // Create a new customer and then a new charge for that customer: 
  stripe.customers.create({
    email: req.body.email
  }).then(function(customer){
    return stripe.customers.createSource(customer.id, {
      source: {
         object: 'card',
         exp_month: req.body.exp_month,
         exp_year: req.body.exp_year,
         number: req.body.card,
         cvc: req.body.cvc
      }
    });
  }).then(function(source) {
    return stripe.charges.create({
      amount: req.body.price * 100,
      currency: 'usd',
      customer: source.customer
    });
  }).then(function(charge) {
    // New charge created on a new customer 
    console.log("The stripe payment completed successfully!");
    res.send(200).json(charge);
  }).catch(function(err) {
    // Deal with an error 
    console.log("There was an error processing the payment",err);
    res.send(500).json(err);
  });
};



via user3561890

No comments:

Post a Comment