diff --git a/src/core/providers/payment-flow/GCProvider.ts b/src/core/providers/payment-flow/GCProvider.ts index 82786e183..87eb1caf5 100644 --- a/src/core/providers/payment-flow/GCProvider.ts +++ b/src/core/providers/payment-flow/GCProvider.ts @@ -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! } }; } @@ -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! }; } @@ -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 || "" } diff --git a/src/core/providers/payment/GCProvider.ts b/src/core/providers/payment/GCProvider.ts index 9a864050c..a81721383 100644 --- a/src/core/providers/payment/GCProvider.ts +++ b/src/core/providers/payment/GCProvider.ts @@ -39,14 +39,14 @@ export default class GCProvider extends PaymentProvider { 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) { @@ -148,7 +148,7 @@ export default class GCProvider extends PaymentProvider { }); 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; diff --git a/src/core/utils/payment/gocardless.ts b/src/core/utils/payment/gocardless.ts index 6fafc7a82..a922df8ac 100644 --- a/src/core/utils/payment/gocardless.ts +++ b/src/core/utils/payment/gocardless.ts @@ -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(); } @@ -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!; } } diff --git a/src/webhooks/handlers/gocardless.ts b/src/webhooks/handlers/gocardless.ts index 9d054fe06..5c4a9b822 100644 --- a/src/webhooks/handlers/gocardless.ts +++ b/src/webhooks/handlers/gocardless.ts @@ -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 ); } @@ -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; } } @@ -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; diff --git a/src/webhooks/utils/gocardless.ts b/src/webhooks/utils/gocardless.ts index 6882f0229..39fb0a797 100644 --- a/src/webhooks/utils/gocardless.ts +++ b/src/webhooks/utils/gocardless.ts @@ -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; @@ -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; } @@ -101,7 +101,10 @@ async function getSubscriptionPeriodEnd(payment: Payment): Promise { ); // 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) @@ -179,7 +182,7 @@ async function findOrCreatePayment( const data = await PaymentService.getDataBy( "mandateId", - gcPayment.links.mandate + gcPayment.links!.mandate! ); if (data) { @@ -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;