Last active
August 1, 2019 12:09
-
-
Save ibrahimlawal/46b67a4bde71b0dda91cfe6f64904e09 to your computer and use it in GitHub Desktop.
Get a list of transactions from Paystack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const paystackTransactionsFetcher = require('./PaystackTransactionsFetcher'); | |
paystackTransactionsFetcher.fetch({ | |
secretKey: 'sk_live_youwishiwereavalidkey___', // secret key | |
perPage: 100, // leave empty to fetch 50 per page | |
status: 'all', // leave empty to fetch only successful | |
startFrom: new Date('2011-01-01'), // Leave empty to fetch only a week ago | |
}) | |
.then((i) => { console.log(JSON.stringify(i, null, 2)); }) | |
.catch(console.error); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const needle = require('needle'); | |
class PaystackTransactionsFetcher { | |
constructor({ | |
secretKey, maxApiCallRetries = 10, startFrom, perPage, status='success' | |
} = {}) { | |
if (!secretKey) { | |
throw new Error('secretKey is required!'); | |
} | |
this.secretKey = secretKey; | |
this.maxApiCallRetries = maxApiCallRetries; | |
this.useFrom = startFrom || PaystackTransactionsFetcher.aWeekAgo(); | |
this.perPage = perPage || 50; | |
this.status = status || 'success' | |
this.page = 1; | |
this.list = []; | |
} | |
static aWeekAgo() { | |
return new Date(Date.now() - 7 * 24 * 60 * 60 * 1000); | |
} | |
get statusFilter() { | |
if (this.status === 'all') { | |
return ''; | |
} | |
return `&status=${this.status}` | |
} | |
async callPaystackListEndpoint() { | |
let statusPath=''; | |
const { raw } = await needle('get', `https://api.paystack.co/transaction?perPage=${this.perPage}&page=${this.page}${this.statusFilter}&from=${this.useFrom}`, { | |
headers: { Authorization: `Bearer ${this.secretKey}` }, | |
}); | |
const resp = raw.toString('utf8'); | |
return JSON.parse(resp); | |
} | |
async pullMoreIfAvailable(respObj) { | |
// if this is not the last page ... | |
if (respObj.meta && (this.page < respObj.meta.pageCount)) { | |
// ... pull next page | |
this.page = this.page + 1; | |
await this.fetchRecentlySuccessfulFromPaystack(); | |
} | |
} | |
async fetchRecentlySuccessfulFromPaystack({ tries = 0 } = {}) { | |
try { | |
const respObj = await this.callPaystackListEndpoint(); | |
if (respObj.status && respObj.data) { | |
this.list.push(respObj.data); | |
await this.pullMoreIfAvailable(respObj); | |
return { concluded: true }; | |
} | |
// invalid response (status was false or empty), try again | |
if (tries < this.maxApiCallRetries) { | |
return this.fetchRecentlySuccessfulFromPaystack({ tries: tries + 1 }); | |
} | |
// we have exhausted our tries | |
return { concluded: false, message: respObj.message }; | |
} catch (error) { | |
// try 'this.maxApiCallRetries' times before giving up | |
if (tries < this.maxApiCallRetries) { | |
return this.fetchRecentlySuccessfulFromPaystack({ tries: tries + 1 }); | |
} | |
return { concluded: false, message: 'There was an error fetching transaction. Please check Paystack API status page for more information.' }; | |
} | |
} | |
} | |
async function fetch({ | |
secretKey, maxApiCallRetries, startFrom, perPage, | |
}) { | |
const fetcher = new PaystackTransactionsFetcher({ | |
secretKey, maxApiCallRetries, startFrom, perPage, | |
}); | |
const { concluded, message } = await fetcher.fetchRecentlySuccessfulFromPaystack(); | |
if (concluded) { | |
return fetcher.list; | |
} | |
throw new Error(message); | |
} | |
module.exports = { fetch }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment