Below show the method to Integrate the Braintree Payment gateway in MEAN Stack while developing software.
The Braintree Node.js library with Braintree’s servers.
Prerequisite:
Install Braintree node package:
For more information or help with this step, see our API Credentials support article.
In Client Controller:
braintree.client.create({ authorization:'*******'//provide authorization code }, function (clientErr, clientInstance) { //Stop if there was a problem creating the client. //This could happen if there is a network error or if the authorization is invalid. if (clientErr) { console.error('Error creating client:', clientErr); return;} //Create a PayPal Checkout component. braintree.paypalCheckout.create({ client: clientInstance },function (paypalCheckoutErr,paypalCheckoutInstance){ //Stop if there was a problem creating PayPal Checkout. //This could happen if there was a network error or if it's incorrectly configured. if(paypalCheckoutErr){ console.error('Error creating PayPal Checkout:', paypalCheckoutErr); return;} //Set up PayPal with the checkout.js library paypal.Button.render({ env: 'sandbox', // or 'production' commit: true, payment: function (){ return paypalCheckoutInstance.createPayment({ flow:'checkout',//Required amount: $scope.totalAmt,//Required,Provide amount here currency:'USD',//Required enableShippingAddress:true, shippingAddressEditable:false, }); }, onAuthorize:function (data, actions){ return paypalCheckoutInstance.tokenizePayment(data) .then(function (payload){ // Submit`payload.nonce`to your server. $http.post('/api/braintree', {params: {braintreeData: data, payload: payload,amount: $scope.totalAmt }})//post to backend nodejs .success( function(success){ console.log(success); //Perform success operation here}) .error( function(error){ console.log(error);}); });}, onCancel: function (data){ console.log(data); },onError: function (err){ console.error('checkout.js error',err); }},'#paypal-button').then(function(){ // The PayPal button will be rendered in an html element with the id //`paypal-button`.This function will be called when the PayPal button //is set up and ready to be used. }); }); });
In the backend server controller
var braintree = require('braintree'); app.post("/api/braintree", function (req, res) { var payload = req.body.params.payload; var braintreeData = req.body.params.braintreeData; var totAmount = req.body.params.amount; var saleRequest = { amount: totAmount, paymentMethodNonce: payload.nonce, orderId: braintreeData.orderID, options: { submitForSettlement: true, paypal: { customField: "PayPal custom field", description: "Description for PayPal email receipt", },} }; var gateway = braintree.connect({ environment: braintree.Environment.Sandbox, // Sandbox Environment merchantId: "*********", // Provide merchant id publicKey: "*********", // Public key privateKey: "*********" // Private key }); gateway.transaction.sale(saleRequest, function (err, result) { if (err) { res.send("Error: " + err + ""); } else if (result.success) { // Perform any database operation here res.json(result); } else { res.send("Error: " + result.message + ""); } }); });
Leave a Reply