Created
September 15, 2022 18:45
-
-
Save RichardSPrins/8be40d50718d0d45f369055bbaccc848 to your computer and use it in GitHub Desktop.
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
import { TRPCError } from "@trpc/server"; | |
import { z } from "zod"; | |
import { createProtectedRouter } from "./context"; | |
export const stripeRouter = createProtectedRouter() | |
.middleware(async ({ ctx, next }) => { | |
// Any queries or mutations after this middleware will | |
// raise an error unless there is a current session | |
if (!ctx.session) { | |
throw new TRPCError({ code: "UNAUTHORIZED" }); | |
} | |
return next(); | |
}) | |
.query("getBalance", { | |
resolve({ ctx }) { | |
// TODO: replace with Stripe Connected Account balance query result | |
// Set your secret key. Remember to switch to your live secret key in production. | |
// See your keys here: https://dashboard.stripe.com/apikeys | |
// const balance = await stripe.balance.retrieve({ | |
// stripeAccount: '{{CONNECTED_STRIPE_ACCOUNT_ID}}' | |
// }); | |
return 100; | |
}, | |
}) | |
.mutation("connectStripe", { | |
input: z.object({ scope: z.string(), code: z.string() }), | |
async resolve({ ctx, input }) { | |
try { | |
const { scope, code } = input; | |
const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY); | |
const result = await stripe.oauth | |
.token({ | |
grant_type: "authorization_code", | |
code: code, | |
}) | |
.catch((err: any) => { | |
throw new Error("Stripe oauth fail", err.message); | |
}); | |
const account = await stripe.accounts | |
?.retrieve(result?.stripe_user_id) | |
?.catch((err: any) => { | |
throw new Error("Error fetching stripe account", err.message); | |
}); | |
// Here we get the important details of the account. | |
const accountAnalysis = { | |
hasConnectedAccount: !!account?.id, // Check if account ID received is actually connected or exists. | |
accountId: account?.id, | |
hasCompletedProcess: account?.details_submitted, | |
isValid: account?.charges_enabled && account?.payouts_enabled, | |
displayName: | |
account?.settings?.dashboard?.display_name || | |
account?.display_name || | |
null, | |
country: account?.country, | |
currency: account?.default_currency, | |
}; | |
// boolean - Once the account is connected, should we let it unlink? | |
const shouldAllowUnlink = | |
accountAnalysis?.hasConnectedAccount && | |
(!accountAnalysis?.isValid || | |
!accountAnalysis?.hasCompletedProcess || | |
!accountAnalysis?.displayName); | |
return { oauth: result, account, accountAnalysis, shouldAllowUnlink }; | |
} catch (error) { | |
console.log(error); | |
} | |
}, | |
}) | |
.mutation("handlePayout", { | |
input: z.object({}), | |
async resolve({ ctx, input }) { | |
try { | |
} catch (error) {} | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment