Tuesday, 4 April 2017

How can I use http request with Firebase to communicate with Stripe Connect API?

To get customers token from Stripe I need to make http requests to Stripe and get the token after I am redirected back to a previously specified URL. Code below .ref shows this using Node.js and express listening to a local port.

I am not sure how this can be implement in Firebase to communicate with my website hosted on Firebase. What port should I listen to if my Node.js code lives in Firebase functions for example?

app.js code responsible for communicating with Stripe and getting the token:

'use strict';

var CLIENT_ID = '******';             
var API_KEY = '********';               
var TOKEN_URI = "https://connect.stripe.com/oauth/token";
var AUTHORIZE_URI = 'https://connect.stripe.com/oauth/authorize';


var qs = require('querystring');
var request = require('request');
var express = require('express');
var app = express();

//triggering initial http request
app.get('/authorize', function(req, res) {
  // Redirect to Stripe /oauth/authorize endpoint
  res.redirect(AUTHORIZE_URI + '?' + qs.stringify({
    response_type: 'code',
    scope: 'read_write',
    client_id: CLIENT_ID
  }));
}); 

//*** this is the redirect address after coming back from Stripe
app.get('/oauth/callback', function(req, res) {

  var code = req.query.code;
  console.log("code : "  , code); 
  // Make /oauth/token endpoint POST request
  request.post({
    url: TOKEN_URI,
    form: {
      grant_type: 'authorization_code',
      client_id: CLIENT_ID,
      code: code,
      client_secret: API_KEY
    }
  }, function(err, r, body) {

    if (err) {
      console.log("eror, " , err) ; 
    } else { 
      console.log("success resp", JSON.parse(body)); 

      const b = JSON.parse(body)
      const refresh_token          = b.refresh_token; 
      const access_token           = b.access_token; 
      const stripe_user_id         = b.stripe_user_id; 
      const stripe_publishable_key = b.stripe_publishable_key; 

      res.send({'refresh_token': refresh_token,
                  'access_token': access_token,
              'stripe_user_id': stripe_user_id,
        'stripe_publishable_key': stripe_publishable_key}); 

    }; 
  });
});

app.listen(9311);



via TheeBen

No comments:

Post a Comment