Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 45 additions & 28 deletions apps/api/src/stripe/events/stripe-payment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,42 +155,59 @@ export class StripePaymentService {
@StripeWebhookHandler('charge.refunded')
async handleRefundCreated(event: Stripe.Event) {
const chargePaymentIntent: Stripe.Charge = event.data.object as Stripe.Charge
const paymentIntentId = chargePaymentIntent.payment_intent as string

Logger.log(
'[ handleRefundCreated ]',
chargePaymentIntent,
chargePaymentIntent.metadata as StripeMetadata,
'[ handleRefundCreated ] Processing refund for charge ' +
chargePaymentIntent.id +
' paymentIntent ' +
paymentIntentId,
)

const paymentIntentId = chargePaymentIntent.payment_intent as string

const donation = await this.donationService.getDonationByPaymentIntent(paymentIntentId)
const campaign = donation?.targetVault?.campaign
try {
const donation = await this.donationService.getDonationByPaymentIntent(paymentIntentId)
const campaign = donation?.targetVault?.campaign

if (!campaign) {
Logger.warn(
'[ handleRefundCreated ] No donation/campaign found for payment intent ' + paymentIntentId,
)
return
}
if (!campaign) {
Logger.error(
'[ handleRefundCreated ] No donation/campaign found for payment intent ' +
paymentIntentId,
)
return
}

const billingData = getPaymentDataFromCharge(chargePaymentIntent)
const billingData = getPaymentDataFromCharge(chargePaymentIntent)

await this.donationService.updateDonationPayment(campaign, billingData, PaymentStatus.refund)
await this.donationService.updateDonationPayment(campaign, billingData, PaymentStatus.refund)

if (billingData.billingEmail !== undefined) {
const recepient = { to: [billingData.billingEmail] }
const mail = new RefundDonationEmailDto({
campaignName: campaign.title,
currency: billingData.currency.toUpperCase(),
netAmount: billingData.netAmount / 100,
taxAmount: (billingData.chargedAmount - billingData.netAmount) / 100,
})
// Send Notification
Logger.log(
'[ handleRefundCreated ] Successfully processed refund for payment intent ' +
paymentIntentId,
)

await this.sendEmail.sendFromTemplate(mail, recepient, {
//Allow users to receive the mail, regardles of unsubscribes
bypassUnsubscribeManagement: { enable: true },
})
if (billingData.billingEmail !== undefined) {
const recepient = { to: [billingData.billingEmail] }
const mail = new RefundDonationEmailDto({
campaignName: campaign.title,
currency: billingData.currency.toUpperCase(),
netAmount: billingData.netAmount / 100,
taxAmount: (billingData.chargedAmount - billingData.netAmount) / 100,
})
// Send Notification

await this.sendEmail.sendFromTemplate(mail, recepient, {
//Allow users to receive the mail, regardles of unsubscribes
bypassUnsubscribeManagement: { enable: true },
})
}
} catch (error) {
Logger.error(
'[ handleRefundCreated ] Failed to process refund for payment intent ' +
paymentIntentId +
': ' +
error,
)
throw error
}
}

Expand Down
Loading