From d9c876bbded4baed3a761857dcd3037bf86c12b7 Mon Sep 17 00:00:00 2001 From: vitoUwu Date: Fri, 21 Feb 2025 01:59:29 -0400 Subject: [PATCH] feat: implement payment related APIs --- vtex/actions/payment/deletePaymentToken.ts | 40 +++++++++++++++ vtex/loaders/payment/paymentSystems.ts | 58 ++++++++++++++++++++++ vtex/loaders/payment/userPayments.ts | 54 ++++++++++++++++++++ 3 files changed, 152 insertions(+) create mode 100644 vtex/actions/payment/deletePaymentToken.ts create mode 100644 vtex/loaders/payment/paymentSystems.ts create mode 100644 vtex/loaders/payment/userPayments.ts diff --git a/vtex/actions/payment/deletePaymentToken.ts b/vtex/actions/payment/deletePaymentToken.ts new file mode 100644 index 000000000..28082b1a8 --- /dev/null +++ b/vtex/actions/payment/deletePaymentToken.ts @@ -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 { + const { io } = ctx; + const { cookie, payload } = parseCookie(req.headers, ctx.account); + + if (!payload?.sub || !payload?.userId) { + return null; + } + + try { + return await io.query({ + query: mutation, + variables: { tokenId: id }, + }, { headers: { cookie } }); + } catch (e) { + console.error(e); + return null; + } +} + +export const defaultVisibility = "private"; +export default loader; diff --git a/vtex/loaders/payment/paymentSystems.ts b/vtex/loaders/payment/paymentSystems.ts new file mode 100644 index 000000000..cb48a1d0d --- /dev/null +++ b/vtex/loaders/payment/paymentSystems.ts @@ -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; diff --git a/vtex/loaders/payment/userPayments.ts b/vtex/loaders/payment/userPayments.ts new file mode 100644 index 000000000..cbdc009cc --- /dev/null +++ b/vtex/loaders/payment/userPayments.ts @@ -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 { + 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;