|
1 | 1 | [![NPM version][npm-version-image]][npm-url] [](https://circleci.com/gh/datatrans/react-datatrans-light-box) |
2 | 2 |
|
3 | | -# react-datatrans-light-box |
| 3 | +# Datatrans React Light Box |
4 | 4 |
|
5 | 5 | Official Datatrans light box library for showing our payment page in React applications. |
6 | 6 | React is defined as a peer dependency and expected to be made available by your project. Other than that this library is completely dependency-free. |
7 | 7 |
|
| 8 | +## Compatibility |
| 9 | + |
| 10 | +Beginning with v3.0.0, this component is using the [Datatrans Payment JSON API](https://api-reference.datatrans.ch/#tag/v1transactions). |
| 11 | + |
| 12 | +If you're still using the legacy XML API, please refer to [react-datatrans-light-box v2.0.2](https://github.com/datatrans/react-datatrans-light-box/tree/2.0.2). |
| 13 | + |
8 | 14 | ## How to install |
9 | 15 |
|
10 | | -```bash |
| 16 | +```sh |
| 17 | +# Use with current JSON API |
11 | 18 | npm install react-datatrans-light-box |
| 19 | + |
| 20 | +# Use with legacy XML API (supported by react-datatrans-light-box <= 2.x) |
| 21 | +npm install react-datatrans-light-box@2 |
12 | 22 | ``` |
13 | 23 |
|
14 | | -## Example Usage of Lightbox component |
15 | | -You can also use a more direct approach and display the Lightbox component whenever or whereever you like. |
| 24 | +## Example usage |
16 | 25 |
|
17 | | -```javascript |
18 | | -import React, { Component } from 'react' |
| 26 | +```js |
| 27 | +import React, { useState } from 'react' |
19 | 28 | import Lightbox from 'react-datatrans-light-box' |
20 | 29 |
|
21 | | -const config = { |
22 | | - merchantId: '1100004624', |
23 | | - refno: 'YOUR_REFERENCE_NUMBER', |
24 | | - amount: '1000', |
25 | | - currency: 'CHF', |
26 | | - sign: '30916165706580013', |
27 | | - production: false, |
28 | | - paymentmethod: ['ECA', 'VIS', 'PFC', 'AMX', 'TWI'], |
29 | | - themeConfiguration: { |
30 | | - brandColor: '#aa9374' |
31 | | - } |
32 | | -} |
| 30 | +export default (props) => { |
| 31 | + const { transactionId } = props |
33 | 32 |
|
34 | | -export default class App extends Component { |
35 | | - constructor(props) { |
36 | | - super(props) |
| 33 | + const [lightbox, showLightbox] = useState(false) |
37 | 34 |
|
38 | | - this.state = { |
39 | | - showLightbox: false |
40 | | - } |
| 35 | + const onLoaded = () => console.log('Loaded') |
| 36 | + const onOpened = () => console.log('Opened') |
| 37 | + const onCancelled = () => showLightbox(false) |
| 38 | + const onError = (data) => { |
| 39 | + console.log('Error:', data) |
| 40 | + showLightbox(false) |
41 | 41 | } |
42 | 42 |
|
43 | | - render() { |
44 | | - return <div> |
45 | | - <h1>Datatrans Lightbox Demo</h1> |
46 | | - <div> |
47 | | - {this.state.showLightbox |
48 | | - ? <Lightbox |
49 | | - {...config} |
50 | | - onLoaded={this.onLoaded} |
51 | | - onOpened={this.onOpened} |
52 | | - onCancelled={this.onCancelled} |
53 | | - onError={this.onError} |
54 | | - /> |
55 | | - : <button onClick={this.start}>Start Lightbox</button> |
56 | | - } |
57 | | - </div> |
58 | | - |
| 43 | + return <div> |
| 44 | + <h1>Datatrans Lightbox Demo</h1> |
| 45 | + <div> |
| 46 | + {lightbox |
| 47 | + ? <Lightbox |
| 48 | + transactionId={transactionId} |
| 49 | + production={true} |
| 50 | + onLoaded={this.onLoaded} |
| 51 | + onOpened={this.onOpened} |
| 52 | + onCancelled={this.onCancelled} |
| 53 | + onError={this.onError} |
| 54 | + /> |
| 55 | + : <button onClick={() => showLightbox(true)}>Start Lightbox</button> |
| 56 | + } |
59 | 57 | </div> |
60 | | - } |
61 | | - |
62 | | - start = () => { |
63 | | - this.setState({ showLightbox: true }) |
64 | | - } |
65 | | - |
66 | | - onLoaded = () => { |
67 | | - console.log('Loaded') |
68 | | - } |
69 | | - |
70 | | - onOpened = () => { |
71 | | - console.log('Opened') |
72 | | - } |
73 | | - |
74 | | - onCancelled = () => { |
75 | | - console.log('Cancelled') |
76 | | - this.setState({ showLightbox: false }) |
77 | | - } |
78 | | - |
79 | | - onError = (data) => { |
80 | | - console.log('Error:', data) |
81 | | - this.setState({ showLightbox: false }) |
82 | | - } |
| 58 | + </div> |
83 | 59 | } |
84 | 60 | ``` |
85 | 61 |
|
86 | 62 | ## Properties |
87 | 63 |
|
88 | 64 | The Lightbox component takes the following properties as input. |
89 | 65 |
|
90 | | -### Mandatory |
91 | | - |
92 | | -Name | Type | Description |
93 | | ------|------|-----| |
94 | | -`merchantId` | String | Merchant identifier provided to you by Datatrans. |
95 | | -`refno` | String | Any value by which you would like to reference the payment.| |
96 | | -`amount` | String |The amount in cents you would like to charge your customer.| |
97 | | -`currency` | String | The type of currency that will be used for the payment.| |
98 | | -`sign` | String | Transaction security parameter. Find it in Datatrans' [Webadmin Tool](https://payment.datatrans.biz/). | |
99 | | - |
100 | | -### Optional |
101 | | - |
102 | | -|Name | Type |Description | |
103 | | -|----- |:------ |------------| |
104 | | -|`production` | Boolean | Indicates whether requests hit Datatrans' production or development environment. Defaults to *false*.| |
105 | | -|`onLoaded` | Function | Called when payment page is loaded.| |
106 | | -|`onOpened` | Function | Called when payment page is opened.| |
107 | | -|`onCancelled` | Function | Called when user has cancelled payment.| |
108 | | -|`onError` | Function | Called when there was an error loading the payment page.| |
109 | | -|and many more... | | Refer to our [DOCS](https://docs.datatrans.ch/docs) to get the full list of supported parameters.| |
| 66 | +| Property | Mandatory | Type | Description | |
| 67 | +| -------- | --------- | ---- | ----------- | |
| 68 | +| `transactionId` | **Yes** | String | Transaction ID returned by [Initializing Transactions](https://docs.datatrans.ch/docs/redirect-lightbox#section-initializing-transactions). | |
| 69 | +| `production` | No | Boolean | Indicates whether requests hit Datatrans' production or sandbox environment. Defaults to `false` (sandbox). | |
| 70 | +| `onLoaded` | No | Function | Called when payment page is loaded. | |
| 71 | +| `onOpened` | No | Function | Called when payment page is opened. | |
| 72 | +| `onCancelled` | No | Function | Called when user has cancelled payment. | |
| 73 | +| `onError` | No | Function | Called when there was an error loading the payment page. | |
110 | 74 |
|
111 | 75 | [npm-url]: https://npmjs.com/package/react-datatrans-light-box |
112 | 76 | [npm-version-image]: https://img.shields.io/npm/v/react-datatrans-light-box.svg?style=flat-square |
0 commit comments