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.

add STRIPE_ERROR_MAP

Kiran K789e6adaa0a3
diff --git a/apps/web/ui/partners/add-discount-code-modal.tsx b/apps/web/ui/partners/add-discount-code-modal.tsx
index 0a6bdf002b..78344225a8 100644
--- a/apps/web/ui/partners/add-discount-code-modal.tsx
+++ b/apps/web/ui/partners/add-discount-code-modal.tsx
@@ -21,6 +21,7 @@ import { useDebounce } from "use-debounce";
import { z } from "zod";
import { X } from "../shared/icons";
import { UpgradeRequiredToast } from "../shared/upgrade-required-toast";
+import { STRIPE_ERROR_MAP } from "./constants";
type FormData = z.infer<typeof createDiscountCodeSchema>;
@@ -91,24 +92,28 @@ const AddDiscountCodeModal = ({
toast.success("Discount code created and copied to clipboard!");
},
onError: (error) => {
- // Check if the error is related to Stripe permissions
- if (
- error.includes(
- "Having the 'read_write' scope would allow this request to continue",
- ) &&
- stripeConnectId
- ) {
- toast.custom(() => (
- <UpgradeRequiredToast
- title="Stripe app upgrade required"
- message="Your connected Stripe account doesn't have the permissions needed to create discount codes. Please upgrade your Stripe app permissions in the dashboard or reach out to our support team for help."
- ctaLabel="Review permissions"
- ctaUrl="https://marketplace.stripe.com/apps/dub-conversions"
- />
- ));
- } else {
- toast.error(error);
+ if (error) {
+ const code = Object.keys(STRIPE_ERROR_MAP).find((key) =>
+ error.startsWith(key),
+ );
+
+ if (code) {
+ const { title, ctaLabel, ctaUrl } = STRIPE_ERROR_MAP[code];
+ const message = error.replace(`${code}: `, "");
+
+ toast.custom(() => (
+ <UpgradeRequiredToast
+ title={title}
+ message={message}
+ ctaLabel={ctaLabel}
+ ctaUrl={ctaUrl}
+ />
+ ));
+ return;
+ }
}
+
+ toast.error(error);
},
});
};
diff --git a/apps/web/ui/partners/constants.ts b/apps/web/ui/partners/constants.ts
index 5dbfc33644..495f4d916c 100644
--- a/apps/web/ui/partners/constants.ts
+++ b/apps/web/ui/partners/constants.ts
@@ -23,3 +23,19 @@ export const REWARD_EVENTS = {
eventName: "sale",
},
} as const;
+
+export const STRIPE_ERROR_MAP: Record<
+ string,
+ { title: string; ctaLabel: string; ctaUrl: string }
+> = {
+ STRIPE_CONNECTION_REQUIRED: {
+ title: "Stripe connection required",
+ ctaLabel: "Install Stripe app",
+ ctaUrl: "https://marketplace.stripe.com/apps/dub-conversions",
+ },
+ STRIPE_APP_UPGRADE_REQUIRED: {
+ title: "Stripe app upgrade required",
+ ctaLabel: "Review permissions",
+ ctaUrl: "https://marketplace.stripe.com/apps/dub-conversions",
+ },
+};
diff --git a/apps/web/ui/partners/discounts/add-edit-discount-sheet.tsx b/apps/web/ui/partners/discounts/add-edit-discount-sheet.tsx
index 53b5e6b75c..26196e79f9 100644
--- a/apps/web/ui/partners/discounts/add-edit-discount-sheet.tsx
+++ b/apps/web/ui/partners/discounts/add-edit-discount-sheet.tsx
@@ -40,6 +40,7 @@ import { FormProvider, useForm, useFormContext } from "react-hook-form";
import { toast } from "sonner";
import { mutate } from "swr";
import { z } from "zod";
+import { STRIPE_ERROR_MAP } from "../constants";
import { RewardDiscountPartnersCard } from "../groups/reward-discount-partners-card";
interface DiscountSheetProps {
@@ -120,29 +121,13 @@ function DiscountSheetContent({
await mutateGroup();
},
onError({ error }) {
- const stripeErrorMap: Record<
- string,
- { title: string; ctaLabel: string; ctaUrl: string }
- > = {
- STRIPE_CONNECTION_REQUIRED: {
- title: "Stripe connection required",
- ctaLabel: "Install Stripe app",
- ctaUrl: "https://marketplace.stripe.com/apps/dub-conversions",
- },
- STRIPE_APP_UPGRADE_REQUIRED: {
- title: "Stripe app upgrade required",
- ctaLabel: "Review permissions",
- ctaUrl: "https://marketplace.stripe.com/apps/dub-conversions",
- },
- };
-
if (error.serverError) {
- const code = Object.keys(stripeErrorMap).find((key) =>
+ const code = Object.keys(STRIPE_ERROR_MAP).find((key) =>
error.serverError!.startsWith(key),
);
if (code) {
- const { title, ctaLabel, ctaUrl } = stripeErrorMap[code];
+ const { title, ctaLabel, ctaUrl } = STRIPE_ERROR_MAP[code];
const message = error.serverError!.replace(`${code}: `, "");
toast.custom(() => (