-
Notifications
You must be signed in to change notification settings - Fork 0
/
stripeService.js
64 lines (58 loc) · 2.63 KB
/
stripeService.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
const dateChecker = require('./dateChecker');
module.exports = {
async retrieveSubscriptionId(intent) {
let subscriptionId = '';
if (intent.invoice) {
const invoice = await stripe.invoices.retrieve(intent.invoice);
const customer = await getCustomerWithSubscriptionDetails(invoice);
subscriptionId = customer.subscriptionId;
}
return subscriptionId;
},
};
async function getCustomerWithSubscriptionDetails(invoice) {
const customer = await stripe.customers.retrieve(
invoice.customer,
{
expand: ['subscriptions.data.default_payment_method'],
},
);
return setSubscriptionDetailsOnCustomerObject(customer, invoice);
}
/**
* The webhook which called this method will fire for every
* customer who's card is expiring (even the non subscription ones).
*
* This loop below attempts to find a subscription start date for the
* card which is expiring. If one doesn't exist then we shouldn't send
* an email because it could be a one off donor's card who's expiring.
*/
function setSubscriptionDetailsOnCustomerObject(customer, invoice) {
const updatedCustomer = customer;
customer.subscriptions.data.forEach((subscription) => {
if (invoice.subscription === subscription.id && (subscription.status === 'active' || subscription.status === 'past_due')) {
updatedCustomer.subscriptionStartDate = dateChecker.convertUnixTimestampToDateString(subscription.start_date);
const subscriptionItem = subscription.items.data[0];
if (subscriptionItem) {
updatedCustomer.subscriptionAmount = convertStripeAmountToMatchHowItIsDisplayedInTheDonationForm(subscriptionItem.price.unit_amount);
}
updatedCustomer.subscriptionId = subscription.id;
if (subscription.default_payment_method && subscription.default_payment_method.card) {
updatedCustomer.card = subscription.default_payment_method.card;
}
}
});
return updatedCustomer;
}
/**
* Stripe stores donation amounts in grosz/cents/pennies.
* So 5 zloties/dollars/pounds will be stored as 500 because of the grosz/cents.
* We remove these extra zeroes because our donation form only takes zloties/dollars/pounds
* @param {int} amount = amount in grosz/cents/pennies
* @returns {int} convertedAmount = amount in zloties/dollars/pennies
*/
function convertStripeAmountToMatchHowItIsDisplayedInTheDonationForm(amount) {
const convertedAmount = amount / 100;
return convertedAmount;
}