Skip to content

Commit 0bd2860

Browse files
authored
chore(sdk): Update to new SDK and add checkout session support (#25)
1 parent 1475f4c commit 0bd2860

File tree

9 files changed

+3398
-4202
lines changed

9 files changed

+3398
-4202
lines changed

package.json

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,33 @@
1717
"/oclif.manifest.json"
1818
],
1919
"dependencies": {
20-
"@gr4vy/node": "^0.48.0",
21-
"@oclif/core": "^2",
22-
"@oclif/plugin-autocomplete": "^2.3.0",
23-
"@oclif/plugin-help": "^5"
20+
"@gr4vy/sdk": "^1.0.3",
21+
"@oclif/core": "^4.4.0",
22+
"@oclif/plugin-autocomplete": "^3.2.31",
23+
"@oclif/plugin-help": "^6.2.29",
24+
"zod": "^3.25.67"
2425
},
2526
"devDependencies": {
26-
"@oclif/test": "^2.3.22",
27-
"@types/chai": "^4",
28-
"@types/mocha": "^9.0.0",
29-
"@types/node": "^18.15.10",
30-
"@types/request": "^2.48.5",
31-
"@typescript-eslint/eslint-plugin": "^5.56.0",
32-
"@typescript-eslint/parser": "^5.56.0",
33-
"chai": "^4",
34-
"eslint": "^8.36.0",
35-
"eslint-config-prettier": "^8.3.0",
36-
"eslint-plugin-import": "^2.23.4",
37-
"eslint-plugin-jest": "^27.2.1",
38-
"eslint-plugin-prettier": "^4.2.1",
39-
"mocha": "^9",
40-
"oclif": "^3",
41-
"prettier": "^2.8.8",
42-
"shx": "^0.3.3",
43-
"ts-node": "^10.9.1",
44-
"tslib": "^2.5.3",
45-
"typescript": "^4.9.5"
27+
"@oclif/test": "^4.1.13",
28+
"@types/chai": "^5.2.2",
29+
"@types/mocha": "^10.0.10",
30+
"@types/node": "^24.0.4",
31+
"@types/request": "^2.48.12",
32+
"@typescript-eslint/eslint-plugin": "^8.35.0",
33+
"@typescript-eslint/parser": "^8.35.0",
34+
"chai": "^5.2.0",
35+
"eslint": "^9.29.0",
36+
"eslint-config-prettier": "^10.1.5",
37+
"eslint-plugin-import": "^2.32.0",
38+
"eslint-plugin-jest": "^29.0.1",
39+
"eslint-plugin-prettier": "^5.5.1",
40+
"mocha": "^11.7.1",
41+
"oclif": "^4.20.1",
42+
"prettier": "^3.6.2",
43+
"shx": "^0.4.0",
44+
"ts-node": "^10.9.2",
45+
"tslib": "^2.8.1",
46+
"typescript": "^5.8.3"
4647
},
4748
"oclif": {
4849
"bin": "gr4vy",

src/commands/checkout-session.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { Gr4vy, withToken } from "@gr4vy/sdk";
2+
import { Flags } from "@oclif/core";
3+
import { BaseCommand } from "../base";
4+
import { decodeJWT } from "../helpers/decode";
5+
6+
export default class CheckoutSession extends BaseCommand {
7+
static summary = "Generate a checkout session ID.";
8+
9+
static description = `This ID can be used with Secure Fields and our native mobile SDKs.
10+
`;
11+
static usage = "checkout-session --merchant-account-id=default";
12+
13+
static strict = false;
14+
15+
static flags = {
16+
mid: Flags.string({
17+
char: "m",
18+
summary: "The merchant account ID to generate a checkout session for.",
19+
description: "This default to no value if not provided.",
20+
}),
21+
debug: Flags.boolean({
22+
summary: "Returns the raw header and claim for the token",
23+
description:
24+
"Returns the decoded header and claim from the JWT token without the signature",
25+
}),
26+
};
27+
28+
public async run(): Promise<void> {
29+
const { flags } = await this.parse(CheckoutSession);
30+
31+
const gr4vy = new Gr4vy({
32+
merchantAccountId: flags.mid as string ?? undefined,
33+
server: this.clientConfig.environment == "production" ? "production" : "sandbox",
34+
id: this.clientConfig.gr4vyId,
35+
bearerAuth: withToken({
36+
privateKey: this.clientConfig.privateKey,
37+
}),
38+
});
39+
40+
const checkoutSession = await gr4vy.checkoutSessions.create();
41+
42+
if (flags.debug) {
43+
this.log(JSON.stringify(checkoutSession, null, 2));
44+
} else {
45+
this.log(checkoutSession.id);
46+
}
47+
}
48+
}

src/commands/embed.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Client } from "@gr4vy/node";
1+
import { getEmbedToken } from "@gr4vy/sdk";
22
import { Args, Flags } from "@oclif/core";
33
import { BaseCommand } from "../base";
44
import { decodeJWT } from "../helpers/decode";
@@ -41,10 +41,12 @@ pinned in the token.
4141

4242
public async run(): Promise<void> {
4343
const { flags, args } = await this.parse(Token);
44-
const data = parseEmbedParams(args.amount, args.currency, this.argv);
44+
const embedParams = parseEmbedParams(args.amount, args.currency, this.argv);
4545

46-
const client = new Client(this.clientConfig as any);
47-
const token = await client.getEmbedToken(data);
46+
const token = await getEmbedToken({
47+
privateKey: this.clientConfig.privateKey,
48+
embedParams
49+
});
4850

4951
if (flags.debug) {
5052
const data = decodeJWT(token);

src/commands/init.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import path from "path";
44
import os from 'os';
55

66
export default class Token extends Command {
7-
static summary = "Generate sample .gr4vyrc.json file";
7+
static summary = "Store configuration into a .gr4vyrc.json file";
88

99
static description = `Generates a config file that can be used to generate the token.
1010
`;

src/commands/token.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Client, JWTScope } from "@gr4vy/node";
1+
import { getToken, JWTScope } from "@gr4vy/sdk";
22
import { Flags } from "@oclif/core";
33
import { BaseCommand } from "../base";
44
import { decodeJWT } from "../helpers/decode";
@@ -73,7 +73,7 @@ restricted to any specific frontend scopes only.
7373
"merchant-accounts.read",
7474
"merchant-accounts.write",
7575
],
76-
parse: async (input) => input.replace("all.", "*."),
76+
parse: async (input: string) => input.replace("all.", "*."),
7777
default: ["*.read", "*.write"],
7878
required: false,
7979
}),
@@ -88,11 +88,12 @@ restricted to any specific frontend scopes only.
8888
public async run(): Promise<void> {
8989
const { flags } = await this.parse(Token);
9090

91-
const client = new Client(this.clientConfig as any);
92-
const token = await client.getBearerToken(
93-
flags.scope as JWTScope[],
94-
flags.expiresIn
95-
);
91+
92+
const token = await getToken({
93+
privateKey: this.clientConfig.privateKey,
94+
expiresIn: flags.expiresIn as string,
95+
scopes: flags.scope as JWTScope[]
96+
})
9697

9798
if (flags.debug) {
9899
const data = decodeJWT(token);

src/helpers/parse-embed-data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {EmbedParams} from '@gr4vy/node/dist/sdk/authentication'
1+
import {EmbedParams} from '@gr4vy/sdk'
22

33
export const parseEmbedParams = (amount: number, currency: string, argv: string[]): EmbedParams => {
44
const params: Record<string, any> = {

test/commands/hello/index.test.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

test/commands/hello/world.test.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)