Stuck with integrating Braintree with Meteor.js - javascript

So, I used sudo npm install -g braintree to install the package to a clean meteor project and have the following code:
if (Meteor.isClient){
Meteor.call('getBraintree')
braintree.setup("/* very long client token */", 'dropin', {
container: 'dropin'
});
};
if (Meteor.isServer) {
Meteor.startup(function(){
var braintreeApi = Meteor.npmRequire('braintree'),
gateway = braintree.connect({
environment: braintree.Environment.Sandbox,
merchantId: "merchantId",
publicKey: "publicKey",
privateKey: "privateKey"
});
});
Meteor.methods({
'getBraintree': function getBraintree(){
var braintreeWeb = Meteor.npmRequire('braintree-web');
}
});
};
...and braintree is not defined # braintree.connect({ (I have sandbox access and all my keys in order). If I used npm to install the package to my Meteor directory, is there anything further that I'd have to do to my packages.js file as shown here, considering npm now works with meteor in v1? More generally, how do I configure a project so npm packages can be installed and used?
Edit: code is updated as of 12/11

Disclaimer: I work for Braintree :) Always feel free to reach out to our support if you are having issues with your integration.
Update: I've created an extremely basic Braintree and Meteor example application that may be of some help you.
Another disclaimer: I know very little about Meteor. I'll try to answer the broader non-meteor specific issues, and update with more meteor specific information if I can get it. Here are few potential issues:
You need both the client and the server side modules for a Braintree integration, braintree is the server-side (node) Braintree library and braintree-web is the client side package. I am not sure on the specifics of consuming a client side npm module in Meteor, so it may be easier for you to use a tool like bower or to hot link to the client side javascript by placing a script tag on your page:
<script src="https://js.braintreegateway.com/v2/braintree.js"></script>
braintree.connect is a server side method, and as such should only be called on the server (probably once during startup). You'll need it to generate a clientToken to be used on the client side. I have very little experience with Meteor, but I think it is suitable to call braintree.connect within Meteor.startup on the server:
// a better pattern would be to place this in a server/index.js file
// within your project, which Meteor knows to load as server only code
Meteor.startup(function () {
if (Meteor.isServer) {
var braintree = Meteor.npmRequire('braintree');
gateway = braintree.connect({
environment: braintree.Environment.Sandbox,
publicKey: process.env.BT_PUBLIC_KEY,
privateKey: process.env.BT_PRIVATE_KEY,
merchantId: process.env.BT_MERCHANT_ID
});
}
});
braintree.setup should only be run on the client side, it is what interacts with the form on the client side.

Braintree has two packages on npm. One is for the server-side and the other is for client side.
https://www.npmjs.com/package/braintree will provide the server code (var gateway = braintree.connect…)
https://www.npmjs.com/package/braintree-web on the other hand, will provide the code for running in the browser (braintree.setup("/*very long client token*/", 'dropin', {).
In the case of meteor, you may need to include both packages. (Make sure to double check that your private key is kept secret on your server! It may be helpful to use the /server special directory to serve this purpose.)

require is not available inside Meteor like that. Use this package https://github.com/meteorhacks/npm to use npm packages.

Here is a package that is Synchronously Wrapped for meteor and Braintree https://atmospherejs.com/patrickml/braintree

Related

Module not found: Error: Can't resolve 'dns' when using MongoDB

