Adapting Chargebee API for use with Meteor

The example chargebee gives for Node:

const express = require('express')
const chargebee = require("chargebee")
// CORS is enabled only for demo. Please dont use this in production unless you know about CORS
const cors = require('cors')

chargebee.configure({site : "honeycomics-v3-test", 
  api_key : "test_jqXGuQLkBHUSR2PM0qgUV21W1VqSFJIU"});
const app = express()

app.use(express.urlencoded())
app.use(cors())

app.post("/api/generate_checkout_new_url", (req, res) => {
  chargebee.hosted_page.checkout_new({
    subscription : {
      plan_id : req.body.plan_id
    },
    customer: {
      first_name: req.body.first_name,
      last_name: req.body.last_name,
      email: req.body.email,
      phone: req.body.phone,
      company: req.body.company,
    }
  }).request(function(error,result){
    if(error){
      //handle error
      console.log(error);
    }else{
      res.send(result.hosted_page);
    }
  });
});

My adaptation for Meteor (server side):

import { HTTP } from 'meteor/http'
import { Meteor } from 'meteor/meteor'
import chargebee from 'chargebee'

Meteor.methods({
  generateCheckoutNewURL () {
    HTTP.post('/api/generate_checkout_new_url', (req, res) => {
      chargebee.hosted_page.checkout_new({
        subscription: {
          plan_id: 'monthly-plan'
        },
        customer: {
          first_name: 'John',
          last_name: 'Smith'
        }
      }).request(function (error, result) {
        if (error) {
          // handle error
          console.log(error)
        } else {
          res.send(result.hosted_page)
        }
      })
    })
  }
})

Calling the above Meteor method results in the following error: Exception while invoking method ‘generateCheckoutNewURL’ Error: url must be absolute and start with http:// or https://

Any suggestions? I just can’t seem to get this working.

Meteor’s HTTP package is for making HTTP calls, not registering HTTP endpoints. The node example registers a POST endpoint at http://localhost:3000/api/generate_checkout_new_url, your code makes a POST call to /api/generate_checkout_new_url, which isn’t a valid website.

2 Likes

How would I do this in a Meteor app then?

I can’t give the definitive answer, but I can share some of my code that may help you:

import { Meteor } from 'meteor/meteor';
import { WebApp } from 'meteor/webapp';
import { json } from 'body-parser';
import Fiber from 'fibers';

const jsonParser = json();

WebApp
  .connectHandlers
  .use('/_hooks/patreon-update', jsonParser)
  .use('/_hooks/patreon-update', (req, res, next) => {
    Fiber(() => {
      res.writeHead(200);
      const {
        data: {
          attributes: {
            amount_cents: amountCents,
          },
          relationships: {
            patron: {
              data: {
                id: patreonId,
              },
            },
          },
        },
      } = req.body;
      Meteor.call('patreon.updatePledge', { amountCents, patreonId });
      res.end();
    }).run();
  });

^this is an endpoint I created so that Patreon can let me know when I have a new Patron

2 Likes

I had made this really basic example a while back for someone. Just exposed a vanilla http endpoint.