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.

Fix Stripe app

Kiran K39870ccb5b2a
diff --git a/packages/stripe-app/src/utils/constants.ts b/packages/stripe-app/src/utils/constants.ts
index 993be69abe..9a57d3265b 100644
--- a/packages/stripe-app/src/utils/constants.ts
+++ b/packages/stripe-app/src/utils/constants.ts
@@ -8,3 +8,4 @@ export const DUB_API_HOST = "https://api.dub.co";
// Stripe
export const STRIPE_MODE: StripeMode = "live";
+export const STRIPE_REDIRECT_URL = `https://dashboard.stripe.com/${STRIPE_MODE === "live" ? "" : "test/"}apps-oauth/dub.co`;
diff --git a/packages/stripe-app/src/utils/oauth.ts b/packages/stripe-app/src/utils/oauth.ts
index 755afda3b0..422340e449 100644
--- a/packages/stripe-app/src/utils/oauth.ts
+++ b/packages/stripe-app/src/utils/oauth.ts
@@ -3,18 +3,11 @@ import {
DUB_API_HOST,
DUB_CLIENT_ID,
DUB_HOST,
- STRIPE_MODE,
+ STRIPE_REDIRECT_URL,
} from "./constants";
-import { getSecret } from "./secrets";
+import { getSecret, setSecret } from "./secrets";
import { Token, Workspace } from "./types";
-// Get the redirect URL for the OAuth flow based on the mode
-function getRedirectURL() {
- return `https://dashboard.stripe.com/${
- STRIPE_MODE === "test" ? "test/" : ""
- }apps-oauth/dub.co`;
-}
-
// Returns the authorization URL
export function getOAuthUrl({
state,
@@ -23,7 +16,7 @@ export function getOAuthUrl({
state: string;
challenge: string;
}) {
- return `${DUB_HOST}/oauth/authorize?client_id=${DUB_CLIENT_ID}&redirect_uri=${getRedirectURL()}&response_type=code&scope=workspaces.write&state=${state}&code_challenge=${challenge}&code_challenge_method=S256`;
+ return `${DUB_HOST}/oauth/authorize?client_id=${DUB_CLIENT_ID}&redirect_uri=${STRIPE_REDIRECT_URL}&response_type=code&scope=workspaces.write&state=${state}&code_challenge=${challenge}&code_challenge_method=S256`;
}
// Exchanges the authorization code for an access token
@@ -44,7 +37,7 @@ export async function getToken({
},
body: new URLSearchParams({
client_id: DUB_CLIENT_ID,
- redirect_uri: getRedirectURL(),
+ redirect_uri: STRIPE_REDIRECT_URL,
grant_type: "authorization_code",
code_verifier: verifier,
code,
@@ -99,10 +92,18 @@ export async function getValidToken({ stripe }: { stripe: Stripe }) {
await getUserInfo({ token });
} catch (e) {
const refreshedToken = await refreshToken({ token });
+
if (!refreshedToken) {
- throw new Error("Failed to refresh access token.");
+ console.error("Failed to refresh access token.");
+ return null;
}
+ await setSecret({
+ stripe,
+ name: "dub_token",
+ payload: JSON.stringify(refreshedToken),
+ });
+
return refreshedToken;
}
diff --git a/packages/stripe-app/src/views/AppSettings.tsx b/packages/stripe-app/src/views/AppSettings.tsx
index 6c019b1302..b05771b092 100644
--- a/packages/stripe-app/src/views/AppSettings.tsx
+++ b/packages/stripe-app/src/views/AppSettings.tsx
@@ -20,7 +20,6 @@ import {
} from "../utils/oauth";
import { deleteSecret, setSecret } from "../utils/secrets";
import { stripe } from "../utils/stripe";
-import { Workspace } from "../utils/types";
const AppSettings = ({ userContext, oauthContext }: ExtensionContextValue) => {
const credentialsUsed = useRef(false);
@@ -34,33 +33,31 @@ const AppSettings = ({ userContext, oauthContext }: ExtensionContextValue) => {
const verifier = oauthContext?.verifier;
// Disconnect workspace
- const disconnectWorkspace = async ({
- workspace,
- }: {
- workspace: Workspace;
- }) => {
+ const disconnectWorkspace = async () => {
setDisconnecting(true);
const token = await getValidToken({ stripe });
- await updateWorkspace({
- token,
- accountId: null,
- });
-
await Promise.all([
deleteSecret({
stripe,
name: "dub_workspace",
}),
- await deleteSecret({
+ deleteSecret({
stripe,
name: "dub_token",
}),
]);
- mutate();
+ if (token) {
+ await updateWorkspace({
+ token,
+ accountId: null,
+ });
+ }
+
+ await mutate();
setDisconnecting(false);
};
@@ -104,7 +101,7 @@ const AppSettings = ({ userContext, oauthContext }: ExtensionContextValue) => {
payload: JSON.stringify(workspace),
});
- mutate();
+ await mutate();
credentialsUsed.current = true;
setConnecting(false);
};
@@ -148,12 +145,8 @@ const AppSettings = ({ userContext, oauthContext }: ExtensionContextValue) => {
disabled={disconnecting}
onPress={async () => {
setDisconnecting(true);
-
- await disconnectWorkspace({
- workspace,
- });
-
- mutate();
+ await disconnectWorkspace();
+ await mutate();
setDisconnecting(false);
}}
>