@gr4vy/sdk 2.3.0 → 2.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +38 -0
- package/index.d.ts +1 -1
- package/index.d.ts.map +1 -1
- package/index.js +2 -1
- package/index.js.map +1 -1
- package/jsr.json +1 -1
- package/lib/auth.d.ts +20 -1
- package/lib/auth.d.ts.map +1 -1
- package/lib/auth.js +20 -1
- package/lib/auth.js.map +1 -1
- package/lib/config.d.ts +3 -3
- package/lib/config.js +3 -3
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/lib/auth.ts +36 -1
- package/src/lib/config.ts +3 -3
- package/tests/auth.test.js +46 -2
- package/tests/flows/transaction-lifecycle.test.ts +30 -0
package/README.md
CHANGED
|
@@ -170,6 +170,44 @@ run();
|
|
|
170
170
|
> **Note:** This will only create a token once. Use `withToken` to dynamically generate a token
|
|
171
171
|
> for every request.
|
|
172
172
|
|
|
173
|
+
### Attaching a checkout session automatically
|
|
174
|
+
|
|
175
|
+
For Embed, it is recommended to attach a checkout session to every transaction. The
|
|
176
|
+
`getEmbedTokenWithCheckoutSession` helper creates a checkout session using your SDK client and
|
|
177
|
+
returns an Embed token with the resulting `checkout_session_id` already pinned, in a single call.
|
|
178
|
+
|
|
179
|
+
```js
|
|
180
|
+
import fs from "node:fs";
|
|
181
|
+
import { Gr4vy, getEmbedTokenWithCheckoutSession, withToken } from "@gr4vy/sdk";
|
|
182
|
+
|
|
183
|
+
async function run() {
|
|
184
|
+
const privateKey = fs.readFileSync("private_key.pem", "utf8")
|
|
185
|
+
|
|
186
|
+
const gr4vy = new Gr4vy({
|
|
187
|
+
server: "sandbox",
|
|
188
|
+
id: "example",
|
|
189
|
+
bearerAuth: withToken({ privateKey }),
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
const token = await getEmbedTokenWithCheckoutSession({
|
|
193
|
+
client: gr4vy,
|
|
194
|
+
privateKey,
|
|
195
|
+
embedParams: {
|
|
196
|
+
amount: 1299,
|
|
197
|
+
currency: 'USD',
|
|
198
|
+
buyerExternalIdentifier: 'user-1234',
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
console.log(token);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
run();
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
You can optionally pass a `checkoutSession` body to seed the session (for example with cart items
|
|
209
|
+
or metadata), and a `merchantAccountId` to override the client's configured merchant account.
|
|
210
|
+
|
|
173
211
|
## Merchant account ID selection
|
|
174
212
|
|
|
175
213
|
Depending on the key used, you might need to explicitly define a merchant account ID to use. In our API,
|
package/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export * from "./lib/config";
|
|
2
2
|
export * from "./sdk";
|
|
3
|
-
export { getEmbedToken, getToken, JWTScope, updateToken, withToken } from "./lib/auth";
|
|
3
|
+
export { getEmbedToken, getEmbedTokenWithCheckoutSession, getToken, JWTScope, updateToken, withToken } from "./lib/auth";
|
|
4
4
|
export type { EmbedParams } from "./lib/auth";
|
|
5
5
|
export { verifyWebhook } from "./lib/webhooks";
|
|
6
6
|
//# sourceMappingURL=index.d.ts.map
|
package/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAIA,cAAc,cAAc,CAAC;AAC7B,cAAc,OAAO,CAAC;AAEtB,OAAO,EACH,aAAa,EACb,QAAQ,EACR,QAAQ,EACR,WAAW,EACX,SAAS,EACZ,MAAM,YAAY,CAAC;AACpB,YAAY,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAC9C,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":"AAIA,cAAc,cAAc,CAAC;AAC7B,cAAc,OAAO,CAAC;AAEtB,OAAO,EACH,aAAa,EACb,gCAAgC,EAChC,QAAQ,EACR,QAAQ,EACR,WAAW,EACX,SAAS,EACZ,MAAM,YAAY,CAAC;AACpB,YAAY,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAC9C,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC"}
|
package/index.js
CHANGED
|
@@ -17,11 +17,12 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
17
17
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
18
18
|
};
|
|
19
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
-
exports.verifyWebhook = exports.withToken = exports.updateToken = exports.JWTScope = exports.getToken = exports.getEmbedToken = void 0;
|
|
20
|
+
exports.verifyWebhook = exports.withToken = exports.updateToken = exports.JWTScope = exports.getToken = exports.getEmbedTokenWithCheckoutSession = exports.getEmbedToken = void 0;
|
|
21
21
|
__exportStar(require("./lib/config"), exports);
|
|
22
22
|
__exportStar(require("./sdk"), exports);
|
|
23
23
|
var auth_1 = require("./lib/auth");
|
|
24
24
|
Object.defineProperty(exports, "getEmbedToken", { enumerable: true, get: function () { return auth_1.getEmbedToken; } });
|
|
25
|
+
Object.defineProperty(exports, "getEmbedTokenWithCheckoutSession", { enumerable: true, get: function () { return auth_1.getEmbedTokenWithCheckoutSession; } });
|
|
25
26
|
Object.defineProperty(exports, "getToken", { enumerable: true, get: function () { return auth_1.getToken; } });
|
|
26
27
|
Object.defineProperty(exports, "JWTScope", { enumerable: true, get: function () { return auth_1.JWTScope; } });
|
|
27
28
|
Object.defineProperty(exports, "updateToken", { enumerable: true, get: function () { return auth_1.updateToken; } });
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;;;;;;;;;;;;;;;AAEH,+CAA6B;AAC7B,wCAAsB;AAEtB,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["src/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;;;;;;;;;;;;;;;AAEH,+CAA6B;AAC7B,wCAAsB;AAEtB,mCAOoB;AANhB,qGAAA,aAAa,OAAA;AACb,wHAAA,gCAAgC,OAAA;AAChC,gGAAA,QAAQ,OAAA;AACR,gGAAA,QAAQ,OAAA;AACR,mGAAA,WAAW,OAAA;AACX,iGAAA,SAAS,OAAA;AAGb,2CAA+C;AAAtC,yGAAA,aAAa,OAAA"}
|
package/jsr.json
CHANGED
package/lib/auth.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { StringValue } from "ms";
|
|
2
|
-
import { CartItem } from "../models/components";
|
|
2
|
+
import { CartItem, CheckoutSessionCreate } from "../models/components";
|
|
3
|
+
import type { Gr4vy } from "../sdk/sdk";
|
|
3
4
|
/**
|
|
4
5
|
* Helper method for generating a bearer token for use with the SDK
|
|
5
6
|
*/
|
|
@@ -34,6 +35,24 @@ export declare const getEmbedToken: (options: {
|
|
|
34
35
|
embedParams?: EmbedParams;
|
|
35
36
|
checkoutSessionId?: string;
|
|
36
37
|
}) => Promise<string>;
|
|
38
|
+
/**
|
|
39
|
+
* Helper method that creates a checkout session and returns an Embed token with
|
|
40
|
+
* the new checkout session ID baked in.
|
|
41
|
+
*
|
|
42
|
+
* This is a convenience wrapper around `getEmbedToken` for the common Embed flow
|
|
43
|
+
* where every transaction should be tied to a checkout session. It uses the
|
|
44
|
+
* provided (already authenticated) SDK client to create the checkout session,
|
|
45
|
+
* then signs an Embed token that pins the resulting `checkout_session_id`.
|
|
46
|
+
*/
|
|
47
|
+
export declare const getEmbedTokenWithCheckoutSession: (options: {
|
|
48
|
+
client: Gr4vy;
|
|
49
|
+
privateKey: string;
|
|
50
|
+
embedParams?: EmbedParams;
|
|
51
|
+
/** Optional checkout session body to seed cart items, metadata, and so on. */
|
|
52
|
+
checkoutSession?: CheckoutSessionCreate;
|
|
53
|
+
/** Optional merchant account ID override. Defaults to the client's configured one. */
|
|
54
|
+
merchantAccountId?: string;
|
|
55
|
+
}) => Promise<string>;
|
|
37
56
|
/**
|
|
38
57
|
* Short hands for scopes. Strings can be used as well.
|
|
39
58
|
*/
|
package/lib/auth.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../src/lib/auth.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,IAAI,CAAC;AAGtC,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../src/lib/auth.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,IAAI,CAAC;AAGtC,OAAO,EAAE,QAAQ,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAC;AAIvE,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAExC;;GAEG;AACH,eAAO,MAAM,SAAS,GAAI,SAAS;IACjC,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,QAAQ,EAAE,GAAG,MAAM,EAAE,CAAC;IAC/B,SAAS,CAAC,EAAE,WAAW,CAAC;CACzB,KAAG,CAAC,MAAM,OAAO,CAAC,MAAM,CAAC,CAezB,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,QAAQ,GAAU,SAAS;IACtC,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,WAAW,CAAC;IACxB,MAAM,CAAC,EAAE,QAAQ,EAAE,GAAG,MAAM,EAAE,CAAC;IAC/B,WAAW,CAAC,EAAE,WAAW,CAAC;IAC1B,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B,KAAG,OAAO,CAAC,MAAM,CA+BjB,CAAC;AAEF,eAAO,MAAM,WAA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AM,CAAC;IACjB,uBAAuB,CAAC,EAAE,MAAM,CAAC;IACjC,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAClC,SAAS,CAAC,EAAE,KAAK,CAAC,QAAQ,CAAC,CAAC;IAC5B,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,iBAAiB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CACzC,CAAC"}
|
package/lib/auth.js
CHANGED
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.JWTScope = exports.getEmbedToken = exports.updateToken = exports.getToken = exports.withToken = void 0;
|
|
6
|
+
exports.JWTScope = exports.getEmbedTokenWithCheckoutSession = exports.getEmbedToken = exports.updateToken = exports.getToken = exports.withToken = void 0;
|
|
7
7
|
const node_crypto_1 = require("node:crypto");
|
|
8
8
|
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
9
9
|
const snakecase_keys_1 = __importDefault(require("snakecase-keys"));
|
|
@@ -74,6 +74,25 @@ const getEmbedToken = async (options) => {
|
|
|
74
74
|
});
|
|
75
75
|
};
|
|
76
76
|
exports.getEmbedToken = getEmbedToken;
|
|
77
|
+
/**
|
|
78
|
+
* Helper method that creates a checkout session and returns an Embed token with
|
|
79
|
+
* the new checkout session ID baked in.
|
|
80
|
+
*
|
|
81
|
+
* This is a convenience wrapper around `getEmbedToken` for the common Embed flow
|
|
82
|
+
* where every transaction should be tied to a checkout session. It uses the
|
|
83
|
+
* provided (already authenticated) SDK client to create the checkout session,
|
|
84
|
+
* then signs an Embed token that pins the resulting `checkout_session_id`.
|
|
85
|
+
*/
|
|
86
|
+
const getEmbedTokenWithCheckoutSession = async (options) => {
|
|
87
|
+
const { client, privateKey, embedParams, checkoutSession, merchantAccountId } = options;
|
|
88
|
+
const session = await client.checkoutSessions.create(checkoutSession, merchantAccountId);
|
|
89
|
+
return (0, exports.getEmbedToken)({
|
|
90
|
+
privateKey,
|
|
91
|
+
...(embedParams ? { embedParams } : {}),
|
|
92
|
+
checkoutSessionId: session.id,
|
|
93
|
+
});
|
|
94
|
+
};
|
|
95
|
+
exports.getEmbedTokenWithCheckoutSession = getEmbedTokenWithCheckoutSession;
|
|
77
96
|
/**
|
|
78
97
|
* Short hands for scopes. Strings can be used as well.
|
|
79
98
|
*/
|
package/lib/auth.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../src/lib/auth.ts"],"names":[],"mappings":";;;;;;AAAA,6CAAyC;AACzC,gEAA+C;AAE/C,oEAA2C;AAG3C,qCAAwC;AACxC,uCAAqC;
|
|
1
|
+
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../src/lib/auth.ts"],"names":[],"mappings":";;;;;;AAAA,6CAAyC;AACzC,gEAA+C;AAE/C,oEAA2C;AAG3C,qCAAwC;AACxC,uCAAqC;AAIrC;;GAEG;AACI,MAAM,SAAS,GAAG,CAAC,OAIzB,EAA2B,EAAE;IAC5B,MAAM,EACJ,UAAU,EACV,MAAM,GAAG,CAAC,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,QAAQ,CAAC,EAC9C,SAAS,GAAG,KAAK,GAClB,GAAG,OAAO,CAAC;IAEZ,IAAI,CAAC,UAAU,EAAE,CAAC;QAChB,MAAM,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAC;IAC7D,CAAC;IAED,OAAO,KAAK,IAAqB,EAAE;QACjC,MAAM,UAAU,GAAG,MAAM,IAAA,gBAAQ,EAAC,EAAE,UAAU,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,CAAC;QACrE,OAAO,UAAU,CAAC;IACpB,CAAC,CAAC;AACJ,CAAC,CAAC;AAnBW,QAAA,SAAS,a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AAA;IAC7B,2CAA+B,CAAA;IAC/B,mDAAuC,CAAA;IACvC,qDAAyC,CAAA;IACzC,kFAAsE,CAAA;IACtE,oFAAwE,CAAA;IACxE,kEAAsD,CAAA;IACtD,oEAAwD,CAAA;IACxD,0EAA8D,CAAA;IAC9D,4EAAgE,CAAA;IAChE,mEAAuD,CAAA;IACvD,qEAAyD,CAAA;AAC3D,CAAC,EApEW,QAAQ,wBAAR,QAAQ,QAoEnB"}
|
package/lib/config.d.ts
CHANGED
|
@@ -44,8 +44,8 @@ export declare function serverURLFromOptions(options: SDKOptions): URL | null;
|
|
|
44
44
|
export declare const SDK_METADATA: {
|
|
45
45
|
readonly language: "typescript";
|
|
46
46
|
readonly openapiDocVersion: "1.0.0";
|
|
47
|
-
readonly sdkVersion: "2.3.
|
|
48
|
-
readonly genVersion: "2.
|
|
49
|
-
readonly userAgent: "speakeasy-sdk/typescript 2.3.
|
|
47
|
+
readonly sdkVersion: "2.3.1";
|
|
48
|
+
readonly genVersion: "2.904.2";
|
|
49
|
+
readonly userAgent: "speakeasy-sdk/typescript 2.3.1 2.904.2 1.0.0 @gr4vy/sdk";
|
|
50
50
|
};
|
|
51
51
|
//# sourceMappingURL=config.d.ts.map
|
package/lib/config.js
CHANGED
|
@@ -37,8 +37,8 @@ function serverURLFromOptions(options) {
|
|
|
37
37
|
exports.SDK_METADATA = {
|
|
38
38
|
language: "typescript",
|
|
39
39
|
openapiDocVersion: "1.0.0",
|
|
40
|
-
sdkVersion: "2.3.
|
|
41
|
-
genVersion: "2.
|
|
42
|
-
userAgent: "speakeasy-sdk/typescript 2.3.
|
|
40
|
+
sdkVersion: "2.3.1",
|
|
41
|
+
genVersion: "2.904.2",
|
|
42
|
+
userAgent: "speakeasy-sdk/typescript 2.3.1 2.904.2 1.0.0 @gr4vy/sdk",
|
|
43
43
|
};
|
|
44
44
|
//# sourceMappingURL=config.js.map
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
package/src/lib/auth.ts
CHANGED
|
@@ -3,10 +3,12 @@ import jwt, { JwtPayload } from "jsonwebtoken";
|
|
|
3
3
|
import type { StringValue } from "ms";
|
|
4
4
|
import snakeCaseKeys from "snakecase-keys";
|
|
5
5
|
|
|
6
|
-
import { CartItem } from "../models/components";
|
|
6
|
+
import { CartItem, CheckoutSessionCreate } from "../models/components";
|
|
7
7
|
import { SDK_METADATA } from "./config";
|
|
8
8
|
import { getKeyId } from "./helpers";
|
|
9
9
|
|
|
10
|
+
import type { Gr4vy } from "../sdk/sdk";
|
|
11
|
+
|
|
10
12
|
/**
|
|
11
13
|
* Helper method for generating a bearer token for use with the SDK
|
|
12
14
|
*/
|
|
@@ -120,6 +122,39 @@ export const getEmbedToken = async (options: {
|
|
|
120
122
|
})
|
|
121
123
|
}
|
|
122
124
|
|
|
125
|
+
/**
|
|
126
|
+
* Helper method that creates a checkout session and returns an Embed token with
|
|
127
|
+
* the new checkout session ID baked in.
|
|
128
|
+
*
|
|
129
|
+
* This is a convenience wrapper around `getEmbedToken` for the common Embed flow
|
|
130
|
+
* where every transaction should be tied to a checkout session. It uses the
|
|
131
|
+
* provided (already authenticated) SDK client to create the checkout session,
|
|
132
|
+
* then signs an Embed token that pins the resulting `checkout_session_id`.
|
|
133
|
+
*/
|
|
134
|
+
export const getEmbedTokenWithCheckoutSession = async (options: {
|
|
135
|
+
client: Gr4vy;
|
|
136
|
+
privateKey: string;
|
|
137
|
+
embedParams?: EmbedParams;
|
|
138
|
+
/** Optional checkout session body to seed cart items, metadata, and so on. */
|
|
139
|
+
checkoutSession?: CheckoutSessionCreate;
|
|
140
|
+
/** Optional merchant account ID override. Defaults to the client's configured one. */
|
|
141
|
+
merchantAccountId?: string;
|
|
142
|
+
}): Promise<string> => {
|
|
143
|
+
const { client, privateKey, embedParams, checkoutSession, merchantAccountId } =
|
|
144
|
+
options;
|
|
145
|
+
|
|
146
|
+
const session = await client.checkoutSessions.create(
|
|
147
|
+
checkoutSession,
|
|
148
|
+
merchantAccountId,
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
return getEmbedToken({
|
|
152
|
+
privateKey,
|
|
153
|
+
...(embedParams ? { embedParams } : {}),
|
|
154
|
+
checkoutSessionId: session.id,
|
|
155
|
+
});
|
|
156
|
+
};
|
|
157
|
+
|
|
123
158
|
/**
|
|
124
159
|
* Short hands for scopes. Strings can be used as well.
|
|
125
160
|
*/
|
package/src/lib/config.ts
CHANGED
|
@@ -77,7 +77,7 @@ export function serverURLFromOptions(options: SDKOptions): URL | null {
|
|
|
77
77
|
export const SDK_METADATA = {
|
|
78
78
|
language: "typescript",
|
|
79
79
|
openapiDocVersion: "1.0.0",
|
|
80
|
-
sdkVersion: "2.3.
|
|
81
|
-
genVersion: "2.
|
|
82
|
-
userAgent: "speakeasy-sdk/typescript 2.3.
|
|
80
|
+
sdkVersion: "2.3.1",
|
|
81
|
+
genVersion: "2.904.2",
|
|
82
|
+
userAgent: "speakeasy-sdk/typescript 2.3.1 2.904.2 1.0.0 @gr4vy/sdk",
|
|
83
83
|
} as const;
|
package/tests/auth.test.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import jwt from "jsonwebtoken";
|
|
2
2
|
import snakecaseKeys from "snakecase-keys";
|
|
3
3
|
import timekeeper from "timekeeper";
|
|
4
|
-
import { describe, expect, test } from "vitest";
|
|
4
|
+
import { describe, expect, test, vi } from "vitest";
|
|
5
5
|
import { version } from "../package.json";
|
|
6
|
-
import { getEmbedToken, getToken, JWTScope, updateToken, SDK_METADATA } from "../src";
|
|
6
|
+
import { getEmbedToken, getEmbedTokenWithCheckoutSession, getToken, JWTScope, updateToken, SDK_METADATA } from "../src";
|
|
7
7
|
|
|
8
8
|
const privateKey = `-----BEGIN PRIVATE KEY-----
|
|
9
9
|
MIHuAgEAMBAGByqGSM49AgEGBSuBBAAjBIHWMIHTAgEBBEIBABM9jQu+HT87oIik
|
|
@@ -142,6 +142,50 @@ describe(".getEmbedToken", () => {
|
|
|
142
142
|
});
|
|
143
143
|
});
|
|
144
144
|
|
|
145
|
+
describe(".getEmbedTokenWithCheckoutSession", () => {
|
|
146
|
+
test("should create a checkout session and pin its ID in the Embed token", async () => {
|
|
147
|
+
const create = vi.fn().mockResolvedValue({ id: checkoutSessionId });
|
|
148
|
+
const client = { checkoutSessions: { create } };
|
|
149
|
+
|
|
150
|
+
const token = await getEmbedTokenWithCheckoutSession({
|
|
151
|
+
client,
|
|
152
|
+
privateKey,
|
|
153
|
+
embedParams,
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
const decoded = jwt.verify(token, privateKey, {
|
|
157
|
+
algorithms: ["ES512"],
|
|
158
|
+
complete: true,
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
expect(create).toHaveBeenCalledTimes(1);
|
|
162
|
+
expect(decoded.payload.scopes).toEqual(["embed"]);
|
|
163
|
+
expect(decoded.payload.checkout_session_id).toEqual(checkoutSessionId);
|
|
164
|
+
|
|
165
|
+
const connOptions =
|
|
166
|
+
embedParams.connectionOptions || embedParams["connection_options"];
|
|
167
|
+
const expected = snakecaseKeys(embedParams, { exclude: ["metadata"] });
|
|
168
|
+
expected["connection_options"] = connOptions;
|
|
169
|
+
expect(decoded.payload.embed).toEqual(expected);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test("should forward the checkout session body and merchant account ID", async () => {
|
|
173
|
+
const create = vi.fn().mockResolvedValue({ id: checkoutSessionId });
|
|
174
|
+
const client = { checkoutSessions: { create } };
|
|
175
|
+
|
|
176
|
+
const checkoutSession = { metadata: { order: "123" } };
|
|
177
|
+
|
|
178
|
+
await getEmbedTokenWithCheckoutSession({
|
|
179
|
+
client,
|
|
180
|
+
privateKey,
|
|
181
|
+
checkoutSession,
|
|
182
|
+
merchantAccountId: "default",
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
expect(create).toHaveBeenCalledWith(checkoutSession, "default");
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
|
|
145
189
|
describe(".updateToken", () => {
|
|
146
190
|
test("should resign a token with a new signature and expiration", async () => {
|
|
147
191
|
timekeeper.freeze(Date.now());
|
|
@@ -84,6 +84,36 @@ describe("Transaction lifecycle", () => {
|
|
|
84
84
|
expect(topLevel.id).toBe(firstRefundId);
|
|
85
85
|
});
|
|
86
86
|
|
|
87
|
+
test("authorize → capture → read captures back via list and get", async () => {
|
|
88
|
+
const transaction = await authorize(4500);
|
|
89
|
+
expect(transaction.status).toBe("authorization_succeeded");
|
|
90
|
+
|
|
91
|
+
unwrapTransaction(
|
|
92
|
+
await gr4vy.transactions.capture({
|
|
93
|
+
transactionId: transaction.id,
|
|
94
|
+
transactionCaptureCreate: { amount: 4500 },
|
|
95
|
+
})
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
// Wait until the capture is recorded against the transaction.
|
|
99
|
+
await pollUntil(
|
|
100
|
+
() => gr4vy.transactions.get(transaction.id),
|
|
101
|
+
(t) => t.status === "capture_succeeded",
|
|
102
|
+
{ description: "capture to succeed" }
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
// List captures, then read one back by id.
|
|
106
|
+
const captures = await gr4vy.transactions.captures.list(transaction.id);
|
|
107
|
+
expect(captures.items.length).toBeGreaterThanOrEqual(1);
|
|
108
|
+
|
|
109
|
+
const captureId = captures.items[0]!.id;
|
|
110
|
+
const fetched = await gr4vy.transactions.captures.get(
|
|
111
|
+
transaction.id,
|
|
112
|
+
captureId
|
|
113
|
+
);
|
|
114
|
+
expect(fetched.id).toBe(captureId);
|
|
115
|
+
});
|
|
116
|
+
|
|
87
117
|
test("authorize → void releases the authorization", async () => {
|
|
88
118
|
const transaction = await authorize(3300);
|
|
89
119
|
expect(transaction.status).toBe("authorization_succeeded");
|