why does it give me POST http://localhost:3000/process-payment 404 (Not Found) when the endpoint in server.js is declared

27 Views Asked by At

this is my angular typescript component for my stripe payment, in which i want the money from the pay(...) method to go to an attribute of a json rest api database, and maybe mongodb database later, what could be the cause of the endpoint not being found

  pay(amount: any) {
    var handler = (<any>window).StripeCheckout.configure({
      key: 'pk_test_51OKda9H5eXnyJcWfb1dh0tGzXv2FclyLrDzPCqL5SGsyuDmZr2ljbfquR7kMZaRVONanewECDPXfPWswAiKQvuxl009QOEIECu',
      locale: 'auto',
      token: (token: any) => {
        console.log(token.id);
        console.log(token);
        alert('Token Created!!');

       ` // Attempt to reach the server endpoint`
        this.http.post('http://localhost:3000/process-payment', { token, amount })
          .subscribe((response: any) => {
            if (response.success) {
              console.log('Payment success!');
              // Update client-side logic as needed
            }
          });
      }
    });


  }
}

server.js

// Express server with Stripe integration
const express = require('express');
const bodyParser = require('body-parser');
const stripe = require('stripe')('my live stripe key');

const app = express();

app.use(bodyParser.json());

// Endpoint for processing payments
app.post('/process-payment', async (req, res) => {
  const { token, amount } = req.body;

  try {
    // Create a payment intent or charge using Stripe API
    const paymentIntent = await stripe.paymentIntents.create({
      amount: amount * 100, // Amount in cents
      currency: 'usd', // Change to your desired currency
      payment_method: token.id,
      confirmation_method: 'manual',
      confirm: true,
    });

    // Update balance in your JSON REST API database
    const userId = req.user.id; // Replace this with your user identification logic
    const userBalance = await Balance.findOne({ userId });

    if (userBalance) {
      userBalance.amount += amount;
      await userBalance.save();
    } else {
      // Create a new balance entry if not exists
      await Balance.create({ userId, amount });
    }

    res.json({ success: true, paymentIntent });
  } catch (error) {
  }
});

0

There are 0 best solutions below