@ojire/pg-js-sdk 1.0.0 → 1.0.1
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 +494 -0
- package/dist/opg-sdk.esm.js +1 -1
- package/dist/opg-sdk.esm.js.map +1 -1
- package/dist/opg-sdk.umd.js +1 -1
- package/dist/opg-sdk.umd.js.map +1 -1
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,494 @@
|
|
|
1
|
+
# @ojire/pg-js-sdk
|
|
2
|
+
|
|
3
|
+
A lightweight JavaScript SDK for integrating Ojire Payment Gateway into your web applications. This SDK provides a simple modal-based payment flow that works with any JavaScript framework or vanilla JS.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- Modal-based payment UI
|
|
8
|
+
- Support for multiple payment methods (QRIS, Virtual Account, Credit Card)
|
|
9
|
+
- Sandbox and Production environments
|
|
10
|
+
- TypeScript support
|
|
11
|
+
- Framework agnostic (works with React, Vue, Angular, or vanilla JS)
|
|
12
|
+
- Lightweight (~5KB gzipped)
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
### NPM
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @ojire/pg-js-sdk
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
### Yarn
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
yarn add @ojire/pg-js-sdk
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### CDN (Script Tag)
|
|
29
|
+
|
|
30
|
+
```html
|
|
31
|
+
<script
|
|
32
|
+
src="https://cdn.jsdelivr.net/npm/@ojire/pg-js-sdk@latest/dist/opg-sdk.umd.js"
|
|
33
|
+
data-client-key="pk_your_public_key_here"
|
|
34
|
+
data-sandbox="true"
|
|
35
|
+
></script>
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Quick Start
|
|
39
|
+
|
|
40
|
+
### 1. Get Your API Keys
|
|
41
|
+
|
|
42
|
+
Obtain your API keys from the Ojire Dashboard:
|
|
43
|
+
- **Public Key** (`pk_...`): Used in frontend to identify your merchant
|
|
44
|
+
- **Secret Key** (`sk_...`): Used in backend to create payment intents (never expose this in frontend!)
|
|
45
|
+
|
|
46
|
+
### 2. Create Payment Intent (Backend)
|
|
47
|
+
|
|
48
|
+
First, create a payment intent from your backend server:
|
|
49
|
+
|
|
50
|
+
```javascript
|
|
51
|
+
// Backend (Node.js example)
|
|
52
|
+
const response = await fetch('https://api.ojire.online/v1/payment-intents', {
|
|
53
|
+
method: 'POST',
|
|
54
|
+
headers: {
|
|
55
|
+
'Content-Type': 'application/json',
|
|
56
|
+
'X-Secret-Key': 'sk_your_secret_key'
|
|
57
|
+
},
|
|
58
|
+
body: JSON.stringify({
|
|
59
|
+
amount: 100000,
|
|
60
|
+
currency: 'IDR',
|
|
61
|
+
merchantId: 'your_merchant_id',
|
|
62
|
+
customerId: 'customer_123',
|
|
63
|
+
description: 'Order #12345',
|
|
64
|
+
metadata: {
|
|
65
|
+
orderId: 'order_12345'
|
|
66
|
+
}
|
|
67
|
+
})
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
const paymentIntent = await response.json();
|
|
71
|
+
// Returns: { id, clientSecret, customerToken, ... }
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### 3. Open Payment Modal (Frontend)
|
|
75
|
+
|
|
76
|
+
#### Using Script Tag (Vanilla JS)
|
|
77
|
+
|
|
78
|
+
```html
|
|
79
|
+
<!DOCTYPE html>
|
|
80
|
+
<html>
|
|
81
|
+
<head>
|
|
82
|
+
<title>Checkout</title>
|
|
83
|
+
</head>
|
|
84
|
+
<body>
|
|
85
|
+
<button id="pay-button">Pay Now</button>
|
|
86
|
+
|
|
87
|
+
<script
|
|
88
|
+
src="https://cdn.jsdelivr.net/npm/@ojire/pg-js-sdk@latest/dist/opg-sdk.umd.js"
|
|
89
|
+
data-client-key="pk_your_public_key"
|
|
90
|
+
data-sandbox="true"
|
|
91
|
+
></script>
|
|
92
|
+
|
|
93
|
+
<script>
|
|
94
|
+
document.getElementById('pay-button').addEventListener('click', function() {
|
|
95
|
+
// paymentIntent should come from your backend
|
|
96
|
+
OPG.openPayment({
|
|
97
|
+
token: paymentIntent.customerToken,
|
|
98
|
+
clientSecret: paymentIntent.clientSecret,
|
|
99
|
+
paymentId: paymentIntent.id,
|
|
100
|
+
|
|
101
|
+
onSuccess: function(result) {
|
|
102
|
+
console.log('Payment successful!', result);
|
|
103
|
+
// Redirect to success page or update UI
|
|
104
|
+
},
|
|
105
|
+
|
|
106
|
+
onPending: function(result) {
|
|
107
|
+
console.log('Payment pending', result);
|
|
108
|
+
// Show pending message
|
|
109
|
+
},
|
|
110
|
+
|
|
111
|
+
onError: function(result) {
|
|
112
|
+
console.log('Payment failed', result);
|
|
113
|
+
// Show error message
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
</script>
|
|
118
|
+
</body>
|
|
119
|
+
</html>
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
#### Using ES Modules
|
|
123
|
+
|
|
124
|
+
```javascript
|
|
125
|
+
import OPG from '@ojire/pg-js-sdk';
|
|
126
|
+
|
|
127
|
+
// Configure SDK
|
|
128
|
+
OPG.configure({
|
|
129
|
+
sandbox: true // Set to false for production
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// Open payment modal
|
|
133
|
+
OPG.openPayment({
|
|
134
|
+
token: paymentIntent.customerToken,
|
|
135
|
+
clientSecret: paymentIntent.clientSecret,
|
|
136
|
+
paymentId: paymentIntent.id,
|
|
137
|
+
|
|
138
|
+
onSuccess: (result) => {
|
|
139
|
+
console.log('Payment successful!', result);
|
|
140
|
+
},
|
|
141
|
+
|
|
142
|
+
onPending: (result) => {
|
|
143
|
+
console.log('Payment pending', result);
|
|
144
|
+
},
|
|
145
|
+
|
|
146
|
+
onError: (result) => {
|
|
147
|
+
console.log('Payment failed', result);
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## API Reference
|
|
153
|
+
|
|
154
|
+
### OPG.openPayment(options)
|
|
155
|
+
|
|
156
|
+
Opens the payment modal.
|
|
157
|
+
|
|
158
|
+
#### Options
|
|
159
|
+
|
|
160
|
+
| Parameter | Type | Required | Description |
|
|
161
|
+
|-----------|------|----------|-------------|
|
|
162
|
+
| `token` | `string` | Yes | Customer token from payment intent |
|
|
163
|
+
| `clientSecret` | `string` | Yes | Client secret from payment intent |
|
|
164
|
+
| `paymentId` | `string` | Yes | Payment intent ID |
|
|
165
|
+
| `sandbox` | `boolean` | No | Override sandbox mode for this payment |
|
|
166
|
+
| `onSuccess` | `function` | No | Callback when payment succeeds |
|
|
167
|
+
| `onPending` | `function` | No | Callback when payment is pending |
|
|
168
|
+
| `onError` | `function` | No | Callback when payment fails |
|
|
169
|
+
|
|
170
|
+
#### Example
|
|
171
|
+
|
|
172
|
+
```javascript
|
|
173
|
+
OPG.openPayment({
|
|
174
|
+
token: 'cust_token_xxx',
|
|
175
|
+
clientSecret: 'pi_secret_xxx',
|
|
176
|
+
paymentId: 'pi_xxx',
|
|
177
|
+
sandbox: true,
|
|
178
|
+
|
|
179
|
+
onSuccess: (result) => {
|
|
180
|
+
console.log('Transaction status:', result.transactionStatus);
|
|
181
|
+
console.log('Mapped status:', result.status);
|
|
182
|
+
console.log('Raw data:', result.rawData);
|
|
183
|
+
},
|
|
184
|
+
|
|
185
|
+
onPending: (result) => {
|
|
186
|
+
// Handle pending payment (e.g., waiting for bank transfer)
|
|
187
|
+
},
|
|
188
|
+
|
|
189
|
+
onError: (result) => {
|
|
190
|
+
// Handle failed payment
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### OPG.closePayment()
|
|
196
|
+
|
|
197
|
+
Programmatically closes the payment modal.
|
|
198
|
+
|
|
199
|
+
```javascript
|
|
200
|
+
OPG.closePayment();
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### OPG.isPaymentOpen()
|
|
204
|
+
|
|
205
|
+
Returns `true` if the payment modal is currently open.
|
|
206
|
+
|
|
207
|
+
```javascript
|
|
208
|
+
if (OPG.isPaymentOpen()) {
|
|
209
|
+
console.log('Modal is open');
|
|
210
|
+
}
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### OPG.configure(config)
|
|
214
|
+
|
|
215
|
+
Configures SDK settings.
|
|
216
|
+
|
|
217
|
+
```javascript
|
|
218
|
+
OPG.configure({
|
|
219
|
+
sandbox: false // Set to true for sandbox, false for production
|
|
220
|
+
});
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
### OPG.isSandbox()
|
|
224
|
+
|
|
225
|
+
Returns `true` if SDK is in sandbox mode.
|
|
226
|
+
|
|
227
|
+
```javascript
|
|
228
|
+
if (OPG.isSandbox()) {
|
|
229
|
+
console.log('Running in sandbox mode');
|
|
230
|
+
}
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
## Callback Result Object
|
|
234
|
+
|
|
235
|
+
All callbacks receive a `PaymentResult` object:
|
|
236
|
+
|
|
237
|
+
```typescript
|
|
238
|
+
interface PaymentResult {
|
|
239
|
+
transactionStatus: TransactionStatus;
|
|
240
|
+
status: 'success' | 'pending' | 'error';
|
|
241
|
+
rawData: Record<string, unknown>;
|
|
242
|
+
}
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
### Transaction Status Values
|
|
246
|
+
|
|
247
|
+
| Status | Mapped To | Description |
|
|
248
|
+
|--------|-----------|-------------|
|
|
249
|
+
| `settlement` | `success` | Payment completed |
|
|
250
|
+
| `capture` | `success` | Payment captured |
|
|
251
|
+
| `pending` | `pending` | Waiting for payment |
|
|
252
|
+
| `challenge` | `pending` | Needs verification |
|
|
253
|
+
| `deny` | `error` | Payment denied |
|
|
254
|
+
| `cancel` | `error` | Payment cancelled |
|
|
255
|
+
| `expire` | `error` | Payment expired |
|
|
256
|
+
| `failure` | `error` | Payment failed |
|
|
257
|
+
|
|
258
|
+
## React Integration
|
|
259
|
+
|
|
260
|
+
```tsx
|
|
261
|
+
import React, { useState } from 'react';
|
|
262
|
+
import OPG from '@ojire/pg-js-sdk';
|
|
263
|
+
import type { PaymentResult } from '@ojire/pg-js-sdk';
|
|
264
|
+
|
|
265
|
+
function CheckoutButton() {
|
|
266
|
+
const [loading, setLoading] = useState(false);
|
|
267
|
+
const [status, setStatus] = useState<string>('');
|
|
268
|
+
|
|
269
|
+
const handlePayment = async () => {
|
|
270
|
+
setLoading(true);
|
|
271
|
+
|
|
272
|
+
try {
|
|
273
|
+
// Get payment intent from your backend
|
|
274
|
+
const response = await fetch('/api/create-payment-intent', {
|
|
275
|
+
method: 'POST',
|
|
276
|
+
headers: { 'Content-Type': 'application/json' },
|
|
277
|
+
body: JSON.stringify({ amount: 100000 })
|
|
278
|
+
});
|
|
279
|
+
const paymentIntent = await response.json();
|
|
280
|
+
|
|
281
|
+
OPG.openPayment({
|
|
282
|
+
token: paymentIntent.customerToken,
|
|
283
|
+
clientSecret: paymentIntent.clientSecret,
|
|
284
|
+
paymentId: paymentIntent.id,
|
|
285
|
+
|
|
286
|
+
onSuccess: (result: PaymentResult) => {
|
|
287
|
+
setLoading(false);
|
|
288
|
+
setStatus('Payment successful!');
|
|
289
|
+
// Redirect or update UI
|
|
290
|
+
},
|
|
291
|
+
|
|
292
|
+
onPending: (result: PaymentResult) => {
|
|
293
|
+
setLoading(false);
|
|
294
|
+
setStatus('Payment pending. Please complete the payment.');
|
|
295
|
+
},
|
|
296
|
+
|
|
297
|
+
onError: (result: PaymentResult) => {
|
|
298
|
+
setLoading(false);
|
|
299
|
+
setStatus(`Payment failed: ${result.transactionStatus}`);
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
} catch (error) {
|
|
303
|
+
setLoading(false);
|
|
304
|
+
setStatus('Failed to initialize payment');
|
|
305
|
+
}
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
return (
|
|
309
|
+
<div>
|
|
310
|
+
<button onClick={handlePayment} disabled={loading}>
|
|
311
|
+
{loading ? 'Processing...' : 'Pay Now'}
|
|
312
|
+
</button>
|
|
313
|
+
{status && <p>{status}</p>}
|
|
314
|
+
</div>
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
export default CheckoutButton;
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
## Vue Integration
|
|
322
|
+
|
|
323
|
+
```vue
|
|
324
|
+
<template>
|
|
325
|
+
<div>
|
|
326
|
+
<button @click="handlePayment" :disabled="loading">
|
|
327
|
+
{{ loading ? 'Processing...' : 'Pay Now' }}
|
|
328
|
+
</button>
|
|
329
|
+
<p v-if="status">{{ status }}</p>
|
|
330
|
+
</div>
|
|
331
|
+
</template>
|
|
332
|
+
|
|
333
|
+
<script setup lang="ts">
|
|
334
|
+
import { ref } from 'vue';
|
|
335
|
+
import OPG from '@ojire/pg-js-sdk';
|
|
336
|
+
import type { PaymentResult } from '@ojire/pg-js-sdk';
|
|
337
|
+
|
|
338
|
+
const loading = ref(false);
|
|
339
|
+
const status = ref('');
|
|
340
|
+
|
|
341
|
+
const handlePayment = async () => {
|
|
342
|
+
loading.value = true;
|
|
343
|
+
|
|
344
|
+
try {
|
|
345
|
+
const response = await fetch('/api/create-payment-intent', {
|
|
346
|
+
method: 'POST',
|
|
347
|
+
headers: { 'Content-Type': 'application/json' },
|
|
348
|
+
body: JSON.stringify({ amount: 100000 })
|
|
349
|
+
});
|
|
350
|
+
const paymentIntent = await response.json();
|
|
351
|
+
|
|
352
|
+
OPG.openPayment({
|
|
353
|
+
token: paymentIntent.customerToken,
|
|
354
|
+
clientSecret: paymentIntent.clientSecret,
|
|
355
|
+
paymentId: paymentIntent.id,
|
|
356
|
+
|
|
357
|
+
onSuccess: (result: PaymentResult) => {
|
|
358
|
+
loading.value = false;
|
|
359
|
+
status.value = 'Payment successful!';
|
|
360
|
+
},
|
|
361
|
+
|
|
362
|
+
onPending: (result: PaymentResult) => {
|
|
363
|
+
loading.value = false;
|
|
364
|
+
status.value = 'Payment pending';
|
|
365
|
+
},
|
|
366
|
+
|
|
367
|
+
onError: (result: PaymentResult) => {
|
|
368
|
+
loading.value = false;
|
|
369
|
+
status.value = `Payment failed: ${result.transactionStatus}`;
|
|
370
|
+
}
|
|
371
|
+
});
|
|
372
|
+
} catch (error) {
|
|
373
|
+
loading.value = false;
|
|
374
|
+
status.value = 'Failed to initialize payment';
|
|
375
|
+
}
|
|
376
|
+
};
|
|
377
|
+
</script>
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
## Configuration
|
|
381
|
+
|
|
382
|
+
### Script Tag Attributes
|
|
383
|
+
|
|
384
|
+
When using the CDN version, configure via data attributes:
|
|
385
|
+
|
|
386
|
+
```html
|
|
387
|
+
<script
|
|
388
|
+
src="https://cdn.jsdelivr.net/npm/@ojire/pg-js-sdk@latest/dist/opg-sdk.umd.js"
|
|
389
|
+
data-client-key="pk_your_public_key"
|
|
390
|
+
data-sandbox="true"
|
|
391
|
+
></script>
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
| Attribute | Description |
|
|
395
|
+
|-----------|-------------|
|
|
396
|
+
| `data-client-key` | Your public key (required) |
|
|
397
|
+
| `data-sandbox` | Set to `"true"` for sandbox, `"false"` for production |
|
|
398
|
+
|
|
399
|
+
### Programmatic Configuration
|
|
400
|
+
|
|
401
|
+
```javascript
|
|
402
|
+
import OPG from '@ojire/pg-js-sdk';
|
|
403
|
+
|
|
404
|
+
OPG.configure({
|
|
405
|
+
sandbox: process.env.NODE_ENV !== 'production'
|
|
406
|
+
});
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
## Environments
|
|
410
|
+
|
|
411
|
+
| Environment | API URL | Payment URL |
|
|
412
|
+
|-------------|---------|-------------|
|
|
413
|
+
| Sandbox | `https://api-dev.ojire.online` | `https://pay-dev.ojire.online` |
|
|
414
|
+
| Production | `https://api.ojire.online` | `https://pay.ojire.online` |
|
|
415
|
+
|
|
416
|
+
## TypeScript Support
|
|
417
|
+
|
|
418
|
+
The SDK includes TypeScript definitions. Import types as needed:
|
|
419
|
+
|
|
420
|
+
```typescript
|
|
421
|
+
import OPG, {
|
|
422
|
+
PaymentResult,
|
|
423
|
+
PaymentCallbacks,
|
|
424
|
+
OpenPaymentOptions,
|
|
425
|
+
TransactionStatus,
|
|
426
|
+
PaymentResultStatus
|
|
427
|
+
} from '@ojire/pg-js-sdk';
|
|
428
|
+
```
|
|
429
|
+
|
|
430
|
+
## Error Handling
|
|
431
|
+
|
|
432
|
+
```javascript
|
|
433
|
+
try {
|
|
434
|
+
OPG.openPayment({
|
|
435
|
+
token: paymentIntent.customerToken,
|
|
436
|
+
clientSecret: paymentIntent.clientSecret,
|
|
437
|
+
paymentId: paymentIntent.id,
|
|
438
|
+
|
|
439
|
+
onError: (result) => {
|
|
440
|
+
switch (result.transactionStatus) {
|
|
441
|
+
case 'expire':
|
|
442
|
+
alert('Payment expired. Please try again.');
|
|
443
|
+
break;
|
|
444
|
+
case 'cancel':
|
|
445
|
+
alert('Payment was cancelled.');
|
|
446
|
+
break;
|
|
447
|
+
case 'deny':
|
|
448
|
+
alert('Payment was denied. Please use a different payment method.');
|
|
449
|
+
break;
|
|
450
|
+
default:
|
|
451
|
+
alert('Payment failed. Please try again.');
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
});
|
|
455
|
+
} catch (error) {
|
|
456
|
+
// Handle SDK initialization errors
|
|
457
|
+
console.error('SDK Error:', error.message);
|
|
458
|
+
}
|
|
459
|
+
```
|
|
460
|
+
|
|
461
|
+
## Security Best Practices
|
|
462
|
+
|
|
463
|
+
1. **Never expose your Secret Key** in frontend code
|
|
464
|
+
2. **Always create payment intents on your backend**
|
|
465
|
+
3. **Validate payment status on your backend** after receiving callbacks
|
|
466
|
+
4. **Use HTTPS** in production
|
|
467
|
+
5. **Verify webhook signatures** for server-to-server notifications
|
|
468
|
+
|
|
469
|
+
## Browser Support
|
|
470
|
+
|
|
471
|
+
- Chrome (latest)
|
|
472
|
+
- Firefox (latest)
|
|
473
|
+
- Safari (latest)
|
|
474
|
+
- Edge (latest)
|
|
475
|
+
- Mobile browsers (iOS Safari, Chrome for Android)
|
|
476
|
+
|
|
477
|
+
## Changelog
|
|
478
|
+
|
|
479
|
+
### v1.0.0
|
|
480
|
+
- Initial release
|
|
481
|
+
- Modal-based payment flow
|
|
482
|
+
- Support for QRIS, Virtual Account, and Credit Card
|
|
483
|
+
- Sandbox and Production environments
|
|
484
|
+
- TypeScript definitions
|
|
485
|
+
|
|
486
|
+
## Support
|
|
487
|
+
|
|
488
|
+
- Documentation: [https://docs.ojire.online](https://docs.ojire.online)
|
|
489
|
+
- Email: support@ojire.online
|
|
490
|
+
- GitHub Issues: [https://github.com/ojire-tech/opg-sdk/issues](https://github.com/ojire-tech/opg-sdk/issues)
|
|
491
|
+
|
|
492
|
+
## License
|
|
493
|
+
|
|
494
|
+
MIT License - see [LICENSE](LICENSE) for details.
|
package/dist/opg-sdk.esm.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const n={isOpen:!1,url:null,callbacks:{}};let e={...n};function t(){return e.isOpen}const o=["settlement","capture"],a=["pending","challenge"],r=["deny","cancel","expire","failure"];var i,d;let s={...{sandbox:!0,sandboxPaymentUrl:null!==(i="https://pay-dev.ojire.online/pay")?i:"",productionPaymentUrl:null!==(d="https://pay.ojire.online/pay")?d:""}};function
|
|
1
|
+
const n={isOpen:!1,url:null,callbacks:{}};let e={...n};function t(){return e.isOpen}const o=["settlement","capture"],a=["pending","challenge"],r=["deny","cancel","expire","failure"];var i,d;let s={...{sandbox:!0,sandboxPaymentUrl:null!==(i="https://pay-dev.ojire.online/pay")?i:"https://pay-dev.ojire.online/pay",productionPaymentUrl:null!==(d="https://pay.ojire.online/pay")?d:"https://pay.ojire.online/pay"}};function l(n){s={...s,...n}}const c="opg-payment-modal",u="opg-payment-backdrop",p="opg-payment-container",m="opg-payment-iframe",f="opg-payment-close",y="opg-payment-spinner";let b=null,g=null;function v(n){var t,i,d,s,l,c,u;const p=e.callbacks;if("COPY_TEXT"===n.data.type)return void navigator.clipboard.writeText(n.data.value);if("PAYMENT_SUCCESS"===(null===(t=n.data)||void 0===t?void 0:t.type)){const n={transactionStatus:"settlement",status:"success",rawData:{type:"PAYMENT_SUCCESS"}};return null===(i=p.onSuccess)||void 0===i||i.call(p,n),void E()}if("PAYMENT_RESULT"===(null===(d=n.data)||void 0===d?void 0:d.type)){const e=n.data.status;let t,o;switch(e){case"expired":t="expire",o="error";break;case"failed":default:t="failure",o="error";break;case"canceled":t="cancel",o="error"}const a={transactionStatus:t,status:o,rawData:{type:"PAYMENT_RESULT",status:e}};return null===(s=p.onError)||void 0===s||s.call(p,a),void E()}if("object"!=typeof(m=n.data)||null===m||"string"!=typeof m.transaction_status)return;var m;const f=function(n){const e=n.transaction_status;return{transactionStatus:e,status:(t=e,o.includes(t)?"success":a.includes(t)?"pending":(r.includes(t),"error")),rawData:n};var t}(n.data);switch(f.status){case"success":null===(l=p.onSuccess)||void 0===l||l.call(p,f);break;case"pending":null===(c=p.onPending)||void 0===c||c.call(p,f);break;case"error":null===(u=p.onError)||void 0===u||u.call(p,f)}}function h(n){"Escape"===n.key&&E()}function x(n){n.target.id===u&&E()}function w(n,t,o,a){const r=function(n){return`${s.sandbox?s.sandboxPaymentUrl:s.productionPaymentUrl}/${n}`}(t);if(document.getElementById(c))return;!function(){if(document.getElementById("opg-sdk-styles"))return;const n=document.createElement("style");n.id="opg-sdk-styles",n.textContent=`\n #${u} {\n position: fixed;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n background: rgba(0, 0, 0, 0.6);\n z-index: 999998;\n }\n\n #${p} {\n position: fixed;\n top: 50%;\n left: 50%;\n transform: translate(-50%, -50%);\n width: 90%;\n max-width: 480px;\n height: 80%;\n max-height: 700px;\n background: #fff;\n border-radius: 12px;\n box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);\n z-index: 999999;\n display: flex;\n flex-direction: column;\n overflow: hidden;\n }\n\n #${f} {\n position: absolute;\n top: 12px;\n right: 12px;\n width: 32px;\n height: 32px;\n border: none;\n background: rgba(0, 0, 0, 0.1);\n border-radius: 50%;\n cursor: pointer;\n display: flex;\n align-items: center;\n justify-content: center;\n z-index: 10;\n transition: background 0.2s;\n }\n\n #${f}:hover {\n background: rgba(0, 0, 0, 0.2);\n }\n\n #${f}::before,\n #${f}::after {\n content: '';\n position: absolute;\n width: 16px;\n height: 2px;\n background: #333;\n }\n\n #${f}::before {\n transform: rotate(45deg);\n }\n\n #${f}::after {\n transform: rotate(-45deg);\n }\n\n #${m} {\n width: 100%;\n height: 100%;\n border: none;\n flex: 1;\n }\n\n #${y} {\n position: absolute;\n top: 50%;\n left: 50%;\n transform: translate(-50%, -50%);\n width: 40px;\n height: 40px;\n border: 3px solid #f3f3f3;\n border-top: 3px solid #3498db;\n border-radius: 50%;\n animation: opg-spin 1s linear infinite;\n }\n\n @keyframes opg-spin {\n 0% { transform: translate(-50%, -50%) rotate(0deg); }\n 100% { transform: translate(-50%, -50%) rotate(360deg); }\n }\n\n #${y}.hidden {\n display: none;\n }\n `,document.head.appendChild(n)}(),document.body.style.overflow="hidden";const i=document.createElement("div");i.id=c;const d=document.createElement("div");d.id=u,d.addEventListener("click",x);const l=document.createElement("div");l.id=p;const w=document.createElement("button");w.id=f,w.type="button",w.setAttribute("aria-label","Close payment"),w.addEventListener("click",()=>E());const k=document.createElement("div");k.id=y;const S=document.createElement("iframe");var P;S.id=m,S.src=r,S.setAttribute("allow","payment"),S.setAttribute("allow","clipboard-write"),S.addEventListener("load",()=>{k.classList.add("hidden")}),S.onload=()=>{var e;null===(e=S.contentWindow)||void 0===e||e.postMessage({type:"INIT",clientSecret:n,publicKey:o,token:a},r)},l.appendChild(w),l.appendChild(k),l.appendChild(S),i.appendChild(d),i.appendChild(l),document.body.appendChild(i),b=v,g=h,window.addEventListener("message",b),document.addEventListener("keydown",g),P={isOpen:!0,url:r},e={...e,...P}}function E(){const t=document.getElementById(c);t&&(b&&(window.removeEventListener("message",b),b=null),g&&(document.removeEventListener("keydown",g),g=null),t.remove(),document.body.style.overflow="",e={...n,isOpen:!1,callbacks:e.callbacks})}var k,S,P;const C=null!==(S=null===(k=document.currentScript)||void 0===k?void 0:k.getAttribute("data-client-key"))&&void 0!==S?S:void 0;function $(n){if(t())return void console.warn("[opg-sdk] Payment modal is already open");if(!n.token)throw new Error("[opg-sdk] Payment token is required");if(!n.clientSecret)throw new Error("[opg-sdk] Payment clientSecret is required");if(!n.paymentId)throw new Error("[opg-sdk] Payment paymentId is required");if(!C)throw new Error("[opg-sdk] Payment publicKey is required");void 0!==n.sandbox&&l({sandbox:n.sandbox});const{clientSecret:o,paymentId:a,token:r,onSuccess:i,onPending:d,onError:s}=n;var c;c={onSuccess:i,onPending:d,onError:s},e.callbacks=c,w(o,a,C,r)}function L(){E()}function T(){return t()}function A(n){l(n)}function I(){return s.sandbox}l({sandbox:"false"!==(null===(P=document.currentScript)||void 0===P?void 0:P.getAttribute("data-sandbox"))});const U={openPayment:$,closePayment:L,isPaymentOpen:T,configure:A,isSandbox:I};export{L as closePayment,A as configure,U as default,T as isPaymentOpen,I as isSandbox,$ as openPayment};
|
|
2
2
|
//# sourceMappingURL=opg-sdk.esm.js.map
|
package/dist/opg-sdk.esm.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"opg-sdk.esm.js","sources":["../src/state.ts","../src/utils.ts","../src/config.ts","../src/modal.ts","../src/index.ts"],"sourcesContent":["import type { ModalState, PaymentCallbacks } from './types';\n\nconst initialState: ModalState = {\n isOpen: false,\n url: null,\n callbacks: {},\n};\n\nlet state: ModalState = { ...initialState };\n\nexport function getState(): ModalState {\n return state;\n}\n\nexport function setState(newState: Partial<ModalState>): void {\n state = { ...state, ...newState };\n}\n\nexport function resetState(): void {\n state = { ...initialState, isOpen: false, callbacks: state.callbacks };\n}\n\nexport function isOpen(): boolean {\n return state.isOpen;\n}\n\nexport function getCallbacks(): PaymentCallbacks {\n return state.callbacks;\n}\n\nexport function setCallbacks(callbacks: PaymentCallbacks): void {\n state.callbacks = callbacks;\n}\n","import type { TransactionStatus, PaymentResultStatus, PaymentResult, PaymentMessage } from './types';\n\nconst SUCCESS_STATUSES: TransactionStatus[] = ['settlement', 'capture'];\nconst PENDING_STATUSES: TransactionStatus[] = ['pending', 'challenge'];\nconst ERROR_STATUSES: TransactionStatus[] = ['deny', 'cancel', 'expire', 'failure'];\n\nexport function mapTransactionStatus(status: TransactionStatus): PaymentResultStatus {\n if (SUCCESS_STATUSES.includes(status)) return 'success';\n if (PENDING_STATUSES.includes(status)) return 'pending';\n if (ERROR_STATUSES.includes(status)) return 'error';\n return 'error';\n}\n\nexport function isValidPaymentMessage(data: unknown): data is PaymentMessage {\n if (typeof data !== 'object' || data === null) return false;\n const msg = data as Record<string, unknown>;\n return typeof msg.transaction_status === 'string';\n}\n\nexport function createPaymentResult(message: PaymentMessage): PaymentResult {\n const transactionStatus = message.transaction_status;\n return {\n transactionStatus,\n status: mapTransactionStatus(transactionStatus),\n rawData: message,\n };\n}\n\nexport function lockBodyScroll(): void {\n document.body.style.overflow = 'hidden';\n}\n\nexport function unlockBodyScroll(): void {\n document.body.style.overflow = '';\n}\n","export interface SDKConfig {\n sandbox: boolean;\n sandboxPaymentUrl: string;\n productionPaymentUrl: string;\n}\n\nconst DEFAULT_CONFIG: SDKConfig = {\n sandbox: true,\n sandboxPaymentUrl: process.env.OPG_SANDBOX_PAYMENT_URL ?? \"\",\n productionPaymentUrl: process.env.OPG_PRODUCTION_PAYMENT_URL ?? \"\",\n};\n\nlet currentConfig: SDKConfig = { ...DEFAULT_CONFIG };\n\nexport function configure(config: Partial<SDKConfig>): void {\n currentConfig = { ...currentConfig, ...config };\n}\n\nexport function getConfig(): SDKConfig {\n return currentConfig;\n}\n\nexport function getPaymentUrl(paymentId: string): string {\n const baseUrl = currentConfig.sandbox \n ? currentConfig.sandboxPaymentUrl \n : currentConfig.productionPaymentUrl;\n \n return `${baseUrl}/${paymentId}`;\n}\n\nexport function isSandboxMode(): boolean {\n return currentConfig.sandbox;\n}\n","import { getCallbacks, setState, resetState } from './state';\nimport { isValidPaymentMessage, createPaymentResult, lockBodyScroll, unlockBodyScroll } from './utils';\nimport { getPaymentUrl } from './config';\n\nconst MODAL_ID = 'opg-payment-modal';\nconst BACKDROP_ID = 'opg-payment-backdrop';\nconst CONTAINER_ID = 'opg-payment-container';\nconst IFRAME_ID = 'opg-payment-iframe';\nconst CLOSE_BTN_ID = 'opg-payment-close';\nconst SPINNER_ID = 'opg-payment-spinner';\n\nlet messageHandler: ((event: MessageEvent) => void) | null = null;\nlet keyHandler: ((event: KeyboardEvent) => void) | null = null;\n\nfunction injectStyles(): void {\n if (document.getElementById('opg-sdk-styles')) return;\n\n const style = document.createElement('style');\n style.id = 'opg-sdk-styles';\n style.textContent = `\n #${BACKDROP_ID} {\n position: fixed;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n background: rgba(0, 0, 0, 0.6);\n z-index: 999998;\n }\n\n #${CONTAINER_ID} {\n position: fixed;\n top: 50%;\n left: 50%;\n transform: translate(-50%, -50%);\n width: 90%;\n max-width: 480px;\n height: 80%;\n max-height: 700px;\n background: #fff;\n border-radius: 12px;\n box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);\n z-index: 999999;\n display: flex;\n flex-direction: column;\n overflow: hidden;\n }\n\n #${CLOSE_BTN_ID} {\n position: absolute;\n top: 12px;\n right: 12px;\n width: 32px;\n height: 32px;\n border: none;\n background: rgba(0, 0, 0, 0.1);\n border-radius: 50%;\n cursor: pointer;\n display: flex;\n align-items: center;\n justify-content: center;\n z-index: 10;\n transition: background 0.2s;\n }\n\n #${CLOSE_BTN_ID}:hover {\n background: rgba(0, 0, 0, 0.2);\n }\n\n #${CLOSE_BTN_ID}::before,\n #${CLOSE_BTN_ID}::after {\n content: '';\n position: absolute;\n width: 16px;\n height: 2px;\n background: #333;\n }\n\n #${CLOSE_BTN_ID}::before {\n transform: rotate(45deg);\n }\n\n #${CLOSE_BTN_ID}::after {\n transform: rotate(-45deg);\n }\n\n #${IFRAME_ID} {\n width: 100%;\n height: 100%;\n border: none;\n flex: 1;\n }\n\n #${SPINNER_ID} {\n position: absolute;\n top: 50%;\n left: 50%;\n transform: translate(-50%, -50%);\n width: 40px;\n height: 40px;\n border: 3px solid #f3f3f3;\n border-top: 3px solid #3498db;\n border-radius: 50%;\n animation: opg-spin 1s linear infinite;\n }\n\n @keyframes opg-spin {\n 0% { transform: translate(-50%, -50%) rotate(0deg); }\n 100% { transform: translate(-50%, -50%) rotate(360deg); }\n }\n\n #${SPINNER_ID}.hidden {\n display: none;\n }\n `;\n document.head.appendChild(style);\n}\n\nfunction handleMessage(event: MessageEvent): void {\n const callbacks = getCallbacks();\n\n if (event.data.type === \"COPY_TEXT\") {\n navigator.clipboard.writeText(event.data.value);\n return;\n }\n\n if (event.data?.type === 'PAYMENT_SUCCESS') {\n\n const result = {\n transactionStatus: 'settlement' as const,\n status: 'success' as const,\n rawData: { type: 'PAYMENT_SUCCESS' },\n };\n\n callbacks.onSuccess?.(result);\n closeModal();\n return;\n }\n\n if (event.data?.type === 'PAYMENT_RESULT') {\n const status = event.data.status;\n let transactionStatus: string;\n let mappedStatus: 'success' | 'pending' | 'error';\n\n switch (status) {\n case 'expired':\n transactionStatus = 'expire';\n mappedStatus = 'error';\n break;\n case 'failed':\n transactionStatus = 'failure';\n mappedStatus = 'error';\n break;\n case 'canceled':\n transactionStatus = 'cancel';\n mappedStatus = 'error';\n break;\n default:\n transactionStatus = 'failure';\n mappedStatus = 'error';\n }\n\n const result = {\n transactionStatus: transactionStatus as any,\n status: mappedStatus,\n rawData: { type: 'PAYMENT_RESULT', status },\n };\n\n callbacks.onError?.(result);\n closeModal();\n return;\n }\n\n if (!isValidPaymentMessage(event.data)) return;\n\n const result = createPaymentResult(event.data);\n\n switch (result.status) {\n case 'success':\n callbacks.onSuccess?.(result);\n break;\n case 'pending':\n callbacks.onPending?.(result);\n break;\n case 'error':\n callbacks.onError?.(result);\n break;\n }\n}\n\nfunction handleKeydown(event: KeyboardEvent): void {\n if (event.key === 'Escape') {\n closeModal();\n }\n}\n\nfunction handleBackdropClick(event: MouseEvent): void {\n if ((event.target as HTMLElement).id === BACKDROP_ID) {\n closeModal();\n }\n}\n\nexport function createModal(clientSecret: string, paymentId: string, publicKey: string, token: string): void {\n const url = getPaymentUrl(paymentId);\n if (document.getElementById(MODAL_ID)) return;\n\n injectStyles();\n lockBodyScroll();\n\n const modal = document.createElement('div');\n modal.id = MODAL_ID;\n\n const backdrop = document.createElement('div');\n backdrop.id = BACKDROP_ID;\n backdrop.addEventListener('click', handleBackdropClick);\n\n const container = document.createElement('div');\n container.id = CONTAINER_ID;\n\n const closeBtn = document.createElement('button');\n closeBtn.id = CLOSE_BTN_ID;\n closeBtn.type = 'button';\n closeBtn.setAttribute('aria-label', 'Close payment');\n closeBtn.addEventListener('click', () => closeModal());\n\n const spinner = document.createElement('div');\n spinner.id = SPINNER_ID;\n const iframe = document.createElement('iframe');\n iframe.id = IFRAME_ID;\n iframe.src = url;\n iframe.setAttribute('allow', 'payment');\n iframe.setAttribute(\"allow\", \"clipboard-write\");\n iframe.addEventListener('load', () => {\n spinner.classList.add('hidden');\n });\n\n iframe.onload = () => {\n iframe.contentWindow?.postMessage(\n {\n type: \"INIT\",\n clientSecret,\n publicKey,\n token\n },\n url\n );\n };\n\n container.appendChild(closeBtn);\n container.appendChild(spinner);\n container.appendChild(iframe);\n modal.appendChild(backdrop);\n modal.appendChild(container);\n document.body.appendChild(modal);\n\n messageHandler = handleMessage;\n keyHandler = handleKeydown;\n window.addEventListener('message', messageHandler);\n document.addEventListener('keydown', keyHandler);\n\n setState({ isOpen: true, url });\n}\n\nexport function closeModal(): void {\n const modal = document.getElementById(MODAL_ID);\n if (!modal) return;\n\n if (messageHandler) {\n window.removeEventListener('message', messageHandler);\n messageHandler = null;\n }\n\n if (keyHandler) {\n document.removeEventListener('keydown', keyHandler);\n keyHandler = null;\n }\n\n modal.remove();\n unlockBodyScroll();\n\n resetState();\n}\n","import type { OpenPaymentOptions, OPGSDK, SDKConfiguration } from './types';\nimport { isOpen, setCallbacks } from './state';\nimport { createModal, closeModal } from './modal';\nimport { configure as configureSDK, isSandboxMode, getConfig } from './config';\n\nexport type {\n TransactionStatus,\n PaymentResultStatus,\n PaymentResult,\n PaymentCallbacks,\n OpenPaymentOptions,\n OPGSDK,\n SDKConfiguration,\n} from './types';\n\nconst publicKey = document.currentScript?.getAttribute(\"data-client-key\") ?? undefined;\nconst sandboxAttr = document.currentScript?.getAttribute(\"data-sandbox\");\nconst isSandboxFromAttr = sandboxAttr !== 'false';\n\nconfigureSDK({ sandbox: isSandboxFromAttr });\n\nfunction openPayment(options: OpenPaymentOptions): void {\n if (isOpen()) {\n console.warn('[opg-sdk] Payment modal is already open');\n return;\n }\n\n if (!options.token) {\n throw new Error('[opg-sdk] Payment token is required');\n }\n\n if (!options.clientSecret) {\n throw new Error('[opg-sdk] Payment clientSecret is required');\n }\n\n if (!options.paymentId) {\n throw new Error('[opg-sdk] Payment paymentId is required');\n }\n\n if (!publicKey) {\n throw new Error('[opg-sdk] Payment publicKey is required');\n }\n\n if (options.sandbox !== undefined) {\n configureSDK({ sandbox: options.sandbox });\n }\n\n const { clientSecret, paymentId, token, onSuccess, onPending, onError } = options;\n\n setCallbacks({\n onSuccess,\n onPending,\n onError,\n });\n\n createModal(clientSecret, paymentId, publicKey, token);\n}\n\nfunction closePayment(): void {\n closeModal();\n}\n\nfunction isPaymentOpen(): boolean {\n return isOpen();\n}\n\nfunction configure(config: Partial<SDKConfiguration>): void {\n configureSDK(config);\n}\n\nfunction isSandbox(): boolean {\n return isSandboxMode();\n}\n\nconst OPG: OPGSDK = {\n openPayment,\n closePayment,\n isPaymentOpen,\n configure,\n isSandbox,\n};\n\nexport { openPayment, closePayment, isPaymentOpen, configure, isSandbox };\nexport default OPG;\n"],"names":["initialState","isOpen","url","callbacks","state","SUCCESS_STATUSES","PENDING_STATUSES","ERROR_STATUSES","currentConfig","sandbox","sandboxPaymentUrl","productionPaymentUrl","configure","config","MODAL_ID","BACKDROP_ID","CONTAINER_ID","IFRAME_ID","CLOSE_BTN_ID","SPINNER_ID","messageHandler","keyHandler","handleMessage","event","data","type","navigator","clipboard","writeText","value","_a","result","transactionStatus","status","rawData","_b","onSuccess","call","closeModal","_c","mappedStatus","_d","onError","transaction_status","message","includes","createPaymentResult","_e","_f","onPending","_g","handleKeydown","key","handleBackdropClick","target","id","createModal","clientSecret","paymentId","publicKey","token","getPaymentUrl","document","getElementById","style","createElement","textContent","head","appendChild","injectStyles","body","overflow","modal","backdrop","addEventListener","container","closeBtn","setAttribute","spinner","iframe","newState","src","classList","add","onload","contentWindow","postMessage","window","removeEventListener","remove","currentScript","getAttribute","undefined","openPayment","options","console","warn","Error","configureSDK","closePayment","isPaymentOpen","isSandbox","OPG"],"mappings":"AAEA,MAAMA,EAA2B,CAC/BC,QAAQ,EACRC,IAAK,KACLC,UAAW,CAAA,GAGb,IAAIC,EAAoB,IAAKJ,YAcbC,IACd,OAAOG,EAAMH,MACf,CCtBA,MAAMI,EAAwC,CAAC,aAAc,WACvDC,EAAwC,CAAC,UAAW,aACpDC,EAAsC,CAAC,OAAQ,SAAU,SAAU,mBCQzE,IAAIC,EAA2B,IANG,CAChCC,SAAS,EACTC,4BAAmB,sCAAuC,GAC1DC,+BAAsB,kCAA0C,KAK5D,SAAUC,EAAUC,GACxBL,EAAgB,IAAKA,KAAkBK,EACzC,CCZA,MAAMC,EAAW,oBACXC,EAAc,uBACdC,EAAe,wBACfC,EAAY,qBACZC,EAAe,oBACfC,EAAa,sBAEnB,IAAIC,EAAyD,KACzDC,EAAsD,KA0G1D,SAASC,EAAcC,qBACrB,MAAMpB,EH5FCC,EAAMD,UG8Fb,GAAwB,cAApBoB,EAAMC,KAAKC,KAEb,YADAC,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AM,gDAGML,IAApBE,EAAQrF,SACVyF,EAAa,CAAEzF,QAASqF,EAAQrF,UAGlC,MAAMgD,aAAEA,EAAYC,UAAEA,EAASE,MAAEA,EAAKxB,UAAEA,EAASa,UAAEA,EAASP,QAAEA,GAAYoD,EJjBtE,IAAuB3F,IImBd,CACXiC,YACAa,YACAP,WJrBFtC,EAAMD,UAAYA,EIwBlBqD,EAAYC,EAAcC,EAAWC,EAAWC,EAClD,CAEA,SAASuC,IACP7D,GACF,CAEA,SAAS8D,IACP,OAAOnG,GACT,CAEA,SAASW,EAAUC,GACjBqF,EAAarF,EACf,CAEA,SAASwF,IACP,OFxCO7F,EAAcC,OEyCvB,CArDAyF,EAAa,CAAEzF,QAF2B,WADA,QAAtB8B,EAAAuB,SAAS4B,qBAAa,IAAAnD,OAAA,EAAAA,EAAEoD,aAAa,mBA0DzD,MAAMW,EAAc,CAClBT,cACAM,eACAC,gBACAxF,YACAyF"}
|
|
1
|
+
{"version":3,"file":"opg-sdk.esm.js","sources":["../src/state.ts","../src/utils.ts","../src/config.ts","../src/modal.ts","../src/index.ts"],"sourcesContent":["import type { ModalState, PaymentCallbacks } from './types';\n\nconst initialState: ModalState = {\n isOpen: false,\n url: null,\n callbacks: {},\n};\n\nlet state: ModalState = { ...initialState };\n\nexport function getState(): ModalState {\n return state;\n}\n\nexport function setState(newState: Partial<ModalState>): void {\n state = { ...state, ...newState };\n}\n\nexport function resetState(): void {\n state = { ...initialState, isOpen: false, callbacks: state.callbacks };\n}\n\nexport function isOpen(): boolean {\n return state.isOpen;\n}\n\nexport function getCallbacks(): PaymentCallbacks {\n return state.callbacks;\n}\n\nexport function setCallbacks(callbacks: PaymentCallbacks): void {\n state.callbacks = callbacks;\n}\n","import type { TransactionStatus, PaymentResultStatus, PaymentResult, PaymentMessage } from './types';\n\nconst SUCCESS_STATUSES: TransactionStatus[] = ['settlement', 'capture'];\nconst PENDING_STATUSES: TransactionStatus[] = ['pending', 'challenge'];\nconst ERROR_STATUSES: TransactionStatus[] = ['deny', 'cancel', 'expire', 'failure'];\n\nexport function mapTransactionStatus(status: TransactionStatus): PaymentResultStatus {\n if (SUCCESS_STATUSES.includes(status)) return 'success';\n if (PENDING_STATUSES.includes(status)) return 'pending';\n if (ERROR_STATUSES.includes(status)) return 'error';\n return 'error';\n}\n\nexport function isValidPaymentMessage(data: unknown): data is PaymentMessage {\n if (typeof data !== 'object' || data === null) return false;\n const msg = data as Record<string, unknown>;\n return typeof msg.transaction_status === 'string';\n}\n\nexport function createPaymentResult(message: PaymentMessage): PaymentResult {\n const transactionStatus = message.transaction_status;\n return {\n transactionStatus,\n status: mapTransactionStatus(transactionStatus),\n rawData: message,\n };\n}\n\nexport function lockBodyScroll(): void {\n document.body.style.overflow = 'hidden';\n}\n\nexport function unlockBodyScroll(): void {\n document.body.style.overflow = '';\n}\n","export interface SDKConfig {\n sandbox: boolean;\n sandboxPaymentUrl: string;\n productionPaymentUrl: string;\n}\n\nconst DEFAULT_CONFIG: SDKConfig = {\n sandbox: true,\n sandboxPaymentUrl: process.env.OPG_SANDBOX_PAYMENT_URL ?? \"https://pay-dev.ojire.online/pay\",\n productionPaymentUrl: process.env.OPG_PRODUCTION_PAYMENT_URL ?? \"https://pay.ojire.online/pay\",\n};\n\nlet currentConfig: SDKConfig = { ...DEFAULT_CONFIG };\n\nexport function configure(config: Partial<SDKConfig>): void {\n currentConfig = { ...currentConfig, ...config };\n}\n\nexport function getConfig(): SDKConfig {\n return currentConfig;\n}\n\nexport function getPaymentUrl(paymentId: string): string {\n const baseUrl = currentConfig.sandbox \n ? currentConfig.sandboxPaymentUrl \n : currentConfig.productionPaymentUrl;\n \n return `${baseUrl}/${paymentId}`;\n}\n\nexport function isSandboxMode(): boolean {\n return currentConfig.sandbox;\n}\n","import { getCallbacks, setState, resetState } from './state';\nimport { isValidPaymentMessage, createPaymentResult, lockBodyScroll, unlockBodyScroll } from './utils';\nimport { getPaymentUrl } from './config';\n\nconst MODAL_ID = 'opg-payment-modal';\nconst BACKDROP_ID = 'opg-payment-backdrop';\nconst CONTAINER_ID = 'opg-payment-container';\nconst IFRAME_ID = 'opg-payment-iframe';\nconst CLOSE_BTN_ID = 'opg-payment-close';\nconst SPINNER_ID = 'opg-payment-spinner';\n\nlet messageHandler: ((event: MessageEvent) => void) | null = null;\nlet keyHandler: ((event: KeyboardEvent) => void) | null = null;\n\nfunction injectStyles(): void {\n if (document.getElementById('opg-sdk-styles')) return;\n\n const style = document.createElement('style');\n style.id = 'opg-sdk-styles';\n style.textContent = `\n #${BACKDROP_ID} {\n position: fixed;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n background: rgba(0, 0, 0, 0.6);\n z-index: 999998;\n }\n\n #${CONTAINER_ID} {\n position: fixed;\n top: 50%;\n left: 50%;\n transform: translate(-50%, -50%);\n width: 90%;\n max-width: 480px;\n height: 80%;\n max-height: 700px;\n background: #fff;\n border-radius: 12px;\n box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);\n z-index: 999999;\n display: flex;\n flex-direction: column;\n overflow: hidden;\n }\n\n #${CLOSE_BTN_ID} {\n position: absolute;\n top: 12px;\n right: 12px;\n width: 32px;\n height: 32px;\n border: none;\n background: rgba(0, 0, 0, 0.1);\n border-radius: 50%;\n cursor: pointer;\n display: flex;\n align-items: center;\n justify-content: center;\n z-index: 10;\n transition: background 0.2s;\n }\n\n #${CLOSE_BTN_ID}:hover {\n background: rgba(0, 0, 0, 0.2);\n }\n\n #${CLOSE_BTN_ID}::before,\n #${CLOSE_BTN_ID}::after {\n content: '';\n position: absolute;\n width: 16px;\n height: 2px;\n background: #333;\n }\n\n #${CLOSE_BTN_ID}::before {\n transform: rotate(45deg);\n }\n\n #${CLOSE_BTN_ID}::after {\n transform: rotate(-45deg);\n }\n\n #${IFRAME_ID} {\n width: 100%;\n height: 100%;\n border: none;\n flex: 1;\n }\n\n #${SPINNER_ID} {\n position: absolute;\n top: 50%;\n left: 50%;\n transform: translate(-50%, -50%);\n width: 40px;\n height: 40px;\n border: 3px solid #f3f3f3;\n border-top: 3px solid #3498db;\n border-radius: 50%;\n animation: opg-spin 1s linear infinite;\n }\n\n @keyframes opg-spin {\n 0% { transform: translate(-50%, -50%) rotate(0deg); }\n 100% { transform: translate(-50%, -50%) rotate(360deg); }\n }\n\n #${SPINNER_ID}.hidden {\n display: none;\n }\n `;\n document.head.appendChild(style);\n}\n\nfunction handleMessage(event: MessageEvent): void {\n const callbacks = getCallbacks();\n\n if (event.data.type === \"COPY_TEXT\") {\n navigator.clipboard.writeText(event.data.value);\n return;\n }\n\n if (event.data?.type === 'PAYMENT_SUCCESS') {\n\n const result = {\n transactionStatus: 'settlement' as const,\n status: 'success' as const,\n rawData: { type: 'PAYMENT_SUCCESS' },\n };\n\n callbacks.onSuccess?.(result);\n closeModal();\n return;\n }\n\n if (event.data?.type === 'PAYMENT_RESULT') {\n const status = event.data.status;\n let transactionStatus: string;\n let mappedStatus: 'success' | 'pending' | 'error';\n\n switch (status) {\n case 'expired':\n transactionStatus = 'expire';\n mappedStatus = 'error';\n break;\n case 'failed':\n transactionStatus = 'failure';\n mappedStatus = 'error';\n break;\n case 'canceled':\n transactionStatus = 'cancel';\n mappedStatus = 'error';\n break;\n default:\n transactionStatus = 'failure';\n mappedStatus = 'error';\n }\n\n const result = {\n transactionStatus: transactionStatus as any,\n status: mappedStatus,\n rawData: { type: 'PAYMENT_RESULT', status },\n };\n\n callbacks.onError?.(result);\n closeModal();\n return;\n }\n\n if (!isValidPaymentMessage(event.data)) return;\n\n const result = createPaymentResult(event.data);\n\n switch (result.status) {\n case 'success':\n callbacks.onSuccess?.(result);\n break;\n case 'pending':\n callbacks.onPending?.(result);\n break;\n case 'error':\n callbacks.onError?.(result);\n break;\n }\n}\n\nfunction handleKeydown(event: KeyboardEvent): void {\n if (event.key === 'Escape') {\n closeModal();\n }\n}\n\nfunction handleBackdropClick(event: MouseEvent): void {\n if ((event.target as HTMLElement).id === BACKDROP_ID) {\n closeModal();\n }\n}\n\nexport function createModal(clientSecret: string, paymentId: string, publicKey: string, token: string): void {\n const url = getPaymentUrl(paymentId);\n if (document.getElementById(MODAL_ID)) return;\n\n injectStyles();\n lockBodyScroll();\n\n const modal = document.createElement('div');\n modal.id = MODAL_ID;\n\n const backdrop = document.createElement('div');\n backdrop.id = BACKDROP_ID;\n backdrop.addEventListener('click', handleBackdropClick);\n\n const container = document.createElement('div');\n container.id = CONTAINER_ID;\n\n const closeBtn = document.createElement('button');\n closeBtn.id = CLOSE_BTN_ID;\n closeBtn.type = 'button';\n closeBtn.setAttribute('aria-label', 'Close payment');\n closeBtn.addEventListener('click', () => closeModal());\n\n const spinner = document.createElement('div');\n spinner.id = SPINNER_ID;\n const iframe = document.createElement('iframe');\n iframe.id = IFRAME_ID;\n iframe.src = url;\n iframe.setAttribute('allow', 'payment');\n iframe.setAttribute(\"allow\", \"clipboard-write\");\n iframe.addEventListener('load', () => {\n spinner.classList.add('hidden');\n });\n\n iframe.onload = () => {\n iframe.contentWindow?.postMessage(\n {\n type: \"INIT\",\n clientSecret,\n publicKey,\n token\n },\n url\n );\n };\n\n container.appendChild(closeBtn);\n container.appendChild(spinner);\n container.appendChild(iframe);\n modal.appendChild(backdrop);\n modal.appendChild(container);\n document.body.appendChild(modal);\n\n messageHandler = handleMessage;\n keyHandler = handleKeydown;\n window.addEventListener('message', messageHandler);\n document.addEventListener('keydown', keyHandler);\n\n setState({ isOpen: true, url });\n}\n\nexport function closeModal(): void {\n const modal = document.getElementById(MODAL_ID);\n if (!modal) return;\n\n if (messageHandler) {\n window.removeEventListener('message', messageHandler);\n messageHandler = null;\n }\n\n if (keyHandler) {\n document.removeEventListener('keydown', keyHandler);\n keyHandler = null;\n }\n\n modal.remove();\n unlockBodyScroll();\n\n resetState();\n}\n","import type { OpenPaymentOptions, OPGSDK, SDKConfiguration } from './types';\nimport { isOpen, setCallbacks } from './state';\nimport { createModal, closeModal } from './modal';\nimport { configure as configureSDK, isSandboxMode, getConfig } from './config';\n\nexport type {\n TransactionStatus,\n PaymentResultStatus,\n PaymentResult,\n PaymentCallbacks,\n OpenPaymentOptions,\n OPGSDK,\n SDKConfiguration,\n} from './types';\n\nconst publicKey = document.currentScript?.getAttribute(\"data-client-key\") ?? undefined;\nconst sandboxAttr = document.currentScript?.getAttribute(\"data-sandbox\");\nconst isSandboxFromAttr = sandboxAttr !== 'false';\n\nconfigureSDK({ sandbox: isSandboxFromAttr });\n\nfunction openPayment(options: OpenPaymentOptions): void {\n if (isOpen()) {\n console.warn('[opg-sdk] Payment modal is already open');\n return;\n }\n\n if (!options.token) {\n throw new Error('[opg-sdk] Payment token is required');\n }\n\n if (!options.clientSecret) {\n throw new Error('[opg-sdk] Payment clientSecret is required');\n }\n\n if (!options.paymentId) {\n throw new Error('[opg-sdk] Payment paymentId is required');\n }\n\n if (!publicKey) {\n throw new Error('[opg-sdk] Payment publicKey is required');\n }\n\n if (options.sandbox !== undefined) {\n configureSDK({ sandbox: options.sandbox });\n }\n\n const { clientSecret, paymentId, token, onSuccess, onPending, onError } = options;\n\n setCallbacks({\n onSuccess,\n onPending,\n onError,\n });\n\n createModal(clientSecret, paymentId, publicKey, token);\n}\n\nfunction closePayment(): void {\n closeModal();\n}\n\nfunction isPaymentOpen(): boolean {\n return isOpen();\n}\n\nfunction configure(config: Partial<SDKConfiguration>): void {\n configureSDK(config);\n}\n\nfunction isSandbox(): boolean {\n return isSandboxMode();\n}\n\nconst OPG: OPGSDK = {\n openPayment,\n closePayment,\n isPaymentOpen,\n configure,\n isSandbox,\n};\n\nexport { openPayment, closePayment, isPaymentOpen, configure, isSandbox };\nexport default OPG;\n"],"names":["initialState","isOpen","url","callbacks","state","SUCCESS_STATUSES","PENDING_STATUSES","ERROR_STATUSES","currentConfig","sandbox","sandboxPaymentUrl","productionPaymentUrl","configure","config","MODAL_ID","BACKDROP_ID","CONTAINER_ID","IFRAME_ID","CLOSE_BTN_ID","SPINNER_ID","messageHandler","keyHandler","handleMessage","event","data","type","navigator","clipboard","writeText","value","_a","result","transactionStatus","status","rawData","_b","onSuccess","call","closeModal","_c","mappedStatus","_d","onError","transaction_status","message","includes","createPaymentResult","_e","_f","onPending","_g","handleKeydown","key","handleBackdropClick","target","id","createModal","clientSecret","paymentId","publicKey","token","getPaymentUrl","document","getElementById","style","createElement","textContent","head","appendChild","injectStyles","body","overflow","modal","backdrop","addEventListener","container","closeBtn","setAttribute","spinner","iframe","newState","src","classList","add","onload","contentWindow","postMessage","window","removeEventListener","remove","currentScript","getAttribute","undefined","openPayment","options","console","warn","Error","configureSDK","closePayment","isPaymentOpen","isSandbox","OPG"],"mappings":"AAEA,MAAMA,EAA2B,CAC/BC,QAAQ,EACRC,IAAK,KACLC,UAAW,CAAA,GAGb,IAAIC,EAAoB,IAAKJ,YAcbC,IACd,OAAOG,EAAMH,MACf,CCtBA,MAAMI,EAAwC,CAAC,aAAc,WACvDC,EAAwC,CAAC,UAAW,aACpDC,EAAsC,CAAC,OAAQ,SAAU,SAAU,mBCQzE,IAAIC,EAA2B,IANG,CAChCC,SAAS,EACTC,4BAAmB,sCAAuC,mCAC1DC,+BAAsB,kCAA0C,iCAK5D,SAAUC,EAAUC,GACxBL,EAAgB,IAAKA,KAAkBK,EACzC,CCZA,MAAMC,EAAW,oBACXC,EAAc,uBACdC,EAAe,wBACfC,EAAY,qBACZC,EAAe,oBACfC,EAAa,sBAEnB,IAAIC,EAAyD,KACzDC,EAAsD,KA0G1D,SAASC,EAAcC,qBACrB,MAAMpB,EH5FCC,EAAMD,UG8Fb,GAAwB,cAApBoB,EAAMC,KAAKC,KAEb,YADAC,UAAUC,UAAUC,UAAUL,EAAMC,KAAKK,OAI3C,GAAyB,6BAArBC,EAAAP,EAAMC,2BAAMC,MAA4B,CAE1C,MAAMM,EAAS,CACbC,kBAAmB,aACnBC,OAAQ,UACRC,QAAS,CAAET,KAAM,oBAKnB,OAFmB,QAAnBU,EAAAhC,EAAUiC,qBAASD,GAAAA,EAAAE,KAAAlC,EAAG4B,QACtBO,GAEF,CAEA,GA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,EAAWC,EAClD,CAEA,SAASuC,IACP7D,GACF,CAEA,SAAS8D,IACP,OAAOnG,GACT,CAEA,SAASW,EAAUC,GACjBqF,EAAarF,EACf,CAEA,SAASwF,IACP,OFxCO7F,EAAcC,OEyCvB,CArDAyF,EAAa,CAAEzF,QAF2B,WADA,QAAtB8B,EAAAuB,SAAS4B,qBAAa,IAAAnD,OAAA,EAAAA,EAAEoD,aAAa,mBA0DzD,MAAMW,EAAc,CAClBT,cACAM,eACAC,gBACAxF,YACAyF"}
|
package/dist/opg-sdk.umd.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((n="undefined"!=typeof globalThis?globalThis:n||self).OPG={})}(this,function(n){"use strict";const e={isOpen:!1,url:null,callbacks:{}};let t={...e};function o(){return t.isOpen}const a=["settlement","capture"],r=["pending","challenge"],i=["deny","cancel","expire","failure"];var d,s;let l={...{sandbox:!0,sandboxPaymentUrl:null!==(d="https://pay-dev.ojire.online/pay")?d:"",productionPaymentUrl:null!==(s="https://pay.ojire.online/pay")?s:""}};function c(n){l={...l,...n}}const u="opg-payment-modal",p="opg-payment-backdrop",f="opg-payment-container",m="opg-payment-iframe",y="opg-payment-close",b="opg-payment-spinner";let g=null,v=null;function h(n){var e,o,d,s,l,c,u;const p=t.callbacks;if("COPY_TEXT"===n.data.type)return void navigator.clipboard.writeText(n.data.value);if("PAYMENT_SUCCESS"===(null===(e=n.data)||void 0===e?void 0:e.type)){const n={transactionStatus:"settlement",status:"success",rawData:{type:"PAYMENT_SUCCESS"}};return null===(o=p.onSuccess)||void 0===o||o.call(p,n),void k()}if("PAYMENT_RESULT"===(null===(d=n.data)||void 0===d?void 0:d.type)){const e=n.data.status;let t,o;switch(e){case"expired":t="expire",o="error";break;case"failed":default:t="failure",o="error";break;case"canceled":t="cancel",o="error"}const a={transactionStatus:t,status:o,rawData:{type:"PAYMENT_RESULT",status:e}};return null===(s=p.onError)||void 0===s||s.call(p,a),void k()}if("object"!=typeof(f=n.data)||null===f||"string"!=typeof f.transaction_status)return;var f;const m=function(n){const e=n.transaction_status;return{transactionStatus:e,status:(t=e,a.includes(t)?"success":r.includes(t)?"pending":(i.includes(t),"error")),rawData:n};var t}(n.data);switch(m.status){case"success":null===(l=p.onSuccess)||void 0===l||l.call(p,m);break;case"pending":null===(c=p.onPending)||void 0===c||c.call(p,m);break;case"error":null===(u=p.onError)||void 0===u||u.call(p,m)}}function x(n){"Escape"===n.key&&k()}function w(n){n.target.id===p&&k()}function E(n,e,o,a){const r=function(n){return`${l.sandbox?l.sandboxPaymentUrl:l.productionPaymentUrl}/${n}`}(e);if(document.getElementById(u))return;!function(){if(document.getElementById("opg-sdk-styles"))return;const n=document.createElement("style");n.id="opg-sdk-styles",n.textContent=`\n #${p} {\n position: fixed;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n background: rgba(0, 0, 0, 0.6);\n z-index: 999998;\n }\n\n #${f} {\n position: fixed;\n top: 50%;\n left: 50%;\n transform: translate(-50%, -50%);\n width: 90%;\n max-width: 480px;\n height: 80%;\n max-height: 700px;\n background: #fff;\n border-radius: 12px;\n box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);\n z-index: 999999;\n display: flex;\n flex-direction: column;\n overflow: hidden;\n }\n\n #${y} {\n position: absolute;\n top: 12px;\n right: 12px;\n width: 32px;\n height: 32px;\n border: none;\n background: rgba(0, 0, 0, 0.1);\n border-radius: 50%;\n cursor: pointer;\n display: flex;\n align-items: center;\n justify-content: center;\n z-index: 10;\n transition: background 0.2s;\n }\n\n #${y}:hover {\n background: rgba(0, 0, 0, 0.2);\n }\n\n #${y}::before,\n #${y}::after {\n content: '';\n position: absolute;\n width: 16px;\n height: 2px;\n background: #333;\n }\n\n #${y}::before {\n transform: rotate(45deg);\n }\n\n #${y}::after {\n transform: rotate(-45deg);\n }\n\n #${m} {\n width: 100%;\n height: 100%;\n border: none;\n flex: 1;\n }\n\n #${b} {\n position: absolute;\n top: 50%;\n left: 50%;\n transform: translate(-50%, -50%);\n width: 40px;\n height: 40px;\n border: 3px solid #f3f3f3;\n border-top: 3px solid #3498db;\n border-radius: 50%;\n animation: opg-spin 1s linear infinite;\n }\n\n @keyframes opg-spin {\n 0% { transform: translate(-50%, -50%) rotate(0deg); }\n 100% { transform: translate(-50%, -50%) rotate(360deg); }\n }\n\n #${b}.hidden {\n display: none;\n }\n `,document.head.appendChild(n)}(),document.body.style.overflow="hidden";const i=document.createElement("div");i.id=u;const d=document.createElement("div");d.id=p,d.addEventListener("click",w);const s=document.createElement("div");s.id=f;const c=document.createElement("button");c.id=y,c.type="button",c.setAttribute("aria-label","Close payment"),c.addEventListener("click",()=>k());const E=document.createElement("div");E.id=b;const P=document.createElement("iframe");var S;P.id=m,P.src=r,P.setAttribute("allow","payment"),P.setAttribute("allow","clipboard-write"),P.addEventListener("load",()=>{E.classList.add("hidden")}),P.onload=()=>{var e;null===(e=P.contentWindow)||void 0===e||e.postMessage({type:"INIT",clientSecret:n,publicKey:o,token:a},r)},s.appendChild(c),s.appendChild(E),s.appendChild(P),i.appendChild(d),i.appendChild(s),document.body.appendChild(i),g=h,v=x,window.addEventListener("message",g),document.addEventListener("keydown",v),S={isOpen:!0,url:r},t={...t,...S}}function k(){const n=document.getElementById(u);n&&(g&&(window.removeEventListener("message",g),g=null),v&&(document.removeEventListener("keydown",v),v=null),n.remove(),document.body.style.overflow="",t={...e,isOpen:!1,callbacks:t.callbacks})}var P,S,C;const $=null!==(S=null===(P=document.currentScript)||void 0===P?void 0:P.getAttribute("data-client-key"))&&void 0!==S?S:void 0;function T(n){if(o())return void console.warn("[opg-sdk] Payment modal is already open");if(!n.token)throw new Error("[opg-sdk] Payment token is required");if(!n.clientSecret)throw new Error("[opg-sdk] Payment clientSecret is required");if(!n.paymentId)throw new Error("[opg-sdk] Payment paymentId is required");if(!$)throw new Error("[opg-sdk] Payment publicKey is required");void 0!==n.sandbox&&c({sandbox:n.sandbox});const{clientSecret:e,paymentId:a,token:r,onSuccess:i,onPending:d,onError:s}=n;var l;l={onSuccess:i,onPending:d,onError:s},t.callbacks=l,E(e,a,$,r)}function L(){k()}function A(){return o()}function O(n){c(n)}function _(){return l.sandbox}c({sandbox:"false"!==(null===(C=document.currentScript)||void 0===C?void 0:C.getAttribute("data-sandbox"))});const
|
|
1
|
+
!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((n="undefined"!=typeof globalThis?globalThis:n||self).OPG={})}(this,function(n){"use strict";const e={isOpen:!1,url:null,callbacks:{}};let t={...e};function o(){return t.isOpen}const a=["settlement","capture"],r=["pending","challenge"],i=["deny","cancel","expire","failure"];var d,s;let l={...{sandbox:!0,sandboxPaymentUrl:null!==(d="https://pay-dev.ojire.online/pay")?d:"https://pay-dev.ojire.online/pay",productionPaymentUrl:null!==(s="https://pay.ojire.online/pay")?s:"https://pay.ojire.online/pay"}};function c(n){l={...l,...n}}const u="opg-payment-modal",p="opg-payment-backdrop",f="opg-payment-container",m="opg-payment-iframe",y="opg-payment-close",b="opg-payment-spinner";let g=null,v=null;function h(n){var e,o,d,s,l,c,u;const p=t.callbacks;if("COPY_TEXT"===n.data.type)return void navigator.clipboard.writeText(n.data.value);if("PAYMENT_SUCCESS"===(null===(e=n.data)||void 0===e?void 0:e.type)){const n={transactionStatus:"settlement",status:"success",rawData:{type:"PAYMENT_SUCCESS"}};return null===(o=p.onSuccess)||void 0===o||o.call(p,n),void k()}if("PAYMENT_RESULT"===(null===(d=n.data)||void 0===d?void 0:d.type)){const e=n.data.status;let t,o;switch(e){case"expired":t="expire",o="error";break;case"failed":default:t="failure",o="error";break;case"canceled":t="cancel",o="error"}const a={transactionStatus:t,status:o,rawData:{type:"PAYMENT_RESULT",status:e}};return null===(s=p.onError)||void 0===s||s.call(p,a),void k()}if("object"!=typeof(f=n.data)||null===f||"string"!=typeof f.transaction_status)return;var f;const m=function(n){const e=n.transaction_status;return{transactionStatus:e,status:(t=e,a.includes(t)?"success":r.includes(t)?"pending":(i.includes(t),"error")),rawData:n};var t}(n.data);switch(m.status){case"success":null===(l=p.onSuccess)||void 0===l||l.call(p,m);break;case"pending":null===(c=p.onPending)||void 0===c||c.call(p,m);break;case"error":null===(u=p.onError)||void 0===u||u.call(p,m)}}function x(n){"Escape"===n.key&&k()}function w(n){n.target.id===p&&k()}function E(n,e,o,a){const r=function(n){return`${l.sandbox?l.sandboxPaymentUrl:l.productionPaymentUrl}/${n}`}(e);if(document.getElementById(u))return;!function(){if(document.getElementById("opg-sdk-styles"))return;const n=document.createElement("style");n.id="opg-sdk-styles",n.textContent=`\n #${p} {\n position: fixed;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n background: rgba(0, 0, 0, 0.6);\n z-index: 999998;\n }\n\n #${f} {\n position: fixed;\n top: 50%;\n left: 50%;\n transform: translate(-50%, -50%);\n width: 90%;\n max-width: 480px;\n height: 80%;\n max-height: 700px;\n background: #fff;\n border-radius: 12px;\n box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);\n z-index: 999999;\n display: flex;\n flex-direction: column;\n overflow: hidden;\n }\n\n #${y} {\n position: absolute;\n top: 12px;\n right: 12px;\n width: 32px;\n height: 32px;\n border: none;\n background: rgba(0, 0, 0, 0.1);\n border-radius: 50%;\n cursor: pointer;\n display: flex;\n align-items: center;\n justify-content: center;\n z-index: 10;\n transition: background 0.2s;\n }\n\n #${y}:hover {\n background: rgba(0, 0, 0, 0.2);\n }\n\n #${y}::before,\n #${y}::after {\n content: '';\n position: absolute;\n width: 16px;\n height: 2px;\n background: #333;\n }\n\n #${y}::before {\n transform: rotate(45deg);\n }\n\n #${y}::after {\n transform: rotate(-45deg);\n }\n\n #${m} {\n width: 100%;\n height: 100%;\n border: none;\n flex: 1;\n }\n\n #${b} {\n position: absolute;\n top: 50%;\n left: 50%;\n transform: translate(-50%, -50%);\n width: 40px;\n height: 40px;\n border: 3px solid #f3f3f3;\n border-top: 3px solid #3498db;\n border-radius: 50%;\n animation: opg-spin 1s linear infinite;\n }\n\n @keyframes opg-spin {\n 0% { transform: translate(-50%, -50%) rotate(0deg); }\n 100% { transform: translate(-50%, -50%) rotate(360deg); }\n }\n\n #${b}.hidden {\n display: none;\n }\n `,document.head.appendChild(n)}(),document.body.style.overflow="hidden";const i=document.createElement("div");i.id=u;const d=document.createElement("div");d.id=p,d.addEventListener("click",w);const s=document.createElement("div");s.id=f;const c=document.createElement("button");c.id=y,c.type="button",c.setAttribute("aria-label","Close payment"),c.addEventListener("click",()=>k());const E=document.createElement("div");E.id=b;const P=document.createElement("iframe");var S;P.id=m,P.src=r,P.setAttribute("allow","payment"),P.setAttribute("allow","clipboard-write"),P.addEventListener("load",()=>{E.classList.add("hidden")}),P.onload=()=>{var e;null===(e=P.contentWindow)||void 0===e||e.postMessage({type:"INIT",clientSecret:n,publicKey:o,token:a},r)},s.appendChild(c),s.appendChild(E),s.appendChild(P),i.appendChild(d),i.appendChild(s),document.body.appendChild(i),g=h,v=x,window.addEventListener("message",g),document.addEventListener("keydown",v),S={isOpen:!0,url:r},t={...t,...S}}function k(){const n=document.getElementById(u);n&&(g&&(window.removeEventListener("message",g),g=null),v&&(document.removeEventListener("keydown",v),v=null),n.remove(),document.body.style.overflow="",t={...e,isOpen:!1,callbacks:t.callbacks})}var P,S,C;const $=null!==(S=null===(P=document.currentScript)||void 0===P?void 0:P.getAttribute("data-client-key"))&&void 0!==S?S:void 0;function T(n){if(o())return void console.warn("[opg-sdk] Payment modal is already open");if(!n.token)throw new Error("[opg-sdk] Payment token is required");if(!n.clientSecret)throw new Error("[opg-sdk] Payment clientSecret is required");if(!n.paymentId)throw new Error("[opg-sdk] Payment paymentId is required");if(!$)throw new Error("[opg-sdk] Payment publicKey is required");void 0!==n.sandbox&&c({sandbox:n.sandbox});const{clientSecret:e,paymentId:a,token:r,onSuccess:i,onPending:d,onError:s}=n;var l;l={onSuccess:i,onPending:d,onError:s},t.callbacks=l,E(e,a,$,r)}function L(){k()}function A(){return o()}function O(n){c(n)}function _(){return l.sandbox}c({sandbox:"false"!==(null===(C=document.currentScript)||void 0===C?void 0:C.getAttribute("data-sandbox"))});const j={openPayment:T,closePayment:L,isPaymentOpen:A,configure:O,isSandbox:_};n.closePayment=L,n.configure=O,n.default=j,n.isPaymentOpen=A,n.isSandbox=_,n.openPayment=T,Object.defineProperty(n,"__esModule",{value:!0})});
|
|
2
2
|
//# sourceMappingURL=opg-sdk.umd.js.map
|
package/dist/opg-sdk.umd.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"opg-sdk.umd.js","sources":["../src/state.ts","../src/utils.ts","../src/config.ts","../src/modal.ts","../src/index.ts"],"sourcesContent":["import type { ModalState, PaymentCallbacks } from './types';\n\nconst initialState: ModalState = {\n isOpen: false,\n url: null,\n callbacks: {},\n};\n\nlet state: ModalState = { ...initialState };\n\nexport function getState(): ModalState {\n return state;\n}\n\nexport function setState(newState: Partial<ModalState>): void {\n state = { ...state, ...newState };\n}\n\nexport function resetState(): void {\n state = { ...initialState, isOpen: false, callbacks: state.callbacks };\n}\n\nexport function isOpen(): boolean {\n return state.isOpen;\n}\n\nexport function getCallbacks(): PaymentCallbacks {\n return state.callbacks;\n}\n\nexport function setCallbacks(callbacks: PaymentCallbacks): void {\n state.callbacks = callbacks;\n}\n","import type { TransactionStatus, PaymentResultStatus, PaymentResult, PaymentMessage } from './types';\n\nconst SUCCESS_STATUSES: TransactionStatus[] = ['settlement', 'capture'];\nconst PENDING_STATUSES: TransactionStatus[] = ['pending', 'challenge'];\nconst ERROR_STATUSES: TransactionStatus[] = ['deny', 'cancel', 'expire', 'failure'];\n\nexport function mapTransactionStatus(status: TransactionStatus): PaymentResultStatus {\n if (SUCCESS_STATUSES.includes(status)) return 'success';\n if (PENDING_STATUSES.includes(status)) return 'pending';\n if (ERROR_STATUSES.includes(status)) return 'error';\n return 'error';\n}\n\nexport function isValidPaymentMessage(data: unknown): data is PaymentMessage {\n if (typeof data !== 'object' || data === null) return false;\n const msg = data as Record<string, unknown>;\n return typeof msg.transaction_status === 'string';\n}\n\nexport function createPaymentResult(message: PaymentMessage): PaymentResult {\n const transactionStatus = message.transaction_status;\n return {\n transactionStatus,\n status: mapTransactionStatus(transactionStatus),\n rawData: message,\n };\n}\n\nexport function lockBodyScroll(): void {\n document.body.style.overflow = 'hidden';\n}\n\nexport function unlockBodyScroll(): void {\n document.body.style.overflow = '';\n}\n","export interface SDKConfig {\n sandbox: boolean;\n sandboxPaymentUrl: string;\n productionPaymentUrl: string;\n}\n\nconst DEFAULT_CONFIG: SDKConfig = {\n sandbox: true,\n sandboxPaymentUrl: process.env.OPG_SANDBOX_PAYMENT_URL ?? \"\",\n productionPaymentUrl: process.env.OPG_PRODUCTION_PAYMENT_URL ?? \"\",\n};\n\nlet currentConfig: SDKConfig = { ...DEFAULT_CONFIG };\n\nexport function configure(config: Partial<SDKConfig>): void {\n currentConfig = { ...currentConfig, ...config };\n}\n\nexport function getConfig(): SDKConfig {\n return currentConfig;\n}\n\nexport function getPaymentUrl(paymentId: string): string {\n const baseUrl = currentConfig.sandbox \n ? currentConfig.sandboxPaymentUrl \n : currentConfig.productionPaymentUrl;\n \n return `${baseUrl}/${paymentId}`;\n}\n\nexport function isSandboxMode(): boolean {\n return currentConfig.sandbox;\n}\n","import { getCallbacks, setState, resetState } from './state';\nimport { isValidPaymentMessage, createPaymentResult, lockBodyScroll, unlockBodyScroll } from './utils';\nimport { getPaymentUrl } from './config';\n\nconst MODAL_ID = 'opg-payment-modal';\nconst BACKDROP_ID = 'opg-payment-backdrop';\nconst CONTAINER_ID = 'opg-payment-container';\nconst IFRAME_ID = 'opg-payment-iframe';\nconst CLOSE_BTN_ID = 'opg-payment-close';\nconst SPINNER_ID = 'opg-payment-spinner';\n\nlet messageHandler: ((event: MessageEvent) => void) | null = null;\nlet keyHandler: ((event: KeyboardEvent) => void) | null = null;\n\nfunction injectStyles(): void {\n if (document.getElementById('opg-sdk-styles')) return;\n\n const style = document.createElement('style');\n style.id = 'opg-sdk-styles';\n style.textContent = `\n #${BACKDROP_ID} {\n position: fixed;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n background: rgba(0, 0, 0, 0.6);\n z-index: 999998;\n }\n\n #${CONTAINER_ID} {\n position: fixed;\n top: 50%;\n left: 50%;\n transform: translate(-50%, -50%);\n width: 90%;\n max-width: 480px;\n height: 80%;\n max-height: 700px;\n background: #fff;\n border-radius: 12px;\n box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);\n z-index: 999999;\n display: flex;\n flex-direction: column;\n overflow: hidden;\n }\n\n #${CLOSE_BTN_ID} {\n position: absolute;\n top: 12px;\n right: 12px;\n width: 32px;\n height: 32px;\n border: none;\n background: rgba(0, 0, 0, 0.1);\n border-radius: 50%;\n cursor: pointer;\n display: flex;\n align-items: center;\n justify-content: center;\n z-index: 10;\n transition: background 0.2s;\n }\n\n #${CLOSE_BTN_ID}:hover {\n background: rgba(0, 0, 0, 0.2);\n }\n\n #${CLOSE_BTN_ID}::before,\n #${CLOSE_BTN_ID}::after {\n content: '';\n position: absolute;\n width: 16px;\n height: 2px;\n background: #333;\n }\n\n #${CLOSE_BTN_ID}::before {\n transform: rotate(45deg);\n }\n\n #${CLOSE_BTN_ID}::after {\n transform: rotate(-45deg);\n }\n\n #${IFRAME_ID} {\n width: 100%;\n height: 100%;\n border: none;\n flex: 1;\n }\n\n #${SPINNER_ID} {\n position: absolute;\n top: 50%;\n left: 50%;\n transform: translate(-50%, -50%);\n width: 40px;\n height: 40px;\n border: 3px solid #f3f3f3;\n border-top: 3px solid #3498db;\n border-radius: 50%;\n animation: opg-spin 1s linear infinite;\n }\n\n @keyframes opg-spin {\n 0% { transform: translate(-50%, -50%) rotate(0deg); }\n 100% { transform: translate(-50%, -50%) rotate(360deg); }\n }\n\n #${SPINNER_ID}.hidden {\n display: none;\n }\n `;\n document.head.appendChild(style);\n}\n\nfunction handleMessage(event: MessageEvent): void {\n const callbacks = getCallbacks();\n\n if (event.data.type === \"COPY_TEXT\") {\n navigator.clipboard.writeText(event.data.value);\n return;\n }\n\n if (event.data?.type === 'PAYMENT_SUCCESS') {\n\n const result = {\n transactionStatus: 'settlement' as const,\n status: 'success' as const,\n rawData: { type: 'PAYMENT_SUCCESS' },\n };\n\n callbacks.onSuccess?.(result);\n closeModal();\n return;\n }\n\n if (event.data?.type === 'PAYMENT_RESULT') {\n const status = event.data.status;\n let transactionStatus: string;\n let mappedStatus: 'success' | 'pending' | 'error';\n\n switch (status) {\n case 'expired':\n transactionStatus = 'expire';\n mappedStatus = 'error';\n break;\n case 'failed':\n transactionStatus = 'failure';\n mappedStatus = 'error';\n break;\n case 'canceled':\n transactionStatus = 'cancel';\n mappedStatus = 'error';\n break;\n default:\n transactionStatus = 'failure';\n mappedStatus = 'error';\n }\n\n const result = {\n transactionStatus: transactionStatus as any,\n status: mappedStatus,\n rawData: { type: 'PAYMENT_RESULT', status },\n };\n\n callbacks.onError?.(result);\n closeModal();\n return;\n }\n\n if (!isValidPaymentMessage(event.data)) return;\n\n const result = createPaymentResult(event.data);\n\n switch (result.status) {\n case 'success':\n callbacks.onSuccess?.(result);\n break;\n case 'pending':\n callbacks.onPending?.(result);\n break;\n case 'error':\n callbacks.onError?.(result);\n break;\n }\n}\n\nfunction handleKeydown(event: KeyboardEvent): void {\n if (event.key === 'Escape') {\n closeModal();\n }\n}\n\nfunction handleBackdropClick(event: MouseEvent): void {\n if ((event.target as HTMLElement).id === BACKDROP_ID) {\n closeModal();\n }\n}\n\nexport function createModal(clientSecret: string, paymentId: string, publicKey: string, token: string): void {\n const url = getPaymentUrl(paymentId);\n if (document.getElementById(MODAL_ID)) return;\n\n injectStyles();\n lockBodyScroll();\n\n const modal = document.createElement('div');\n modal.id = MODAL_ID;\n\n const backdrop = document.createElement('div');\n backdrop.id = BACKDROP_ID;\n backdrop.addEventListener('click', handleBackdropClick);\n\n const container = document.createElement('div');\n container.id = CONTAINER_ID;\n\n const closeBtn = document.createElement('button');\n closeBtn.id = CLOSE_BTN_ID;\n closeBtn.type = 'button';\n closeBtn.setAttribute('aria-label', 'Close payment');\n closeBtn.addEventListener('click', () => closeModal());\n\n const spinner = document.createElement('div');\n spinner.id = SPINNER_ID;\n const iframe = document.createElement('iframe');\n iframe.id = IFRAME_ID;\n iframe.src = url;\n iframe.setAttribute('allow', 'payment');\n iframe.setAttribute(\"allow\", \"clipboard-write\");\n iframe.addEventListener('load', () => {\n spinner.classList.add('hidden');\n });\n\n iframe.onload = () => {\n iframe.contentWindow?.postMessage(\n {\n type: \"INIT\",\n clientSecret,\n publicKey,\n token\n },\n url\n );\n };\n\n container.appendChild(closeBtn);\n container.appendChild(spinner);\n container.appendChild(iframe);\n modal.appendChild(backdrop);\n modal.appendChild(container);\n document.body.appendChild(modal);\n\n messageHandler = handleMessage;\n keyHandler = handleKeydown;\n window.addEventListener('message', messageHandler);\n document.addEventListener('keydown', keyHandler);\n\n setState({ isOpen: true, url });\n}\n\nexport function closeModal(): void {\n const modal = document.getElementById(MODAL_ID);\n if (!modal) return;\n\n if (messageHandler) {\n window.removeEventListener('message', messageHandler);\n messageHandler = null;\n }\n\n if (keyHandler) {\n document.removeEventListener('keydown', keyHandler);\n keyHandler = null;\n }\n\n modal.remove();\n unlockBodyScroll();\n\n resetState();\n}\n","import type { OpenPaymentOptions, OPGSDK, SDKConfiguration } from './types';\nimport { isOpen, setCallbacks } from './state';\nimport { createModal, closeModal } from './modal';\nimport { configure as configureSDK, isSandboxMode, getConfig } from './config';\n\nexport type {\n TransactionStatus,\n PaymentResultStatus,\n PaymentResult,\n PaymentCallbacks,\n OpenPaymentOptions,\n OPGSDK,\n SDKConfiguration,\n} from './types';\n\nconst publicKey = document.currentScript?.getAttribute(\"data-client-key\") ?? undefined;\nconst sandboxAttr = document.currentScript?.getAttribute(\"data-sandbox\");\nconst isSandboxFromAttr = sandboxAttr !== 'false';\n\nconfigureSDK({ sandbox: isSandboxFromAttr });\n\nfunction openPayment(options: OpenPaymentOptions): void {\n if (isOpen()) {\n console.warn('[opg-sdk] Payment modal is already open');\n return;\n }\n\n if (!options.token) {\n throw new Error('[opg-sdk] Payment token is required');\n }\n\n if (!options.clientSecret) {\n throw new Error('[opg-sdk] Payment clientSecret is required');\n }\n\n if (!options.paymentId) {\n throw new Error('[opg-sdk] Payment paymentId is required');\n }\n\n if (!publicKey) {\n throw new Error('[opg-sdk] Payment publicKey is required');\n }\n\n if (options.sandbox !== undefined) {\n configureSDK({ sandbox: options.sandbox });\n }\n\n const { clientSecret, paymentId, token, onSuccess, onPending, onError } = options;\n\n setCallbacks({\n onSuccess,\n onPending,\n onError,\n });\n\n createModal(clientSecret, paymentId, publicKey, token);\n}\n\nfunction closePayment(): void {\n closeModal();\n}\n\nfunction isPaymentOpen(): boolean {\n return isOpen();\n}\n\nfunction configure(config: Partial<SDKConfiguration>): void {\n configureSDK(config);\n}\n\nfunction isSandbox(): boolean {\n return isSandboxMode();\n}\n\nconst OPG: OPGSDK = {\n openPayment,\n closePayment,\n isPaymentOpen,\n configure,\n isSandbox,\n};\n\nexport { openPayment, closePayment, isPaymentOpen, configure, isSandbox };\nexport default OPG;\n"],"names":["initialState","isOpen","url","callbacks","state","SUCCESS_STATUSES","PENDING_STATUSES","ERROR_STATUSES","currentConfig","sandbox","sandboxPaymentUrl","productionPaymentUrl","configure","config","MODAL_ID","BACKDROP_ID","CONTAINER_ID","IFRAME_ID","CLOSE_BTN_ID","SPINNER_ID","messageHandler","keyHandler","handleMessage","event","data","type","navigator","clipboard","writeText","value","_a","result","transactionStatus","status","rawData","_b","onSuccess","call","closeModal","_c","mappedStatus","_d","onError","transaction_status","message","includes","createPaymentResult","_e","_f","onPending","_g","handleKeydown","key","handleBackdropClick","target","id","createModal","clientSecret","paymentId","publicKey","token","getPaymentUrl","document","getElementById","style","createElement","textContent","head","appendChild","injectStyles","body","overflow","modal","backdrop","addEventListener","container","closeBtn","setAttribute","spinner","iframe","newState","src","classList","add","onload","contentWindow","postMessage","window","removeEventListener","remove","currentScript","getAttribute","undefined","openPayment","options","console","warn","Error","configureSDK","closePayment","isPaymentOpen","isSandbox","OPG"],"mappings":"0OAEA,MAAMA,EAA2B,CAC/BC,QAAQ,EACRC,IAAK,KACLC,UAAW,CAAA,GAGb,IAAIC,EAAoB,IAAKJ,YAcbC,IACd,OAAOG,EAAMH,MACf,CCtBA,MAAMI,EAAwC,CAAC,aAAc,WACvDC,EAAwC,CAAC,UAAW,aACpDC,EAAsC,CAAC,OAAQ,SAAU,SAAU,mBCQzE,IAAIC,EAA2B,IANG,CAChCC,SAAS,EACTC,4BAAmB,sCAAuC,GAC1DC,+BAAsB,kCAA0C,KAK5D,SAAUC,EAAUC,GACxBL,EAAgB,IAAKA,KAAkBK,EACzC,CCZA,MAAMC,EAAW,oBACXC,EAAc,uBACdC,EAAe,wBACfC,EAAY,qBACZC,EAAe,oBACfC,EAAa,sBAEnB,IAAIC,EAAyD,KACzDC,EAAsD,KA0G1D,SAASC,EAAcC,qBACrB,MAAMpB,EH5FCC,EAAMD,UG8Fb,GAAwB,cAApBoB,EAAMC,KAAKC,KAEb,YADAC,UAAUC,UAAUC,UAAUL,EAAMC,KAAKK,OAI3C,GAAyB,6BAArBC,EAAAP,EAAMC,2BAAMC,MAA4B,CAE1C,MAAMM,EAAS,CACbC,kBAAmB,aACnBC,OAAQ,UACRC,QAAS,CAAET,KAAM,oBAKnB,OAFmB,QAAnBU,EAAAhC,EAAUiC,qBAASD,GAAAA,EAAAE,KAAAlC,EAAG4B,QACtBO,GAEF,CAEA,GAAyB,4BAArBC,EAAAhB,EAAMC,2BAAMC,MAA2B,CACzC,MAAMQ,EAASV,EAAMC,KAAKS,OAC1B,IAAID,EACAQ,EAEJ,OAAQP,GACN,IAAK,UACHD,EAAoB,SACpBQ,EAAe,QACf,MACF,IAAK,SAQL,QACER,EAAoB,UACpBQ,EAAe,cANjB,IAAK,WACHR,EAAoB,SACpBQ,EAAe,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AAA,EAAEoD,aAAa,mBA0DzD,MAAMW,EAAc,CAClBT,cACAM,eACAC,gBACAxF,YACAyF"}
|
|
1
|
+
{"version":3,"file":"opg-sdk.umd.js","sources":["../src/state.ts","../src/utils.ts","../src/config.ts","../src/modal.ts","../src/index.ts"],"sourcesContent":["import type { ModalState, PaymentCallbacks } from './types';\n\nconst initialState: ModalState = {\n isOpen: false,\n url: null,\n callbacks: {},\n};\n\nlet state: ModalState = { ...initialState };\n\nexport function getState(): ModalState {\n return state;\n}\n\nexport function setState(newState: Partial<ModalState>): void {\n state = { ...state, ...newState };\n}\n\nexport function resetState(): void {\n state = { ...initialState, isOpen: false, callbacks: state.callbacks };\n}\n\nexport function isOpen(): boolean {\n return state.isOpen;\n}\n\nexport function getCallbacks(): PaymentCallbacks {\n return state.callbacks;\n}\n\nexport function setCallbacks(callbacks: PaymentCallbacks): void {\n state.callbacks = callbacks;\n}\n","import type { TransactionStatus, PaymentResultStatus, PaymentResult, PaymentMessage } from './types';\n\nconst SUCCESS_STATUSES: TransactionStatus[] = ['settlement', 'capture'];\nconst PENDING_STATUSES: TransactionStatus[] = ['pending', 'challenge'];\nconst ERROR_STATUSES: TransactionStatus[] = ['deny', 'cancel', 'expire', 'failure'];\n\nexport function mapTransactionStatus(status: TransactionStatus): PaymentResultStatus {\n if (SUCCESS_STATUSES.includes(status)) return 'success';\n if (PENDING_STATUSES.includes(status)) return 'pending';\n if (ERROR_STATUSES.includes(status)) return 'error';\n return 'error';\n}\n\nexport function isValidPaymentMessage(data: unknown): data is PaymentMessage {\n if (typeof data !== 'object' || data === null) return false;\n const msg = data as Record<string, unknown>;\n return typeof msg.transaction_status === 'string';\n}\n\nexport function createPaymentResult(message: PaymentMessage): PaymentResult {\n const transactionStatus = message.transaction_status;\n return {\n transactionStatus,\n status: mapTransactionStatus(transactionStatus),\n rawData: message,\n };\n}\n\nexport function lockBodyScroll(): void {\n document.body.style.overflow = 'hidden';\n}\n\nexport function unlockBodyScroll(): void {\n document.body.style.overflow = '';\n}\n","export interface SDKConfig {\n sandbox: boolean;\n sandboxPaymentUrl: string;\n productionPaymentUrl: string;\n}\n\nconst DEFAULT_CONFIG: SDKConfig = {\n sandbox: true,\n sandboxPaymentUrl: process.env.OPG_SANDBOX_PAYMENT_URL ?? \"https://pay-dev.ojire.online/pay\",\n productionPaymentUrl: process.env.OPG_PRODUCTION_PAYMENT_URL ?? \"https://pay.ojire.online/pay\",\n};\n\nlet currentConfig: SDKConfig = { ...DEFAULT_CONFIG };\n\nexport function configure(config: Partial<SDKConfig>): void {\n currentConfig = { ...currentConfig, ...config };\n}\n\nexport function getConfig(): SDKConfig {\n return currentConfig;\n}\n\nexport function getPaymentUrl(paymentId: string): string {\n const baseUrl = currentConfig.sandbox \n ? currentConfig.sandboxPaymentUrl \n : currentConfig.productionPaymentUrl;\n \n return `${baseUrl}/${paymentId}`;\n}\n\nexport function isSandboxMode(): boolean {\n return currentConfig.sandbox;\n}\n","import { getCallbacks, setState, resetState } from './state';\nimport { isValidPaymentMessage, createPaymentResult, lockBodyScroll, unlockBodyScroll } from './utils';\nimport { getPaymentUrl } from './config';\n\nconst MODAL_ID = 'opg-payment-modal';\nconst BACKDROP_ID = 'opg-payment-backdrop';\nconst CONTAINER_ID = 'opg-payment-container';\nconst IFRAME_ID = 'opg-payment-iframe';\nconst CLOSE_BTN_ID = 'opg-payment-close';\nconst SPINNER_ID = 'opg-payment-spinner';\n\nlet messageHandler: ((event: MessageEvent) => void) | null = null;\nlet keyHandler: ((event: KeyboardEvent) => void) | null = null;\n\nfunction injectStyles(): void {\n if (document.getElementById('opg-sdk-styles')) return;\n\n const style = document.createElement('style');\n style.id = 'opg-sdk-styles';\n style.textContent = `\n #${BACKDROP_ID} {\n position: fixed;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n background: rgba(0, 0, 0, 0.6);\n z-index: 999998;\n }\n\n #${CONTAINER_ID} {\n position: fixed;\n top: 50%;\n left: 50%;\n transform: translate(-50%, -50%);\n width: 90%;\n max-width: 480px;\n height: 80%;\n max-height: 700px;\n background: #fff;\n border-radius: 12px;\n box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);\n z-index: 999999;\n display: flex;\n flex-direction: column;\n overflow: hidden;\n }\n\n #${CLOSE_BTN_ID} {\n position: absolute;\n top: 12px;\n right: 12px;\n width: 32px;\n height: 32px;\n border: none;\n background: rgba(0, 0, 0, 0.1);\n border-radius: 50%;\n cursor: pointer;\n display: flex;\n align-items: center;\n justify-content: center;\n z-index: 10;\n transition: background 0.2s;\n }\n\n #${CLOSE_BTN_ID}:hover {\n background: rgba(0, 0, 0, 0.2);\n }\n\n #${CLOSE_BTN_ID}::before,\n #${CLOSE_BTN_ID}::after {\n content: '';\n position: absolute;\n width: 16px;\n height: 2px;\n background: #333;\n }\n\n #${CLOSE_BTN_ID}::before {\n transform: rotate(45deg);\n }\n\n #${CLOSE_BTN_ID}::after {\n transform: rotate(-45deg);\n }\n\n #${IFRAME_ID} {\n width: 100%;\n height: 100%;\n border: none;\n flex: 1;\n }\n\n #${SPINNER_ID} {\n position: absolute;\n top: 50%;\n left: 50%;\n transform: translate(-50%, -50%);\n width: 40px;\n height: 40px;\n border: 3px solid #f3f3f3;\n border-top: 3px solid #3498db;\n border-radius: 50%;\n animation: opg-spin 1s linear infinite;\n }\n\n @keyframes opg-spin {\n 0% { transform: translate(-50%, -50%) rotate(0deg); }\n 100% { transform: translate(-50%, -50%) rotate(360deg); }\n }\n\n #${SPINNER_ID}.hidden {\n display: none;\n }\n `;\n document.head.appendChild(style);\n}\n\nfunction handleMessage(event: MessageEvent): void {\n const callbacks = getCallbacks();\n\n if (event.data.type === \"COPY_TEXT\") {\n navigator.clipboard.writeText(event.data.value);\n return;\n }\n\n if (event.data?.type === 'PAYMENT_SUCCESS') {\n\n const result = {\n transactionStatus: 'settlement' as const,\n status: 'success' as const,\n rawData: { type: 'PAYMENT_SUCCESS' },\n };\n\n callbacks.onSuccess?.(result);\n closeModal();\n return;\n }\n\n if (event.data?.type === 'PAYMENT_RESULT') {\n const status = event.data.status;\n let transactionStatus: string;\n let mappedStatus: 'success' | 'pending' | 'error';\n\n switch (status) {\n case 'expired':\n transactionStatus = 'expire';\n mappedStatus = 'error';\n break;\n case 'failed':\n transactionStatus = 'failure';\n mappedStatus = 'error';\n break;\n case 'canceled':\n transactionStatus = 'cancel';\n mappedStatus = 'error';\n break;\n default:\n transactionStatus = 'failure';\n mappedStatus = 'error';\n }\n\n const result = {\n transactionStatus: transactionStatus as any,\n status: mappedStatus,\n rawData: { type: 'PAYMENT_RESULT', status },\n };\n\n callbacks.onError?.(result);\n closeModal();\n return;\n }\n\n if (!isValidPaymentMessage(event.data)) return;\n\n const result = createPaymentResult(event.data);\n\n switch (result.status) {\n case 'success':\n callbacks.onSuccess?.(result);\n break;\n case 'pending':\n callbacks.onPending?.(result);\n break;\n case 'error':\n callbacks.onError?.(result);\n break;\n }\n}\n\nfunction handleKeydown(event: KeyboardEvent): void {\n if (event.key === 'Escape') {\n closeModal();\n }\n}\n\nfunction handleBackdropClick(event: MouseEvent): void {\n if ((event.target as HTMLElement).id === BACKDROP_ID) {\n closeModal();\n }\n}\n\nexport function createModal(clientSecret: string, paymentId: string, publicKey: string, token: string): void {\n const url = getPaymentUrl(paymentId);\n if (document.getElementById(MODAL_ID)) return;\n\n injectStyles();\n lockBodyScroll();\n\n const modal = document.createElement('div');\n modal.id = MODAL_ID;\n\n const backdrop = document.createElement('div');\n backdrop.id = BACKDROP_ID;\n backdrop.addEventListener('click', handleBackdropClick);\n\n const container = document.createElement('div');\n container.id = CONTAINER_ID;\n\n const closeBtn = document.createElement('button');\n closeBtn.id = CLOSE_BTN_ID;\n closeBtn.type = 'button';\n closeBtn.setAttribute('aria-label', 'Close payment');\n closeBtn.addEventListener('click', () => closeModal());\n\n const spinner = document.createElement('div');\n spinner.id = SPINNER_ID;\n const iframe = document.createElement('iframe');\n iframe.id = IFRAME_ID;\n iframe.src = url;\n iframe.setAttribute('allow', 'payment');\n iframe.setAttribute(\"allow\", \"clipboard-write\");\n iframe.addEventListener('load', () => {\n spinner.classList.add('hidden');\n });\n\n iframe.onload = () => {\n iframe.contentWindow?.postMessage(\n {\n type: \"INIT\",\n clientSecret,\n publicKey,\n token\n },\n url\n );\n };\n\n container.appendChild(closeBtn);\n container.appendChild(spinner);\n container.appendChild(iframe);\n modal.appendChild(backdrop);\n modal.appendChild(container);\n document.body.appendChild(modal);\n\n messageHandler = handleMessage;\n keyHandler = handleKeydown;\n window.addEventListener('message', messageHandler);\n document.addEventListener('keydown', keyHandler);\n\n setState({ isOpen: true, url });\n}\n\nexport function closeModal(): void {\n const modal = document.getElementById(MODAL_ID);\n if (!modal) return;\n\n if (messageHandler) {\n window.removeEventListener('message', messageHandler);\n messageHandler = null;\n }\n\n if (keyHandler) {\n document.removeEventListener('keydown', keyHandler);\n keyHandler = null;\n }\n\n modal.remove();\n unlockBodyScroll();\n\n resetState();\n}\n","import type { OpenPaymentOptions, OPGSDK, SDKConfiguration } from './types';\nimport { isOpen, setCallbacks } from './state';\nimport { createModal, closeModal } from './modal';\nimport { configure as configureSDK, isSandboxMode, getConfig } from './config';\n\nexport type {\n TransactionStatus,\n PaymentResultStatus,\n PaymentResult,\n PaymentCallbacks,\n OpenPaymentOptions,\n OPGSDK,\n SDKConfiguration,\n} from './types';\n\nconst publicKey = document.currentScript?.getAttribute(\"data-client-key\") ?? undefined;\nconst sandboxAttr = document.currentScript?.getAttribute(\"data-sandbox\");\nconst isSandboxFromAttr = sandboxAttr !== 'false';\n\nconfigureSDK({ sandbox: isSandboxFromAttr });\n\nfunction openPayment(options: OpenPaymentOptions): void {\n if (isOpen()) {\n console.warn('[opg-sdk] Payment modal is already open');\n return;\n }\n\n if (!options.token) {\n throw new Error('[opg-sdk] Payment token is required');\n }\n\n if (!options.clientSecret) {\n throw new Error('[opg-sdk] Payment clientSecret is required');\n }\n\n if (!options.paymentId) {\n throw new Error('[opg-sdk] Payment paymentId is required');\n }\n\n if (!publicKey) {\n throw new Error('[opg-sdk] Payment publicKey is required');\n }\n\n if (options.sandbox !== undefined) {\n configureSDK({ sandbox: options.sandbox });\n }\n\n const { clientSecret, paymentId, token, onSuccess, onPending, onError } = options;\n\n setCallbacks({\n onSuccess,\n onPending,\n onError,\n });\n\n createModal(clientSecret, paymentId, publicKey, token);\n}\n\nfunction closePayment(): void {\n closeModal();\n}\n\nfunction isPaymentOpen(): boolean {\n return isOpen();\n}\n\nfunction configure(config: Partial<SDKConfiguration>): void {\n configureSDK(config);\n}\n\nfunction isSandbox(): boolean {\n return isSandboxMode();\n}\n\nconst OPG: OPGSDK = {\n openPayment,\n closePayment,\n isPaymentOpen,\n configure,\n isSandbox,\n};\n\nexport { openPayment, closePayment, isPaymentOpen, configure, isSandbox };\nexport default OPG;\n"],"names":["initialState","isOpen","url","callbacks","state","SUCCESS_STATUSES","PENDING_STATUSES","ERROR_STATUSES","currentConfig","sandbox","sandboxPaymentUrl","productionPaymentUrl","configure","config","MODAL_ID","BACKDROP_ID","CONTAINER_ID","IFRAME_ID","CLOSE_BTN_ID","SPINNER_ID","messageHandler","keyHandler","handleMessage","event","data","type","navigator","clipboard","writeText","value","_a","result","transactionStatus","status","rawData","_b","onSuccess","call","closeModal","_c","mappedStatus","_d","onError","transaction_status","message","includes","createPaymentResult","_e","_f","onPending","_g","handleKeydown","key","handleBackdropClick","target","id","createModal","clientSecret","paymentId","publicKey","token","getPaymentUrl","document","getElementById","style","createElement","textContent","head","appendChild","injectStyles","body","overflow","modal","backdrop","addEventListener","container","closeBtn","setAttribute","spinner","iframe","newState","src","classList","add","onload","contentWindow","postMessage","window","removeEventListener","remove","currentScript","getAttribute","undefined","openPayment","options","console","warn","Error","configureSDK","closePayment","isPaymentOpen","isSandbox","OPG"],"mappings":"0OAEA,MAAMA,EAA2B,CAC/BC,QAAQ,EACRC,IAAK,KACLC,UAAW,CAAA,GAGb,IAAIC,EAAoB,IAAKJ,YAcbC,IACd,OAAOG,EAAMH,MACf,CCtBA,MA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