Skip to content

Instantly share code, notes, and snippets.

@maiatoday
Created March 29, 2018 18:12
Show Gist options
  • Save maiatoday/828884fd164a752fd6a5b0c60732a62c to your computer and use it in GitHub Desktop.
Save maiatoday/828884fd164a752fd6a5b0c60732a62c to your computer and use it in GitHub Desktop.
Root code to donate to the animal welfare when I drink a coffee
// This runs before the deduct
function beforeTransaction(transaction) {
return true; // Approve transaction
}
// This runs after the deduct
function afterTransaction(transaction) {
var amountString = zarString(transaction.amount);
var balanceString = zarString(root.account().balance);
var shopString = transaction.merchant.name;
console.log('Coffee! ' + amountString + ". Balance "+ balanceString + " at shop "+shopString);
var donation = 2000
var donationString = zarString(donation)
console.log("This is a coffee shop? " + isThisACoffeeShop(shopString))
if (isThisACoffeeShop(shopString)) {
var data = {
amount: donation,
description: 'Doggo Donations',
their_reference: 'Doggo Donations',
contact_name: 'AWSS',
bank_name: 'Nedbank',
bank_number: '1498042856',
save_contact: false, // optional
};
root.eft(data);
var message = 'You donated coffee money to animal welfare: ' + donationString + '. Your remaining balance is ' + balanceString + '.';
root.sendSMS(message);
console.log(message);
}
}
function isThisACoffeeShop(name) {
var nameLower = name.toLowerCase();
var coffeeShops = ['vernuft',
'ya ya cafe',
'wild break',
'blue crane',
'deluxe coffee'];
for (var i = 0, l=coffeeShops.length; i < l; i++) {
if (nameLower.includes(coffeeShops[i])) {
return true;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment