@mopay/node-sdk 0.1.0
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/LICENSE +21 -0
- package/README.md +246 -0
- package/dist/base.d.ts +13 -0
- package/dist/base.d.ts.map +1 -0
- package/dist/base.js +122 -0
- package/dist/checkout.d.ts +6 -0
- package/dist/checkout.d.ts.map +1 -0
- package/dist/checkout.js +402 -0
- package/dist/errors.d.ts +15 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +15 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +58 -0
- package/dist/payments/index.d.ts +7 -0
- package/dist/payments/index.d.ts.map +1 -0
- package/dist/payments/index.js +14 -0
- package/dist/redirects/index.d.ts +7 -0
- package/dist/redirects/index.d.ts.map +1 -0
- package/dist/redirects/index.js +11 -0
- package/dist/sessions/index.d.ts +11 -0
- package/dist/sessions/index.d.ts.map +1 -0
- package/dist/sessions/index.js +58 -0
- package/dist/types.d.ts +132 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/dist/utils.d.ts +8 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +125 -0
- package/package.json +45 -0
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
export type PaymentFrequency = "ONCE" | "MONTHLY" | "ANNUALLY";
|
|
2
|
+
export type PaymentMethod = "mpesa" | "ecocash" | "card" | "none" | (string & {});
|
|
3
|
+
export type RedirectStatus = "success" | "failed" | "cancelled" | (string & {});
|
|
4
|
+
export type SessionStatus = "CREATED" | "PROCESSING" | "COMPLETED" | "FAILED" | "CANCELLED" | "EXPIRED" | (string & {});
|
|
5
|
+
export type TransactionStatus = "success" | "failed" | "cancelled" | null | (string & {});
|
|
6
|
+
export type FetchLike = (input: string | URL | globalThis.Request, init?: globalThis.RequestInit) => Promise<globalThis.Response>;
|
|
7
|
+
export interface MoPayConfig {
|
|
8
|
+
apiKey: string;
|
|
9
|
+
baseUrl?: string;
|
|
10
|
+
timeoutMs?: number;
|
|
11
|
+
fetch?: FetchLike;
|
|
12
|
+
userAgent?: string;
|
|
13
|
+
checkout?: CheckoutOptions;
|
|
14
|
+
}
|
|
15
|
+
export interface RequestOptions {
|
|
16
|
+
signal?: AbortSignal;
|
|
17
|
+
}
|
|
18
|
+
export interface CreatePaymentSessionParams {
|
|
19
|
+
amount: string | number;
|
|
20
|
+
reference: string;
|
|
21
|
+
redirectUrl: string;
|
|
22
|
+
description?: string;
|
|
23
|
+
customerEmail?: string;
|
|
24
|
+
customerName?: string;
|
|
25
|
+
paymentFrequency?: PaymentFrequency;
|
|
26
|
+
productName?: string;
|
|
27
|
+
productDetails?: string;
|
|
28
|
+
productImage?: string;
|
|
29
|
+
payWhatYouWant?: boolean;
|
|
30
|
+
minimumAmount?: string | number;
|
|
31
|
+
}
|
|
32
|
+
export interface CreatePaymentSessionResponse {
|
|
33
|
+
success: true;
|
|
34
|
+
sessionId: string;
|
|
35
|
+
paymentUrl: string;
|
|
36
|
+
checkoutUrl?: string;
|
|
37
|
+
checkout_url?: string;
|
|
38
|
+
checkoutToken?: string;
|
|
39
|
+
checkout_token?: string;
|
|
40
|
+
reference: string;
|
|
41
|
+
amount: string;
|
|
42
|
+
}
|
|
43
|
+
export interface Session {
|
|
44
|
+
id: string;
|
|
45
|
+
sessionId: string;
|
|
46
|
+
amount: string;
|
|
47
|
+
reference: string;
|
|
48
|
+
transactionStatus: TransactionStatus;
|
|
49
|
+
redirectUrl: string;
|
|
50
|
+
status: SessionStatus;
|
|
51
|
+
selectedPaymentMethod?: PaymentMethod | null;
|
|
52
|
+
transactionId?: string | null;
|
|
53
|
+
createdAt?: string;
|
|
54
|
+
completedAt?: string | null;
|
|
55
|
+
[key: string]: unknown;
|
|
56
|
+
}
|
|
57
|
+
export interface RetrieveSessionResponse {
|
|
58
|
+
success: true;
|
|
59
|
+
session: Session;
|
|
60
|
+
}
|
|
61
|
+
export interface ErrorResponse {
|
|
62
|
+
success?: false;
|
|
63
|
+
error?: string;
|
|
64
|
+
message?: string;
|
|
65
|
+
code?: string;
|
|
66
|
+
[key: string]: unknown;
|
|
67
|
+
}
|
|
68
|
+
export interface VerifiedRedirect {
|
|
69
|
+
status: RedirectStatus;
|
|
70
|
+
reference?: string;
|
|
71
|
+
transactionId?: string;
|
|
72
|
+
sessionId: string;
|
|
73
|
+
amount?: string;
|
|
74
|
+
paymentMethod?: PaymentMethod;
|
|
75
|
+
error?: string;
|
|
76
|
+
raw: Record<string, string>;
|
|
77
|
+
}
|
|
78
|
+
export type CheckoutMode = "popup" | "dialog" | "redirect";
|
|
79
|
+
export type CheckoutStatus = "success" | "failed" | "cancelled" | "pending" | (string & {});
|
|
80
|
+
export interface CheckoutMessage {
|
|
81
|
+
type?: string;
|
|
82
|
+
status?: CheckoutStatus;
|
|
83
|
+
sessionId?: string;
|
|
84
|
+
transactionId?: string;
|
|
85
|
+
reference?: string;
|
|
86
|
+
amount?: string;
|
|
87
|
+
paymentMethod?: PaymentMethod;
|
|
88
|
+
error?: string;
|
|
89
|
+
raw: unknown;
|
|
90
|
+
}
|
|
91
|
+
export interface CheckoutOptions {
|
|
92
|
+
mode?: CheckoutMode;
|
|
93
|
+
target?: Window;
|
|
94
|
+
popupName?: string;
|
|
95
|
+
width?: number;
|
|
96
|
+
height?: number;
|
|
97
|
+
minWidth?: number;
|
|
98
|
+
minHeight?: number;
|
|
99
|
+
maxWidth?: number;
|
|
100
|
+
maxHeight?: number;
|
|
101
|
+
resizable?: boolean;
|
|
102
|
+
closeButtonLabel?: string;
|
|
103
|
+
onOpen?: (checkout: CheckoutHandle) => void;
|
|
104
|
+
onClose?: () => void;
|
|
105
|
+
onMessage?: (message: CheckoutMessage) => void;
|
|
106
|
+
onSuccess?: (message: CheckoutMessage) => void;
|
|
107
|
+
onFailed?: (message: CheckoutMessage) => void;
|
|
108
|
+
onCancel?: (message: CheckoutMessage) => void;
|
|
109
|
+
onPending?: (message: CheckoutMessage) => void;
|
|
110
|
+
}
|
|
111
|
+
export interface CheckoutHandle {
|
|
112
|
+
mode: CheckoutMode;
|
|
113
|
+
close: () => void;
|
|
114
|
+
}
|
|
115
|
+
export interface MoPayCheckoutOpenOptions extends CheckoutOptions {
|
|
116
|
+
checkoutUrl?: string;
|
|
117
|
+
checkout_url?: string;
|
|
118
|
+
checkoutToken?: string;
|
|
119
|
+
checkout_token?: string;
|
|
120
|
+
baseUrl?: string;
|
|
121
|
+
allowedOrigin?: string;
|
|
122
|
+
allowedOrigins?: string[];
|
|
123
|
+
}
|
|
124
|
+
export interface CheckoutFlowOptions extends CheckoutOptions, RequestOptions {
|
|
125
|
+
onSessionCreated?: (session: CreatePaymentSessionResponse) => void;
|
|
126
|
+
}
|
|
127
|
+
export interface WaitForSessionOptions extends RequestOptions {
|
|
128
|
+
intervalMs?: number;
|
|
129
|
+
timeoutMs?: number;
|
|
130
|
+
terminalStatuses?: SessionStatus[];
|
|
131
|
+
}
|
|
132
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,gBAAgB,GAAG,MAAM,GAAG,SAAS,GAAG,UAAU,CAAC;AAE/D,MAAM,MAAM,aAAa,GAAG,OAAO,GAAG,SAAS,GAAG,MAAM,GAAG,MAAM,GAAG,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC;AAElF,MAAM,MAAM,cAAc,GAAG,SAAS,GAAG,QAAQ,GAAG,WAAW,GAAG,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC;AAEhF,MAAM,MAAM,aAAa,GACrB,SAAS,GACT,YAAY,GACZ,WAAW,GACX,QAAQ,GACR,WAAW,GACX,SAAS,GACT,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC;AAElB,MAAM,MAAM,iBAAiB,GAAG,SAAS,GAAG,QAAQ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Q,CAAC,EAAE,CAAC,OAAO,EAAE,eAAe,KAAK,IAAI,CAAC;IAC9C,QAAQ,CAAC,EAAE,CAAC,OAAO,EAAE,eAAe,KAAK,IAAI,CAAC;IAC9C,SAAS,CAAC,EAAE,CAAC,OAAO,EAAE,eAAe,KAAK,IAAI,CAAC;CAChD;AAED,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,YAAY,CAAC;IACnB,KAAK,EAAE,MAAM,IAAI,CAAC;CACnB;AAED,MAAM,WAAW,wBAAyB,SAAQ,eAAe;IAC/D,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;CAC3B;AAED,MAAM,WAAW,mBAAoB,SAAQ,eAAe,EAAE,cAAc;IAC1E,gBAAgB,CAAC,EAAE,CAAC,OAAO,EAAE,4BAA4B,KAAK,IAAI,CAAC;CACpE;AAED,MAAM,WAAW,qBAAsB,SAAQ,cAAc;IAC3D,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,gBAAgB,CAAC,EAAE,aAAa,EAAE,CAAC;CACpC"}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { CreatePaymentSessionParams, VerifiedRedirect } from "./types.js";
|
|
2
|
+
export declare function applyMixins(derivedCtor: any, baseCtors: any[]): void;
|
|
3
|
+
export declare function isValidReference(reference: string): boolean;
|
|
4
|
+
export declare function formatAmount(amount: string | number): string;
|
|
5
|
+
export declare function validateCreatePaymentSessionParams(params: CreatePaymentSessionParams): void;
|
|
6
|
+
export declare function serializeCreatePaymentSessionParams(params: CreatePaymentSessionParams): Record<string, string | boolean>;
|
|
7
|
+
export declare function parseRedirect(input: string | URL | URLSearchParams | Record<string, string>): VerifiedRedirect;
|
|
8
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,0BAA0B,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAE/E,wBAAgB,WAAW,CAAC,WAAW,EAAE,GAAG,EAAE,SAAS,EAAE,GAAG,EAAE,GAAG,IAAI,CAcpE;AAID,wBAAgB,gBAAgB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAE3D;AAED,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,CAe5D;AAED,wBAAgB,kCAAkC,CAAC,MAAM,EAAE,0BAA0B,GAAG,IAAI,CAsB3F;AAED,wBAAgB,mCAAmC,CACjD,MAAM,EAAE,0BAA0B,GACjC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,CA0BlC;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,GAAG,GAAG,eAAe,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,gBAAgB,CA2B9G"}
|
package/dist/utils.js
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { MoPayValidationError } from "./errors.js";
|
|
2
|
+
export function applyMixins(derivedCtor, baseCtors) {
|
|
3
|
+
baseCtors.forEach((baseCtor) => {
|
|
4
|
+
Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => {
|
|
5
|
+
if (name === "constructor" || Object.prototype.hasOwnProperty.call(derivedCtor.prototype, name)) {
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
const descriptor = Object.getOwnPropertyDescriptor(baseCtor.prototype, name);
|
|
9
|
+
if (descriptor) {
|
|
10
|
+
Object.defineProperty(derivedCtor.prototype, name, descriptor);
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
const REFERENCE_PATTERN = /^[A-Za-z0-9]+$/;
|
|
16
|
+
export function isValidReference(reference) {
|
|
17
|
+
return REFERENCE_PATTERN.test(reference);
|
|
18
|
+
}
|
|
19
|
+
export function formatAmount(amount) {
|
|
20
|
+
if (typeof amount === "number") {
|
|
21
|
+
if (!Number.isFinite(amount) || amount <= 0) {
|
|
22
|
+
throw new MoPayValidationError("amount must be a positive number");
|
|
23
|
+
}
|
|
24
|
+
return amount.toFixed(2);
|
|
25
|
+
}
|
|
26
|
+
const trimmed = amount.trim();
|
|
27
|
+
if (!/^\d+(\.\d{1,2})?$/.test(trimmed) || Number(trimmed) <= 0) {
|
|
28
|
+
throw new MoPayValidationError("amount must be a positive decimal string");
|
|
29
|
+
}
|
|
30
|
+
return trimmed;
|
|
31
|
+
}
|
|
32
|
+
export function validateCreatePaymentSessionParams(params) {
|
|
33
|
+
formatAmount(params.amount);
|
|
34
|
+
if (!params.reference || !isValidReference(params.reference)) {
|
|
35
|
+
throw new MoPayValidationError("reference must contain only letters and numbers, with no spaces or symbols");
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
new URL(params.redirectUrl);
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
if (error instanceof MoPayValidationError) {
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
44
|
+
throw new MoPayValidationError("redirectUrl must be a valid absolute URL");
|
|
45
|
+
}
|
|
46
|
+
if (params.minimumAmount !== undefined) {
|
|
47
|
+
formatAmount(params.minimumAmount);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
export function serializeCreatePaymentSessionParams(params) {
|
|
51
|
+
validateCreatePaymentSessionParams(params);
|
|
52
|
+
const body = {
|
|
53
|
+
amount: formatAmount(params.amount),
|
|
54
|
+
reference: params.reference,
|
|
55
|
+
redirectUrl: params.redirectUrl,
|
|
56
|
+
};
|
|
57
|
+
addOptional(body, "description", params.description);
|
|
58
|
+
addOptional(body, "customerEmail", params.customerEmail);
|
|
59
|
+
addOptional(body, "customerName", params.customerName);
|
|
60
|
+
addOptional(body, "paymentFrequency", params.paymentFrequency);
|
|
61
|
+
addOptional(body, "productName", params.productName);
|
|
62
|
+
addOptional(body, "productDetails", params.productDetails);
|
|
63
|
+
addOptional(body, "productImage", params.productImage);
|
|
64
|
+
if (params.payWhatYouWant !== undefined) {
|
|
65
|
+
body.payWhatYouWant = params.payWhatYouWant;
|
|
66
|
+
}
|
|
67
|
+
if (params.minimumAmount !== undefined) {
|
|
68
|
+
body.minimumAmount = formatAmount(params.minimumAmount);
|
|
69
|
+
}
|
|
70
|
+
return body;
|
|
71
|
+
}
|
|
72
|
+
export function parseRedirect(input) {
|
|
73
|
+
const params = toSearchParams(input);
|
|
74
|
+
const raw = Object.fromEntries(params.entries());
|
|
75
|
+
const status = params.get("status");
|
|
76
|
+
const sessionId = params.get("sessionId");
|
|
77
|
+
if (!status) {
|
|
78
|
+
throw new MoPayValidationError("redirect is missing status");
|
|
79
|
+
}
|
|
80
|
+
if (!sessionId) {
|
|
81
|
+
throw new MoPayValidationError("redirect is missing sessionId");
|
|
82
|
+
}
|
|
83
|
+
const redirect = {
|
|
84
|
+
status,
|
|
85
|
+
sessionId,
|
|
86
|
+
raw,
|
|
87
|
+
};
|
|
88
|
+
addOptionalRedirectValue(redirect, "reference", params.get("reference"));
|
|
89
|
+
addOptionalRedirectValue(redirect, "transactionId", params.get("transactionId"));
|
|
90
|
+
addOptionalRedirectValue(redirect, "amount", params.get("amount"));
|
|
91
|
+
addOptionalRedirectValue(redirect, "paymentMethod", params.get("paymentMethod"));
|
|
92
|
+
addOptionalRedirectValue(redirect, "error", params.get("error"));
|
|
93
|
+
return redirect;
|
|
94
|
+
}
|
|
95
|
+
function addOptional(body, key, value) {
|
|
96
|
+
if (value !== undefined) {
|
|
97
|
+
body[key] = value;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
function optional(value) {
|
|
101
|
+
return value === null || value === "" ? undefined : value;
|
|
102
|
+
}
|
|
103
|
+
function addOptionalRedirectValue(redirect, key, value) {
|
|
104
|
+
const parsed = optional(value);
|
|
105
|
+
if (parsed !== undefined) {
|
|
106
|
+
redirect[key] = parsed;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function toSearchParams(input) {
|
|
110
|
+
if (input instanceof URLSearchParams) {
|
|
111
|
+
return input;
|
|
112
|
+
}
|
|
113
|
+
if (input instanceof URL) {
|
|
114
|
+
return input.searchParams;
|
|
115
|
+
}
|
|
116
|
+
if (typeof input === "string") {
|
|
117
|
+
try {
|
|
118
|
+
return new URL(input).searchParams;
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
return new URLSearchParams(input.startsWith("?") ? input.slice(1) : input);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return new URLSearchParams(input);
|
|
125
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mopay/node-sdk",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Official TypeScript SDK for the MoPay payment API.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"source": "src/index.ts",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"module": "./dist/index.js",
|
|
9
|
+
"unpkg": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"default": "./dist/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "rm -rf dist && tsc -p tsconfig.json",
|
|
25
|
+
"dev": "tsc -p tsconfig.json --watch",
|
|
26
|
+
"test": "pnpm run build && node --test test/*.test.mjs",
|
|
27
|
+
"prepack": "pnpm run build",
|
|
28
|
+
"prepublishOnly": "pnpm test"
|
|
29
|
+
},
|
|
30
|
+
"keywords": [
|
|
31
|
+
"mopay",
|
|
32
|
+
"payments",
|
|
33
|
+
"lesotho",
|
|
34
|
+
"mpesa",
|
|
35
|
+
"ecocash",
|
|
36
|
+
"fintech"
|
|
37
|
+
],
|
|
38
|
+
"license": "MIT",
|
|
39
|
+
"engines": {
|
|
40
|
+
"node": ">=18"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"typescript": "^4.9.5"
|
|
44
|
+
}
|
|
45
|
+
}
|