0

This is my first time posting for help so please bear with me.

I've created a react app that uses express to create payment intents with Stripe. Locally on my machine, the app works perfectly fine. I can navigate to the page that fetches the POST method and creates a payment intent that I can see on the Stripe dashboard. However, when I've uploaded to Heroku, every time I hit the page, I immediately get a 405 Not Allowed error for my post method.

I noticed the response header was set to text/plain and tried to change that to JSON using res.type('json'), however the error still showed the response header was set to text/plain. I don't quite know if that's the issue. Any help would be very appreciated.

Screenshot of Error Screenshot of Success on my local machine

Server.js file.

const express = require("express");
const app = express();
const { resolve } = require("path");
const path = require('path');
const cors = require('cors')
// This is your real test secret API key.
const stripe = require("stripe")(hidden);

const PORT = process.env.PORT || 4242;

app.use(express.static("."));
app.use(express.json());
app.use(cors());

app.use(express.static(path.join(__dirname, 'build')));

app.get('/*', (req, res) => {
  res.sendFile(path.join(__dirname, 'build', 'index.html'));
});

const calculateOrderAmount = items => {
  let result = items.map(a => a.qty * 1500);
  const reducer = (accumulator, currentValue) => accumulator + currentValue;
  return result.reduce(reducer, 500)
};

app.post("/create-payment-intent", async (req, res) => {
  const { items } = req.body;
  // Create a PaymentIntent with the order amount and currency
  const paymentIntent = await stripe.paymentIntents.create({
    amount: calculateOrderAmount(items),
    currency: "usd",
    payment_method_types: ['card'],
  });
  res.send({
    clientSecret: paymentIntent.client_secret
  });
});

Function calling the POST Request inside of my React Function

useEffect(() => {
    // Create PaymentIntent as soon as the page loads
    window
      .fetch("/create-payment-intent", {
        method: "POST",
        headers: {
          "Content-Type": "application/json"
        },
        body: JSON.stringify({items: [{ qty: qty1 }, {qty: qty2}, {qty: qty3}] })  
      })
      .then(res => {
        return res.json();
      })
      .then(data => {
        setClientSecret(data.clientSecret);
      });
  }, [qty1, qty2, qty3]);

Is there anything else I need to provide to get help with this issue?

Bryan Rhee
  • 11
  • 1
  • 1
    I think this might be an issue with Heroku. How exactly are you deploying to Heroku? Are you using a heroku/static buildpack? – Justin Michael Jun 26 '20 at 22:55
  • I believe I am. Here's a section of my latest Build Log. `-----> Build succeeded! =====> Downloading Buildpack: https://github.com/mars/create-react-app-inner-buildpack.git =====> Detected Framework: React.js (create-react-app) Writing `static.json` to support create-react-app Enabling runtime environment variables =====> Downloading Buildpack: https://github.com/heroku/heroku-buildpack-static.git =====> Detected Framework: Static HTML ` – Bryan Rhee Jun 26 '20 at 23:22
  • I'm uploading to github and using heroku's automatic deployment. I think this answers your questions. – Bryan Rhee Jun 26 '20 at 23:24
  • I think this comment describes your issue: https://stackoverflow.com/questions/50970961/how-to-enable-post-method-on-heroku-server-for-react-app-i-am-getting-405-met#comment88962190_50971384 Can you contact Heroku support and confirm that's still the case? – Justin Michael Jun 27 '20 at 16:13
  • Hmmmm, ok. I've reached out to Heroku support to see if this is still the issue. Thanks for helping out Justin. I'll let you know how it turns out. – Bryan Rhee Jun 28 '20 at 00:03
  • Thank you so much Justin. The issue was the static build pack. I reconfigured the whole page to use a standard node package and was able to get the post request to work! – Bryan Rhee Jun 28 '20 at 18:45
  • Great to hear it's working now! I added the solution as a proper answer so it's easily visible to other people who find this post. – Justin Michael Jun 29 '20 at 00:36

2 Answers2

0

The issue here is that static build packs on Heroku only allow GET requests, not POST requests. Switching to a standard node package solves the problem.

Justin Michael
  • 5,634
  • 1
  • 29
  • 32
0

So, after spending too much time on this, I have solved my problem.
I tried the Justine's way but couldn't resolved it.
Here what I did.
I have used the mars/create-react-app
Run in console after loging in and creating the app
heroku buildpacks:set mars/create-react-app -a blogsterapp

https://elements.heroku.com/buildpacks/mars/create-react-app-buildpack
In above post there is section of proxy for deployment do as it says - create the static.json. This will make any request having /api in its request in react will be directed to the backend.
Add the config var in heroku and you are done.