@paystack/inline-js 2.19.0-dev.9 → 2.19.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,53 +1,451 @@
1
- # Inline JS
1
+ Public Docs
2
+ ===
3
+ # Documentation for Paystack Inline
2
4
 
3
- This is the client-side JS library for loading the Paystack checkout form. It makes it easy to accept payments from a web application and to create custom, secure payment experiences for merchant apps and websites.
5
+ Paystack Inline is a Javascript library that loads the Paystack Checkout form. This offers a simple, secure and convenient payment flow for web and mobile and can be integrated in a few lines of code. It makes it possible for you to start and end the payment flow on the same page, avoiding redirect fatigue.
4
6
 
5
- # Quick start
7
+ # Easy
6
8
 
7
- ### Requirements
9
+ The easiest way to use Paystack Inline is to add your transactions parameters to a script tag in a form. When the transaction is successful, it will call the action on the form.
8
10
 
11
+ ```html
12
+ <form action="/process" method="POST" >
13
+ <script
14
+ src="https://js.paystack.co/v2/inline.js"
15
+ data-key="pk_test_221221122121"
16
+ data-email="customer@email.com"
17
+ data-amount="10000"
18
+ data-first-name="Opemipo"
19
+ data-last-name="Aikomo"
20
+ >
21
+ </script>
22
+ </form>
9
23
  ```
