Skip to content

Instantly share code, notes, and snippets.

@mihaiserban
Created December 7, 2018 18:40
Show Gist options
  • Save mihaiserban/0673065002f305d1d83e97fa312f1206 to your computer and use it in GitHub Desktop.
Save mihaiserban/0673065002f305d1d83e97fa312f1206 to your computer and use it in GitHub Desktop.
braintree payments stream search transactions
router.get('/payments', verifyJWT_MW, function(req, res) {
try {
let transactions = [];
const stream = Braintree.gateway.transaction.search(function(search) {
search.customerId().is(req.user.account.braintreeCustomerId);
});
stream.on('ready', function() {
console.log(stream.searchResponse.length());
});
stream.on('data', function(transaction) {
transactions.push({
title: 'Topup Credit',
id: transaction.id,
status: transaction.status,
amount: transaction.amount,
currency: transaction.currencyIsoCode,
createdAt: transaction.createdAt,
card: {
type: transaction.creditCard.cardType,
last4: transaction.creditCard.last4
}
});
});
stream.on('error', function(e) {
throw e;
});
stream.on('end', function() {
res.status(200).json({
success: true,
message: 'Successfully retrieved transactions for account.',
data: transactions
});
});
} catch (error) {
res.status(500).json({
success: false,
message: error.message
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment