refunds/chargebacks

This commit is contained in:
Kiara Grouwstra 2021-10-20 23:09:28 +02:00
parent 81c7c6f346
commit 296c7b43b5
1 changed files with 22 additions and 1 deletions

View File

@ -4,12 +4,17 @@ import { unparse } from 'papaparse';
import { createMollieClient, MollieClient } from '@mollie/api-client';
import { log, mapKeys, prepend } from './utils';
import { mollieKeys, listAll } from './mollie';
import assert from 'assert';
async function main() {
const mollie = createMollieClient({ apiKey: mollieKeys.leden });
const apiKey = mollieKeys.leden;
const mollie = createMollieClient({ apiKey });
const customers = await exportMollieCustomers(mollie);
const customerObj = Object.fromEntries(customers.map(({ id, name, email, createdAt }) => ([id, { name, email, createdAt }])));
const payments = await exportMolliePayments(mollie, customerObj);
assert(apiKey.slice(0,5) == 'live_');
const refunds = await exportMollieRefunds(mollie);
const chargebacks = await exportMollieChargebacks(mollie);
}
async function exportMollieCustomers(mollie: MollieClient): Promise<{[k: string]: any}[]> {
@ -20,6 +25,22 @@ async function exportMollieCustomers(mollie: MollieClient): Promise<{[k: string]
return list;
}
async function exportMollieRefunds(mollie: MollieClient): Promise<{[k: string]: any}[]> {
const page$ = mollie.refunds.list();
const list = (await listAll(page$, 500))
.map(({ resource, id, amount, status, createdAt, description, metadata, paymentId, settlementId, settlementAmount, _links, lines }) => ({ resource, id, status, createdAt, description, metadata, paymentId, settlementId, lines, ...mapKeys(prepend('amount.'))(amount), ...mapKeys(prepend('settlementAmount.'))(settlementAmount)/*, ...mapKeys(prepend('_links.'))(_links)*/ }));
writeFileSync('./data/refunds.csv', unparse(list));
return list;
}
async function exportMollieChargebacks(mollie: MollieClient): Promise<{[k: string]: any}[]> {
const page$ = mollie.chargebacks.list();
const list = (await listAll(page$, 500))
.map(({ resource, id, amount, createdAt, reason, paymentId, settlementAmount, _links }) => ({ resource, id, createdAt, paymentId, ...mapKeys(prepend('reason.'))(reason), ...mapKeys(prepend('amount.'))(amount), ...mapKeys(prepend('settlementAmount.'))(settlementAmount)/*, ...mapKeys(prepend('_links.'))(_links)*/ }));
writeFileSync('./data/chargebacks.csv', unparse(list));
return list;
}
async function exportMolliePayments(mollie: MollieClient, customerObj: { [id: string]: { name: string, email: string, createdAt: string } }): Promise<{[k: string]: any}[]> {
const page$ = mollie.payments.list();
const list = (await listAll(page$, 500))