Friday 9 June 2017

Passing data in client side to server node

Hi Im very new in node js 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 dont 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 im just really new in node js



via VLR

No comments:

Post a Comment