@vindhq/sloud-payment-sdk 1.0.9 → 1.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.
@@ -0,0 +1,297 @@
1
+ /**
2
+ * Common fields shared across all payment types
3
+ */
4
+ /**
5
+ * Data passed to the onSuccess callback after a successful payment
6
+ */
7
+ interface PaymentSuccessData {
8
+ reference: string;
9
+ amount: number;
10
+ currency: string;
11
+ }
12
+ interface BasePaymentWidgetOptions {
13
+ public_key: string;
14
+ isLocalEnv?: boolean;
15
+ transaction_id?: string;
16
+ customer_id?: string;
17
+ business_id?: string;
18
+ /** Called when a payment completes successfully */
19
+ onSuccess?: (data: PaymentSuccessData) => void;
20
+ }
21
+ interface Customer {
22
+ first_name: string;
23
+ last_name: string;
24
+ phone_number: string;
25
+ email: string;
26
+ }
27
+ interface DeliveryDetails {
28
+ street: string;
29
+ city: string;
30
+ state: string;
31
+ country: string;
32
+ note?: string;
33
+ }
34
+ interface Product {
35
+ product_id: string;
36
+ quantity: number;
37
+ variation?: string[];
38
+ selected_variation_id?: string;
39
+ }
40
+ /**
41
+ * String literal types for payment modes
42
+ * These provide better IntelliSense suggestions
43
+ */
44
+ type PaymentType = "external" | "storefront" | "sloudfront";
45
+ /**
46
+ * String literal types for channel options
47
+ * These provide better IntelliSense suggestions
48
+ */
49
+ type ChannelType = "pickup" | "delivery";
50
+ /**
51
+ * Enum for backward compatibility and convenience
52
+ * Use either string literals or enum values
53
+ */
54
+ declare enum ModeType {
55
+ Storefront = "storefront",
56
+ External = "external",
57
+ Sloudfront = "sloudfront"
58
+ }
59
+ /**
60
+ * Options for external payment type
61
+ * Used for simple payments with customer details
62
+ */
63
+ interface ExternalPaymentWidgetOptions extends BasePaymentWidgetOptions {
64
+ type: "external";
65
+ amount: number;
66
+ customer: Customer;
67
+ }
68
+ /**
69
+ * Base options for storefront payment type (shared fields)
70
+ */
71
+ interface BaseStorefrontPaymentWidgetOptions extends BasePaymentWidgetOptions {
72
+ type: "storefront";
73
+ slug: string;
74
+ amount: number;
75
+ discount?: number;
76
+ customer: Customer;
77
+ products: Product[];
78
+ }
79
+ /**
80
+ * Storefront options when channel is 'pickup' - delivery_details is optional
81
+ */
82
+ interface StorefrontPickupOptions extends BaseStorefrontPaymentWidgetOptions {
83
+ channel: "pickup";
84
+ delivery_details?: DeliveryDetails;
85
+ }
86
+ /**
87
+ * Storefront options when channel is 'delivery' - delivery_details is required
88
+ */
89
+ interface StorefrontDeliveryOptions extends BaseStorefrontPaymentWidgetOptions {
90
+ channel: "delivery";
91
+ delivery_details: DeliveryDetails;
92
+ }
93
+ /**
94
+ * Options for storefront payment type
95
+ * Used for e-commerce orders with products and delivery
96
+ * delivery_details is required only when channel is 'delivery'
97
+ */
98
+ type StorefrontPaymentWidgetOptions = StorefrontPickupOptions | StorefrontDeliveryOptions;
99
+ /**
100
+ * Base options for sloudfront payment type (shared fields)
101
+ */
102
+ interface BaseSloudfrontPaymentWidgetOptions extends BasePaymentWidgetOptions {
103
+ type: "sloudfront";
104
+ amount: number;
105
+ discount?: number;
106
+ customer: Customer;
107
+ products: Product[];
108
+ transaction_id?: string;
109
+ customer_id: string;
110
+ business_id: string;
111
+ }
112
+ /**
113
+ * Sloudfront options when channel is 'pickup' - delivery_details is optional
114
+ */
115
+ interface SloudfrontPickupOptions extends BaseSloudfrontPaymentWidgetOptions {
116
+ channel: "pickup";
117
+ delivery_details?: DeliveryDetails;
118
+ }
119
+ /**
120
+ * Sloudfront options when channel is 'delivery' - delivery_details is required
121
+ */
122
+ interface SloudfrontDeliveryOptions extends BaseSloudfrontPaymentWidgetOptions {
123
+ channel: "delivery";
124
+ delivery_details: DeliveryDetails;
125
+ }
126
+ /**
127
+ * Options for sloudfront payment type
128
+ * delivery_details is required only when channel is 'delivery'
129
+ */
130
+ type SloudfrontPaymentWidgetOptions = SloudfrontPickupOptions | SloudfrontDeliveryOptions;
131
+ /**
132
+ * Discriminated union of all payment widget option types
133
+ * TypeScript will enforce that the correct fields are provided based on the type
134
+ */
135
+ type PaymentWidgetOptions = ExternalPaymentWidgetOptions | StorefrontPaymentWidgetOptions | SloudfrontPaymentWidgetOptions;
136
+
137
+ declare class PaymentWidget {
138
+ private options;
139
+ private container;
140
+ private root;
141
+ constructor(options: PaymentWidgetOptions);
142
+ /**
143
+ * Creates the container div if it doesn’t exist.
144
+ */
145
+ private ensureContainer;
146
+ /**
147
+ * Renders the widget popup.
148
+ */
149
+ showPopup(): void;
150
+ /**
151
+ * Optionally allow hiding/unmounting the widget.
152
+ */
153
+ hidePopup(): void;
154
+ }
155
+
156
+ declare class PaymentWidgetValidationError extends Error {
157
+ constructor(message: string);
158
+ }
159
+ /**
160
+ * Validates payment widget options based on the type
161
+ * @throws {PaymentWidgetValidationError} if validation fails
162
+ */
163
+ declare function validatePaymentWidgetOptions(options: PaymentWidgetOptions): void;
164
+
165
+ interface IExternalPaymentPayload {
166
+ type: "external";
167
+ amount: number;
168
+ customer: {
169
+ first_name: string;
170
+ last_name: string;
171
+ phone_number: string;
172
+ email: string;
173
+ };
174
+ }
175
+ interface IStorefrontPaymentPayload {
176
+ type: "storefront";
177
+ slug: string;
178
+ order: {
179
+ additional_amount?: number;
180
+ discount?: number;
181
+ items: Array<{
182
+ product_id: string;
183
+ quantity: number;
184
+ variation?: string[];
185
+ selected_variation_id?: string;
186
+ }>;
187
+ payment_method?: string;
188
+ channel?: string;
189
+ buyer_details: {
190
+ first_name: string;
191
+ last_name: string;
192
+ email: string;
193
+ phone_number: string;
194
+ };
195
+ delivery_details?: {
196
+ street: string;
197
+ city: string;
198
+ state: string;
199
+ country: string;
200
+ note?: string;
201
+ };
202
+ };
203
+ }
204
+ interface ISloudfrontPaymentPayload {
205
+ type: "sloudfront";
206
+ manual_order: {
207
+ additional_amount?: number;
208
+ discount?: number;
209
+ items: Array<{
210
+ product_id: string;
211
+ quantity: number;
212
+ variation?: string[];
213
+ selected_variation_id?: string;
214
+ }>;
215
+ payment_method?: string;
216
+ channel?: string;
217
+ buyer_details?: {
218
+ first_name: string;
219
+ last_name: string;
220
+ email: string;
221
+ phone_number: string;
222
+ };
223
+ delivery_details?: {
224
+ street: string;
225
+ city: string;
226
+ state: string;
227
+ country: string;
228
+ note?: string;
229
+ };
230
+ transaction_id?: string;
231
+ customer_id: string;
232
+ business_id: string;
233
+ };
234
+ }
235
+ type IGeneratePaymentInstrumentPayload = IExternalPaymentPayload | IStorefrontPaymentPayload | ISloudfrontPaymentPayload;
236
+
237
+ /**
238
+ * Base interface for payload builders
239
+ */
240
+ interface PayloadBuilder<T extends IGeneratePaymentInstrumentPayload> {
241
+ build(options: PaymentWidgetOptions, payment_method?: string): T;
242
+ }
243
+ /**
244
+ * Factory function to build payment instrument payload based on options type
245
+ *
246
+ * @param options - Payment widget options
247
+ * @returns Constructed payload for the payment instrument API
248
+ * @throws Error if the payment type is not supported
249
+ *
250
+ * @example
251
+ * // External payment
252
+ * const payload = buildPaymentInstrumentPayload({
253
+ * type: "external",
254
+ * amount: 25000,
255
+ * // ... other options
256
+ * });
257
+ *
258
+ * @example
259
+ * // Storefront payment
260
+ * const payload = buildPaymentInstrumentPayload({
261
+ * type: "storefront",
262
+ * slug: "my-store",
263
+ * products: [...],
264
+ * // ... other options
265
+ * });
266
+ */
267
+ declare function buildPaymentInstrumentPayload(options: PaymentWidgetOptions, payment_method?: string): IGeneratePaymentInstrumentPayload;
268
+ /**
269
+ * Register a new payload builder for a custom payment type
270
+ * This allows for extending the system with new payment types without modifying existing code
271
+ *
272
+ * @param type - The payment type identifier
273
+ * @param builder - The payload builder instance
274
+ *
275
+ * @example
276
+ * class SubscriptionPaymentPayloadBuilder implements PayloadBuilder<ISubscriptionPaymentPayload> {
277
+ * build(options: PaymentWidgetOptions): ISubscriptionPaymentPayload {
278
+ * return {
279
+ * type: "subscription",
280
+ * plan_id: options.plan_id,
281
+ * // ... other fields
282
+ * };
283
+ * }
284
+ * }
285
+ *
286
+ * registerPayloadBuilder("subscription", new SubscriptionPaymentPayloadBuilder());
287
+ */
288
+ declare function registerPayloadBuilder(type: string, builder: PayloadBuilder<IGeneratePaymentInstrumentPayload>): void;
289
+ /**
290
+ * Get all registered payment types
291
+ *
292
+ * @returns Array of registered payment type identifiers
293
+ */
294
+ declare function getRegisteredPaymentTypes(): string[];
295
+
296
+ export { ModeType, PaymentWidgetValidationError, buildPaymentInstrumentPayload, PaymentWidget as default, getRegisteredPaymentTypes, registerPayloadBuilder, validatePaymentWidgetOptions };
297
+ export type { ChannelType, ExternalPaymentWidgetOptions, PaymentSuccessData, PaymentType, PaymentWidgetOptions, SloudfrontPaymentWidgetOptions, StorefrontPaymentWidgetOptions };