Skip to content

Instantly share code, notes, and snippets.

@davimacedo
Created May 20, 2016 23:33
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davimacedo/d21ab5aa0d0823956a2992e5eb2df673 to your computer and use it in GitHub Desktop.
Save davimacedo/d21ab5aa0d0823956a2992e5eb2df673 to your computer and use it in GitHub Desktop.
Cloud code for braintree integration
// Requiring npm module
var braintree = require("braintree");
// Initializing gateway
var gateway = braintree.connect({
environment: braintree.Environment.Sandbox,
merchantId: "useYourMerchantId",
publicKey: "useYourPublicKey",
privateKey: "useYourPrivateKey"
});
// Function for client token generation
Parse.Cloud.define("generateToken", function(request, response) {
gateway.clientToken.generate({}, function (err, res) {
if (err) {
response.error(err);
} else {
response.success(res.clientToken);
}
});
});
// Function for checkout
Parse.Cloud.define("checkout", function (request, response) {
var nonce = request.params.payment_method_nonce;
// Use payment method nonce here, for example:
gateway.transaction.sale({
amount: '10.00', // $ 10.00 sale
paymentMethodNonce: nonce,
options: {
submitForSettlement: true
}
}, function (err, result) {
if (err) {
response.error(err);
} else {
response.success();
}
});
});
@Longwater1234
Copy link

Longwater1234 commented Dec 30, 2020

there is an updated version using ES6 notation. Got this from Back4App technical team. Works perfectly. See below.


/* FINAL FINAL FINAL*/
//LAST UPDATED 30.12.2020

// Requiring npm module
const braintree = require("braintree");

// Initializing gateway
const gateway = new braintree.BraintreeGateway({
    environment: braintree.Environment.Sandbox,
    merchantId: "YOUR_MERCHANT_ID_HERE",
    publicKey: "YOUR_PUBLIC_KEY_HERE",
    privateKey: "YOUR_PRIVATE_KEY_HERE"
});

//Function for client-token generation

Parse.Cloud.define("generateToken", async(request) => {
    //Getting id from request parameter
    return gateway.clientToken.generate({}).then(res => {
        // pass clientToken to your front-end
        console.log(res.clientToken);
        return res.clientToken.toString();
    }, err => {
        //return any errors back to the client
        return err
    });
});

// Function for checkout

Parse.Cloud.define("checkout", async(request) => {
    return gateway.transaction.sale({
        amount: request.params.amount,
        paymentMethodNonce: "fake-valid-no-billing-address-nonce",
        options: {
            submitForSettlement: true
        }
    }).then((result) => {
        // return result back to client 
        if (result.success === true) {
            console.log(result);
            return result.transaction.id;
        } else {
            //return any errors back to the client
            console.log(result);
            return result.message;
        }
    }, (error) => {
        console.log(error);
    });
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment