@ngandu-dev/flexpay 2.0.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025-present Bernard Ngandu and contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,147 @@
1
+ # FlexPay TypeScript
2
+
3
+ ![npm](https://img.shields.io/npm/v/@ngandu-dev/flexpay?style=flat-square)
4
+ ![npm](https://img.shields.io/npm/dt/@ngandu-dev/flexpay?style=flat-square)
5
+ [![Quality](https://github.com/ngandu-dev/flexpay/actions/workflows/quality.yml/badge.svg?branch=main)](https://github.com/ngandu-dev/flexpay/actions/workflows/quality.yml)
6
+ [![Tests](https://github.com/ngandu-dev/flexpay/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/ngandu-dev/flexpay/actions/workflows/test.yml)
7
+ ![GitHub](https://img.shields.io/github/license/ngandu-dev/flexpay?style=flat-square)
8
+
9
+ For privacy reasons, FlexPay's original documentation cannot be shared without written permission.
10
+ For credentials and implementation details, contact [FlexPay](https://flexpay.cd).
11
+
12
+ ## Features
13
+
14
+ - Typed mobile-money and card payment requests
15
+ - Transaction status checks and callback validation
16
+ - Development and production API environments
17
+ - ESM and CommonJS distributions with TypeScript declarations
18
+
19
+ ## Requirements
20
+
21
+ - Node.js 24 or newer
22
+
23
+ ## Installation
24
+
25
+ Add the TypeScript client to your application's dependencies:
26
+
27
+ ```bash
28
+ npm install @ngandu-dev/flexpay
29
+ ```
30
+
31
+ ## Quick start
32
+
33
+ ### Authentication
34
+
35
+ - **Step 1**. Contact FlexPay to get a Merchant Account
36
+ You will receive a Merchant Form to complete in order to provide your business details and preferred Cash out Wallet or Banking Details.
37
+ - **Step 2**. Once the paperwork is completed, you will be issued with Live and Sandbox Accounts (Merchant Code and Authorization token)
38
+
39
+ Then use these credentials to authenticate your client
40
+
41
+ ```ts
42
+ import { Client as Flexpay } from "@ngandu-dev/flexpay";
43
+
44
+ const flexpay = new Flexpay("merchant_code", "token", "dev"); // use "prod" for production
45
+ ```
46
+
47
+ ## Usage
48
+
49
+ ### Create a Payment Request
50
+
51
+ ```typescript
52
+ import type { CardRequest, MobileRequest } from "@ngandu-dev/flexpay";
53
+
54
+ const mobile = {
55
+ amount: 10, // 10 USD
56
+ currency: "USD",
57
+ phone: "243999999999",
58
+ reference: "mobile_order_12345",
59
+ description: "your_transaction_description",
60
+ callbackUrl: "https://example.com/flexpay/callback",
61
+ } satisfies MobileRequest;
62
+
63
+ const card = {
64
+ amount: 10, // 10 USD
65
+ currency: "USD",
66
+ reference: "card_order_12345",
67
+ description: "your_transaction_description",
68
+ callbackUrl: "https://example.com/flexpay/callback",
69
+ approveUrl: "https://example.com/flexpay/approved",
70
+ cancelUrl: "https://example.com/flexpay/cancelled",
71
+ declineUrl: "https://example.com/flexpay/declined",
72
+ homeUrl: "https://example.com",
73
+ } satisfies CardRequest;
74
+ ```
75
+
76
+ > **Note**: we highly recommend your `callbacks` urls to be unique for each transaction.
77
+
78
+ ### Mobile Payment
79
+ Once called, Flexpay will send a payment request to the user's mobile money account, and the user will have to confirm the payment on their phone.
80
+ after that the payment will be processed and the callback url will be called with the transaction details.
81
+
82
+ ```typescript
83
+ const mobileResponse = await flexpay.pay(mobile);
84
+ ```
85
+
86
+ ### Visa Card Payment
87
+ You can set up card payment via VPOS features, which is typically used for online payments.
88
+ it's a gateway that allows you to accept payments from your customers using their credit cards.
89
+
90
+ ```typescript
91
+ const cardResponse = await flexpay.pay(card);
92
+ // redirect to cardResponse.url to complete the payment
93
+ ```
94
+
95
+ #### **handling callback (callbackUrl, approveUrl, cancelUrl, declineUrl)**
96
+ Flexpay will send a POST request to the defined callbackUrl and the response will contain the transaction details.
97
+ you can use the following code to validate the incoming payload.
98
+
99
+ ```typescript
100
+ const webhook = flexpay.handleCallback(req.body);
101
+ flexpay.isSuccessful(webhook); // true or false
102
+ ```
103
+
104
+ ### Check Transaction state
105
+ You don't trust webhook ? you can always check the transaction state by providing the order number.
106
+
107
+ ```typescript
108
+ if (!mobileResponse.orderNumber) {
109
+ throw new Error("FlexPay did not return an order number");
110
+ }
111
+
112
+ const tx = await flexpay.check(mobileResponse.orderNumber);
113
+ flexpay.isSuccessful(tx); // true or false
114
+ ```
115
+
116
+ ## Migrating to `@ngandu-dev/flexpay`
117
+
118
+ Replace the dependency and every import with `@ngandu-dev/flexpay`. Version 2 is a clean package
119
+ move and does not provide an alias for the old scope.
120
+
121
+ ## Development
122
+
123
+ Install dependencies with `bun install`, then run `bun run quality` before opening a pull request.
124
+ See [CONTRIBUTING.md](CONTRIBUTING.md) for the complete contribution workflow.
125
+
126
+ ## Testing
127
+
128
+ Run `bun run test` for the test suite or `bun run test:coverage` for a coverage report.
129
+
130
+ ## Contributing
131
+
132
+ Contributions are welcome. Please read [CONTRIBUTING.md](CONTRIBUTING.md) and follow our
133
+ [Code of Conduct](CODE_OF_CONDUCT.md).
134
+
135
+ ## Security
136
+
137
+ Please report vulnerabilities privately as described in [SECURITY.md](SECURITY.md).
138
+
139
+ ## License
140
+
141
+ Released under the [MIT License](LICENSE).
142
+
143
+ ## Contributors
144
+
145
+ <a href="https://github.com/ngandu-dev/flexpay/graphs/contributors" title="Show all contributors">
146
+ <img src="https://contrib.rocks/image?repo=ngandu-dev/flexpay" alt="Contributors" />
147
+ </a>
@@ -0,0 +1,169 @@
1
+ import * as z from 'zod';
2
+
3
+ type EnvironmentType = "prod" | "dev";
4
+ declare class Environment {
5
+ readonly value: EnvironmentType;
6
+ constructor(value: EnvironmentType);
7
+ getMobilePaymentUrl(): string;
8
+ getPayoutUrl(): string;
9
+ getCheckStatusUrl(orderNumber: string): string;
10
+ getCardPaymentUrl(): string;
11
+ private getBaseUrl;
12
+ }
13
+
14
+ declare enum Status {
15
+ SUCCESS = 0,// When the request is successfully processed
16
+ FAILURE = 1
17
+ }
18
+ declare const CurrencySchema: z.ZodEnum<{
19
+ USD: "USD";
20
+ CDF: "CDF";
21
+ }>;
22
+ type Currency = z.infer<typeof CurrencySchema>;
23
+ declare const StatusSchema: z.ZodPipe<z.ZodUnion<readonly [z.ZodLiteral<Status.SUCCESS>, z.ZodLiteral<Status.FAILURE>, z.ZodLiteral<"0">, z.ZodLiteral<"1">]>, z.ZodTransform<Status, "0" | "1" | Status>>;
24
+ declare const MobileResponseSchema: z.ZodPipe<z.ZodObject<{
25
+ code: z.ZodPipe<z.ZodUnion<readonly [z.ZodLiteral<Status.SUCCESS>, z.ZodLiteral<Status.FAILURE>, z.ZodLiteral<"0">, z.ZodLiteral<"1">]>, z.ZodTransform<Status, "0" | "1" | Status>>;
26
+ message: z.ZodDefault<z.ZodOptional<z.ZodString>>;
27
+ orderNumber: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNull]>>;
28
+ provider_reference: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNull]>>;
29
+ providerReference: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNull]>>;
30
+ reference: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNull]>>;
31
+ url: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNull]>>;
32
+ }, z.core.$strip>, z.ZodTransform<{
33
+ code: Status;
34
+ message: string;
35
+ orderNumber: string | null;
36
+ providerReference: string | null;
37
+ reference: string | null;
38
+ url: string | null;
39
+ }, {
40
+ code: Status;
41
+ message: string;
42
+ orderNumber?: string | null | undefined;
43
+ provider_reference?: string | null | undefined;
44
+ providerReference?: string | null | undefined;
45
+ reference?: string | null | undefined;
46
+ url?: string | null | undefined;
47
+ }>>;
48
+ declare const CardResponseSchema: z.ZodObject<{
49
+ code: z.ZodPipe<z.ZodUnion<readonly [z.ZodLiteral<Status.SUCCESS>, z.ZodLiteral<Status.FAILURE>, z.ZodLiteral<"0">, z.ZodLiteral<"1">]>, z.ZodTransform<Status, "0" | "1" | Status>>;
50
+ message: z.ZodDefault<z.ZodOptional<z.ZodString>>;
51
+ orderNumber: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNull]>>;
52
+ url: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNull]>>;
53
+ }, z.core.$strip>;
54
+ declare const TransactionSchema: z.ZodObject<{
55
+ amount: z.ZodUnion<readonly [z.ZodNumber, z.ZodPipe<z.ZodPipe<z.ZodString, z.ZodTransform<number, string>>, z.ZodNumber>]>;
56
+ amountCustomer: z.ZodUnion<readonly [z.ZodNumber, z.ZodPipe<z.ZodPipe<z.ZodString, z.ZodTransform<number, string>>, z.ZodNumber>]>;
57
+ channel: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNull]>>;
58
+ createdAt: z.ZodString;
59
+ currency: z.ZodEnum<{
60
+ USD: "USD";
61
+ CDF: "CDF";
62
+ }>;
63
+ orderNumber: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNull]>>;
64
+ reference: z.ZodString;
65
+ status: z.ZodPipe<z.ZodUnion<readonly [z.ZodLiteral<Status.SUCCESS>, z.ZodLiteral<Status.FAILURE>, z.ZodLiteral<"0">, z.ZodLiteral<"1">]>, z.ZodTransform<Status, "0" | "1" | Status>>;
66
+ }, z.core.$strip>;
67
+ declare const CheckResponseSchema: z.ZodObject<{
68
+ code: z.ZodPipe<z.ZodUnion<readonly [z.ZodLiteral<Status.SUCCESS>, z.ZodLiteral<Status.FAILURE>, z.ZodLiteral<"0">, z.ZodLiteral<"1">]>, z.ZodTransform<Status, "0" | "1" | Status>>;
69
+ message: z.ZodDefault<z.ZodOptional<z.ZodString>>;
70
+ transaction: z.ZodDefault<z.ZodOptional<z.ZodUnion<readonly [z.ZodObject<{
71
+ amount: z.ZodUnion<readonly [z.ZodNumber, z.ZodPipe<z.ZodPipe<z.ZodString, z.ZodTransform<number, string>>, z.ZodNumber>]>;
72
+ amountCustomer: z.ZodUnion<readonly [z.ZodNumber, z.ZodPipe<z.ZodPipe<z.ZodString, z.ZodTransform<number, string>>, z.ZodNumber>]>;
73
+ channel: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNull]>>;
74
+ createdAt: z.ZodString;
75
+ currency: z.ZodEnum<{
76
+ USD: "USD";
77
+ CDF: "CDF";
78
+ }>;
79
+ orderNumber: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNull]>>;
80
+ reference: z.ZodString;
81
+ status: z.ZodPipe<z.ZodUnion<readonly [z.ZodLiteral<Status.SUCCESS>, z.ZodLiteral<Status.FAILURE>, z.ZodLiteral<"0">, z.ZodLiteral<"1">]>, z.ZodTransform<Status, "0" | "1" | Status>>;
82
+ }, z.core.$strip>, z.ZodNull]>>>;
83
+ }, z.core.$strip>;
84
+ declare const PayoutResponseSchema: z.ZodObject<{
85
+ code: z.ZodPipe<z.ZodUnion<readonly [z.ZodLiteral<Status.SUCCESS>, z.ZodLiteral<Status.FAILURE>, z.ZodLiteral<"0">, z.ZodLiteral<"1">]>, z.ZodTransform<Status, "0" | "1" | Status>>;
86
+ message: z.ZodDefault<z.ZodOptional<z.ZodString>>;
87
+ orderNumber: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNull]>>;
88
+ }, z.core.$strip>;
89
+ declare const MobileRequestSchema: z.ZodObject<{
90
+ amount: z.ZodNumber;
91
+ approveUrl: z.ZodNullable<z.ZodOptional<z.ZodString>>;
92
+ callbackUrl: z.ZodString;
93
+ cancelUrl: z.ZodNullable<z.ZodOptional<z.ZodString>>;
94
+ currency: z.ZodEnum<{
95
+ USD: "USD";
96
+ CDF: "CDF";
97
+ }>;
98
+ declineUrl: z.ZodNullable<z.ZodOptional<z.ZodString>>;
99
+ description: z.ZodNullable<z.ZodOptional<z.ZodString>>;
100
+ reference: z.ZodString;
101
+ phone: z.ZodString;
102
+ }, z.core.$strip>;
103
+ declare const CardRequestSchema: z.ZodObject<{
104
+ amount: z.ZodNumber;
105
+ approveUrl: z.ZodString;
106
+ callbackUrl: z.ZodString;
107
+ cancelUrl: z.ZodString;
108
+ currency: z.ZodEnum<{
109
+ USD: "USD";
110
+ CDF: "CDF";
111
+ }>;
112
+ declineUrl: z.ZodString;
113
+ description: z.ZodString;
114
+ homeUrl: z.ZodString;
115
+ reference: z.ZodString;
116
+ }, z.core.$strip>;
117
+ declare const PayoutRequestSchema: z.ZodObject<{
118
+ amount: z.ZodNumber;
119
+ callbackUrl: z.ZodString;
120
+ currency: z.ZodEnum<{
121
+ USD: "USD";
122
+ CDF: "CDF";
123
+ }>;
124
+ phone: z.ZodString;
125
+ reference: z.ZodString;
126
+ }, z.core.$strip>;
127
+ type MobileResponse = z.infer<typeof MobileResponseSchema>;
128
+ type CardResponse = z.infer<typeof CardResponseSchema>;
129
+ type Transaction = z.infer<typeof TransactionSchema>;
130
+ type CheckResponse = z.infer<typeof CheckResponseSchema>;
131
+ type PayoutResponse = z.infer<typeof PayoutResponseSchema>;
132
+ type MobileRequest = z.infer<typeof MobileRequestSchema>;
133
+ type CardRequest = z.infer<typeof CardRequestSchema>;
134
+ type PayoutRequest = z.infer<typeof PayoutRequestSchema>;
135
+
136
+ declare class Client {
137
+ private readonly credential;
138
+ private readonly env;
139
+ constructor(merchant: string, token: string, env?: EnvironmentType);
140
+ mobile(request: MobileRequest): Promise<MobileResponse>;
141
+ card(request: CardRequest): Promise<CardResponse>;
142
+ pay(request: MobileRequest): Promise<MobileResponse>;
143
+ pay(request: CardRequest): Promise<CardResponse>;
144
+ check(orderNumber: string): Promise<CheckResponse>;
145
+ payout(request: PayoutRequest): Promise<PayoutResponse>;
146
+ handleCallback(data: unknown): MobileResponse;
147
+ isSuccessful<T extends {
148
+ code: Status;
149
+ }>(response: T): response is T & {
150
+ code: Status.SUCCESS;
151
+ };
152
+ private requestJson;
153
+ private parseWith;
154
+ }
155
+
156
+ declare class NetworkException extends Error {
157
+ readonly status?: number;
158
+ readonly type?: string;
159
+ constructor(message: string, type?: string | null, status?: number | null);
160
+ static create(message: string, type: string, status: number): NetworkException;
161
+ }
162
+ declare class AccountException extends NetworkException {
163
+ }
164
+ declare class ClientException extends NetworkException {
165
+ }
166
+ declare class ServerException extends NetworkException {
167
+ }
168
+
169
+ export { AccountException, type CardRequest, CardRequestSchema, type CardResponse, CardResponseSchema, type CheckResponse, CheckResponseSchema, Client, ClientException, type Currency, CurrencySchema, Environment, type EnvironmentType, type MobileRequest, MobileRequestSchema, type MobileResponse, MobileResponseSchema, NetworkException, type PayoutRequest, PayoutRequestSchema, type PayoutResponse, PayoutResponseSchema, ServerException, Status, StatusSchema, type Transaction, TransactionSchema };
@@ -0,0 +1,169 @@
1
+ import * as z from 'zod';
2
+
3
+ type EnvironmentType = "prod" | "dev";
4
+ declare class Environment {
5
+ readonly value: EnvironmentType;
6
+ constructor(value: EnvironmentType);
7
+ getMobilePaymentUrl(): string;
8
+ getPayoutUrl(): string;
9
+ getCheckStatusUrl(orderNumber: string): string;
10
+ getCardPaymentUrl(): string;
11
+ private getBaseUrl;
12
+ }
13
+
14
+ declare enum Status {
15
+ SUCCESS = 0,// When the request is successfully processed
16
+ FAILURE = 1
17
+ }
18
+ declare const CurrencySchema: z.ZodEnum<{
19
+ USD: "USD";
20
+ CDF: "CDF";
21
+ }>;
22
+ type Currency = z.infer<typeof CurrencySchema>;
23
+ declare const StatusSchema: z.ZodPipe<z.ZodUnion<readonly [z.ZodLiteral<Status.SUCCESS>, z.ZodLiteral<Status.FAILURE>, z.ZodLiteral<"0">, z.ZodLiteral<"1">]>, z.ZodTransform<Status, "0" | "1" | Status>>;
24
+ declare const MobileResponseSchema: z.ZodPipe<z.ZodObject<{
25
+ code: z.ZodPipe<z.ZodUnion<readonly [z.ZodLiteral<Status.SUCCESS>, z.ZodLiteral<Status.FAILURE>, z.ZodLiteral<"0">, z.ZodLiteral<"1">]>, z.ZodTransform<Status, "0" | "1" | Status>>;
26
+ message: z.ZodDefault<z.ZodOptional<z.ZodString>>;
27
+ orderNumber: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNull]>>;
28
+ provider_reference: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNull]>>;
29
+ providerReference: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNull]>>;
30
+ reference: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNull]>>;
31
+ url: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNull]>>;
32
+ }, z.core.$strip>, z.ZodTransform<{
33
+ code: Status;
34
+ message: string;
35
+ orderNumber: string | null;
36
+ providerReference: string | null;
37
+ reference: string | null;
38
+ url: string | null;
39
+ }, {
40
+ code: Status;
41
+ message: string;
42
+ orderNumber?: string | null | undefined;
43
+ provider_reference?: string | null | undefined;
44
+ providerReference?: string | null | undefined;
45
+ reference?: string | null | undefined;
46
+ url?: string | null | undefined;
47
+ }>>;
48
+ declare const CardResponseSchema: z.ZodObject<{
49
+ code: z.ZodPipe<z.ZodUnion<readonly [z.ZodLiteral<Status.SUCCESS>, z.ZodLiteral<Status.FAILURE>, z.ZodLiteral<"0">, z.ZodLiteral<"1">]>, z.ZodTransform<Status, "0" | "1" | Status>>;
50
+ message: z.ZodDefault<z.ZodOptional<z.ZodString>>;
51
+ orderNumber: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNull]>>;
52
+ url: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNull]>>;
53
+ }, z.core.$strip>;
54
+ declare const TransactionSchema: z.ZodObject<{
55
+ amount: z.ZodUnion<readonly [z.ZodNumber, z.ZodPipe<z.ZodPipe<z.ZodString, z.ZodTransform<number, string>>, z.ZodNumber>]>;
56
+ amountCustomer: z.ZodUnion<readonly [z.ZodNumber, z.ZodPipe<z.ZodPipe<z.ZodString, z.ZodTransform<number, string>>, z.ZodNumber>]>;
57
+ channel: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNull]>>;
58
+ createdAt: z.ZodString;
59
+ currency: z.ZodEnum<{
60
+ USD: "USD";
61
+ CDF: "CDF";
62
+ }>;
63
+ orderNumber: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNull]>>;
64
+ reference: z.ZodString;
65
+ status: z.ZodPipe<z.ZodUnion<readonly [z.ZodLiteral<Status.SUCCESS>, z.ZodLiteral<Status.FAILURE>, z.ZodLiteral<"0">, z.ZodLiteral<"1">]>, z.ZodTransform<Status, "0" | "1" | Status>>;
66
+ }, z.core.$strip>;
67
+ declare const CheckResponseSchema: z.ZodObject<{
68
+ code: z.ZodPipe<z.ZodUnion<readonly [z.ZodLiteral<Status.SUCCESS>, z.ZodLiteral<Status.FAILURE>, z.ZodLiteral<"0">, z.ZodLiteral<"1">]>, z.ZodTransform<Status, "0" | "1" | Status>>;
69
+ message: z.ZodDefault<z.ZodOptional<z.ZodString>>;
70
+ transaction: z.ZodDefault<z.ZodOptional<z.ZodUnion<readonly [z.ZodObject<{
71
+ amount: z.ZodUnion<readonly [z.ZodNumber, z.ZodPipe<z.ZodPipe<z.ZodString, z.ZodTransform<number, string>>, z.ZodNumber>]>;
72
+ amountCustomer: z.ZodUnion<readonly [z.ZodNumber, z.ZodPipe<z.ZodPipe<z.ZodString, z.ZodTransform<number, string>>, z.ZodNumber>]>;
73
+ channel: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNull]>>;
74
+ createdAt: z.ZodString;
75
+ currency: z.ZodEnum<{
76
+ USD: "USD";
77
+ CDF: "CDF";
78
+ }>;
79
+ orderNumber: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNull]>>;
80
+ reference: z.ZodString;
81
+ status: z.ZodPipe<z.ZodUnion<readonly [z.ZodLiteral<Status.SUCCESS>, z.ZodLiteral<Status.FAILURE>, z.ZodLiteral<"0">, z.ZodLiteral<"1">]>, z.ZodTransform<Status, "0" | "1" | Status>>;
82
+ }, z.core.$strip>, z.ZodNull]>>>;
83
+ }, z.core.$strip>;
84
+ declare const PayoutResponseSchema: z.ZodObject<{
85
+ code: z.ZodPipe<z.ZodUnion<readonly [z.ZodLiteral<Status.SUCCESS>, z.ZodLiteral<Status.FAILURE>, z.ZodLiteral<"0">, z.ZodLiteral<"1">]>, z.ZodTransform<Status, "0" | "1" | Status>>;
86
+ message: z.ZodDefault<z.ZodOptional<z.ZodString>>;
87
+ orderNumber: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNull]>>;
88
+ }, z.core.$strip>;
89
+ declare const MobileRequestSchema: z.ZodObject<{
90
+ amount: z.ZodNumber;
91
+ approveUrl: z.ZodNullable<z.ZodOptional<z.ZodString>>;
92
+ callbackUrl: z.ZodString;
93
+ cancelUrl: z.ZodNullable<z.ZodOptional<z.ZodString>>;
94
+ currency: z.ZodEnum<{
95
+ USD: "USD";
96
+ CDF: "CDF";
97
+ }>;
98
+ declineUrl: z.ZodNullable<z.ZodOptional<z.ZodString>>;
99
+ description: z.ZodNullable<z.ZodOptional<z.ZodString>>;
100
+ reference: z.ZodString;
101
+ phone: z.ZodString;
102
+ }, z.core.$strip>;
103
+ declare const CardRequestSchema: z.ZodObject<{
104
+ amount: z.ZodNumber;
105
+ approveUrl: z.ZodString;
106
+ callbackUrl: z.ZodString;
107
+ cancelUrl: z.ZodString;
108
+ currency: z.ZodEnum<{
109
+ USD: "USD";
110
+ CDF: "CDF";
111
+ }>;
112
+ declineUrl: z.ZodString;
113
+ description: z.ZodString;
114
+ homeUrl: z.ZodString;
115
+ reference: z.ZodString;
116
+ }, z.core.$strip>;
117
+ declare const PayoutRequestSchema: z.ZodObject<{
118
+ amount: z.ZodNumber;
119
+ callbackUrl: z.ZodString;
120
+ currency: z.ZodEnum<{
121
+ USD: "USD";
122
+ CDF: "CDF";
123
+ }>;
124
+ phone: z.ZodString;
125
+ reference: z.ZodString;
126
+ }, z.core.$strip>;
127
+ type MobileResponse = z.infer<typeof MobileResponseSchema>;
128
+ type CardResponse = z.infer<typeof CardResponseSchema>;
129
+ type Transaction = z.infer<typeof TransactionSchema>;
130
+ type CheckResponse = z.infer<typeof CheckResponseSchema>;
131
+ type PayoutResponse = z.infer<typeof PayoutResponseSchema>;
132
+ type MobileRequest = z.infer<typeof MobileRequestSchema>;
133
+ type CardRequest = z.infer<typeof CardRequestSchema>;
134
+ type PayoutRequest = z.infer<typeof PayoutRequestSchema>;
135
+
136
+ declare class Client {
137
+ private readonly credential;
138
+ private readonly env;
139
+ constructor(merchant: string, token: string, env?: EnvironmentType);
140
+ mobile(request: MobileRequest): Promise<MobileResponse>;
141
+ card(request: CardRequest): Promise<CardResponse>;
142
+ pay(request: MobileRequest): Promise<MobileResponse>;
143
+ pay(request: CardRequest): Promise<CardResponse>;
144
+ check(orderNumber: string): Promise<CheckResponse>;
145
+ payout(request: PayoutRequest): Promise<PayoutResponse>;
146
+ handleCallback(data: unknown): MobileResponse;
147
+ isSuccessful<T extends {
148
+ code: Status;
149
+ }>(response: T): response is T & {
150
+ code: Status.SUCCESS;
151
+ };
152
+ private requestJson;
153
+ private parseWith;
154
+ }
155
+
156
+ declare class NetworkException extends Error {
157
+ readonly status?: number;
158
+ readonly type?: string;
159
+ constructor(message: string, type?: string | null, status?: number | null);
160
+ static create(message: string, type: string, status: number): NetworkException;
161
+ }
162
+ declare class AccountException extends NetworkException {
163
+ }
164
+ declare class ClientException extends NetworkException {
165
+ }
166
+ declare class ServerException extends NetworkException {
167
+ }
168
+
169
+ export { AccountException, type CardRequest, CardRequestSchema, type CardResponse, CardResponseSchema, type CheckResponse, CheckResponseSchema, Client, ClientException, type Currency, CurrencySchema, Environment, type EnvironmentType, type MobileRequest, MobileRequestSchema, type MobileResponse, MobileResponseSchema, NetworkException, type PayoutRequest, PayoutRequestSchema, type PayoutResponse, PayoutResponseSchema, ServerException, Status, StatusSchema, type Transaction, TransactionSchema };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";var J=Object.create;var R=Object.defineProperty;var L=Object.getOwnPropertyDescriptor;var W=Object.getOwnPropertyNames;var D=Object.getPrototypeOf,F=Object.prototype.hasOwnProperty;var G=(r,t)=>{for(var n in t)R(r,n,{get:t[n],enumerable:!0})},j=(r,t,n,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let a of W(t))!F.call(r,a)&&a!==n&&R(r,a,{get:()=>t[a],enumerable:!(o=L(t,a))||o.enumerable});return r};var H=(r,t,n)=>(n=r!=null?J(D(r)):{},j(t||!r||!r.__esModule?R(n,"default",{value:r,enumerable:!0}):n,r)),Z=r=>j(R({},"__esModule",{value:!0}),r);var K={};G(K,{AccountException:()=>h,CardRequestSchema:()=>U,CardResponseSchema:()=>C,CheckResponseSchema:()=>x,Client:()=>q,ClientException:()=>d,CurrencySchema:()=>y,Environment:()=>m,MobileRequestSchema:()=>w,MobileResponseSchema:()=>f,NetworkException:()=>i,PayoutRequestSchema:()=>k,PayoutResponseSchema:()=>P,ServerException:()=>S,Status:()=>z,StatusSchema:()=>l,TransactionSchema:()=>v});module.exports=Z(K);var m=class{constructor(t){this.value=t}value;getMobilePaymentUrl(){return`${this.getBaseUrl()}/paymentService`}getPayoutUrl(){return`${this.getBaseUrl()}/merchantPayOutService`}getCheckStatusUrl(t){return`${this.getBaseUrl()}/check/${encodeURIComponent(t)}`}getCardPaymentUrl(){return this.value==="prod"?"https://cardpayment.flexpay.cd/v1.1/pay":"https://beta-cardpayment.flexpay.cd/v1.1/pay"}getBaseUrl(){return this.value==="prod"?"https://backend.flexpay.cd/api/rest/v1":"https://beta-backend.flexpay.cd/api/rest/v1"}};var i=class r extends Error{status;type;constructor(t,n,o){let a=o!=null?`${t} (HTTP ${o}/${n??"unknown"})`:t;super(a),this.name=this.constructor.name,this.status=o??void 0,this.type=n??void 0,Object.setPrototypeOf(this,new.target.prototype)}static create(t,n,o){let a=t.trim()===""?"No message was provided":t;return o===401||o===429?new h(a,n,o):o>=400&&o<=499?new d(a,n,o):o>=500&&o<=599?new S(a,n,o):new r(a,n,o)}},h=class extends i{},d=class extends i{},S=class extends i{};var e=H(require("zod")),z=(n=>(n[n.SUCCESS=0]="SUCCESS",n[n.FAILURE=1]="FAILURE",n))(z||{});var y=e.enum(["USD","CDF"]),O=e.object({merchant:e.string().min(1,"Merchant cannot be empty"),token:e.string().min(1,"The authorization token cannot be empty")}),l=e.union([e.literal(0),e.literal(1),e.literal("0"),e.literal("1")]).transform(r=>Number(r)),N=e.union([e.number().finite(),e.string().trim().min(1).transform(r=>Number(r)).pipe(e.number().finite())]),c=e.union([e.string(),e.null()]).optional(),f=e.object({code:l,message:e.string().optional().default(""),orderNumber:c,provider_reference:c,providerReference:c,reference:c,url:c}).transform(r=>({code:r.code,message:r.message??"",orderNumber:r.orderNumber??null,providerReference:r.providerReference??r.provider_reference??null,reference:r.reference??null,url:r.url??null})),C=e.object({code:l,message:e.string().optional().default(""),orderNumber:c,url:c}),v=e.object({amount:N,amountCustomer:N,channel:c,createdAt:e.string(),currency:y,orderNumber:c,reference:e.string(),status:l}),x=e.object({code:l,message:e.string().optional().default(""),transaction:e.union([v,e.null()]).optional().default(null)}),P=e.object({code:l,message:e.string().optional().default(""),orderNumber:c}),p=e.string().min(1),_=e.object({amount:e.number().gt(0,"The transaction amount should be greater than 0"),approveUrl:p.optional().nullable(),callbackUrl:p,cancelUrl:p.optional().nullable(),currency:y,declineUrl:p.optional().nullable(),description:e.string().optional().nullable(),reference:e.string().min(1,"The transaction reference is mandatory")}),w=_.extend({phone:e.string().length(12,"The phone number should be 12 characters long, eg: 243123456789")}),U=e.object({amount:e.number().gt(0),approveUrl:p,callbackUrl:p,cancelUrl:p,currency:y,declineUrl:p,description:e.string().min(1,"The description must be provided"),homeUrl:p,reference:e.string().min(1).max(25,"The reference must be between 1 and 25 characters")}),k=e.object({amount:e.number().gt(0),callbackUrl:p,currency:y,phone:e.string().length(12,"The phone number should be 12 characters long, eg: 243123456789"),reference:e.string().min(1)});var q=class{credential;env;constructor(t,n,o="dev"){this.credential=O.parse({merchant:t,token:n}),this.env=new m(o)}async mobile(t){let n={...w.parse(t),merchant:this.credential.merchant,type:1},o=await this.requestJson("POST",this.env.getMobilePaymentUrl(),n);return this.parseWith(f,o)}async card(t){let n={...U.parse(t),authorization:`Bearer ${this.credential.token}`,merchant:this.credential.merchant},o=await this.requestJson("POST",this.env.getCardPaymentUrl(),n);return this.parseWith(C,o)}async pay(t){if("phone"in t&&typeof t.phone=="string")return this.mobile(t);if("homeUrl"in t&&typeof t.homeUrl=="string")return this.card(t);throw new Error("Unsupported request shape")}async check(t){let n=await this.requestJson("GET",this.env.getCheckStatusUrl(t));return this.parseWith(x,n)}async payout(t){let n={...k.parse(t),merchant:this.credential.merchant,type:1},o=await this.requestJson("POST",this.env.getPayoutUrl(),n);return this.parseWith(P,o)}handleCallback(t){return this.parseWith(f,t)}isSuccessful(t){return t.code===0}async requestJson(t,n,o){let a={Accept:"application/json",Authorization:`Bearer ${this.credential.token}`,"Content-Type":"application/json"},T=3,B=500;for(let b=0;b<=T;b++)try{let s=await fetch(n,{body:t==="POST"?JSON.stringify(o??{}):void 0,headers:a,method:t}),M=(s.headers.get("content-type")||"").includes("application/json"),u={};if(M)try{u=await s.json()}catch{u={}}if(!s.ok){let g=typeof u=="object"&&u!==null?u:{},A=typeof g.message=="string"?g.message:"",$=typeof g.error=="string"?g.error:"unknown",I=s.status;throw i.create(A,$,I)}return u}catch(s){if(s instanceof h||s instanceof d||s instanceof i&&typeof s.status=="number"&&s.status>=400&&s.status<500)throw s;if(b<T){let E=B*2**b;await new Promise(M=>setTimeout(M,E));continue}throw s instanceof i?s:new i(s instanceof Error?s.message:"Network error")}throw new i("Unexpected request flow")}parseWith(t,n){return t.parse(n)}};0&&(module.exports={AccountException,CardRequestSchema,CardResponseSchema,CheckResponseSchema,Client,ClientException,CurrencySchema,Environment,MobileRequestSchema,MobileResponseSchema,NetworkException,PayoutRequestSchema,PayoutResponseSchema,ServerException,Status,StatusSchema,TransactionSchema});
package/dist/index.mjs ADDED
@@ -0,0 +1 @@
1
+ var m=class{constructor(t){this.value=t}value;getMobilePaymentUrl(){return`${this.getBaseUrl()}/paymentService`}getPayoutUrl(){return`${this.getBaseUrl()}/merchantPayOutService`}getCheckStatusUrl(t){return`${this.getBaseUrl()}/check/${encodeURIComponent(t)}`}getCardPaymentUrl(){return this.value==="prod"?"https://cardpayment.flexpay.cd/v1.1/pay":"https://beta-cardpayment.flexpay.cd/v1.1/pay"}getBaseUrl(){return this.value==="prod"?"https://backend.flexpay.cd/api/rest/v1":"https://beta-backend.flexpay.cd/api/rest/v1"}};var a=class o extends Error{status;type;constructor(t,n,r){let p=r!=null?`${t} (HTTP ${r}/${n??"unknown"})`:t;super(p),this.name=this.constructor.name,this.status=r??void 0,this.type=n??void 0,Object.setPrototypeOf(this,new.target.prototype)}static create(t,n,r){let p=t.trim()===""?"No message was provided":t;return r===401||r===429?new h(p,n,r):r>=400&&r<=499?new d(p,n,r):r>=500&&r<=599?new S(p,n,r):new o(p,n,r)}},h=class extends a{},d=class extends a{},S=class extends a{};import*as e from"zod";var z=(n=>(n[n.SUCCESS=0]="SUCCESS",n[n.FAILURE=1]="FAILURE",n))(z||{});var y=e.enum(["USD","CDF"]),E=e.object({merchant:e.string().min(1,"Merchant cannot be empty"),token:e.string().min(1,"The authorization token cannot be empty")}),u=e.union([e.literal(0),e.literal(1),e.literal("0"),e.literal("1")]).transform(o=>Number(o)),T=e.union([e.number().finite(),e.string().trim().min(1).transform(o=>Number(o)).pipe(e.number().finite())]),i=e.union([e.string(),e.null()]).optional(),g=e.object({code:u,message:e.string().optional().default(""),orderNumber:i,provider_reference:i,providerReference:i,reference:i,url:i}).transform(o=>({code:o.code,message:o.message??"",orderNumber:o.orderNumber??null,providerReference:o.providerReference??o.provider_reference??null,reference:o.reference??null,url:o.url??null})),C=e.object({code:u,message:e.string().optional().default(""),orderNumber:i,url:i}),j=e.object({amount:T,amountCustomer:T,channel:i,createdAt:e.string(),currency:y,orderNumber:i,reference:e.string(),status:u}),x=e.object({code:u,message:e.string().optional().default(""),transaction:e.union([j,e.null()]).optional().default(null)}),P=e.object({code:u,message:e.string().optional().default(""),orderNumber:i}),c=e.string().min(1),$=e.object({amount:e.number().gt(0,"The transaction amount should be greater than 0"),approveUrl:c.optional().nullable(),callbackUrl:c,cancelUrl:c.optional().nullable(),currency:y,declineUrl:c.optional().nullable(),description:e.string().optional().nullable(),reference:e.string().min(1,"The transaction reference is mandatory")}),w=$.extend({phone:e.string().length(12,"The phone number should be 12 characters long, eg: 243123456789")}),U=e.object({amount:e.number().gt(0),approveUrl:c,callbackUrl:c,cancelUrl:c,currency:y,declineUrl:c,description:e.string().min(1,"The description must be provided"),homeUrl:c,reference:e.string().min(1).max(25,"The reference must be between 1 and 25 characters")}),k=e.object({amount:e.number().gt(0),callbackUrl:c,currency:y,phone:e.string().length(12,"The phone number should be 12 characters long, eg: 243123456789"),reference:e.string().min(1)});var q=class{credential;env;constructor(t,n,r="dev"){this.credential=E.parse({merchant:t,token:n}),this.env=new m(r)}async mobile(t){let n={...w.parse(t),merchant:this.credential.merchant,type:1},r=await this.requestJson("POST",this.env.getMobilePaymentUrl(),n);return this.parseWith(g,r)}async card(t){let n={...U.parse(t),authorization:`Bearer ${this.credential.token}`,merchant:this.credential.merchant},r=await this.requestJson("POST",this.env.getCardPaymentUrl(),n);return this.parseWith(C,r)}async pay(t){if("phone"in t&&typeof t.phone=="string")return this.mobile(t);if("homeUrl"in t&&typeof t.homeUrl=="string")return this.card(t);throw new Error("Unsupported request shape")}async check(t){let n=await this.requestJson("GET",this.env.getCheckStatusUrl(t));return this.parseWith(x,n)}async payout(t){let n={...k.parse(t),merchant:this.credential.merchant,type:1},r=await this.requestJson("POST",this.env.getPayoutUrl(),n);return this.parseWith(P,r)}handleCallback(t){return this.parseWith(g,t)}isSuccessful(t){return t.code===0}async requestJson(t,n,r){let p={Accept:"application/json",Authorization:`Bearer ${this.credential.token}`,"Content-Type":"application/json"},M=3,N=500;for(let f=0;f<=M;f++)try{let s=await fetch(n,{body:t==="POST"?JSON.stringify(r??{}):void 0,headers:p,method:t}),R=(s.headers.get("content-type")||"").includes("application/json"),l={};if(R)try{l=await s.json()}catch{l={}}if(!s.ok){let b=typeof l=="object"&&l!==null?l:{},O=typeof b.message=="string"?b.message:"",B=typeof b.error=="string"?b.error:"unknown",A=s.status;throw a.create(O,B,A)}return l}catch(s){if(s instanceof h||s instanceof d||s instanceof a&&typeof s.status=="number"&&s.status>=400&&s.status<500)throw s;if(f<M){let v=N*2**f;await new Promise(R=>setTimeout(R,v));continue}throw s instanceof a?s:new a(s instanceof Error?s.message:"Network error")}throw new a("Unexpected request flow")}parseWith(t,n){return t.parse(n)}};export{h as AccountException,U as CardRequestSchema,C as CardResponseSchema,x as CheckResponseSchema,q as Client,d as ClientException,y as CurrencySchema,m as Environment,w as MobileRequestSchema,g as MobileResponseSchema,a as NetworkException,k as PayoutRequestSchema,P as PayoutResponseSchema,S as ServerException,z as Status,u as StatusSchema,j as TransactionSchema};
package/package.json ADDED
@@ -0,0 +1,96 @@
1
+ {
2
+ "name": "@ngandu-dev/flexpay",
3
+ "version": "2.0.0",
4
+ "main": "./dist/index.js",
5
+ "types": "./dist/index.d.ts",
6
+ "module": "./dist/index.mjs",
7
+ "type": "commonjs",
8
+ "exports": {
9
+ ".": {
10
+ "import": {
11
+ "types": "./dist/index.d.mts",
12
+ "default": "./dist/index.mjs"
13
+ },
14
+ "require": {
15
+ "types": "./dist/index.d.ts",
16
+ "default": "./dist/index.js"
17
+ },
18
+ "default": "./dist/index.js"
19
+ },
20
+ "./package.json": "./package.json"
21
+ },
22
+ "files": [
23
+ "dist",
24
+ "README.md"
25
+ ],
26
+ "scripts": {
27
+ "build": "tsup --minify",
28
+ "check:package": "publint --strict && attw --pack .",
29
+ "dev": "tsup --watch",
30
+ "format": "biome check --write .",
31
+ "lint": "biome check .",
32
+ "prepack": "bun run build",
33
+ "prepare": "husky",
34
+ "quality": "bun run lint && bun run typecheck && bun run test && bun run build && bun run check:package",
35
+ "release": "npm publish --access public",
36
+ "test": "vitest run",
37
+ "test:coverage": "vitest run --coverage",
38
+ "test:watch": "vitest",
39
+ "typecheck": "tsc --noEmit",
40
+ "version": "changeset version"
41
+ },
42
+ "repository": {
43
+ "type": "git",
44
+ "url": "git+https://github.com/ngandu-dev/flexpay.git"
45
+ },
46
+ "commitlint": {
47
+ "extends": [
48
+ "@commitlint/config-conventional"
49
+ ]
50
+ },
51
+ "keywords": [
52
+ "api",
53
+ "payment-gateway",
54
+ "mobile-money"
55
+ ],
56
+ "author": "Bernard Ngandu <bernard@ngandu.dev>",
57
+ "license": "MIT",
58
+ "bugs": {
59
+ "url": "https://github.com/ngandu-dev/flexpay/issues"
60
+ },
61
+ "homepage": "https://github.com/ngandu-dev/flexpay#readme",
62
+ "description": "FlexPay API TypeScript client",
63
+ "dependencies": {
64
+ "zod": "^4.4.3"
65
+ },
66
+ "devDependencies": {
67
+ "@arethetypeswrong/cli": "^0.18.5",
68
+ "@biomejs/biome": "^2.5.9",
69
+ "@changesets/cli": "^3.0.1",
70
+ "@commitlint/cli": "^21.2.2",
71
+ "@commitlint/config-conventional": "^21.2.2",
72
+ "@types/node": "^26.2.0",
73
+ "@vitest/coverage-v8": "^4.1.11",
74
+ "husky": "^9.1.7",
75
+ "publint": "^0.3.24",
76
+ "tsup": "^8.5.1",
77
+ "typescript": "^5.9.3",
78
+ "vitest": "^4.1.11"
79
+ },
80
+ "overrides": {
81
+ "ajv": "8.18.0",
82
+ "esbuild": "0.28.1",
83
+ "fast-uri": "3.1.5",
84
+ "js-yaml": "4.3.1",
85
+ "nanoid": "3.3.18",
86
+ "picomatch": "4.0.5",
87
+ "postcss": "8.5.26",
88
+ "rollup": "4.62.4",
89
+ "vite": "8.2.1"
90
+ },
91
+ "engines": {
92
+ "node": ">=24"
93
+ },
94
+ "packageManager": "bun@1.3.14",
95
+ "sideEffects": false
96
+ }