javascriptnode.jschargebee

Calling apis like chargebee with node.js


I'm a little be confused when it comes to calling APIs over node.js. I have a server running node js where I can install frameworks like the one for chargebee.

I created a html page where I make subscriptions etc. Now I would want to call the corresponding chargebee function to make the subscription. If I try to load chargebee with require('chargebee')it failes. I can only load it in the server js.

So how would it be possible for me to use the functionalities of chargebee?

Is it possible that I invoke a function from chargbee by a click on the button? Do i have to provide this function by express?

I think I did not understand the difference between client side code and server side code when it comes to node.js. How can function on the server side be invoked by clicks on html buttons for example?


Solution

  • In order to trigger request from client side you can use forms or AJAX. Here is an example with express framework in which form is used to trigger request and create subscription in chargebee

    Client-Side Code:

    <html>
       <body>
          <form action="/subscribe" method="post">
                <label for="name">First Name:</label>
                <input type="text" id="name" name="customer[first_name]" placeholder="first name" />
                <br />
                <label for="name">Last Name:</label>
                <input type="text" id="name" name="customer[last_name]" placeholder="last name" />
                <br />
                <label for="email">Email:</label>
                <input type="email" id="email" name="customer[email]" placeholder="Enter your email address" />
                <br />
                <input type="submit" value="Create Profile" />
          </form>
       </body>
    </html>
    

    Node-Server code:

    var express = require('express');
    var chargebee = require("chargebee");
    var bodyParser = require('body-parser');
    var app = express();
    app.use(bodyParser.json());       // to support JSON-encoded bodies
    app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
      extended: true
    }));
    
    chargebee.configure({site : "<<site_name>>",
      api_key : "<<api_key>>"
    
    app.get('/', function(req, res){
      res.sendFile(__dirname + '/form.html');
    });
    
    app.post('/subscribe', function(req, res){
          var params = req.body;// getting form params as JSON
          params['plan_id']='enterprise'; // plan id that is present in your Chargebee site
          chargebee.subscription.create(params).request(function(error,result){
            if(error){
              //handle error
              console.log(error);
            }else{
              console.log(result);
              var subscription = result.subscription;
              res.writeHead(200, {
                 'content-type': 'text/plain'
              });
              res.write('Successfully created subscription\n\n' + 'id :: '+ subscription.id);
              res.end();
           }
         });
    });
    
    app.listen(3000);
    console.log("server listening on 3000");