Skip to content

Instantly share code, notes, and snippets.

@vernondegoede
Created April 9, 2019 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vernondegoede/567fd39a2ad19a44fdf448c81c488552 to your computer and use it in GitHub Desktop.
Save vernondegoede/567fd39a2ad19a44fdf448c81c488552 to your computer and use it in GitHub Desktop.
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