I am trying to implement a PayPal - Braintree payment service for an Android app I am developing, but I am having real trouble working out the server side code. I am following the docs here and I'm planning on using Node.js server side code as I have a little background in JavaScript. In short, I have no idea what code I should be writing and the docs are quite confusing. I also have very little background in server sided web development.
The docs state that an example of getting a client token from "my" server can be done with the following code:
AsyncHttpClient client = new AsyncHttpClient();
client.get("https://your-server/client_token", new TextHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, String clientToken) {
this.clientToken = clientToken;
}
});
My interpretation of this is I would be somehow executing a JavaScript file which would create a "server" on local host and retrieve the client token.
Then for the server sided code it has written this:
1. npm install braintree
2. construct gateway
var gateway = braintree.connect({
accessToken: useYourAccessToken
});
3. get a client token
app.get("/client_token", function (req, res) {
gateway.clientToken.generate({}, function (err, response) {
res.send(response.clientToken);
});
});
Up until step 3 is fine; however, I am unable to execute the code through cmd, let alone the android app. The page just says it cannot be reached.
Putting all of this together is supposed to generate a client token for a PayPal Braintree payment. I'm not quite ready to get to the "Create Transaction" step until I completely understand this.
What I am really asking is how do I put together this code to get a working client token retrieval system going? I have a website that I can store files at if need be. Could someone point me to a tutorial or example or point me in the right direction?
Cheers!
// Initialize the Braintree SDK:
// 1. Import the Braintree SDK module
var braintree = require('braintree');
var express = require('express');
express()
var gateway = braintree.connect({
environment: braintree.Environment.Sandbox,
accessToken: "access_token$sandbox$XXXXXXXXXXXXXX"
});
gateway.clientToken.generate({}, function (err, response) {
console.log(response.clientToken);
});
For detailed steps installing the SDK, I will suggest referring the the Braintree Github page at https://github.com/braintree/braintree_node .. and and integration guide at https://developers.braintreepayments.com/start/hello-server/node#generate-a-client-token
However the integration guide needed to integrate PayPal Checkout with Braintree SDK can be found on this page . This type of integration only requires a PayPal account and thus there is no need for a Braintree merchant account. But the integration is very similar to the Direct Braintree integration and as such, the Braintree integration guide can be cross referenced and used for this integration.
The Braintree SDK and the express module can be installed with the following commands:
npm install braintree
npm install express