autopv-cli
Version:
AutoPrivacy DSAR evidence-pack generator - Automated GDPR compliance for SaaS companies
69 lines (68 loc) • 2.43 kB
JavaScript
import Stripe from 'stripe';
export class StripeProvider {
stripe;
constructor(secretKey) {
this.stripe = new Stripe(secretKey, {
apiVersion: '2023-10-16',
});
}
async exportCustomerData(email) {
const result = {
customers: [],
charges: [],
methods: []
};
try {
// Search for customers by email
const customerSearch = await this.stripe.customers.search({
query: `email:'${email}'`,
limit: 100
});
result.customers = customerSearch.data;
if (result.customers.length === 0) {
console.warn(`No Stripe customers found with email: ${email}`);
return result;
}
// For each customer, get their charges and payment methods
for (const customer of result.customers) {
try {
// Get charges for this customer
const charges = await this.stripe.charges.list({
customer: customer.id,
limit: 100
});
result.charges.push(...charges.data);
// Get payment methods for this customer
const paymentMethods = await this.stripe.paymentMethods.list({
customer: customer.id,
limit: 100
});
result.methods.push(...paymentMethods.data);
}
catch (customerError) {
console.warn(`Error fetching data for customer ${customer.id}:`, customerError.message);
}
}
}
catch (error) {
throw new Error(`Stripe API error: ${error.message}`);
}
return result;
}
calculateTotalCharges(charges) {
return charges.reduce((total, charge) => {
// Only count successful charges
if (charge.status === 'succeeded') {
return total + charge.amount;
}
return total;
}, 0);
}
formatAmount(amountInCents, currency = 'usd') {
const amount = amountInCents / 100;
return new Intl.NumberFormat('en-US', {
style: 'currency',
currency: currency.toUpperCase(),
}).format(amount);
}
}