ga-wdi-boston / team-project

Other
2 stars 39 forks source link

500 for controller action with stripe #328

Closed Maggicorp closed 7 years ago

Maggicorp commented 7 years ago

this is the issue we are getting. We defined a charge method in the buyer controller with a route that looks like this. We moved the more specific route to be read first.

.post('/charge', 'buyers#pay')
.resources('buyers')

Here is our ajax call

const makeCharge = function (token) {
  console.log('at make charge')
  return $.ajax({
    url: config.apiOrigin + '/charge',
    method: 'GET',
    headers: {
      'Authorization': 'Token token=' + store.user.token
    },
    data: JSON.stringify(token)
  })
}

and here is the controller method. It's not hitting the controller method so we can't deal with any issues in that yet.

const pay = (req, res, next)  => {
  let amount = 500
  console.log('at pay req is ', req)
  stripe.customers.create({
    email: req.body.email,
    card: req.body.id
  })
  .then(customer =>
    stripe.charges.create({
      amount,
      description: "Sample Charge",
      currency: "usd",
      customer: customer.id
    }))
  .then(charge => res.send(charge))
  .catch(err => {
    console.log("Error:", err)
    res.status(500).send({error: "Purchase Failed"})
  })
}
benjimelito commented 7 years ago

I noticed that you're defining the route as a POST : .post('/charge', 'buyers#pay'), but when you're actually making the ajax request you're setting the method to GET

benjimelito commented 7 years ago

Scott mentioned that this is what the documentation said to do, but I would be surprised if this wasn't causing some sort of issue

Maggicorp commented 7 years ago

Changed it to POST, which helped. New errors. Will update with new error message.

Maggicorp commented 7 years ago

The problem is that the request isn't passing the right data. req.body.email and req.body.id are both returning undefined. It seems that something is being lost when we changed the fetch to an ajax request.

benjimelito commented 7 years ago

Would you mind console logging what req looks like in the controller and posting that?

On May 24, 2017 5:56 PM, "Maggicorp" notifications@github.com wrote:

The problem is that the request isn't passing the right data. req.body.email and req.body.id are both returning undefined. It seems that something is being lost when we changed the fetch to an ajax request.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/ga-wdi-boston/team-project/issues/328#issuecomment-303863138, or mute the thread https://github.com/notifications/unsubscribe-auth/AGwgMrRusc5xufHgYROz8sxvgzJ5YIa3ks5r9KeNgaJpZM4NloVe .

Maggicorp commented 7 years ago

I got those properties, new issue is that it can't make the request with them. It is returning a 500 error and not hitting the .catch or .then that comes afterwards. So something is happening here

stripe.customers.create({
    email: req.body.email,
    card: req.body.id
  })
Maggicorp commented 7 years ago

It gets req.body.email and req.body.id to b tok_1AN8dZE4UNzYgNSt3sXXxULe and 3@1.1, which is correct.

Maggicorp commented 7 years ago

Okay, I kept it in the folder and it correctly makes the request now. I added these headers.
const express = require("express"); const stripe = require("stripe")(keySecret); const bodyParser = require("body-parser");

Maggicorp commented 7 years ago

Sadly, this does not work in the deployed version even thought it works locally. :(

benjimelito commented 7 years ago

What sort of errors are you getting in the deployed version?

Maggicorp commented 7 years ago

I am having a really hard time figuring out why stripe isn't working deployed and what it is actually creating. Can anyone do a one on one with me about it? I have also just paste some of my questions but I could really use some help looking at it overall

Maggicorp commented 7 years ago

I had to save my secret password in the heroku configuration for it to worked deployed.