Created
April 9, 2019 06:52
-
-
Save vernondegoede/567fd39a2ad19a44fdf448c81c488552 to your computer and use it in GitHub Desktop.
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 apiKey = "test_xxx"; | |
const mollie = require("@mollie/api-client")({ apiKey }); | |
const MANDATE_STATUS_VALID = "valid"; | |
const retrieveCustomerMandates = customerId => | |
mollie.customers_mandates.all({ customerId }); | |
const customerHasValidMandates = customerId => { | |
return new Promise((resolve, reject) => { | |
console.log(`Checking whether customer ${customerId} has valid mandates`); | |
retrieveCustomerMandates(customerId) | |
.then(mandates => { | |
const firstValidMandate = mandates.find( | |
({ status }) => status === MANDATE_STATUS_VALID, | |
); | |
if (firstValidMandate) { | |
resolve(firstValidMandate); | |
} | |
reject(`No valid mandate found for customer ${customerId}`); | |
}) | |
.catch(error => { | |
reject(error); | |
}); | |
}); | |
}; | |
customerHasValidMandates("cst_pzhEvnttJ2") | |
.then(mandate => | |
console.log("Found mandate for customer cst_pzhEvnttJ2", mandate), | |
) | |
.catch(console.log); | |
customerHasValidMandates("cst_UK9ArCbc2K") | |
.then(mandate => | |
console.log("Found mandate for customer cst_UK9ArCbc2K", mandate), | |
) | |
.catch(console.log); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment