-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Stripe] Implement Refund event listener in Stripe webhook #556
Changes from all commits
dbdaab0
ac18174
124f0f8
f5c6bee
dc03c24
e1b88c2
6ecd8fe
75c4806
5af12e2
398c341
9a7cbe8
4ba18d7
1bed959
700087a
a7ce421
9f7b4ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"subject": "Заявка за възстановяване на пари от дарение в Подкрепи.бг" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<mjml> | ||
<mj-body background-color="#ffffff" font-size="13px"> | ||
<mj-section | ||
background-color="#009FE3" | ||
vertical-align="top" | ||
padding-bottom="0px" | ||
padding-top="0"> | ||
<mj-column vertical-align="top" width="100%"> | ||
<mj-text | ||
align="left" | ||
color="#ffffff" | ||
font-size="45px" | ||
font-weight="bold" | ||
font-family="open Sans Helvetica, Arial, sans-serif" | ||
padding-left="25px" | ||
padding-right="25px" | ||
padding-bottom="30px" | ||
padding-top="50px"> | ||
Върнати пари от дарение в Подкрепи.бг | ||
</mj-text> | ||
</mj-column> | ||
</mj-section> | ||
<mj-section background-color="#009fe3" padding-bottom="20px" padding-top="20px"> | ||
<mj-column vertical-align="middle" width="100%"> | ||
<mj-text | ||
align="left" | ||
color="#ffffff" | ||
font-size="22px" | ||
font-family="open Sans Helvetica, Arial, sans-serif" | ||
padding-left="25px" | ||
padding-right="25px"> | ||
<span style="color: #feeb35"> Здравейте, </span> | ||
<br /><br /> | ||
</mj-text> | ||
<mj-text | ||
align="left" | ||
color="#ffffff" | ||
font-size="15px" | ||
font-family="open Sans Helvetica, Arial, sans-serif" | ||
padding-left="25px" | ||
padding-right="25px"> | ||
Това е автоматичен e-mail, който потвърждава, че ще получите обратно парите от вашето | ||
дарение към кампанията {{campaignName}}. <br /> | ||
За съжаление, поради ограничения на Stripe, не можем да ви възстановим таксите, които са | ||
удържани от тях. <br /><br /> | ||
|
||
Допълнителни подробности:<br /> | ||
Сумата която ще възстановим е на стойност {{netAmount}} {{currency}}.<br /> | ||
Таксата удържана от Stripe е на стойност {{taxAmount}} {{currency}}<br /> | ||
Транзакцията ще отнеме от 5 до 10 дни и парите ще ви бъдат върнати към същата карта, с | ||
която сте превели дарението към Страйп. <br /><br /> | ||
|
||
Благодарим ви за разбирането! | ||
</mj-text> | ||
<mj-text | ||
align="left" | ||
color="#ffffff" | ||
font-size="15px" | ||
font-family="open Sans Helvetica, Arial, sans-serif" | ||
padding-left="25px" | ||
padding-right="25px"> | ||
Поздрави, <br /> | ||
Екипът на Подкрепи.бг</mj-text | ||
> | ||
</mj-column> | ||
</mj-section> | ||
</mj-body> | ||
</mjml> |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -482,6 +482,28 @@ export class DonationsService { | |
return this.createInitialDonationFromIntent(campaign, inputDto, intent) | ||
} | ||
|
||
/** | ||
* Refund a stipe payment donation | ||
* https://stripe.com/docs/api/refunds/create | ||
* @param inputDto Refund-stripe params | ||
* @returns {Promise<Stripe.Response<Stripe.Refund>>} | ||
*/ | ||
async refundStripePayment(paymentIntentId: string): Promise<Stripe.Response<Stripe.Refund>> { | ||
const intent = await this.stripeClient.paymentIntents.retrieve(paymentIntentId) | ||
if (!intent) { | ||
throw new BadRequestException('Payment Intent is missing from stripe') | ||
} | ||
|
||
if (!intent.metadata.campaignId) { | ||
throw new BadRequestException('Campaign id is missing from payment intent metadata') | ||
} | ||
|
||
return await this.stripeClient.refunds.create({ | ||
payment_intent: paymentIntentId, | ||
reason: 'requested_by_customer', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [TODO] for next version, it would be good to allow several predefined reasons and a free text that can be selected/entered on the fronted from the Admin UI. |
||
}) | ||
} | ||
|
||
/** | ||
* Update a payment intent for a donation | ||
* https://stripe.com/docs/api/payment_intents/update | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,9 @@ import { | |
PaymentData, | ||
} from '../helpers/payment-intent-helpers' | ||
import { DonationStatus, CampaignState } from '@prisma/client' | ||
import { EmailService } from '../../email/email.service' | ||
import { RefundDonationEmailDto } from '../../email/template.interface' | ||
import { PrismaService } from '../../prisma/prisma.service' | ||
|
||
/** Testing Stripe on localhost is described here: | ||
* https://github.com/podkrepi-bg/api/blob/master/TESTING.md#testing-stripe | ||
|
@@ -25,6 +28,8 @@ export class StripePaymentService { | |
constructor( | ||
private campaignService: CampaignService, | ||
private recurringDonationService: RecurringDonationService, | ||
private sendEmail: EmailService, | ||
private prismaService: PrismaService, | ||
) {} | ||
|
||
@StripeWebhookHandler('payment_intent.created') | ||
|
@@ -147,6 +152,50 @@ export class StripePaymentService { | |
} | ||
} | ||
|
||
@StripeWebhookHandler('charge.refunded') | ||
async handleRefundCreated(event: Stripe.Event) { | ||
const chargePaymentIntent: Stripe.Charge = event.data.object as Stripe.Charge | ||
Logger.log( | ||
'[ handleRefundCreated ]', | ||
chargePaymentIntent, | ||
chargePaymentIntent.metadata as DonationMetadata, | ||
) | ||
|
||
const metadata: DonationMetadata = chargePaymentIntent.metadata as DonationMetadata | ||
|
||
if (!metadata.campaignId) { | ||
Logger.debug('[ handleRefundCreated ] No campaignId in metadata ' + chargePaymentIntent.id) | ||
return | ||
} | ||
|
||
const billingData = getPaymentDataFromCharge(chargePaymentIntent) | ||
|
||
const campaign = await this.campaignService.getCampaignById(metadata.campaignId) | ||
|
||
await this.campaignService.updateDonationPayment( | ||
campaign, | ||
billingData, | ||
DonationStatus.refund, | ||
metadata, | ||
) | ||
|
||
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 }, | ||
}) | ||
Comment on lines
+192
to
+195
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good call here! |
||
} | ||
} | ||
|
||
@StripeWebhookHandler('customer.subscription.created') | ||
async handleSubscriptionCreated(event: Stripe.Event) { | ||
const subscription: Stripe.Subscription = event.data.object as Stripe.Subscription | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great for including the reference to the docs!