Skip to content

Commit

Permalink
feat: implement payment related APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
vitoUwu committed Feb 21, 2025
1 parent fdc26a5 commit d9c876b
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 0 deletions.
40 changes: 40 additions & 0 deletions vtex/actions/payment/deletePaymentToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { AppContext } from "../../mod.ts";
import { parseCookie } from "../../utils/vtexId.ts";

export interface DeleteCard {
deletePaymentToken: boolean;
}

const mutation = `mutation DeleteCreditCardToken($tokenId: ID!) {
deletePaymentToken(tokenId: $tokenId) @context(provider: "vtex.my-cards-graphql@2.x")
}`;

interface Props {
id: string;
}

async function loader(
{ id }: Props,
req: Request,
ctx: AppContext,
): Promise<DeleteCard | null> {
const { io } = ctx;
const { cookie, payload } = parseCookie(req.headers, ctx.account);

if (!payload?.sub || !payload?.userId) {
return null;
}

try {
return await io.query<DeleteCard, { tokenId: string }>({
query: mutation,
variables: { tokenId: id },
}, { headers: { cookie } });
} catch (e) {
console.error(e);
return null;
}
}

export const defaultVisibility = "private";
export default loader;
58 changes: 58 additions & 0 deletions vtex/loaders/payment/paymentSystems.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { AppContext } from "../../mod.ts";
import { parseCookie } from "../../utils/vtexId.ts";

const query = `query getPaymentSystems {
paymentSystems {
name
groupName
requiresDocument
displayDocument
validator {
regex
mask
cardCodeMask
cardCodeRegex
}
}
}`;

export interface PaymentSystem {
name: string;
groupName: string;
requiresDocument: boolean;
displayDocument: boolean;
validator: {
regex: string | null;
mask: string | null;
cardCodeMask: string | null;
cardCodeRegex: string | null;
};
}

async function loader(
_props: unknown,
req: Request,
ctx: AppContext,
) {
const { io } = ctx;
const { cookie, payload } = parseCookie(req.headers, ctx.account);

if (!payload?.sub || !payload?.userId) {
return null;
}

try {
const data = await io.query<{ paymentSystems: PaymentSystem[] }, null>(
{ query },
{ headers: { cookie } },
);

return data.paymentSystems;
} catch (e) {
console.error(e);
return null;
}
}

export const defaultVisibility = "private";
export default loader;
54 changes: 54 additions & 0 deletions vtex/loaders/payment/userPayments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { AppContext } from "../../mod.ts";
import { parseCookie } from "../../utils/vtexId.ts";

const query = `query getUserPayments {
profile {
payments {
accountStatus
cardNumber
expirationDate
id
isExpired
paymentSystem
paymentSystemName
}
}
}`;

export interface Payment {
accountStatus: string | null;
cardNumber: string;
expirationDate: string;
id: string;
isExpired: boolean;
paymentSystem: string;
paymentSystemName: string;
}

async function loader(
_props: unknown,
req: Request,
ctx: AppContext,
): Promise<Payment[] | null> {
const { io } = ctx;
const { cookie, payload } = parseCookie(req.headers, ctx.account);

if (!payload?.sub || !payload?.userId) {
return null;
}

try {
const data = await io.query<{ profile: { payments: Payment[] } }, null>(
{ query },
{ headers: { cookie } },
);

return data.profile.payments;
} catch (e) {
console.error(e);
return null;
}
}

export const defaultVisibility = "private";
export default loader;

0 comments on commit d9c876b

Please sign in to comment.