mmpay-browser-sdk 1.0.3 → 1.0.5

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/dist/cjs/index.js CHANGED
@@ -2,14 +2,18 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MMPaySDK = void 0;
4
4
  class MMPaySDK {
5
+ /**
6
+ * constructor
7
+ * @param publishableKey
8
+ * @param options
9
+ */
5
10
  constructor(publishableKey, options = {}) {
6
11
  this.pollIntervalId = undefined;
7
12
  this.onCompleteCallback = null;
8
13
  this.overlayElement = null;
9
- // Properties to store pending data for re-rendering after cancel attempt
10
14
  this.pendingApiResponse = null;
11
15
  this.pendingPaymentPayload = null;
12
- this.QR_SIZE = 300;
16
+ this.QR_SIZE = 290;
13
17
  if (!publishableKey) {
14
18
  throw new Error("A Publishable Key is required to initialize [MMPaySDK].");
15
19
  }
@@ -17,7 +21,7 @@ class MMPaySDK {
17
21
  this.environment = options.environment || 'production';
18
22
  this.baseUrl = options.baseUrl || 'https://api.mm-pay.com';
19
23
  this.merchantName = options.merchantName || 'Your Merchant';
20
- this.POLL_INTERVAL_MS = options.pollInterval || 3000;
24
+ this.POLL_INTERVAL_MS = options.pollInterval || 5000;
21
25
  }
22
26
  /**
23
27
  * _callApi
@@ -26,12 +30,20 @@ class MMPaySDK {
26
30
  * @returns
27
31
  */
28
32
  async _callApi(endpoint, data = {}) {
33
+ let config = {
34
+ 'Content-Type': 'application/json',
35
+ 'Authorization': `Bearer ${this.publishableKey}`
36
+ };
37
+ if (this.tokenKey) {
38
+ config = {
39
+ 'Content-Type': 'application/json',
40
+ 'Authorization': `Bearer ${this.publishableKey}`,
41
+ 'X-MMPay-Btoken': `${this.tokenKey}`
42
+ };
43
+ }
29
44
  const response = await fetch(`${this.baseUrl}${endpoint}`, {
30
45
  method: 'POST',
31
- headers: {
32
- 'Content-Type': 'application/json',
33
- 'Authorization': `Bearer ${this.publishableKey}`
34
- },
46
+ headers: config,
35
47
  body: JSON.stringify(data)
36
48
  });
37
49
  if (!response.ok) {
@@ -41,11 +53,38 @@ class MMPaySDK {
41
53
  return response.json();
42
54
  }
43
55
  /**
44
- * createPaymentRequest
45
- * @param {PaymentData} payload
46
- * @returns
56
+ * _callApiTokenRequest
57
+ * @param {ICreateTokenRequestParams} payload
58
+ * @param {number} payload.amount
59
+ * @param {string} payload.currency
60
+ * @param {string} payload.orderId
61
+ * @param {string} payload.nonce
62
+ * @param {string} payload.callbackUrl
63
+ * @returns {Promise<ICreateTokenResponse>}
64
+ */
65
+ async _callApiTokenRequest(payload) {
66
+ try {
67
+ const endpoint = this.environment === 'sandbox'
68
+ ? '/xpayments/sandbox-token-request'
69
+ : '/xpayments/production-token-request';
70
+ return await this._callApi(endpoint, payload);
71
+ }
72
+ catch (error) {
73
+ console.error("Token request failed:", error);
74
+ throw error;
75
+ }
76
+ }
77
+ /**
78
+ * _callApiPaymentRequest
79
+ * @param {ICreatePaymentRequestParams} payload
80
+ * @param {number} payload.amount
81
+ * @param {string} payload.currency
82
+ * @param {string} payload.orderId
83
+ * @param {string} payload.nonce
84
+ * @param {string} payload.callbackUrl
85
+ * @returns {Promise<ICreatePaymentResponse>}
47
86
  */
48
- async createPaymentRequest(payload) {
87
+ async _callApiPaymentRequest(payload) {
49
88
  try {
50
89
  const endpoint = this.environment === 'sandbox'
51
90
  ? '/xpayments/sandbox-payment-create'
@@ -57,10 +96,75 @@ class MMPaySDK {
57
96
  throw error;
58
97
  }
59
98
  }
99
+ /**
100
+ * createPayment
101
+ * @param {ICorePayParams} params
102
+ * @param {number} params.amount
103
+ * @param {string} params.orderId
104
+ * @param {string} params.callbackUrl
105
+ * @returns {Promise<ICreatePaymentResponse>}
106
+ */
107
+ async createPayment(params) {
108
+ const payload = {
109
+ amount: params.amount,
110
+ orderId: params.orderId,
111
+ callbackUrl: params.callbackUrl,
112
+ currency: 'MMK',
113
+ nonce: new Date().getTime().toString() + '_mmp'
114
+ };
115
+ try {
116
+ const tokenResponse = await this._callApiTokenRequest(payload);
117
+ this.tokenKey = tokenResponse.token;
118
+ const apiResponse = await this._callApiPaymentRequest(payload);
119
+ return apiResponse;
120
+ }
121
+ catch (error) {
122
+ console.error("Payment request failed:", error);
123
+ throw error;
124
+ }
125
+ }
126
+ /**
127
+ * showPaymentModal
128
+ * @param {ICorePayParams} params
129
+ * @param {number} params.amount
130
+ * @param {string} params.orderId
131
+ * @param {string} params.callbackUrl
132
+ * @param {Function} onComplete
133
+ */
134
+ async showPaymentModal(params, onComplete) {
135
+ const initialContent = `<div class="mmpay-overlay-content"><div style="text-align: center; color: #fff;">ငွေပေးချေမှု စတင်နေသည်...</div></div>`;
136
+ this._createAndRenderModal(initialContent, false);
137
+ this.onCompleteCallback = onComplete;
138
+ const payload = {
139
+ amount: params.amount,
140
+ orderId: params.orderId,
141
+ callbackUrl: params.callbackUrl,
142
+ currency: 'MMK',
143
+ nonce: new Date().getTime().toString() + '_mmp'
144
+ };
145
+ try {
146
+ const tokenResponse = await this._callApiTokenRequest(payload);
147
+ this.tokenKey = tokenResponse.token;
148
+ const apiResponse = await this._callApiPaymentRequest(payload);
149
+ if (apiResponse && apiResponse.qr && apiResponse.transactionRefId) {
150
+ this.pendingApiResponse = apiResponse;
151
+ this.pendingPaymentPayload = payload;
152
+ this._renderQrModalContent(apiResponse, payload, this.merchantName);
153
+ this._startPolling(payload, onComplete);
154
+ }
155
+ else {
156
+ this._showTerminalMessage(apiResponse.orderId || 'N/A', 'FAILED', 'ငွေပေးချေမှု စတင်ရန် မအောင်မြင်ပါ။ QR ဒေတာ မရရှိပါ။');
157
+ }
158
+ }
159
+ catch (error) {
160
+ this.tokenKey = null;
161
+ this._showTerminalMessage(payload.orderId || 'N/A', 'FAILED', 'ငွေပေးချေမှု စတင်စဉ် အမှားအယွင်း ဖြစ်ပွားသည်။ ကွန်ဆိုးလ်တွင် ကြည့်ပါ။');
162
+ }
163
+ }
60
164
  /**
61
165
  * _createAndRenderModal
62
166
  * @param {string} contentHtml
63
- * @param isTerminal
167
+ * @param {boolean} isTerminal
64
168
  * @returns
65
169
  */
66
170
  _createAndRenderModal(contentHtml, isTerminal = false) {
@@ -72,7 +176,6 @@ class MMPaySDK {
72
176
  const style = document.createElement('style');
73
177
  style.innerHTML = `
74
178
  @import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700;800&family=Padauk:wght@400;700&display=swap');
75
-
76
179
  #mmpay-full-modal {
77
180
  position: fixed;
78
181
  top: 0;
@@ -172,41 +275,15 @@ class MMPaySDK {
172
275
  document.body.style.overflow = 'hidden'; // FIX: Prevent body scroll when modal is open
173
276
  return overlay;
174
277
  }
175
- /**
176
- * showPaymentModal
177
- * @param {PaymentData} payload
178
- * @param {Function} onComplete
179
- */
180
- async showPaymentModal(payload, onComplete) {
181
- const initialContent = `<div class="mmpay-overlay-content"><div style="text-align: center; color: #fff;">ငွေပေးချေမှု စတင်နေသည်...</div></div>`;
182
- this._createAndRenderModal(initialContent, false);
183
- this.onCompleteCallback = onComplete;
184
- try {
185
- const apiResponse = await this.createPaymentRequest(payload);
186
- if (apiResponse && apiResponse.qr && apiResponse.transactionId) {
187
- this.pendingApiResponse = apiResponse;
188
- this.pendingPaymentPayload = payload;
189
- this._renderQrModalContent(apiResponse, payload, this.merchantName);
190
- this._startPolling(apiResponse._id, onComplete);
191
- }
192
- else {
193
- this._showTerminalMessage(apiResponse.orderId || 'N/A', 'FAILED', 'ငွေပေးချေမှု စတင်ရန် မအောင်မြင်ပါ။ QR ဒေတာ မရရှိပါ။');
194
- }
195
- }
196
- catch (error) {
197
- // Myanmar translation for "Error during payment initiation. See console."
198
- this._showTerminalMessage(payload.orderId || 'N/A', 'FAILED', 'ငွေပေးချေမှု စတင်စဉ် အမှားအယွင်း ဖြစ်ပွားသည်။ ကွန်ဆိုးလ်တွင် ကြည့်ပါ။');
199
- }
200
- }
201
278
  /**
202
279
  * _renderQrModalContent
203
- * @param {CreatePaymentResponse} apiResponse
204
- * @param {PaymentData} payload
280
+ * @param {ICreatePaymentResponse} apiResponse
281
+ * @param {CreatePaymentRequest} payload
205
282
  * @param {string} merchantName
206
283
  */
207
284
  _renderQrModalContent(apiResponse, payload, merchantName) {
208
285
  const qrData = apiResponse.qr;
209
- const amountDisplay = `${apiResponse.amount.toFixed(2)} ${apiResponse.currency}`;
286
+ const amountDisplay = `${apiResponse.amount.toFixed(2)} MMK`;
210
287
  const qrCanvasId = 'mmpayQrCanvas';
211
288
  const orderId = payload.orderId;
212
289
  window.MMPayDownloadQR = function () {
@@ -273,7 +350,7 @@ class MMPaySDK {
273
350
  <span class="mmpay-text-myanmar">မှာယူမှုနံပါတ်:</span> <strong>${apiResponse.orderId}</strong>
274
351
  </div>
275
352
  <div class="mmpay-detail">
276
- <span class="mmpay-text-myanmar">ငွေပေးငွေယူနံပါတ်:</span> <strong>${apiResponse.transactionId}</strong>
353
+ <span class="mmpay-text-myanmar">ငွေပေးငွေယူနံပါတ်:</span> <strong>${apiResponse.transactionRefId}</strong>
277
354
  </div>
278
355
 
279
356
  <p class="mmpay-warning mmpay-text-myanmar">
@@ -382,7 +459,7 @@ class MMPaySDK {
382
459
  }
383
460
  /**
384
461
  * Cleans up the modal and stops polling.
385
- * @param restoreBodyScroll
462
+ * @param {boolean} restoreBodyScroll
386
463
  */
387
464
  _cleanupModal(restoreBodyScroll) {
388
465
  if (this.pollIntervalId !== undefined) {
@@ -428,10 +505,15 @@ class MMPaySDK {
428
505
  }
429
506
  /**
430
507
  * _startPolling
431
- * @param {string} _id
508
+ * @param {IPollingRequest} payload
509
+ * @param {number} payload.amount
510
+ * @param {string} payload.currency
511
+ * @param {string} payload.orderId
512
+ * @param {string} payload.nonce
513
+ * @param {string} payload.callbackUrl
432
514
  * @param {Function} onComplete
433
515
  */
434
- async _startPolling(_id, onComplete) {
516
+ async _startPolling(payload, onComplete) {
435
517
  if (this.pollIntervalId !== undefined) {
436
518
  window.clearInterval(this.pollIntervalId);
437
519
  }
@@ -440,7 +522,7 @@ class MMPaySDK {
440
522
  const endpoint = this.environment === 'sandbox'
441
523
  ? '/xpayments/sandbox-payment-polling'
442
524
  : '/xpayments/production-payment-polling';
443
- const response = await this._callApi(endpoint, { _id: _id });
525
+ const response = await this._callApi(endpoint, payload);
444
526
  const status = (response.status || '').toUpperCase();
445
527
  if (status === 'SUCCESS' || status === 'FAILED' || status === 'EXPIRED') {
446
528
  window.clearInterval(this.pollIntervalId);
@@ -451,6 +533,7 @@ class MMPaySDK {
451
533
  `ငွေပေးချေမှု ${status === 'FAILED' ? 'မအောင်မြင်ပါ' : 'သက်တမ်းကုန်သွားပါပြီ'}.`;
452
534
  this._showTerminalMessage(response.orderId || 'N/A', status, message);
453
535
  if (onComplete) {
536
+ this.tokenKey = null;
454
537
  onComplete({ success: success, transaction: response });
455
538
  }
456
539
  return;
@@ -1,46 +1,49 @@
1
- export interface PaymentData {
1
+ export interface ICorePayParams {
2
2
  amount: number;
3
- currency: string;
4
3
  orderId: string;
5
4
  callbackUrl?: string;
6
5
  }
7
- export interface CreatePaymentResponse {
6
+ export interface ICreatePaymentRequestParams {
7
+ amount: number;
8
+ currency?: string;
9
+ orderId: string;
10
+ callbackUrl?: string;
11
+ nonce?: string;
12
+ }
13
+ export interface ICreatePaymentResponse {
8
14
  _id: string;
9
15
  amount: number;
10
16
  orderId: string;
11
- currency: string;
12
- transactionId: string;
17
+ currency?: string;
18
+ transactionRefId: string;
13
19
  qr: string;
14
- url: string;
15
20
  }
16
- export interface PollingResponse {
17
- _id: string;
18
- appId: string;
21
+ export interface ICreateTokenRequestParams {
22
+ amount: number;
23
+ currency?: string;
24
+ orderId: string;
25
+ callbackUrl?: string;
26
+ nonce?: string;
27
+ }
28
+ export interface ICreateTokenResponse {
19
29
  orderId: string;
30
+ token: string;
31
+ }
32
+ export interface IPollingRequest {
20
33
  amount: number;
21
- currency: string;
22
- method?: string;
23
- vendor?: string;
34
+ currency?: string;
35
+ orderId: string;
24
36
  callbackUrl?: string;
25
- callbackUrlStatus?: 'PENDING' | 'SUCCESS' | 'FAILED';
26
- callbackAt?: Date;
27
- disbursementStatus?: 'NONE' | 'PENDING' | 'COMPLETED' | 'FAILED' | 'CANCELLED';
28
- disburseAt?: Date;
29
- items: {
30
- name: string;
31
- amount: number;
32
- quantity: number;
33
- }[];
34
- merchantId: string;
37
+ nonce?: string;
38
+ }
39
+ export interface IPollingResponse {
40
+ orderId: string;
41
+ transactionRefId: string;
35
42
  status: 'PENDING' | 'SUCCESS' | 'FAILED' | 'EXPIRED';
36
- createdAt: Date;
37
- transactionRefId?: string;
38
- qr?: string;
39
- redirectUrl?: string;
40
43
  }
41
44
  export interface PolliongResult {
42
45
  success: boolean;
43
- transaction: PollingResponse;
46
+ transaction: IPollingResponse;
44
47
  }
45
48
  export interface SDKOptions {
46
49
  pollInterval?: number;
@@ -50,6 +53,7 @@ export interface SDKOptions {
50
53
  }
51
54
  export declare class MMPaySDK {
52
55
  private POLL_INTERVAL_MS;
56
+ private tokenKey;
53
57
  private publishableKey;
54
58
  private baseUrl;
55
59
  private merchantName;
@@ -60,6 +64,11 @@ export declare class MMPaySDK {
60
64
  private pendingApiResponse;
61
65
  private pendingPaymentPayload;
62
66
  private readonly QR_SIZE;
67
+ /**
68
+ * constructor
69
+ * @param publishableKey
70
+ * @param options
71
+ */
63
72
  constructor(publishableKey: string, options?: SDKOptions);
64
73
  /**
65
74
  * _callApi
@@ -69,28 +78,56 @@ export declare class MMPaySDK {
69
78
  */
70
79
  private _callApi;
71
80
  /**
72
- * createPaymentRequest
73
- * @param {PaymentData} payload
74
- * @returns
81
+ * _callApiTokenRequest
82
+ * @param {ICreateTokenRequestParams} payload
83
+ * @param {number} payload.amount
84
+ * @param {string} payload.currency
85
+ * @param {string} payload.orderId
86
+ * @param {string} payload.nonce
87
+ * @param {string} payload.callbackUrl
88
+ * @returns {Promise<ICreateTokenResponse>}
75
89
  */
76
- createPaymentRequest(payload: PaymentData): Promise<CreatePaymentResponse>;
90
+ private _callApiTokenRequest;
77
91
  /**
78
- * _createAndRenderModal
79
- * @param {string} contentHtml
80
- * @param isTerminal
81
- * @returns
92
+ * _callApiPaymentRequest
93
+ * @param {ICreatePaymentRequestParams} payload
94
+ * @param {number} payload.amount
95
+ * @param {string} payload.currency
96
+ * @param {string} payload.orderId
97
+ * @param {string} payload.nonce
98
+ * @param {string} payload.callbackUrl
99
+ * @returns {Promise<ICreatePaymentResponse>}
82
100
  */
83
- private _createAndRenderModal;
101
+ private _callApiPaymentRequest;
102
+ /**
103
+ * createPayment
104
+ * @param {ICorePayParams} params
105
+ * @param {number} params.amount
106
+ * @param {string} params.orderId
107
+ * @param {string} params.callbackUrl
108
+ * @returns {Promise<ICreatePaymentResponse>}
109
+ */
110
+ createPayment(params: ICorePayParams): Promise<ICreatePaymentResponse>;
84
111
  /**
85
112
  * showPaymentModal
86
- * @param {PaymentData} payload
113
+ * @param {ICorePayParams} params
114
+ * @param {number} params.amount
115
+ * @param {string} params.orderId
116
+ * @param {string} params.callbackUrl
87
117
  * @param {Function} onComplete
88
118
  */
89
- showPaymentModal(payload: PaymentData, onComplete: (result: PolliongResult) => void): Promise<void>;
119
+ showPaymentModal(params: ICorePayParams, onComplete: (result: PolliongResult) => void): Promise<void>;
120
+ /**
121
+ * _createAndRenderModal
122
+ * @param {string} contentHtml
123
+ * @param {boolean} isTerminal
124
+ * @returns
125
+ */
126
+ private _createAndRenderModal;
90
127
  /**
91
128
  * _renderQrModalContent
92
- * @param {CreatePaymentResponse} apiResponse
93
- * @param {PaymentData} payload
129
+ * @param {ICreatePaymentResponse} apiResponse
130
+ * @param {CreatePaymentRequest} payload
94
131
  * @param {string} merchantName
95
132
  */
96
133
  private _renderQrModalContent;
@@ -111,7 +148,7 @@ export declare class MMPaySDK {
111
148
  private _reRenderPendingModalInstance;
112
149
  /**
113
150
  * Cleans up the modal and stops polling.
114
- * @param restoreBodyScroll
151
+ * @param {boolean} restoreBodyScroll
115
152
  */
116
153
  private _cleanupModal;
117
154
  /**
@@ -122,7 +159,12 @@ export declare class MMPaySDK {
122
159
  private _injectQrScript;
123
160
  /**
124
161
  * _startPolling
125
- * @param {string} _id
162
+ * @param {IPollingRequest} payload
163
+ * @param {number} payload.amount
164
+ * @param {string} payload.currency
165
+ * @param {string} payload.orderId
166
+ * @param {string} payload.nonce
167
+ * @param {string} payload.callbackUrl
126
168
  * @param {Function} onComplete
127
169
  */
128
170
  private _startPolling;