-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Notify partners via email when a stablecoin payout fails or is returned #4264
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
Open
devkiran
wants to merge
2
commits into
main
Choose a base branch
from
stablecoin-payout-failed-email
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
...b/app/(ee)/api/stripe/connect/v2/webhook/utils/notify-partner-stablecoin-payout-failed.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { prisma } from "@/lib/prisma"; | ||
| import { sendEmail } from "@dub/email"; | ||
| import PartnerStablecoinPayoutFailed from "@dub/email/templates/partner-stablecoin-payout-failed"; | ||
| import { currencyFormatter, prettyPrint } from "@dub/utils"; | ||
| import { PayoutStatus } from "@prisma/client"; | ||
|
|
||
| function maskCryptoWalletAddress(address: string) { | ||
| return address.length > 10 | ||
| ? `${address.slice(0, 6)}••••${address.slice(-4)}` | ||
| : address; | ||
| } | ||
|
|
||
| export async function notifyPartnerStablecoinPayoutFailed( | ||
| outboundPaymentId: string, | ||
| ) { | ||
| const payouts = await prisma.payout.findMany({ | ||
| where: { | ||
| stripePayoutId: outboundPaymentId, | ||
| status: PayoutStatus.failed, | ||
| }, | ||
| include: { | ||
| partner: { | ||
| select: { | ||
| email: true, | ||
| cryptoWalletAddress: true, | ||
| }, | ||
| }, | ||
| program: { | ||
| select: { | ||
| name: true, | ||
| }, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| if (payouts.length === 0) { | ||
| console.log( | ||
| `No payouts found for outbound payment ${outboundPaymentId}. Skipping email send...`, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| const partner = payouts[0].partner; | ||
|
|
||
| if (!partner.email) { | ||
| console.log( | ||
| `Partner email not found for outbound payment ${outboundPaymentId}. Skipping email send...`, | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| const totalAmount = payouts.reduce((acc, payout) => acc + payout.amount, 0); | ||
| const failureReason = payouts[0].failureReason ?? undefined; | ||
|
|
||
| const programs = [ | ||
| ...new Map( | ||
| payouts.map((payout) => [payout.program.name, payout.program]), | ||
| ).values(), | ||
| ]; | ||
|
|
||
| const maskedAddress = partner.cryptoWalletAddress | ||
| ? maskCryptoWalletAddress(partner.cryptoWalletAddress) | ||
| : undefined; | ||
|
|
||
| const emailResponse = await sendEmail({ | ||
| variant: "notifications", | ||
| subject: `Your recent partner payout of ${currencyFormatter(totalAmount)} failed`, | ||
| to: partner.email, | ||
| react: PartnerStablecoinPayoutFailed({ | ||
| email: partner.email, | ||
| programs, | ||
| payout: { | ||
| amount: totalAmount, | ||
| failureReason, | ||
| }, | ||
| wallet: maskedAddress | ||
| ? { | ||
| maskedAddress, | ||
| } | ||
| : undefined, | ||
| }), | ||
| }); | ||
|
devkiran marked this conversation as resolved.
|
||
|
|
||
| console.log( | ||
| `Stablecoin payout failed email sent to partner ${partner.email}:`, | ||
| prettyPrint(emailResponse), | ||
| ); | ||
|
devkiran marked this conversation as resolved.
|
||
| } | ||
135 changes: 135 additions & 0 deletions
135
packages/email/src/templates/partner-stablecoin-payout-failed.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| import { currencyFormatter, DUB_WORDMARK } from "@dub/utils"; | ||
| import { | ||
| Body, | ||
| Container, | ||
| Head, | ||
| Heading, | ||
| Html, | ||
| Img, | ||
| Link, | ||
| Preview, | ||
| Section, | ||
| Tailwind, | ||
| Text, | ||
| } from "@react-email/components"; | ||
| import { Footer } from "../components/footer"; | ||
|
|
||
| // Send this email to the partner when a stablecoin payout fails or is returned | ||
| export default function PartnerStablecoinPayoutFailed({ | ||
| programs = [{ name: "Acme" }], | ||
| payout = { | ||
| amount: 530000, | ||
| failureReason: "The destination wallet address is invalid.", | ||
| }, | ||
| wallet = { | ||
| maskedAddress: "0x1234••••5678", | ||
| network: "ethereum", | ||
| }, | ||
| email = "panic@thedis.co", | ||
| }: { | ||
| programs: { | ||
| name: string; | ||
| }[]; | ||
| payout: { | ||
| amount: number; // in cents | ||
| failureReason?: string; | ||
| }; | ||
| wallet?: { | ||
| maskedAddress?: string; | ||
| network?: string; | ||
| }; | ||
| email: string; | ||
| }) { | ||
| const amountFormatted = currencyFormatter(payout.amount); | ||
| const programNamesFormatted = programs.map((p) => p.name).join(", "); | ||
|
|
||
| return ( | ||
| <Html> | ||
| <Head /> | ||
| <Preview>Action Required - Your recent stablecoin payout failed</Preview> | ||
| <Tailwind> | ||
| <Body className="mx-auto my-auto bg-white font-sans"> | ||
| <Container className="mx-auto my-8 max-w-[600px] px-8 py-8"> | ||
| <Section className="mt-8"> | ||
| <Img src={DUB_WORDMARK} height="32" alt="Dub" /> | ||
| </Section> | ||
|
|
||
| <Heading className="mx-0 my-8 p-0 text-lg font-medium text-black"> | ||
| Your recent payout | ||
| {programs.length === 1 | ||
| ? ` from ${programNamesFormatted}` | ||
| : ""}{" "} | ||
| failed | ||
| </Heading> | ||
|
|
||
| <Text> | ||
| We attempted to send your recent payout of{" "} | ||
| <span className="font-semibold text-purple-600"> | ||
| {amountFormatted} | ||
| </span> | ||
| {programs.length > 1 ? ( | ||
| <> | ||
| {" "} | ||
| from{" "} | ||
| <span className="font-semibold text-purple-600"> | ||
| {programNamesFormatted} | ||
| </span> | ||
| </> | ||
| ) : null}{" "} | ||
| to your stablecoin wallet | ||
| {wallet?.maskedAddress ? ( | ||
| <> | ||
| {" "} | ||
| ( | ||
| <span className="font-semibold text-purple-600"> | ||
| {wallet.maskedAddress} | ||
| {wallet.network ? ` · ${wallet.network}` : ""} | ||
| </span> | ||
| ) | ||
| </> | ||
| ) : null} | ||
| , but the transaction failed. | ||
| </Text> | ||
|
|
||
| {payout.failureReason && ( | ||
| <Text className="text-sm leading-6 text-neutral-600"> | ||
| Reason:{" "} | ||
| <span className="font-semibold italic text-neutral-800"> | ||
| {payout.failureReason} | ||
| </span> | ||
| </Text> | ||
| )} | ||
|
|
||
| <Text> | ||
| Payout failures are usually due to incorrect wallet configuration. | ||
| Please reconnect your stablecoin wallet at your earliest | ||
| convenience and retry the payout from your{" "} | ||
| <Link | ||
| href="https://partners.dub.co/payouts?settings=true" | ||
| className="font-medium text-black underline" | ||
| > | ||
| Payout settings | ||
| </Link> | ||
| . | ||
| </Text> | ||
|
|
||
| <Section className="my-8"> | ||
| <Link | ||
| className="rounded-lg bg-neutral-900 px-6 py-3 text-[13px] font-medium text-white no-underline" | ||
| href="https://partners.dub.co/payouts?settings=true" | ||
| > | ||
| Reconnect wallet | ||
| </Link> | ||
| </Section> | ||
|
|
||
| <Text className="text-sm leading-6 text-neutral-600"> | ||
| If you have any questions, just reply to this email. | ||
| </Text> | ||
|
|
||
| <Footer email={email} /> | ||
| </Container> | ||
| </Body> | ||
| </Tailwind> | ||
| </Html> | ||
| ); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.