Skip to content

Instantly share code, notes, and snippets.

@Longwater1234
Last active March 28, 2022 13:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Longwater1234/2d3632aa8213dc22dce8dacefd135757 to your computer and use it in GitHub Desktop.
Save Longwater1234/2d3632aa8213dc22dce8dacefd135757 to your computer and use it in GitHub Desktop.
Back4App Parse Braintree Cloud Code
/* FINAL FINAL FINAL*/
// UPLOAD THIS CODE IN your BACK4APP CLOUD CONSOLE.
//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
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) => {
const firstName = request.params.firstName; //optional
const lastName = request.params.lastName; //optional
return gateway.transaction.sale({
amount: request.params.amount,
paymentMethodNonce: "fake-valid-no-billing-address-nonce",
billing: {
// this billing part is optional. Can be ommitted.
firstName: firstName,
lastName: lastName,
},
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