xendit-components-web 0.0.9 → 0.0.11
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/CHANGELOG.md +23 -0
- package/README.md +27 -13
- package/package.json +34 -28
- package/sdk/dist/index.d.ts +49 -0
- package/sdk/dist/index.esm.js +3 -2
- package/sdk/dist/index.esm.js.map +1 -1
- package/sdk/dist/index.umd.js +3 -2
- package/sdk/dist/index.umd.js.map +1 -1
- package/README.md.bak +0 -315
package/README.md.bak
DELETED
|
@@ -1,315 +0,0 @@
|
|
|
1
|
-
# xendit-components-web
|
|
2
|
-
|
|
3
|
-
Xendit Components allows you to accept payments directly on your website using the Xendit Sessions API.
|
|
4
|
-
|
|
5
|
-
This SDK's role is to take a Session (created on your server using the Xendit Sessions API) and render a payment UI, and allow you to customize the UI to match your site's look & feel.
|
|
6
|
-
|
|
7
|
-
## Installation
|
|
8
|
-
|
|
9
|
-
Install using npm:
|
|
10
|
-
|
|
11
|
-
```sh
|
|
12
|
-
npm install xendit-components-web --save
|
|
13
|
-
```
|
|
14
|
-
|
|
15
|
-
Or load it directly from our CDN:
|
|
16
|
-
|
|
17
|
-
```html
|
|
18
|
-
<script src="https://assets.xendit.co/components/0.0.9/index.umd.js"></script>
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
Our npm package includes TypeScript types. If you're using the CDN, download type declarations from `https://assets.xendit.co/components/0.0.9/index.d.ts`
|
|
22
|
-
|
|
23
|
-
## Sessions
|
|
24
|
-
|
|
25
|
-
The Xendit Session API is an abstraction over the Xendit Payments API, representing one transaction (or tokenization). Using one session, a user can make any number
|
|
26
|
-
of attempts to pay, one of which can be successful. A successful payment completes the session.
|
|
27
|
-
|
|
28
|
-
Sessions also abstract away any differences between channels, allowing you to write once, and accept payments from any channel Xendit supports.
|
|
29
|
-
|
|
30
|
-
Two types of sessions are available:
|
|
31
|
-
|
|
32
|
-
- `PAY` sessions collect a payment and optionally save a payment token for later use
|
|
33
|
-
- `SAVE` sessions save a payment token
|
|
34
|
-
|
|
35
|
-
## Quick Start
|
|
36
|
-
|
|
37
|
-
First, initialize the SDK either with either:
|
|
38
|
-
|
|
39
|
-
- `XenditComponentsTest` for frontend development or unit tests
|
|
40
|
-
- `XenditComponents`, for production, which requires a `componentsSdkKey`. That key comes from the session object you create on your server. Make an endpoint that creates a session for the user's current transaction, return the `components_sdk_key` to the client, and pass it into the constructor.
|
|
41
|
-
|
|
42
|
-
```typescript
|
|
43
|
-
// For frontend development, use XenditComponentsTest
|
|
44
|
-
const components: XenditComponents = new XenditComponentsTest({});
|
|
45
|
-
// For production or e2e testing, use XenditComponents, passing in the components_sdk_key from the Session object
|
|
46
|
-
const components: XenditComponents = new XenditComponents({ componentsSdkKey });
|
|
47
|
-
|
|
48
|
-
// Create a channel picker component
|
|
49
|
-
const channelPicker: HTMLElement = components.createChannelPickerComponent();
|
|
50
|
-
|
|
51
|
-
// Insert the channel picker into your document
|
|
52
|
-
myCheckoutPage.replaceChildren(channelPicker);
|
|
53
|
-
|
|
54
|
-
// Call submit() when the user clicks your submit button
|
|
55
|
-
mySubmitButton.addEventListener("click", () => {
|
|
56
|
-
components.submit();
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
// Listen to the status of the session
|
|
60
|
-
components.addEventListener("session-complete", () => {
|
|
61
|
-
alert("Payment Success");
|
|
62
|
-
});
|
|
63
|
-
components.addEventListener("session-expired-or-canceled", () => {
|
|
64
|
-
alert("Payment cancelled or expired");
|
|
65
|
-
});
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
## Components API
|
|
69
|
-
|
|
70
|
-
### `XenditComponents` and `XenditComponentsTest`
|
|
71
|
-
|
|
72
|
-
The constructor.
|
|
73
|
-
|
|
74
|
-
For production and e2e testing, you need to pass the `componentsSdkKey`, which you can get when you create a Session on your server.
|
|
75
|
-
|
|
76
|
-
For development and unit testing, use `XenditComponentsTest`, which uses mock data and doesn't connect to Xendit servers.
|
|
77
|
-
|
|
78
|
-
### `createChannelPickerComponent`
|
|
79
|
-
|
|
80
|
-
```typescript
|
|
81
|
-
const htmlElement = components.createChannelPickerComponent();
|
|
82
|
-
myContainer.replaceChildren(htmlElement);
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
Creates a UI for the user to select a payment channel and fill any required information.
|
|
86
|
-
|
|
87
|
-
This returns a `HTMLElement`, which you need to insert into your document.
|
|
88
|
-
|
|
89
|
-
This method uses caching, it will always return the same channel picker element.
|
|
90
|
-
Changing the current channel will update the channel picker UI, even if it's unmounted.
|
|
91
|
-
If you don't want this, use `destroyComponent.`
|
|
92
|
-
|
|
93
|
-
### `getActiveChannels`
|
|
94
|
-
|
|
95
|
-
```typescript
|
|
96
|
-
const channels = components.getActiveChannels();
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
Returns the list of channels available in this session.
|
|
100
|
-
|
|
101
|
-
### `getActiveChannelGroups`
|
|
102
|
-
|
|
103
|
-
```typescript
|
|
104
|
-
const groups = components.getActiveChannelGroups();
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
Returns a list of channel groups. This can be used to categorize channels by type, if you want to build
|
|
108
|
-
your own channel selection UI. Each channel has a `uiGroup` property which matches one group's `id` property.
|
|
109
|
-
|
|
110
|
-
### `createChannelComponent`
|
|
111
|
-
|
|
112
|
-
```typescript
|
|
113
|
-
const channel = components.getActiveChannels({ filter: "CARDS" })[0];
|
|
114
|
-
if (channel) {
|
|
115
|
-
const htmlElement = components.createChannelPickerComponent(channel);
|
|
116
|
-
myContainer.replaceChildren(htmlElement);
|
|
117
|
-
}
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
Selects a payment channel and creates a UI for the user to fill any required information. You
|
|
121
|
-
need to pass in the channel you want to use, as returned from getActiveChannels.
|
|
122
|
-
|
|
123
|
-
This returns a `HTMLElement`, which you need to insert into your document.
|
|
124
|
-
|
|
125
|
-
This method uses caching, it will always return the same element for the same channel, to preserve the
|
|
126
|
-
values the user enters into any form fields. If you don't want that, use `destroyComponent`.
|
|
127
|
-
|
|
128
|
-
### `createActionContainerComponent`
|
|
129
|
-
|
|
130
|
-
```typescript
|
|
131
|
-
components.addEventListener("action-begin", () => {
|
|
132
|
-
const htmlElement = components.createActionContainerComponent();
|
|
133
|
-
myActionContainer.replaceChildren(htmlElement);
|
|
134
|
-
});
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
Creates a container into which any additional actions (e.g. 3DS, QR Codes) will be rendered.
|
|
138
|
-
|
|
139
|
-
This is optional, if you don't create one, the SDK will create a modal with an action container for you.
|
|
140
|
-
You cannot create an action container during an action (i.e. after the `action-begin` event).
|
|
141
|
-
|
|
142
|
-
This returns a `HTMLElement`, which you need to insert into your document.
|
|
143
|
-
|
|
144
|
-
This method does not use caching.
|
|
145
|
-
|
|
146
|
-
### `submit`
|
|
147
|
-
|
|
148
|
-
```typescript
|
|
149
|
-
function onSubmitButtonClick() {
|
|
150
|
-
components.submit();
|
|
151
|
-
}
|
|
152
|
-
```
|
|
153
|
-
|
|
154
|
-
Begins submission for the active payment channel.
|
|
155
|
-
|
|
156
|
-
Call this from the click event of your submit button.
|
|
157
|
-
|
|
158
|
-
Submission is only available when the session is active, a channel is made current by creating a channel component, any required information is collected, and
|
|
159
|
-
another submission is not in progress. Use the `submission-ready` and `submission-not-ready` events to know when submission is available.
|
|
160
|
-
|
|
161
|
-
This calls the [create payment request](https://docs.xendit.co/apidocs/create-payment-request)
|
|
162
|
-
or [create payment token](https://docs.xendit.co/apidocs/create-payment-token) endpoint depending on the session type. You
|
|
163
|
-
may listen to the corresponding webhooks on your server.
|
|
164
|
-
|
|
165
|
-
### `simulatePayment`
|
|
166
|
-
|
|
167
|
-
```typescript
|
|
168
|
-
components.simulatePayment();
|
|
169
|
-
```
|
|
170
|
-
|
|
171
|
-
Calls the [simulate payment](https://docs.xendit.co/apidocs/simulate-payment-test-mode) endpoint.
|
|
172
|
-
|
|
173
|
-
This is only available in test mode sessions. It also requires the payment channel to be a QR, OTC, or VA channel, and it requires an action
|
|
174
|
-
to be in-progress.
|
|
175
|
-
|
|
176
|
-
### `abortSubmission`
|
|
177
|
-
|
|
178
|
-
```typescript
|
|
179
|
-
components.abortSubmission();
|
|
180
|
-
```
|
|
181
|
-
|
|
182
|
-
Cancels the current submission, if any.
|
|
183
|
-
|
|
184
|
-
### `destroyComponent`
|
|
185
|
-
|
|
186
|
-
```typescript
|
|
187
|
-
components.destroyComponent(htmlElement);
|
|
188
|
-
```
|
|
189
|
-
|
|
190
|
-
Destroys a component, deleting any cached data and removing the element from the document. Manual cleanup is not normally required,
|
|
191
|
-
but is made available if you want it.
|
|
192
|
-
|
|
193
|
-
### `showValidationErrors`
|
|
194
|
-
|
|
195
|
-
```typescript
|
|
196
|
-
components.showValidationErrors();
|
|
197
|
-
```
|
|
198
|
-
|
|
199
|
-
Reveals hidden validation errors in the current channel's form, if any.
|
|
200
|
-
|
|
201
|
-
Validation errors are normally hidden until the user changes and unfocusses the input.
|
|
202
|
-
|
|
203
|
-
### `getCurrentChannel`
|
|
204
|
-
|
|
205
|
-
```typescript
|
|
206
|
-
const channel: XenditChannel = components.getCurrentChannel();
|
|
207
|
-
```
|
|
208
|
-
|
|
209
|
-
Returns the current channel.
|
|
210
|
-
|
|
211
|
-
The current channel is the one you or the channel picker component selected by calling `createChannelComponent` or `setCurrentChannel`.
|
|
212
|
-
|
|
213
|
-
The current channel:
|
|
214
|
-
|
|
215
|
-
- Will be used for submission when you call `submit()`
|
|
216
|
-
- Is interactive (other channel components are disabled)
|
|
217
|
-
|
|
218
|
-
### `setCurrentChannel`
|
|
219
|
-
|
|
220
|
-
```typescript
|
|
221
|
-
const channel = components.getActiveChannels({ filter: "CARDS" })[0];
|
|
222
|
-
if (channel) {
|
|
223
|
-
components.setCurrentChannel(channel);
|
|
224
|
-
}
|
|
225
|
-
```
|
|
226
|
-
|
|
227
|
-
Makes the provided channel the current channel.
|
|
228
|
-
|
|
229
|
-
## Events
|
|
230
|
-
|
|
231
|
-
### `init`
|
|
232
|
-
|
|
233
|
-
Notifies you when the session information is loaded. Most SDK functions require the session to be loaded and can only be called after this event.
|
|
234
|
-
|
|
235
|
-
`createChannelPickerComponent` is available before the init event.
|
|
236
|
-
|
|
237
|
-
### `session-complete` and `session-expired-or-canceled`
|
|
238
|
-
|
|
239
|
-
Notifies you when the session is in a terminal state.
|
|
240
|
-
|
|
241
|
-
`session-complete` means the session was successful, `session-expired-or-canceled` means the session was cancelled or expired.
|
|
242
|
-
|
|
243
|
-
### `submission-ready` and `submission-not-ready`
|
|
244
|
-
|
|
245
|
-
Notifies you when the user is ready to submit the payment, meaning a channel is selected and all required information is collected.
|
|
246
|
-
|
|
247
|
-
`submit` will only work in the ready state, or it will throw. Calling it when there are form validation errors will also reveal those errors
|
|
248
|
-
to the user.
|
|
249
|
-
|
|
250
|
-
You might want to disable your submit button when not in the ready state.
|
|
251
|
-
|
|
252
|
-
### `submission-begin` and `submission-end`
|
|
253
|
-
|
|
254
|
-
Notifies you when a submission is in progress.
|
|
255
|
-
|
|
256
|
-
You might want to show a pending state UI when in the submission state.
|
|
257
|
-
|
|
258
|
-
### `action-begin` and `action-end`
|
|
259
|
-
|
|
260
|
-
Notifies you when an action is in progress.
|
|
261
|
-
|
|
262
|
-
You might want to create an action container in the action-begin event.
|
|
263
|
-
|
|
264
|
-
## Appearance
|
|
265
|
-
|
|
266
|
-
### CSS Variables
|
|
267
|
-
|
|
268
|
-
The Xendit Components SDK is customizable by overriding its CSS. The SDK inserts its CSS above any other CSS at the time of loading to allow it to be easily overridden.
|
|
269
|
-
|
|
270
|
-
CSS variables can be overridden to change styles across all components.
|
|
271
|
-
|
|
272
|
-
The following variables are available:
|
|
273
|
-
| Variable | Description |
|
|
274
|
-
| :- | -: |
|
|
275
|
-
| --xendit-font-family | Font applied to all xendit components |
|
|
276
|
-
| --xendit-color-primary | Accent color |
|
|
277
|
-
| --xendit-color-text | Base text color |
|
|
278
|
-
| --xendit-color-text-secondary | Lighter text color |
|
|
279
|
-
| --xendit-color-text-placeholder | Placeholder color |
|
|
280
|
-
| --xendit-color-disabled | Background color of disabled elements |
|
|
281
|
-
| --xendit-color-danger | Border color of elements with validation errors and text color of validation errors |
|
|
282
|
-
| --xendit-color-border | Border color used on accordions, input fields, and logos |
|
|
283
|
-
| --xendit-color-background | Background color of input fields |
|
|
284
|
-
| --xendit-focus-shadow | Box-shadow applied to elements with focus |
|
|
285
|
-
| --xendit-animation-duration | Duration of animations (affects the channel picker accordion) |
|
|
286
|
-
| --xendit-animation-ease | Ease function of animations |
|
|
287
|
-
| --xendit-radius-1 | Border radius applied to some components |
|
|
288
|
-
| --xendit-z-index-focus | Z-index applied to focused fields |
|
|
289
|
-
|
|
290
|
-
### Appearance of Iframe fields
|
|
291
|
-
|
|
292
|
-
Some form fields (credit card inputs) are implemented inside iframes to protect the user's information.
|
|
293
|
-
|
|
294
|
-
You can't override the CSS inside the iframe fields. Instead, you can pass some limited styles to the constructor
|
|
295
|
-
which we'll pass along to the iframes.
|
|
296
|
-
|
|
297
|
-
```typescript
|
|
298
|
-
const sdk = new XenditComponents({
|
|
299
|
-
iframeFieldAppearance: {
|
|
300
|
-
inputStyles: {
|
|
301
|
-
// apply styles to inputs within iframe fields
|
|
302
|
-
color: "#000",
|
|
303
|
-
},
|
|
304
|
-
placeholderStyles: {
|
|
305
|
-
// apply styles to input placeholders in iframe fields
|
|
306
|
-
color: "#ccc",
|
|
307
|
-
},
|
|
308
|
-
fontFace: {
|
|
309
|
-
// insert a @font-face rule inside iframe fields
|
|
310
|
-
source: "url(https://example.com/my-font-file) format(woff2)",
|
|
311
|
-
descriptors: { display: "swap" },
|
|
312
|
-
},
|
|
313
|
-
},
|
|
314
|
-
});
|
|
315
|
-
```
|