I'm new to Reactjs, Nodejs and MongoDB. I'm currently trying to change Mediums snowflake tool to store users scores in a database. I have installed yarn, mongodb and mongodb-core through npm. It is a single page web application which is what I think is causing me trouble. I add
var MongoClient = require('mongodb');
To SnowflakeApp.js and encounter the following error:
Module not found: Error: Can't resolve 'dns' in
'/home/mlAHO174/snowflake/node_modules/mongodb-core/lib'
I've tried googling this error and have discovered it could be a range of things. I'm not sure if it is because React is front end and I'm trying to alter back end or because mongoDB is installed incorrectly. I'm new to this so would be grateful for help!
DNS is a core module of Node.JS. Telling people they need to install DNS via NPM will end up with them having a completely different module that does something else.
https://nodejs.org/api/dns.html vs https://www.npmjs.com/package/dns
This error most likely means you are trying to do something from the client-side that needs to be done on the server-side. If MongoDB module can't find the DNS component, it's running on the client-side.
MongoDB has to run on the server. In order to access data from React dynamically you'll need to set up an API using something like Express or Apollo.
Update:
A great way to do this is with Azure Functions (TypeScript) or AWS (Lambda) functions
For anyone who encounters this Error while importing the clientPromise (like in the with-mongodb template):
Make sure you're in the /pages/ directory!
It won't work in other directories like /components.
(and you should take a break or get some coffee...)
The problem is that you are trying to connect to the database from the front end. If this were possible that would open up a whole world of security issues. You need to set up your database connections on the backend and then have the front end make requests to the backend to handle the database.
I solved this by installing and using 'bson' instead of 'mongodb' for the client part of the code. 'bson' has a tiny bit of what 'mongodb' has and it might have what you are looking for. 'bson' is built for the browser.
In my case I needed the "ObjectId" in the browser and pulling it in from 'bson' did the trick as I didn't want to reference 'mongodb' because of the error described in the OP.
The other answers are also correct depending on why you're getting this error.
I think - mongo package is meant to be run on servers only, not in the browser.
It does not work in Next.js pages file components too, but does work in getStaticProps, getServerSideProps, getStaticPaths etc - because they run on the server, not the client.
Alternative - use Firebase Realtime database, you can access it in client-side code too. Example - a website (say a React app) that is hosted on GitHub pages or some other static server, but doesn't have a web app server (aka backend).
welcome to stack overflow.
You need to understand and learn few basics of web-applications. There's frontend, backend and a layer between them and a layer between backend and database. Frontend includes react.js, angular.js or anything else that is on browser. Backend is used to take request from frontend, providing API's to frontend and ask for data from other API's or database. Database includes sql, no-sql.
The error you are facing if of a NPM module mongodb-core.js. Either it's not installed properly, or installed using wrong version of module which is not comparable with your node version, or wrong version of NPM, or module using another NPM module which is not installed.
The issue in your case is mongodb-core uses a module dns which is not been installed. Try to install dns with npm i dns. or remove and install mongodb-core again.

How to deploy Node.js REST API to production mode

I've created a REST API using Node.js and Express, so now I need to share it and publish it on a server in order to connect from Front-end.
Can you tell me a proper way to do it?
You could use Heroku for deployment, this way you can know how your app will fare. It's free moreover.
If you're satisfied with it then you can go ahead and buy a dyno or use other platforms like Azure or AWS.
To learn more on how to deploy your existing app to Heroku, visit this page.
The question you are asking is very broad. It can be done in a lot of ways. For me this 2 part tutorial was very helpful:
https://hackernoon.com/tutorial-creating-and-managing-a-node-js-server-on-aws-part-1-d67367ac5171. However, this only covers the installation on AWS EC2. This doesn't differ much from deploying it to Google Cloud, Azure or something local.
In general you need to fix the following steps:
1) Create a server somewhere (local or in the cloud)
2) Install all the stuff to run your app. In your case Node.js at least
3) Put a copy of your app on the server
4) Run it with node
5) Go to the ip of the server
The tutorial gives more details. DISCLAIMER: If you actually want to use this in production there is way more to consider. For example, security policies, setting up proxies, installing certificates etc. Please read up on that properly before you start running production apps.
You can install node on the the production server and then where the project is situated just .
copy that path
Open the Cmd >>
Enter "cd copied path .>>enter
you npm will be install & REST Api will Work.

How do I install Stripe for Meteor 1.3?

