Skip to content

Commit

Permalink
configured stripe client and added connect and disconnect resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
jalajcodes committed Sep 2, 2020
1 parent 2f62ebf commit 1760a7d
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 4 deletions.
85 changes: 82 additions & 3 deletions src/graphql/resolvers/Viewer/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { IResolvers } from 'apollo-server-express';
import crypto from 'crypto';
import { Viewer, User, Database } from '../../../libs/types';
import { Google } from '../../../libs/api';
import { LogInArgs } from './types';
import { Google, Stripe } from '../../../libs/api';
import { authorize } from '../../../libs/utils';

import { LogInArgs, StripeConnectArgs } from './types';
import { Response, Request } from 'express';

const cookieOptions = {
Expand All @@ -22,7 +24,7 @@ const loginViaGoogle = async (code: string, token: string, db: Database, res: Re
throw new Error('Google Login Error');
}

// Names/Photos/Emails List
// Names/Photos/Emails List from google
const userNamesList = user.names && user.names.length ? user.names : null;
const userPhotosList = user.photos && user.photos.length ? user.photos : null;
const userEmailsList = user.emailAddresses && user.emailAddresses.length ? user.emailAddresses : null;
Expand Down Expand Up @@ -188,6 +190,83 @@ export const ViewerResolver: IResolvers = {
throw new Error(`Unable to login as guest: ${error}`);
}
},

stripeConnect: async (
_root: undefined,
{ input }: StripeConnectArgs,
{ db, req }: { db: Database; req: Request }
) => {
try {
const { code } = input;
// get the logged in user info
let viewer = await authorize(req, db);

if (!viewer) {
throw new Error(`Viewer can't be found`);
}

// connect to stripe
const stripeInfo = await Stripe.connect(code);
if (!stripeInfo) {
throw new Error(`Stripe grant error`);
}
// get walletId from the response
const walletId = stripeInfo.stripe_user_id;

// update users walletId
const updateUser = await db.users.findOneAndUpdate(
{ _id: viewer._id },
{ $set: { walletId } },
{ returnOriginal: false }
);

if (!updateUser.value) {
throw new Error(`Unable to update the viewer`);
}
// set the viewer variable to updated user value
viewer = updateUser.value;

return {
id: viewer._id,
token: viewer.token,
avatar: viewer.avatar,
walletId: viewer.walletId,
didRequest: true,
};
} catch (error) {
throw new Error(`Unable to connect to stripe: ${error}`);
}
},
stripeDisconnect: async (_root: undefined, _args: undefined, { db, req }: { db: Database; req: Request }) => {
try {
let viewer = await authorize(req, db);
if (!viewer) {
throw new Error('viewer cannot be found');
}

const updateRes = await db.users.findOneAndUpdate(
{ _id: viewer._id },
{ $set: { walletId: undefined } },
{ returnOriginal: false }
);

if (!updateRes.value) {
throw new Error('viewer could not be updated');
}

viewer = updateRes.value;

return {
_id: viewer._id,
token: viewer.token,
avatar: viewer.avatar,
walletId: viewer.walletId,
didRequest: true,
};
} catch (error) {
throw new Error(`Failed to disconnect with Stripe: ${error}`);
}
},
},
Viewer: {
id: (viewer: Viewer): string | undefined => {
Expand Down
3 changes: 3 additions & 0 deletions src/graphql/resolvers/Viewer/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export interface LogInArgs {
input: { code: string } | null;
}
export interface StripeConnectArgs {
input: { code: string };
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const mount = async (app: Application) => {

// Instantiate apollo server instance
const server = new ApolloServer({ typeDefs, resolvers, context: ({ req, res }) => ({ db, req, res }) });
// Use the middleware provided by apollol
// Use the middleware provided by apollo
server.applyMiddleware({ app, path: '/api' });

app.listen(process.env.PORT, () =>
Expand Down
14 changes: 14 additions & 0 deletions src/libs/api/Stripe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import stripe from 'stripe';

const client = new stripe(`${process.env.S_SECRET_KEY}`, { apiVersion: '2020-08-27' });

export const Stripe = {
connect: async (code: string) => {
const response = await client.oauth.token({
code,
grant_type: 'authorization_code',
});

return response;
},
};

0 comments on commit 1760a7d

Please sign in to comment.