@ariary/pay 1.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/README.md ADDED
@@ -0,0 +1,123 @@
1
+ # @ariary/pay
2
+
3
+ Payment and product SDK for the Ariary API.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ yarn add @ariary/pay
9
+ ```
10
+
11
+ ## Setup
12
+
13
+ ```ts
14
+ import Ariari from '@ariary/pay'
15
+
16
+ Ariari.config({
17
+ projectId: 'your-project-id',
18
+ secret: 'your-secret',
19
+ })
20
+ ```
21
+
22
+ ## Products
23
+
24
+ Manage products linked to your project.
25
+
26
+ ```ts
27
+ // create
28
+ const product = await Ariari.products.create({ name: 'Forfait Premium', price: 5000, imgUrl: 'https://example.com/img.png' })
29
+
30
+ // list all
31
+ const products = await Ariari.products.list()
32
+
33
+ // delete
34
+ await Ariari.products.delete(product.id)
35
+ ```
36
+
37
+ ## Create a Payment
38
+
39
+ `Ariari.create()` returns a `Payment` object with the generated payment URL.
40
+
41
+ ```ts
42
+ // with amount
43
+ const payment = await Ariari.create({ amount: 5000 })
44
+
45
+ // with product
46
+ const payment = await Ariari.create({ productId: product.id })
47
+
48
+ // with hooks and metadata
49
+ const payment = await Ariari.create({
50
+ amount: 10000,
51
+ name: 'T-shirt bleu',
52
+ imgUrl: 'https://example.com/img.png',
53
+ hooks: {
54
+ success: 'https://example.com/success',
55
+ failure: 'https://example.com/failure',
56
+ },
57
+ })
58
+ ```
59
+
60
+ ## Track a Payment
61
+
62
+ ```ts
63
+ const payment = await Ariari.create({ amount: 5000 })
64
+
65
+ // check status
66
+ const info = await payment.status()
67
+ // { id, amount, rest, status, parts, url, ... }
68
+
69
+ // apply a ticket code
70
+ const result = await payment.pay('Y-1234')
71
+ ```
72
+
73
+ Payment statuses: `pending` | `incomplete` | `paid` | `failed`
74
+
75
+ A `Status` enum is also exported:
76
+
77
+ ```ts
78
+ import { Status } from '@ariary/pay'
79
+
80
+ if (info.status === Status.PAID) { ... }
81
+ ```
82
+
83
+ ## Retrieve an existing Payment
84
+
85
+ Store `payment.id` to retrieve it later with `.getPayment()`.
86
+
87
+ ```ts
88
+ const payment = await Ariari.create({ amount: 5000 })
89
+ const paymentId = payment.id // save this
90
+
91
+ // later
92
+ const payment = Ariari.getPayment(paymentId)
93
+ const info = await payment.status()
94
+ ```
95
+
96
+ ## Multiple instances
97
+
98
+ Use `name` to manage separate Ariari instances with different credentials. `Ariari.config()` returns the instance directly.
99
+
100
+ ```ts
101
+ const shop = Ariari.config({
102
+ name: 'shop',
103
+ projectId: 'project-a',
104
+ secret: 'secret-a',
105
+ })
106
+
107
+ const billing = Ariari.config({
108
+ name: 'billing',
109
+ projectId: 'project-b',
110
+ secret: 'secret-b',
111
+ })
112
+
113
+ await shop.create({ amount: 5000 })
114
+ await billing.create({ productId: 'prod-123' })
115
+ ```
116
+
117
+ You can also retrieve a named instance later with `Ariari.get()`.
118
+
119
+ ```ts
120
+ const shop = Ariari.get('shop')
121
+ ```
122
+
123
+ Without `name`, the instance defaults to `"main"` and is used by static methods (`Ariari.create`, `Ariari.products`, `Ariari.getPayment`).
@@ -0,0 +1,105 @@
1
+ type Data = {
2
+ body?: Record<string, any>;
3
+ params?: Record<string, any>;
4
+ public?: boolean;
5
+ };
6
+ type PData = Omit<Data, 'body'>;
7
+ type Config = {
8
+ projectId: string;
9
+ secret: string;
10
+ baseUrl?: string;
11
+ };
12
+ declare const createRequester: (config: Config) => {
13
+ get: <T = any>(url: string, data?: PData) => Promise<T>;
14
+ post: <T = any>(url: string, data?: Data) => Promise<T>;
15
+ patch: <T = any>(url: string, data?: Data) => Promise<T>;
16
+ put: <T = any>(url: string, data?: Data) => Promise<T>;
17
+ delete: <T = any>(url: string, data?: PData) => Promise<T>;
18
+ };
19
+
20
+ declare enum Status {
21
+ PENDING = "pending",
22
+ INCOMPLETE = "incomplete",
23
+ PAID = "paid",
24
+ FAILED = "failed"
25
+ }
26
+ type Hooks = {
27
+ success?: string;
28
+ progress?: string;
29
+ failure?: string;
30
+ };
31
+ type CreatePaymentData = {
32
+ amount?: number;
33
+ productId?: string;
34
+ name?: string;
35
+ imgUrl?: string;
36
+ hooks?: Hooks;
37
+ };
38
+ type PaymentPart = {
39
+ id: string;
40
+ amount: number;
41
+ transactionId: string;
42
+ date: string;
43
+ };
44
+ type PaymentResponse = {
45
+ id: string;
46
+ amount: number;
47
+ rest: number;
48
+ productId?: string;
49
+ name?: string;
50
+ imgUrl?: string;
51
+ status: Status;
52
+ parts: PaymentPart[];
53
+ createdAt: string;
54
+ updatedAt: string;
55
+ url: string;
56
+ };
57
+ type CreateProductData = {
58
+ name: string;
59
+ price: number;
60
+ imgUrl?: string;
61
+ };
62
+ type Product = {
63
+ id: string;
64
+ name: string;
65
+ price: number;
66
+ imgUrl?: string;
67
+ createdAt: string;
68
+ updatedAt: string;
69
+ };
70
+
71
+ declare class Payment {
72
+ id: string;
73
+ private _requester;
74
+ constructor(id: string, requester: ReturnType<typeof createRequester>);
75
+ status(): Promise<PaymentResponse>;
76
+ pay(ticketCode: string): Promise<PaymentResponse>;
77
+ }
78
+ declare class Ariari {
79
+ private _config;
80
+ private _requester;
81
+ private constructor();
82
+ static config: (nameAndConfig: Config & {
83
+ name?: string;
84
+ }) => Ariari;
85
+ static init: (nameAndConfig: Config & {
86
+ name?: string;
87
+ }) => Ariari;
88
+ static get: (name?: string) => Ariari;
89
+ create: (data: CreatePaymentData) => Promise<Payment>;
90
+ getPayment: (id: string) => Payment;
91
+ products: {
92
+ create: (data: CreateProductData) => Promise<Product>;
93
+ list: () => Promise<Product[]>;
94
+ delete: (id: string) => Promise<void>;
95
+ };
96
+ static create: (data: CreatePaymentData) => Promise<Payment>;
97
+ static getPayment: (id: string) => Payment;
98
+ static products: {
99
+ create: (data: CreateProductData) => Promise<Product>;
100
+ list: () => Promise<Product[]>;
101
+ delete: (id: string) => Promise<void>;
102
+ };
103
+ }
104
+
105
+ export { Status, Ariari as default };
@@ -0,0 +1,105 @@
1
+ type Data = {
2
+ body?: Record<string, any>;
3
+ params?: Record<string, any>;
4
+ public?: boolean;
5
+ };
6
+ type PData = Omit<Data, 'body'>;
7
+ type Config = {
8
+ projectId: string;
9
+ secret: string;
10
+ baseUrl?: string;
11
+ };
12
+ declare const createRequester: (config: Config) => {
13
+ get: <T = any>(url: string, data?: PData) => Promise<T>;
14
+ post: <T = any>(url: string, data?: Data) => Promise<T>;
15
+ patch: <T = any>(url: string, data?: Data) => Promise<T>;
16
+ put: <T = any>(url: string, data?: Data) => Promise<T>;
17
+ delete: <T = any>(url: string, data?: PData) => Promise<T>;
18
+ };
19
+
20
+ declare enum Status {
21
+ PENDING = "pending",
22
+ INCOMPLETE = "incomplete",
23
+ PAID = "paid",
24
+ FAILED = "failed"
25
+ }
26
+ type Hooks = {
27
+ success?: string;
28
+ progress?: string;
29
+ failure?: string;
30
+ };
31
+ type CreatePaymentData = {
32
+ amount?: number;
33
+ productId?: string;
34
+ name?: string;
35
+ imgUrl?: string;
36
+ hooks?: Hooks;
37
+ };
38
+ type PaymentPart = {
39
+ id: string;
40
+ amount: number;
41
+ transactionId: string;
42
+ date: string;
43
+ };
44
+ type PaymentResponse = {
45
+ id: string;
46
+ amount: number;
47
+ rest: number;
48
+ productId?: string;
49
+ name?: string;
50
+ imgUrl?: string;
51
+ status: Status;
52
+ parts: PaymentPart[];
53
+ createdAt: string;
54
+ updatedAt: string;
55
+ url: string;
56
+ };
57
+ type CreateProductData = {
58
+ name: string;
59
+ price: number;
60
+ imgUrl?: string;
61
+ };
62
+ type Product = {
63
+ id: string;
64
+ name: string;
65
+ price: number;
66
+ imgUrl?: string;
67
+ createdAt: string;
68
+ updatedAt: string;
69
+ };
70
+
71
+ declare class Payment {
72
+ id: string;
73
+ private _requester;
74
+ constructor(id: string, requester: ReturnType<typeof createRequester>);
75
+ status(): Promise<PaymentResponse>;
76
+ pay(ticketCode: string): Promise<PaymentResponse>;
77
+ }
78
+ declare class Ariari {
79
+ private _config;
80
+ private _requester;
81
+ private constructor();
82
+ static config: (nameAndConfig: Config & {
83
+ name?: string;
84
+ }) => Ariari;
85
+ static init: (nameAndConfig: Config & {
86
+ name?: string;
87
+ }) => Ariari;
88
+ static get: (name?: string) => Ariari;
89
+ create: (data: CreatePaymentData) => Promise<Payment>;
90
+ getPayment: (id: string) => Payment;
91
+ products: {
92
+ create: (data: CreateProductData) => Promise<Product>;
93
+ list: () => Promise<Product[]>;
94
+ delete: (id: string) => Promise<void>;
95
+ };
96
+ static create: (data: CreatePaymentData) => Promise<Payment>;
97
+ static getPayment: (id: string) => Payment;
98
+ static products: {
99
+ create: (data: CreateProductData) => Promise<Product>;
100
+ list: () => Promise<Product[]>;
101
+ delete: (id: string) => Promise<void>;
102
+ };
103
+ }
104
+
105
+ export { Status, Ariari as default };
package/dist/index.js ADDED
@@ -0,0 +1,132 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ Status: () => Status,
24
+ default: () => src_default
25
+ });
26
+ module.exports = __toCommonJS(src_exports);
27
+
28
+ // ../common/http.ts
29
+ async function request(verb, endpoint, options, config) {
30
+ if (!config) throw new Error("Ariari package not configured.");
31
+ const url = `${config.baseUrl || "https://back.ariari.mg"}${endpoint}`;
32
+ const headers = { "Content-Type": "application/json" };
33
+ if (!options.public) {
34
+ headers["x-project-id"] = config.projectId;
35
+ headers["x-secret"] = config.secret;
36
+ }
37
+ const fetchOptions = { method: verb, headers };
38
+ if ("body" in options && options.body) {
39
+ fetchOptions.body = JSON.stringify(options.body);
40
+ }
41
+ try {
42
+ const response = await fetch(url, fetchOptions);
43
+ const data = await response.json();
44
+ if (!response.ok) {
45
+ const message = data?.message || "Unknown error";
46
+ throw new Error(`[${response.status}] ${message}`);
47
+ }
48
+ return data;
49
+ } catch (error) {
50
+ if (error instanceof Error) {
51
+ throw error;
52
+ }
53
+ throw new Error("Network error");
54
+ }
55
+ }
56
+ var createRequester = (config) => ({
57
+ get: (url, data = {}) => request("GET", url, data, config),
58
+ post: (url, data = {}) => request("POST", url, data, config),
59
+ patch: (url, data = {}) => request("PATCH", url, data, config),
60
+ put: (url, data = {}) => request("PUT", url, data, config),
61
+ delete: (url, data = {}) => request("DELETE", url, data, config)
62
+ });
63
+
64
+ // src/types/index.ts
65
+ var Status = /* @__PURE__ */ ((Status2) => {
66
+ Status2["PENDING"] = "pending";
67
+ Status2["INCOMPLETE"] = "incomplete";
68
+ Status2["PAID"] = "paid";
69
+ Status2["FAILED"] = "failed";
70
+ return Status2;
71
+ })(Status || {});
72
+
73
+ // src/index.ts
74
+ var instances = {};
75
+ var Payment = class {
76
+ constructor(id, requester) {
77
+ this.id = id;
78
+ this._requester = requester;
79
+ }
80
+ async status() {
81
+ const res = await this._requester.get(`/api/payments/${this.id}`);
82
+ return res.data;
83
+ }
84
+ async pay(ticketCode) {
85
+ const res = await this._requester.put(`/api/payments/${this.id}/pay`, { body: { ticketCode } });
86
+ return res.data;
87
+ }
88
+ };
89
+ var _Ariari = class _Ariari {
90
+ constructor({ name = "main", ...config }) {
91
+ this.create = async (data) => {
92
+ const res = await this._requester.post("/api/payments", { body: data });
93
+ return new Payment(res.id, this._requester);
94
+ };
95
+ this.getPayment = (id) => new Payment(id, this._requester);
96
+ this.products = {
97
+ create: async (data) => {
98
+ const res = await this._requester.post("/api/products", { body: data });
99
+ return res.data;
100
+ },
101
+ list: async () => {
102
+ const res = await this._requester.get("/api/products");
103
+ return res.data;
104
+ },
105
+ delete: async (id) => {
106
+ await this._requester.delete(`/api/products/${id}`);
107
+ }
108
+ };
109
+ this._config = config;
110
+ this._requester = createRequester(config);
111
+ instances[name] = this;
112
+ }
113
+ };
114
+ _Ariari.config = (nameAndConfig) => new _Ariari(nameAndConfig);
115
+ _Ariari.init = _Ariari.config;
116
+ _Ariari.get = (name = "main") => {
117
+ if (!instances[name]) throw new Error(`The instance of Ariari with the name "${name}" doesn't exist yet. Call Ariari.config(...)`);
118
+ return instances[name];
119
+ };
120
+ _Ariari.create = async (data) => await _Ariari.get("main")?.create(data);
121
+ _Ariari.getPayment = (id) => _Ariari.get("main")?.getPayment(id);
122
+ _Ariari.products = {
123
+ create: async (data) => await _Ariari.get("main")?.products.create(data),
124
+ list: async () => await _Ariari.get("main")?.products.list(),
125
+ delete: async (id) => await _Ariari.get("main")?.products.delete(id)
126
+ };
127
+ var Ariari = _Ariari;
128
+ var src_default = Ariari;
129
+ // Annotate the CommonJS export names for ESM import in node:
130
+ 0 && (module.exports = {
131
+ Status
132
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,105 @@
1
+ // ../common/http.ts
2
+ async function request(verb, endpoint, options, config) {
3
+ if (!config) throw new Error("Ariari package not configured.");
4
+ const url = `${config.baseUrl || "https://back.ariari.mg"}${endpoint}`;
5
+ const headers = { "Content-Type": "application/json" };
6
+ if (!options.public) {
7
+ headers["x-project-id"] = config.projectId;
8
+ headers["x-secret"] = config.secret;
9
+ }
10
+ const fetchOptions = { method: verb, headers };
11
+ if ("body" in options && options.body) {
12
+ fetchOptions.body = JSON.stringify(options.body);
13
+ }
14
+ try {
15
+ const response = await fetch(url, fetchOptions);
16
+ const data = await response.json();
17
+ if (!response.ok) {
18
+ const message = data?.message || "Unknown error";
19
+ throw new Error(`[${response.status}] ${message}`);
20
+ }
21
+ return data;
22
+ } catch (error) {
23
+ if (error instanceof Error) {
24
+ throw error;
25
+ }
26
+ throw new Error("Network error");
27
+ }
28
+ }
29
+ var createRequester = (config) => ({
30
+ get: (url, data = {}) => request("GET", url, data, config),
31
+ post: (url, data = {}) => request("POST", url, data, config),
32
+ patch: (url, data = {}) => request("PATCH", url, data, config),
33
+ put: (url, data = {}) => request("PUT", url, data, config),
34
+ delete: (url, data = {}) => request("DELETE", url, data, config)
35
+ });
36
+
37
+ // src/types/index.ts
38
+ var Status = /* @__PURE__ */ ((Status2) => {
39
+ Status2["PENDING"] = "pending";
40
+ Status2["INCOMPLETE"] = "incomplete";
41
+ Status2["PAID"] = "paid";
42
+ Status2["FAILED"] = "failed";
43
+ return Status2;
44
+ })(Status || {});
45
+
46
+ // src/index.ts
47
+ var instances = {};
48
+ var Payment = class {
49
+ constructor(id, requester) {
50
+ this.id = id;
51
+ this._requester = requester;
52
+ }
53
+ async status() {
54
+ const res = await this._requester.get(`/api/payments/${this.id}`);
55
+ return res.data;
56
+ }
57
+ async pay(ticketCode) {
58
+ const res = await this._requester.put(`/api/payments/${this.id}/pay`, { body: { ticketCode } });
59
+ return res.data;
60
+ }
61
+ };
62
+ var _Ariari = class _Ariari {
63
+ constructor({ name = "main", ...config }) {
64
+ this.create = async (data) => {
65
+ const res = await this._requester.post("/api/payments", { body: data });
66
+ return new Payment(res.id, this._requester);
67
+ };
68
+ this.getPayment = (id) => new Payment(id, this._requester);
69
+ this.products = {
70
+ create: async (data) => {
71
+ const res = await this._requester.post("/api/products", { body: data });
72
+ return res.data;
73
+ },
74
+ list: async () => {
75
+ const res = await this._requester.get("/api/products");
76
+ return res.data;
77
+ },
78
+ delete: async (id) => {
79
+ await this._requester.delete(`/api/products/${id}`);
80
+ }
81
+ };
82
+ this._config = config;
83
+ this._requester = createRequester(config);
84
+ instances[name] = this;
85
+ }
86
+ };
87
+ _Ariari.config = (nameAndConfig) => new _Ariari(nameAndConfig);
88
+ _Ariari.init = _Ariari.config;
89
+ _Ariari.get = (name = "main") => {
90
+ if (!instances[name]) throw new Error(`The instance of Ariari with the name "${name}" doesn't exist yet. Call Ariari.config(...)`);
91
+ return instances[name];
92
+ };
93
+ _Ariari.create = async (data) => await _Ariari.get("main")?.create(data);
94
+ _Ariari.getPayment = (id) => _Ariari.get("main")?.getPayment(id);
95
+ _Ariari.products = {
96
+ create: async (data) => await _Ariari.get("main")?.products.create(data),
97
+ list: async () => await _Ariari.get("main")?.products.list(),
98
+ delete: async (id) => await _Ariari.get("main")?.products.delete(id)
99
+ };
100
+ var Ariari = _Ariari;
101
+ var src_default = Ariari;
102
+ export {
103
+ Status,
104
+ src_default as default
105
+ };
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@ariary/pay",
3
+ "version": "1.0.0",
4
+ "description": "Payment SDK pour l'API Ariary",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.js"
13
+ }
14
+ },
15
+ "files": [
16
+ "dist"
17
+ ],
18
+ "scripts": {
19
+ "build": "tsup",
20
+ "test": "node --env-file=.env node_modules/.bin/tsx test.ts"
21
+ },
22
+ "publishConfig": {
23
+ "access": "public"
24
+ },
25
+ "author": "Ariary",
26
+ "license": "ISC",
27
+ "dependencies": {},
28
+ "devDependencies": {
29
+ "@types/node": "^25.3.0",
30
+ "tsup": "^8.5.1",
31
+ "tsx": "^4.21.0",
32
+ "typescript": "^5.9.3"
33
+ }
34
+ }