Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
Fix possible undefined errors from GoCardless
Browse files Browse the repository at this point in the history
For some mad reason they've made everything optional, even fields that
aren't.
gocardless/gocardless-nodejs#135
  • Loading branch information
wpf500 committed Jan 5, 2023
1 parent 0dfff2b commit bf441fb
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 24 deletions.
10 changes: 5 additions & 5 deletions src/core/providers/payment-flow/GCProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class GCProvider implements PaymentFlowProvider {
log.info("Created redirect flow " + redirectFlow.id);

return {
id: redirectFlow.id,
id: redirectFlow.id!,
params: {
redirectUrl: redirectFlow.redirect_url
redirectUrl: redirectFlow.redirect_url!
}
};
}
Expand All @@ -49,8 +49,8 @@ class GCProvider implements PaymentFlowProvider {

return {
paymentMethod: joinFlow.joinForm.paymentMethod,
customerId: redirectFlow.links.customer,
mandateId: redirectFlow.links.mandate
customerId: redirectFlow.links!.customer!,
mandateId: redirectFlow.links!.mandate!
};
}

Expand All @@ -66,7 +66,7 @@ class GCProvider implements PaymentFlowProvider {
...(customer.family_name && { lastname: customer.family_name }),
billingAddress: {
line1: customer.address_line1 || "",
line2: customer.address_line2,
line2: customer.address_line2 || undefined,
city: customer.city || "",
postcode: customer.postal_code || ""
}
Expand Down
10 changes: 5 additions & 5 deletions src/core/providers/payment/GCProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ export default class GCProvider extends PaymentProvider<GCPaymentData> {
try {
const mandate = await gocardless.mandates.get(this.data.mandateId);
const bankAccount = await gocardless.customerBankAccounts.get(
mandate.links.customer_bank_account
mandate.links!.customer_bank_account!
);

paymentSource = {
method: PaymentMethod.GoCardlessDirectDebit,
bankName: bankAccount.bank_name,
accountHolderName: bankAccount.account_holder_name,
accountNumberEnding: bankAccount.account_number_ending
bankName: bankAccount.bank_name || "",
accountHolderName: bankAccount.account_holder_name || "",
accountNumberEnding: bankAccount.account_number_ending || ""
};
pendingPayment = await hasPendingPayment(this.data.mandateId);
} catch (err: any) {
Expand Down Expand Up @@ -148,7 +148,7 @@ export default class GCProvider extends PaymentProvider<GCPaymentData> {
});

this.data.cancelledAt = null;
this.data.subscriptionId = subscription.id;
this.data.subscriptionId = subscription.id!;
this.data.payFee = paymentForm.payFee;
this.data.nextMonthlyAmount = startNow ? null : paymentForm.monthlyAmount;

Expand Down
4 changes: 2 additions & 2 deletions src/core/utils/payment/gocardless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export async function getSubscriptionNextChargeDate(
// include pending payments
const date = pendingPayment
? pendingPayment.charge_date
: subscription.upcoming_payments[0].charge_date;
: subscription.upcoming_payments![0].charge_date;
return moment.utc(date).add(config.gracePeriod).toDate();
}

Expand All @@ -84,7 +84,7 @@ export async function createSubscription(
const mandate = await gocardless.mandates.get(mandateId);
// next_possible_charge_date will always have a value as this is an active mandate
if (startDate < mandate.next_possible_charge_date!) {
startDate = mandate.next_possible_charge_date;
startDate = mandate.next_possible_charge_date!;
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/webhooks/handlers/gocardless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ async function handlePaymentResourceEvent(event: Event) {
// limited. It seems like we can pretty safely assume paid out payments
// haven't changed though.
if (event.action === PaymentStatus.PaidOut) {
await updatePaymentStatus(event.links.payment, PaymentStatus.PaidOut);
await updatePaymentStatus(event.links!.payment!, PaymentStatus.PaidOut);
} else {
await updatePayment(
event.links.payment,
event.links!.payment!,
event.action === PaymentStatus.Confirmed
);
}
Expand All @@ -103,7 +103,7 @@ async function handleSubscriptionResourceEvent(event: Event) {
case "customer_approval_denied":
case "cancelled":
case "finished":
await cancelSubscription(event.links.subscription);
await cancelSubscription(event.links!.subscription!);
break;
}
}
Expand All @@ -128,14 +128,14 @@ async function handleMandateResourceEvent(event: Event) {
case "failed":
case "expired":
// Remove the mandate from the database
await cancelMandate(event.links.mandate);
await cancelMandate(event.links!.mandate!);
break;
}
}

async function handleRefundResourceEvent(event: Event) {
const refund = await gocardless.refunds.get(event.links.refund);
await updatePayment(refund.links.payment);
const refund = await gocardless.refunds.get(event.links!.refund!);
await updatePayment(refund.links!.payment!);
}

export default app;
15 changes: 9 additions & 6 deletions src/webhooks/utils/gocardless.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function updatePayment(
const payment = await findOrCreatePayment(gcPayment);

if (payment) {
payment.status = convertStatus(gcPayment.status);
payment.status = convertStatus(gcPayment.status!);
payment.description = gcPayment.description || "Unknown";
payment.amount = Number(gcPayment.amount) / 100;
payment.amountRefunded = Number(gcPayment.amount_refunded) / 100;
Expand Down Expand Up @@ -68,7 +68,7 @@ async function confirmPayment(
if (payment.subscriptionId !== gcData.subscriptionId) {
log.error("Mismatched subscription IDs for payment " + payment.id, {
ourSubscriptionId: payment.subscriptionId,
gcSubscriptionId: gcPayment.links.subscription
gcSubscriptionId: gcPayment.links!.subscription
});
return;
}
Expand Down Expand Up @@ -101,7 +101,10 @@ async function getSubscriptionPeriodEnd(payment: Payment): Promise<Date> {
);

// If the subscription has been cancelled there won't be any upcoming payments
if (subscription.upcoming_payments.length > 0) {
if (
subscription.upcoming_payments &&
subscription.upcoming_payments.length > 0
) {
return moment
.utc(subscription.upcoming_payments[0].charge_date)
.add(config.gracePeriod)
Expand Down Expand Up @@ -179,7 +182,7 @@ async function findOrCreatePayment(

const data = await PaymentService.getDataBy(
"mandateId",
gcPayment.links.mandate
gcPayment.links!.mandate!
);

if (data) {
Expand All @@ -188,9 +191,9 @@ async function findOrCreatePayment(
gcPaymentId: gcPayment.id
});
const newPayment = new Payment();
newPayment.id = gcPayment.id;
newPayment.id = gcPayment.id!;
newPayment.contact = data.contact;
if (gcPayment.links.subscription) {
if (gcPayment.links?.subscription) {
newPayment.subscriptionId = gcPayment.links.subscription;
}
return newPayment;
Expand Down

0 comments on commit bf441fb

Please sign in to comment.