-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement payment related APIs
- Loading branch information
Showing
3 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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; |
This file contains 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
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; |
This file contains 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
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; |