10
- node >= 18.0.0
11
- npm >= 10.0.0
24
+
25
+ Transaction parameters should be passed as dashed data attributes. To see a list of all transaction parameters, [click here](#general-configuration-options). You can also pass additional attributes for styling
26
+
27
+ ### Configuration Options for Button Styling
28
+
29
+ | Name | Type | Description |
30
+ | ---------------------------- | --------------- | -------------------------------------------------------------------------------------------- |
31
+ | data-button-text | String | This overrides the default button action text. Default is "Pay {Amount}". |
32
+ | data-button-variant | normal or light | You can either render the normal green button or a white button. Default is normal |
33
+ | data-button-wordmark-variant | normal or light | You can also render a white version of the "Secured by Paystack" wordmark. Default is normal |
34
+
35
+ # Custom
36
+
37
+ To set up your custom implementation of Popup, include the javascript directly on your site
38
+
39
+ ```html
40
+ <script src="https://js.paystack.co/v2/inline.js">
41
+ ```
42
+
43
+ or import from npm
44
+
45
+ ```js
46
+ import PaystackPop from '@paystack/inline-js';
47
+ ```
48
+
49
+ ## Quickstart
50
+
51
+ Start a new Paystack transaction and show a message when it is successful
52
+
53
+ ```js
54
+ const paystackInstance = new PaystackPop();
55
+ const onSuccess = transaction => alert(`Succesful! Ref: ${transaction.reference}`);
56
+ paystackInstance.newTransaction({
57
+ key: 'pk_test_TYooMQauvdEDq54NiTphI7jx',
58
+ email: 'demo@paystack.com',
59
+ amount: 10000,
60
+ onSuccess
61
+ });
62
+ ```
63
+
64
+ ---
65
+
66
+ # The PaystackPop Object
67
+
68
+ ### Properties
69
+
70
+ | Name | Description | Response Type |
71
+ | ----------------------- | ----------------------------------------------------------------------------------------- | ------------------ |
72
+ | id | Autogenerated id for the Popup instance | `String` |
73
+ | backgroundDiv | A placeholder div that shows a loading indicator when the main checkout iFrame is loading | `domElement` |
74
+ | checkoutIframe | The iframe where the payment happens | `domElement` |
75
+ | preCheckoutModal | A div for an wallet payments i.e Apple pay pre checkout experience | `domElement` \| `null` |
76
+ | paymentRequestContainer | The div container for wallet payment buttons i.e Apple Pay | `domElement` \| `null` |
77
+
78
+ ## Object Methods
79
+
80
+ - PaystackPop.isLoaded()
81
+ - PaystackPop.newTransaction()
82
+ - PaystackPop.resumeTransaction()
83
+ - PaystackPop.cancelTransaction()
84
+ - PaystackPop.preloadTransaction()
85
+ - PaystackPop.checkout()
86
+ - PaystackPop.paymentRequest()
87
+
88
+ # PaystackPop.isLoaded()
89
+
90
+ This method checks if PaystackPop has downloaded and set up the checkout form. It returns `true` or `false`.
91
+
92
+ # PaystackPop.newTransaction({options})
93
+
94
+ This method starts a new transaction on the checkout form.
95
+
96
+ ### General Configuration Options
97
+ | Option | Required | Type | Description |
98
+ | -------------- | -------- | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
99
+ | key | True | `String` | Your Paystack public key. You can find this on your dashboard in Settings > API Keys & Webhooks |
100
+ | amount | True | `Number` | The amount of the transaction in kobo |
101
+ | currency | False | `String` | The currency of the transaction. Available options in PaystackPop.CURRENCIES object |
102
+ | email | True | `String` | The email address of the customer |
103
+ | firstName | False | `String` | The first name of the customer |
104
+ | lastName | False | `String` | The last name of the customer |
105
+ | phone | False | `String` | The phone number of the customer |
106
+ | customerCode | False | `String` | A valid Paystack customer code. If provided, this overrides all the customer information above |
107
+ | channels | False | `Array` | An array of payment channels to use. By default, all options available in in PaystackPop.CHANNELS are used |
108
+ | paymentRequest | False | `String` | A valid Paystack payment request id |
109
+ | paymentPage | False | `String` | A valid Paystack payment page id |
110
+ | metadata | False | Object | A valid object of extra information that you want to be saved to the transaction. To show this on the dashboard, see [Seeing your metadata on the dashboard](#tip-seeing-your-metadata-on-the-dashboard) |
111
+ | reference | False | `String` | Unique case sensitive transaction reference. Only -,`.`, = and alphanumeric characters allowed. |
112
+
113
+
114
+ ### Configuration Options for Callbacks
115
+
116
+ | Event Name | Description | Response Properties |
117
+ | -------------------- | ----------------------------------------------------| ---------------------------------------------------------- |
118
+ | onError | Called when the transaction was not successfully loaded | `{ message: error message from API }` |
119
+ | onCancel | Called when the customer cancels the transaction | |
120
+ | onLoad | Called when the transaction is successful loaded and the customer can see the checkout form | `{ id: transaction id from API, customer: customer object from API, accessCode: transaction access code }` |
121
+ | onSuccess | Called when the customer successfully completes a transaction | `{ id: transaction id from API, reference: transaction reference, message: message from API }` |
122
+
123
+
124
+
125
+ ### Configuration Options for Split Payments
126
+
127
+ | Option | Required | Type | Description |
128
+ | ----------------------- | -------------- | ---------------| ----------------------------------------------------------------------------------- |
129
+ | subaccountCode | False | `String` | A valid Paystack subaccount code e.g. `ACCT_8f4s1eq7ml6rlzj` |
130
+ | split_code | False | `String` | A valid Paystack split code e.g. `SPL_qQsdYLXddd` |
131
+ | bearer | False | `Number` | Who bears Paystack charges? `account` or `subaccount` (defaults to `account`). |
132
+ | transactionCharge | False | `String` | A flat fee (in kobo) to charge the subaccount for this transaction. This overrides the split percentage set when the subaccount was created. |
133
+
134
+ ### Configuration Options for Subscribing to an Existing Plan
135
+
136
+ | Option | Required | Type | Description |
137
+ | ----------------------- | -------------- | ---------------| ------------------------------------------------------- |
138
+ | planCode | False | `String` | A valid Paystack plan code e.g. `PLN_cujsmvoyq2209ws` |
139
+ | subscriptionCount | False | `Number` | The number of subscriptions to create for this plan |
140
+
141
+ ### Configuration Options for Creating a New Subscription
142
+
143
+ | Option | Required | Type | Description |
144
+ | ----------------------- | -------------- | ---------------| ----------------------------------------------------------------------------------- |
145
+ | planInterval | False | `String` | Interval for the plan. Valid intervals are `hourly`, `daily`, `weekly`, `monthly`, `annually` |
146
+ | subscriptionLimit | False | `Number` | The number of times to charge for this subscription |
147
+ | subscriptionStartDate | False | `String` | The start date for the subscription (after the first charge) |
148
+
149
+
150
+ ## Method Response
151
+
152
+ This method returns a [PopupTransaction Instance](#the-popuptransaction-object)
153
+
154
+ ---
155
+
156
+ ## Tip: **Seeing your metadata on the dashboard**
157
+
158
+ You can add any information you want to save to the transaction in your metadata. However, for the information to be visible on your Paystack dashboard, the metadata has to include the `custom_fields` property, an array of objects containing `display_name`, `variable_name`, and `value`.
159
+
160
+ ```js
161
+ const parameters = {
162
+ key: 'pk_test_TYooMQauvdEDq54NiTphI7jx',
163
+ email: 'demo@paystack.com',
164
+ amount: 10000,
165
+ metadata: {
166
+ custom_fields: [{
167
+ display_name: 'Cart ID',
168
+ variable_name: 'cart_id',
169
+ value: '8393',
170
+ }],
171
+ },
172
+ };
173
+ ```
174
+
175
+ ---
176
+
177
+ # PaystackPop.resumeTransaction({options})
178
+
179
+ This method resumes a transaction using the access code created on your server with [the Paystack API](https://developers.paystack.co/v1.0/reference#initialize-a-transaction).
180
+
181
+ ### Configuration Options
182
+
183
+ | Option | Required | Type | Description |
184
+ | ----------------------------- | -------------- | --------------------| ----------------------------------------------------------------------------- |
185
+ | accessCode | True | `String` | Access code created on the API via the [transaction/initialize](https://paystack.com/docs/#initialize-a-transaction) endpoint |
186
+
187
+ ## Method Response
188
+
189
+ This method returns a [PopupTransaction Instance](#the-popuptransaction-object)
190
+
191
+ ## Tip: Reduced Abandonment Rates
192
+
193
+ When you call `PaystackPop.newTransaction()`, it first checks if there is any **abandoned** transaction attempted with the **same parameters**. If there is one, it resumes that transaction instead of creating a new one.
194
+
195
+ ---
196
+
197
+ # PaystackPop.cancelTransaction(id or transactionObject)
198
+
199
+ Use this to cancel a transaction and hide the checkout iFrame.
200
+
201
+ ### Configuration Options
202
+
203
+ | Option | Required | Type | Description |
204
+ | ----------------------------- | -------------- | -------------------------------| ---------------------------------------------------- |
205
+ | id or transactionObject | True | `String` or `PopupTransaction` | ID or transaction to cancel |
206
+
207
+ ## For Example
208
+
209
+ Cancel Popup and use Paystack Redirect if the transaction doesn't load after 10 seconds.
210
+
211
+ ```js
212
+ const paystackInstance = new PaystackPop();
213
+ const transaction = paystackInstance.newTransaction({
214
+ key: 'pk_test_TYooMQauvdEDq54NiTphI7jx',
215
+ email: 'demo@paystack.com',
216
+ amount: 10000,
217
+ onLoad() {
218
+ window.clearInterval(redirectTimer);
219
+ },
220
+ });
221
+
222
+ let timeElapsed = 0;
223
+ const timeLimit = 10;
224
+ const redirectURL = 'https://link/to/your/server';
225
+ const redirectTimer = setInterval(() => {
226
+ timeElapsed += 1;
227
+ if (timeElapsed === timeLimit) {
228
+ paystackPop.cancelTransaction(transaction);
229
+ window.location.href = redirectURL;
230
+ }
231
+ }, 1000);
12
232
  ```
13
233
 
14
- ### Setup
234
+ ---
235
+
236
+ # PaystackPop.preloadTransaction({options})
237
+
238
+ This method loads a transaction on the checkout form without opening it. It returns a function that can be used to open the checkout form at a later time.
239
+
240
+ ## Configuration Options
241
+
242
+ All General and Custom configuration options that are accepted by [PaystackPop.newTransaction](https://www.notion.so/Documentation-for-Paystack-Inline-d6e595c0277d413db73b9a0d81a9c16c)
243
+
244
+ ## Method Response
245
+
246
+ This method returns a function to open the checkout form
247
+
248
+ ## For Example
15
249
 
16
- Clone this repository
250
+ Load the transaction for a checkout form and enable a button on the page to open the checkout form when a user clicks it
17
251
 
18
- git clone https://github.com/PaystackHQ/inline-js.git
252
+ ```js
19
253
 
20
- Install dependencies with:
254
+ const paystackPop = new PaystackPop();
21
255
 
22
- npm install
256
+ try {
257
+ const loadPopup = paystackPop.preloadTransaction({
258
+ key: config.publicLiveKey,
259
+ email: 'testuser@paystack.com',
260
+ amount: 10000,
261
+ currency: "NGN"
262
+ });
263
+
264
+ document.querySelector("#pay-with-paystack").onclick = loadPopup;
23
265
 
24
- # Local development
266
+ } catch (error) {
25
267
 
26
- To test Inline JS locally, run the command;
268
+ }
27
269
 
28
270
  ```
29
- npm run dev
271
+
272
+ ---
273
+
274
+ # PaystackPop.checkout({options})
275
+
276
+ This method loads a transaction on the checkout form but shows a pre checkout modal before loading the form if a wallet payment e.g Apple Pay is supported.
277
+
278
+ ## Configuration Options
279
+
280
+ All General and Custom configuration options that are accepted by [PopupTransaction Instance](#the-popuptransaction-object)
281
+
282
+ ## Method Response
283
+
284
+ This method returns a Promise which resolves to a [PopupTransaction Instance](#the-popuptransaction-object)
285
+
286
+ ## For Example
287
+
288
+ ```js
289
+ const myPopup = new PaystackPop();
290
+
291
+ const baseParameters = {
292
+ key: config.publicLiveKey,
293
+ email: "testuser@paystack.com",
294
+ channels: ["card", "apple_pay"],
295
+ };
296
+
297
+ const onSuccess = (response) => {
298
+ alert("success. transaction ref is " + response.reference);
299
+ };
300
+
301
+ const onLoad = (response) => {
302
+ console.log(`Successfully loaded transaction: ${response.id}`);
303
+ };
304
+
305
+ const onCancel = (response) => {
306
+ console.log("Transaction cancelled");
307
+ };
308
+
309
+ const onError = (error) => {
310
+ console.log(`Error occured: ${error.message}`);
311
+ };
312
+
313
+ const callbacks = {
314
+ onError,
315
+ onLoad,
316
+ onSuccess,
317
+ onCancel,
318
+ };
319
+
320
+ async function checkout(amount) {
321
+ try {
322
+ const parameters = { ...baseParameters, ...callbacks, amount };
323
+ document.querySelector("#pay-button").disabled = true;
324
+ document.querySelector("#pay-button").innerHTML = "Please wait...";
325
+ const transaction = await myPopup.checkout(parameters);
326
+ document.querySelector("#pay-button").disabled = false;
327
+ document.querySelector("#pay-button").innerHTML = "Pay";
328
+ } catch (e) {
329
+ console.log(e);
330
+ }
331
+ }
30
332
  ```
31
333
 
32
- This spins up a `http-server` serving the `dev` folder and you can then visit the different `HTML` files in that folder to test various APIs and payment flows.
334
+ ---
335
+
336
+ # PaystackPop.paymentRequest({options})
337
+
338
+ This method mounts a wallet payment button e.g Apple pay on a provided div and also provides the option to allow a provided button open the checkout form.
339
+
340
+ ## General Configuration Options
341
+
342
+ All General and Custom configuration options that are accepted by [PaystackPop.newTransaction](https://www.notion.so/Documentation-for-Paystack-Inline-d6e595c0277d413db73b9a0d81a9c16c)
343
+
344
+ ### Configuration Options
33
345
 
34
- ### Testing
346
+ | Option | Required | Type | Description |
347
+ | ----------------------------- | -------------- | --------------------| ---------------------------------------------------- |
348
+ | container | True | `String` | ID of div to mount the payment request button |
349
+ | loadPaystackCheckoutButton | False | `String` | ID of button to open checkout form |
350
+ | styles | False | `Object` | { <br>&nbsp;&nbsp;&nbsp;&nbsp;theme: 'dark' or 'light', <br>&nbsp;&nbsp;&nbsp;&nbsp;applePay: { <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;width: '100%', <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;height: '50px' <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;borderRadius: '3px', <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;type: 'plain', <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;locale: 'en' <br>&nbsp;&nbsp;&nbsp;&nbsp;} <br>} |
35
351
 
36
- This project uses [Jest](https://jestjs.io/) for unit testing. To run all tests, use the command;
352
+ ### Configuration Options for Callbacks
353
+ | Event name | Description | Response properties |
354
+ | ---------------------------- | -------------------------------------------------------- | -----------------------------------|
355
+ | onElementsMount | Called when the payment request button has been mounted | `{ applePay: true }` or `null` |
37
356
 
357
+
358
+ ## Method Response
359
+
360
+ This method returns a Promise which resolves to a [PopupTransaction Instance](#the-popuptransaction-object)
361
+
362
+ ## For Example
363
+
364
+ Mount a payment button and change the text of the button to open the checkout form if the apple pay button mounts
365
+
366
+ ```html
367
+ <div id="payment-request-buttons"></div>
368
+ <button id="pay-button">Pay</button>
38
369
  ```
39
- npm test
370
+
371
+ ```js
372
+ const paystackPop = new PaystackPop();
373
+
374
+ const onElementsMount = (elements) => {
375
+ if (elements && elements.applePay) {
376
+ document.querySelector("#pay-button").innerText = "More Payment Options";
377
+ }
378
+ }
379
+
380
+ async function loadApplePayButton() {
381
+ try {
382
+ await paystackPop.paymentRequest({
383
+ key: config.publicLiveKey,
384
+ email: 'testuser@paystack.com',
385
+ amount: 10000,
386
+ currency: "NGN",
387
+ container: 'payment-request-buttons',
388
+ loadPaystackCheckoutButton: 'pay-button',
389
+ styles: {
390
+ theme: 'dark',
391
+ applePay: {
392
+ width: '100%',
393
+ height: '50px',
394
+ borderRadius: '3px',
395
+ type: 'plain',
396
+ locale: 'en'
397
+ }
398
+ },
399
+ onElementsMount,
400
+ });
401
+ } catch(error) {
402
+
403
+ }
404
+ }
405
+
406
+ loadApplePayButton()
40
407
  ```
41
408
 
42
- # Deployment
409
+ ---
410
+
411
+ # The PopupTransaction Object
412
+
413
+ `PaystackPop.newTransaction()` and `PaystackPop.resumeTransaction()` both return an instance of `PopupTransaction`.
414
+
415
+ ```js
416
+ const paystackInstance = new PaystackPop();
417
+ const transaction = paystackInstance.newTransaction({
418
+ key: 'pk_test_TYooMQauvdEDq54NiTphI7jx',
419
+ email: 'demo@paystack.com',
420
+ amount: 10000,
421
+ });
422
+ typeof transaction === PopupTransaction; // true
423
+ ```
424
+
425
+ ## PopupTransaction.getStatus()
426
+
427
+ This method returns all available transaction information. This is populated throughout the lifecycle of the transaction.
428
+
429
+ ### Method Response
430
+
431
+ | Name | Description | Response Type |
432
+ | ----------- | ------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
433
+ | status | Status of the transaction | `String`<br>`null` - fetching transaction<br>`error` - transaction error<br>`abandoned` - user has closed the page<br>`auth` - external authentication in progress<br>`failed` - payment failed<br>`success` - payment was completed<br>`pending` - payment was completed; pending confirmation<br> |
434
+ | id | Transaction id, if loaded | `String` |
435
+ | errors | List of transaction errors, if any | `Array` |
436
+ | response | API response from last charge attempt | `Object` |
437
+ | checkoutUrl | Checkout url if transaction is loaded | `String` | |
438
+
439
+ ---
440
+
441
+ ## **Browser Support**
43
442
 
44
- Inline JS is deployed to:
443
+ Paystack Inline is designed to support all recent versions of major browsers. The distributed file ([https://js.paystack.co/v2/inline.js](https://js.paystack.co/v2/popup.js)) is compiled to ES5 and poly-filled to ensure it can work on as many devices as possible. We do not support outdated browsers like IE9 nor browsers that disable the use of Javascript, like Opera mini*.
45
444
 
46
- - Staging: [https://js-studio.paystack.co/v2/inline.js](https://js-studio.paystack.co/v2/inline.js)
47
- - Production: [https://js.paystack.co/v2/inline.js](https://js.paystack.co/v2/inline.js)
48
- - npm: [@paystack/inline-js](https://www.npmjs.com/package/@paystack/inline-js)
445
+ - We support Chrome and Safari on all platforms and Firefox on desktop platforms.
446
+ - We support the Android native browser on Android 4.4 and later.
447
+ - We require TLS 1.2 to be supported by the browser.
49
448
 
50
- # Documents
449
+ If you have an issue with Paystack Inline on a specific browser, kindly reach out to us so we can quickly resolve any issues.
51
450
 
52
- - [Development](./DEV_DOCS.md)
53
- - [Public](./PUBLIC_DOCS.md)
451
+ * We don't support Opera Mini because Javascript is disabled in it. We support the Opera browser however, except in super saver mode where JS is disabled as well.