I'm creating a meteor web app that will sell one of our customer's products. I've decided to try and use Stripe to handle the payments (in particular Stripe Connect), and to charge an 'application fee' for each sale.
My questions:
Do I still need to use mrgalaxy:stripe or kadira:stripe-connect in order to use the Node API for Stripe? I am getting incredibly confused with the documentation of these packages, which I now believe are outdated.
If not, exactly how should I install and import Stripe for my app?
What do I need to do differently on client and on server to import Stripe?
What I have tried:
In my app directory:
meteor npm install --save stripe.
In my client side javascript code:
import stripe from "stripe"
var stripe = require("stripe"("sk_******************")
Meteor.startup(function() {
stripe.setPublishableKey("sk_******************");
});
I believe the first are meant to do the same thing, but neither works!
Chrome debugger gives me :
Uncaught TypeError: require("http").createServer is not a function
which is running in node_modules/stripe/lib/stripe.js
I am epically confused right now and would give my right index finger for clear instruction on what exactly to write in my javascript file, so I can go from where I am now to creating my first charge object.
Thanks in advance!
The Stripe npm package needs to be run on the server side, not client side. Stripe.js if for client side. You create a token with Stripe.js and then send that token to your server and use the NPM package to create a charge.

MongoDB / Meteor / Export MONGO_URL to deployed applications

I tried to export a settings.json as documented in the meteor.js documentation in order to connect my Meteor.js app with an external MongoHQ database :
{
"env": {
"MONGO_URL" : "mongodb://xxx:xxxx#troup.mongohq.com:10037/xxx"
}
}
with the command :
mrt deploy myapp.meteor.com --settings settings.json
It doesn't even work, My app continue to connect the local database provided with the Meteor.app !
My MONGO_URL env variable didnt changed.
Is there any solution to export my MONGO_URL env variable to connect an external MongoDB database ?
I saw that is possible to change it while using heroku or modulus, what about the standard deploying meteor.com solution ?
You are not able to use your own MONGO_URL with Meteor deploy hosting.
Meteor deploy hosting takes care of the Email with Mailgun (if you use it), and provides mongodb for all apps deployed there.
It is possible to change the MAIL_URL, but it is not possible to use a different mongodb.
You can try, though im not sure it will work:
Place this in your server side code somewhere
process.env.MONGO_URL = '..';
Create a lib folder under the server folder and write:
Meteor.settings = { //your settings };
According to documentation everything inside a folder named lib will be executed before anything, so this way we ensure that no code will be execute before this, preventing errors from accessing Metrics that don't exist.
If you're already using the lib folder you've to named right to run before anything else that might conflict, check the docs about it.
Enjoy.
The application-configuration package (built in meteor package) contains the code to set mongo_url. See my answer here https://stackoverflow.com/a/23485319/2391620
Regarded to the Unoficial Meteor.js FAQ, thats not possible to do it easily, that might be tricky.
So i signed up for a modulus Account, with MongoHQ Database.
It works right now.
having a settings.json file is not enough you need to run with
meteor --settings settings.json
which is you cant do with meteor.com deploy.

How can I use a javascript library on the server side of a NodeJS app when it was designed to run on the client?

I'm diving into NodeJS and Express (it's sooo complicated to me) to build a real-time web app. At the moment, I'm trying to understand how I can use an existing javascript library on the server side. The problem is the library appears to be designed to run on the client side and, as a result, the instructions only show you how to use it on the client side. The library I'm talking about can be found here...
https://github.com/replit/jsrepl
Questions:
Since a NodeJS web app is built on javascript, is it fair to say I can run any non-gui javascript library on the server side?
Can anyone offer some guidance on how I can add that jsrepl library to my Express 3.0 app in a way that allows me to use it in the same way that I would use it on the client side in a browser? Do I have to modify the jsrepl code and add "exports." to the methods I want to use?
Meaning, on the server side, I can execute the following code...
var jsrepl = new JSREPL({
input: inputCallback,
output: outputCallback,
result: resultCallback,
error: errorCallback,
progress: progressCallback,
timeout: {
time: 30000,
callback: timeoutCallback
}
});
Thanks in advance for all your wisdom! I'm doing my best to understand all this.
So this is possible, but you need some serious hackery in order to get it working. Since this is not a node module and is written from the browser as others have noted, you need a DOM within node to execute it into. Luckily, we have the wonderful jsdom project which allows us to do just that. So let's get this thing set up.
cd into your node project (create one if there isn't one already)
clone down the jsrepl repo git clone git://github.com/replit/jsrepl.git
cd into jsrepl and initialize submodules git submodule update --init --recursive
still in the folder, run npm install coffee-script and npm install uglify-js, dependencies that are not mentioned anywhere in the repo (ugh).
make sure java is installed and run cake bake. After a lengthy process of compiling java files and such the command will finish and jsrepl will be built and ready to go.
run npm install jsdom, then we can start writing an example file
Here's a minimal example:
var jsdom = require('jsdom'),
fs = require('fs'),
jsrepl = fs.readFileSync('./jsrepl/repl.js', 'utf8');
jsdom.env({
html: "<script src='jsrepl.js' id='jsrepl-script'></script> ",
src: [jsrepl],
done: function(err, window){
if (err) return console.error(err);
run_jsrepl.call(window);
}
});
function run_jsrepl(){
console.log(this.JSREPL)
}
Here's the minimum amount of code required to get JSREPL into a place where it's working. All we're doing here is requiring jsdom and instantiating it, reading in the jsrepl source straight from the file. If you run this file with node filename it will log out your JSREPL object, which can be used just like it's in the browser : )
You can run phantomjs in Node, which is a headless webkit browser. Then run jsrepl inside of phantomjs.
No. There are things on the client side that you don't have on the server side (and vice-versa): for instance, the DOM.
I've never worked with jsrepl myself, but assuming that it's platform-agnostic, require()ing it from a node module should be OK. However, there seem to be some DOM-specific things in the scripts in question (e.g. document.getElementById) that suggest otherwise.

Categories

Resources