VLR
VLR

Reputation: 312

Passing data in client side to server node

Hi I'm very new in NodeJS and I just created a very simple API to pass some of my data in stripe just to see the functionality of the stripe.charges.create, and I want to create an API to pass my token and create a charge but I really don't know how to GET the data hwen the user click pay.

This is my client side

var handler = StripeCheckout.configure({
            key: 'MY publiskKEY',
            image: 'img/logo.jpg',
            locale: 'auto',
            token: function(token) {
                // You can access the token ID with `token.id`.
                // Get the token ID to your server-side code for use.
                $http({
                    method: 'POST',
                    url: 'http://localhost:8080/api',
                    data: {
                        token: token.id
                    }


                }).success(function(data) {


                }).error(function() {



                });
                console.log(token)
                console.log(token.id)
            
            }
        });

        $scope.stripeForm = function(e) {

            handler.open({
                name: 'Sample Form',
                description: 'Payment',
                amount: 1000
            });


            // Close Checkout on page navigation:
            window.addEventListener('popstate', function() {
                handler.close();
            });

        }

This is my server side

var express = require('express');
var app = express(); 
var bodyParser = require('body-parser');
var stripe = require('stripe')('my publish key');

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var port = process.env.PORT || 8080; 


var router = express.Router();



router.get('/', function(req, res) {
    var charge = stripe.charges.create({
    amount: 1000,
    currency: "usd",
    description: "Example charge",
    source: 'This is i want to store my token.id',
}, function(err, charge) {
    console.log(err, charge)
});
    res.json({ token: token });
});


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

app.listen(port);
console.log('Magic happens on port ' + port);

Sorry for the noob question I'm just really new in NodeJS.

Upvotes: 0

Views: 1485

Answers (2)

Sravan
Sravan

Reputation: 18647

First of all change get to post in api

Since you are using a post request from the client side you should use post

Now, you will get the data you sent from the client side in req.body since you used body-parser a body is attached to request.

So, you can use your token, as req.body.token

router.post('/', function(req, res) {

    // here you can use your request object
    // like req.body.token  req.body
    console.log(req.body)
    console.log(req.body.token)

    var charge = stripe.charges.create({
    amount: 1000,
    currency: "usd",
    description: "Example charge",
    source: 'This is i want to store my token.id',

}, function(err, charge) {
    console.log(err, charge)
});
    res.json({ token: token });
});

Upvotes: 2

XPLOT1ON
XPLOT1ON

Reputation: 3223

On your node server, where you define your routes. Also, It will be a good practice to use POST request method

router.post('/', function(req, res) {
    ...
    console.log(req.body.token);
}

Link to documentation Node ExpressJs req.body

Upvotes: 1

Related Questions