Skip to content

Instantly share code, notes, and snippets.

@damianesteban
Created June 18, 2016 15:48
Show Gist options
  • Save damianesteban/35d7e5468b6a1010ea036820754fe7f9 to your computer and use it in GitHub Desktop.
Save damianesteban/35d7e5468b6a1010ea036820754fe7f9 to your computer and use it in GitHub Desktop.
Braintree Release from Escrow after 48 hours
// new Date Object.
const today = new Date();
// releases a transaction from escrow via the transaction id.
function releaseFromEscrow(transactionId) {
gateway.transaction.releaseFromEscrow(transactionId, (err, result) => {
if (err) {
console.log(`Error releasing transaction - ${err}`);
}
console.log(`Transaction released - ${result}`);
});
}
console.log(`Searching gateway for transactions created at least two days ago - ${today.getDate() - 2}`);
// Performs a search for transactions that were created at least two days ago.
gateway.transaction.search((search) => {
search.createdAt().max(today.getDate() - 2);
}, (err, response) => {
response.each((error, transaction) => {
if (transaction.escrowStatus === 'held') {
releaseFromEscrow(transaction.id);
console.log(`Transaction released from escrow: ${transaction.id}`);
}
});
});
@elopinto
Copy link

I just noticed that today.getDate() - 2 is an integer rather than a date. That might be a problem if today.getDate() == 1, because subtracting two will give a negative number. Also, max might not be properly comparing the createdAt date with the integer.

Maybe instead we should import moment.js, and do something like:

const twoDaysAgo = moment().subtract(2, 'days');
// Then the search:
search.createdAt().max(twoDaysAgo.toDate());

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