Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Filter stripe refunded invoices (#3634)

Pedro Ladeirafae25d7fb543
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/commissions/create-commission-sheet.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/commissions/create-commission-sheet.tsx
index d7ade26b1d..72d2c034a8 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/commissions/create-commission-sheet.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/commissions/create-commission-sheet.tsx
@@ -677,7 +677,11 @@ function CreateCommissionSheetContent({
</a>
<p className="mt-0.5 flex flex-wrap items-center gap-x-2 text-xs text-neutral-500">
{formatDate(inv.createdAt)}
- {inv.dubCommissionId && (
+ {inv.refunded ? (
+ <span className="rounded-md bg-neutral-200/80 px-1.5 py-0.5 text-xs text-neutral-500">
+ Refunded
+ </span>
+ ) : inv.dubCommissionId ? (
<a
href={`/${slug}/program/commissions?partnerId=${partnerId}&customerId=${customerId}`}
target="_blank"
@@ -685,7 +689,7 @@ function CreateCommissionSheetContent({
>
Already imported
</a>
- )}
+ ) : null}
</p>
</div>
<span
diff --git a/apps/web/lib/actions/partners/create-manual-commission.ts b/apps/web/lib/actions/partners/create-manual-commission.ts
index 5c521d2302..df799f7182 100644
--- a/apps/web/lib/actions/partners/create-manual-commission.ts
+++ b/apps/web/lib/actions/partners/create-manual-commission.ts
@@ -253,6 +253,12 @@ export const createManualCommissionAction = authActionClient
currency: saleEvent.currency,
invoiceId: saleEvent.invoice_id,
createdAt: new Date(saleEvent.timestamp),
+ // if the invoice payment was refunded on Stripe, set the commission status to refunded as well
+ ...(stripeCustomerInvoices.find(
+ (invoice) => invoice.id === saleEvent.invoice_id,
+ )?.refunded && {
+ status: "refunded",
+ }),
user,
context: {
customer: { country: customer.country },
diff --git a/apps/web/lib/api/customers/get-customer-stripe-invoices.ts b/apps/web/lib/api/customers/get-customer-stripe-invoices.ts
index 4bd0f65e6e..837b8c5014 100644
--- a/apps/web/lib/api/customers/get-customer-stripe-invoices.ts
+++ b/apps/web/lib/api/customers/get-customer-stripe-invoices.ts
@@ -1,11 +1,25 @@
import { stripeAppClient } from "@/lib/stripe";
import { StripeCustomerInvoiceSchema } from "@/lib/zod/schemas/customers";
import { prisma } from "@dub/prisma";
+import Stripe from "stripe";
const stripe = stripeAppClient({
...(process.env.VERCEL_ENV && { mode: "live" }),
});
+type ExpandedStripeInvoice = Stripe.Invoice & {
+ id: string;
+ payments?: {
+ data?: Array<{
+ payment?: {
+ type?: "charge" | "payment_intent" | "payment_record";
+ charge?: string | Stripe.Charge | null;
+ payment_intent?: string | Stripe.PaymentIntent | null;
+ };
+ }>;
+ };
+};
+
export async function getCustomerStripeInvoices({
stripeCustomerId,
stripeConnectId,
@@ -20,20 +34,37 @@ export async function getCustomerStripeInvoices({
customer: stripeCustomerId,
status: "paid",
limit: 100,
+ expand: ["data.payments.data.payment"],
},
{
stripeAccount: stripeConnectId,
},
);
- const validInvoices = data.filter(
- (invoice): invoice is (typeof data)[number] & { id: string } =>
- typeof invoice.id === "string",
- );
+ const invoices = data.filter(
+ (invoice) => invoice.id,
+ ) as ExpandedStripeInvoice[];
+
+ let charges: Stripe.Charge[] = [];
+ try {
+ const res = await stripe.charges.list(
+ {
+ limit: 100,
+ customer: stripeCustomerId,
+ },
+ {
+ stripeAccount: stripeConnectId,
+ },
+ );
+ charges = res.data;
+ } catch (error) {
+ // Stripe integration might be outdated and don't have charges.read permission
+ console.warn(error);
+ }
const commissions = await prisma.commission.findMany({
where: {
invoiceId: {
- in: validInvoices.map((invoice) => invoice.id),
+ in: invoices.map((invoice) => invoice.id),
},
programId: programId,
},
@@ -41,19 +72,54 @@ export async function getCustomerStripeInvoices({
const invoiceIdCommissionIdMap = commissions.reduce(
(acc, commission) => {
- acc[commission.invoiceId!] = commission.id;
+ if (commission.invoiceId) {
+ acc[commission.invoiceId] = commission.id;
+ }
return acc;
},
{} as Record<string, string>,
);
- const stripeCustomerInvoices = validInvoices.map((invoice) =>
+ const processInvoice = (invoice: ExpandedStripeInvoice) => {
+ const { payments, ...rest } = invoice;
+ if (!payments || !payments.data || !payments.data.length) {
+ return {
+ refunded: false,
+ metadata: rest,
+ };
+ }
+
+ for (const payment of payments.data) {
+ if (
+ payment.payment?.type === "payment_intent" &&
+ payment.payment?.payment_intent
+ ) {
+ const charge = charges.find(
+ (charge) => charge.payment_intent === payment.payment?.payment_intent,
+ );
+ if (charge?.refunded || (charge?.amount_refunded ?? 0) > 0) {
+ return {
+ refunded: true,
+ metadata: rest,
+ };
+ }
+ }
+ }
+
+ return {
+ refunded: false,
+ metadata: rest,
+ };
+ };
+
+ const stripeCustomerInvoices = invoices.map((invoice) =>
StripeCustomerInvoiceSchema.parse({
id: invoice.id,
amount: invoice.amount_paid,
createdAt: new Date(invoice.created * 1000),
- metadata: invoice,
+ refunded: processInvoice(invoice).refunded,
dubCommissionId: invoiceIdCommissionIdMap[invoice.id],
+ metadata: processInvoice(invoice).metadata,
}),
);
diff --git a/apps/web/lib/zod/schemas/customers.ts b/apps/web/lib/zod/schemas/customers.ts
index aeb02e6e7f..36ac2b377a 100644
--- a/apps/web/lib/zod/schemas/customers.ts
+++ b/apps/web/lib/zod/schemas/customers.ts
@@ -213,8 +213,9 @@ export const StripeCustomerInvoiceSchema = z.object({
id: z.string(),
amount: z.number(),
createdAt: z.date(),
- metadata: z.any(),
+ refunded: z.boolean(),
dubCommissionId: z.string().nullish(),
+ metadata: z.any(),
});
export const CUSTOMER_EXPORT_COLUMNS = [
diff --git a/packages/stripe-app/package.json b/packages/stripe-app/package.json
index 64efaca39d..e10825cb1e 100644
--- a/packages/stripe-app/package.json
+++ b/packages/stripe-app/package.json
@@ -1,7 +1,7 @@
{
"name": "com.example.dub",
- "version": "0.0.5",
- "description": "Dub Conversions",
+ "version": "0.0.19",
+ "description": "Dub Partners",
"private": true,
"license": "~~proprietary~~",
"dependencies": {
diff --git a/packages/stripe-app/stripe-app.json b/packages/stripe-app/stripe-app.json
index 1dd4df826e..fc0284b5f2 100644
--- a/packages/stripe-app/stripe-app.json
+++ b/packages/stripe-app/stripe-app.json
@@ -1,6 +1,6 @@
{
"id": "dub.co",
- "version": "0.0.18",
+ "version": "0.0.19",
"name": "Dub Partners",
"icon": "./stripe-icon.png",
"permissions": [
@@ -16,6 +16,10 @@
"permission": "invoice_read",
"purpose": "Allows Dub to read invoice information."
},
+ {
+ "permission": "charge_read",
+ "purpose": "Allows Dub to read charges to detect refunded payments."
+ },
{
"permission": "checkout_session_read",
"purpose": "Allows Dub to read checkout session information."
@@ -57,7 +61,6 @@
"purpose": "Allows Dub to read promotion codes for an account."
}
],
- "connect_permissions": null,
"ui_extension": {
"views": [
{
@@ -77,13 +80,13 @@
}
},
"post_install_action": {
- "type": "settings"
+ "type": "settings",
+ "url": ""
},
"allowed_redirect_uris": [
"https://app.dub.co/api/stripe/integration/callback",
"https://preview.dub.co/api/stripe/integration/callback"
],
"stripe_api_access_type": "oauth",
- "distribution_type": "public",
- "sandbox_install_compatible": true
+ "distribution_type": "public"
}
\ No newline at end of file