Skip to content

Commit 5ba6b63

Browse files
committed
doc(exampes): examples update
Updates the examples code and Readme's to use the smartform. See: KJS-2758
1 parent 5ad6d52 commit 5ba6b63

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+9779
-16103
lines changed

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"editor.formatOnSave": true
3+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ If you try to pay, you will have the following error: **CLIENT_998: Demo form, s
9696
It's because the **formToken** you have defined using **KR.setFormConfig** is set to **DEMO-TOKEN-TO-BE-REPLACED**.
9797

9898
you have to create a **formToken** before displaying the payment form using Charge/CreatePayment web-service.
99-
For more information, please take a look to:
99+
For more information, please see:
100100

101101
* [Embedded form quick start][JS quick start]
102102
* [embedded form integration guide][JS integration guide]

app/KryptonGlue.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ class Glue {
2020
}
2121

2222
loadLibrary(domain, publicKey, formToken = null) {
23-
const domainRegex = /^(?:http(s)?:\/\/)[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#[\]@!$&'()*+,;=.]+$/g
23+
const domainRegex =
24+
/^(?:http(s)?:\/\/)[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#[\]@!$&'()*+,;=.]+$/g
2425
const pubKeyRegex = /^\d{2,8}:(|test)publickey_.+$/g
2526

2627
if (this.loaded) return this.getKrypton(publicKey)
@@ -46,12 +47,12 @@ class Glue {
4647
return Promise.reject('The library cannot be loaded')
4748
}
4849

49-
loadKryptonClient() {
50+
loadKryptonClient() {
5051
if (!this.loading) {
5152
const publicKey = this.publicKey
5253
let domain = this.domain
5354
this.loading = true
54-
55+
5556
const script = document.createElement('script')
5657
script.type = 'text/javascript'
5758

@@ -71,8 +72,8 @@ class Glue {
7172
if (!$script && document.body) document.body.appendChild(script)
7273
else if (!document.body) console.warn('document.body is undefined')
7374
}
74-
75-
return new Promise((resolve) => {
75+
76+
return new Promise(resolve => {
7677
whenDefined(window, 'KR', () => {
7778
whenDefined(window.KR, 'ready', () => {
7879
this.loaded = true

examples/angular/README.md

Lines changed: 80 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -1,136 +1,130 @@
1-
# Payment form from scratch with angular-cli
1+
# Payment form + Angular
22

3-
This page explains how-to create a dynamic payment form from scratch using
4-
Angular and angular-cli and embedded-form-glue library.
3+
This page explains how to create a dynamic payment form from scratch using
4+
Angular and the embedded-form-glue library.
55

66
## Requirements
77

88
You need to install [node.js LTS version](https://nodejs.org/en/).
99

10-
Next, install vue-cli:
11-
12-
```bash
13-
npm install -g @angular/cli
14-
# OR
15-
yarn global add @angular/cli
16-
```
17-
18-
More details on [angular-cli web-site](https://angular.io/guide/quickstart).
19-
2010
## Create the project
2111

22-
First, create the angular-cli minimal-example project (with default options):
12+
Start a new vue project with:
2313

24-
```sh
25-
ng new minimal-example
14+
```bash
15+
npm install -g @angular/cli
16+
ng new project-name
2617
```
2718

28-
Add the dependency with yarn:
19+
More information on [Angular docs](https://angular.io/docs).
2920

3021
```bash
31-
cd minimal-example
32-
# with npm
22+
cd project-name
23+
npm install
24+
# Add the dependency to the project
3325
npm install --save @lyracom/embedded-form-glue
34-
# or with yarn
35-
yarn add @lyracom/embedded-form-glue
36-
```
37-
38-
Run and test it (in minimal-example folder):
39-
40-
```sh
41-
ng serve --open
26+
# Run the project
27+
npm run start
4228
```
4329

44-
see the result on http://localhost:4200/
45-
46-
## add the payment form
30+
## Add the payment form
4731

4832
First you have to add 2 theme files:
4933

5034
| File | Description |
5135
| ----------------- | ----------------------------------------------------------------------------- |
52-
| classic-reset.css | default style applied before the [Lyra Javascript Library][js link] is loaded |
53-
| classic.js | theme logic, like waiting annimation on submit button, ... |
36+
| neon-reset.css | default style applied before the [Lyra Javascript Library][js link] is loaded |
37+
| neon.js | theme logic, like waiting annimation on submit button, ... |
5438

55-
Add them in src/index.html in the the HEAD section:
39+
Add them in public/index.html in the the HEAD section:
5640

5741
```javascript
5842
<!-- theme and plugins. should be loaded in the HEAD section -->
5943
<link rel="stylesheet"
60-
href="~~CHANGE_ME_ENDPOINT~~/static/js/krypton-client/V4.0/ext/classic-reset.css">
44+
href="https://~~CHANGE_ME_ENDPOINT~~/static/js/krypton-client/V4.0/ext/neon-reset.css">
6145
<script
62-
src="~~CHANGE_ME_ENDPOINT~~/static/js/krypton-client/V4.0/ext/classic.js">
46+
src="https://~~CHANGE_ME_ENDPOINT~~/static/js/krypton-client/V4.0/ext/neon.js">
6347
</script>
6448
```
6549

66-
**note**: Replace **[CHANGE_ME]** with your credentials and end-points.
50+
> **NOTE:** Replace **[CHANGE_ME]** with your credentials and endpoints.
6751
6852
For more information about theming, take a look to [Lyra theming documentation][js themes]
6953

70-
Change the src/app/app.component.html template to:
54+
reate the src/app/mycomponent/mycomponent.html with:
7155

7256
```html
57+
(...)
7358
<div class="form">
7459
<h1>{{ title }}</h1>
7560
<div class="container">
76-
<div id="myPaymentForm"></div>
61+
<div id="myPaymentForm">
62+
<div class="kr-smart-form" kr-card-form-expanded></div>
63+
</div>
64+
<div data-test="payment-message">{{ message }}</div>
7765
</div>
7866
</div>
7967
```
8068

81-
Your payment form will be added to #myPaymentForm element.
82-
83-
Update the src/app/app.component.css styles to:
69+
Then, create the src/app/mycomponent/mycomponent.ts with:
8470

85-
```css
86-
h1 {
87-
margin: 40px 0 20px 0;
88-
width: 100%;
89-
text-align: center;
90-
}
91-
.container {
92-
display: flex;
93-
justify-content: center;
94-
}
95-
```
96-
97-
Update the default component src/app/app.component.ts to:
71+
```javascript
72+
import { HttpClient } from '@angular/common/http'
73+
import { Component, AfterViewInit, ChangeDetectorRef } from '@angular/core'
74+
import KRGlue from '@lyracom/embedded-form-glue'
75+
import { firstValueFrom } from 'rxjs'
9876

99-
```js
100-
import { Component, OnInit } from "@angular/core";
101-
import KRGlue from "@lyracom/embedded-form-glue";
10277
@Component({
103-
selector: "app-root",
104-
templateUrl: "./app.component.html",
105-
styleUrls: ["./app.component.css"]
78+
selector: 'app-root',
79+
templateUrl: './app.component.html',
80+
styleUrls: ['./app.component.css']
10681
})
107-
export class AppComponent implements OnInit {
108-
title = "minimal-example";
109-
ngOnInit() {
110-
const endpoint = "~~CHANGE_ME_ENDPOINT~~";
111-
const publicKey = "~~CHANGE_ME_PUBLIC_KEY~~";
112-
const formToken = "DEMO-TOKEN-TO-BE-REPLACED";
113-
KRGlue.loadLibrary(endpoint, publicKey) /* Load the remote library */
82+
export class AppComponent implements AfterViewInit {
83+
title: string = 'Angular + KR.attachForm'
84+
message: string = ''
85+
86+
constructor(private http: HttpClient, private chRef: ChangeDetectorRef) {}
87+
88+
ngAfterViewInit() {
89+
const endpoint = '~~CHANGE_ME_ENDPOINT~~'
90+
const publicKey = '~~CHANGE_ME_PUBLIC_KEY~~'
91+
let formToken = 'DEMO-TOKEN-TO-BE-REPLACED'
92+
93+
const observable = this.http.post(
94+
'http://localhost:3000/createPayment',
95+
{ paymentConf: { amount: 10000, currency: 'USD' } },
96+
{ responseType: 'text' }
97+
)
98+
firstValueFrom(observable)
99+
.then((resp: any) => {
100+
formToken = resp
101+
return KRGlue.loadLibrary(
102+
endpoint,
103+
publicKey
104+
) /* Load the remote library */
105+
})
114106
.then(({ KR }) =>
115107
KR.setFormConfig({
116108
/* set the minimal configuration */
117109
formToken: formToken,
118-
"kr-language": "en-US" /* to update initialization parameter */
110+
'kr-language': 'en-US' /* to update initialization parameter */
119111
})
120112
)
121113
.then(({ KR }) =>
122-
KR.addForm("#myPaymentForm")
123-
) /* add a payment form to myPaymentForm div*/
114+
KR.attachForm('#myPaymentForm')
115+
) /* Attach a payment form to myPaymentForm div*/
124116
.then(({ KR, result }) =>
125117
KR.showForm(result.formId)
126-
); /* show the payment form */
118+
) /* show the payment form */
119+
.catch(error => {
120+
this.message = error.message + ' (see console for more details)'
121+
})
127122
}
128123
}
129-
```
130124

131-
**note**: Replace **[CHANGE_ME]** with your credentials and end-points.
125+
```
132126

133-
## your first transaction
127+
## Your first transaction
134128

135129
The payment form is up and ready, you can try to make a transaction using
136130
a test card with the debug toolbar (at the bottom of the page).
@@ -139,97 +133,38 @@ If you try to pay, you will have the following error: **CLIENT_998: Demo form, s
139133
It's because the **formToken** you have defined using **KR.setFormConfig** is set to **DEMO-TOKEN-TO-BE-REPLACED**.
140134

141135
you have to create a **formToken** before displaying the payment form using Charge/CreatePayment web-service.
142-
For more information, please take a look to:
136+
For more information, please see:
143137

144138
- [Embedded form quick start][js quick start]
145139
- [embedded form integration guide][js integration guide]
146140
- [Payment REST API reference][rest api]
147141

148-
## Payment hash verification
149-
150-
Payment hash must be validated on the server side to prevent the exposure of your
151-
personal hash key.
152-
153-
On the server side:
154-
155-
```js
156-
const express = require('express')
157-
const hmacSHA256 = require('crypto-js/hmac-sha256')
158-
const Hex = require('crypto-js/enc-hex')
159-
const app = express()
160-
(...)
161-
// Validates the given payment data (hash)
162-
app.post('/validatePayment', (req, res) => {
163-
const answer = req.body.clientAnswer
164-
const hash = req.body.hash
165-
const answerHash = Hex.stringify(
166-
hmacSHA256(JSON.stringify(answer), 'CHANGE_ME: HMAC SHA256 KEY')
167-
)
168-
if (hash === answerHash) res.status(200).send('Valid payment')
169-
else res.status(500).send('Payment hash mismatch')
170-
})
171-
(...)
172-
```
173142

174-
On the client side:
143+
## Payment hash verification
175144

176-
```js
177-
import { Component, OnInit } from "@angular/core";
178-
import KRGlue from "@lyracom/embedded-form-glue";
179-
import axios from 'axios'
180-
@Component({
181-
selector: "app-root",
182-
templateUrl: "./app.component.html",
183-
styleUrls: ["./app.component.css"]
184-
})
185-
export class AppComponent implements OnInit {
186-
title = "minimal-example";
187-
(...),
188-
ngOnInit() {
189-
/* Use your endpoint and personal public key */
190-
const endpoint = '~~CHANGE_ME_ENDPOINT~~'
191-
const publicKey = '~~CHANGE_ME_PUBLIC_KEY~~'
192-
const formToken = 'DEMO-TOKEN-TO-BE-REPLACED'
193-
KRGlue.loadLibrary(endpoint, publicKey) /* Load the remote library */
194-
.then(({KR}) => KR.setFormConfig({ /* set the minimal configuration */
195-
formToken: formToken,
196-
'kr-language': 'en-US',
197-
})) /* to update initialization parameter */
198-
.then(({KR}) => KR.onSubmit(resp => {
199-
axios
200-
.post('http://localhost:3000/validatePayment', paymentData)
201-
.then(response => {
202-
if (response.status === 200) this.message = 'Payment successful!'
203-
})
204-
return false
205-
}))
206-
.then(({KR}) => KR.addForm('#myPaymentForm')) /* create a payment form */
207-
.then(({KR, result}) => KR.showForm(result.formId)); /* show the payment form */
208-
}
209-
(...)
210-
}
211-
```
145+
To learn how to verify the payment hash, please see the [payment hash verification information](../server/README.md).
212146

213-
## Run it from github
147+
## Run this example
214148

215-
You can try the current example from the current github repository doing:
149+
You can try the current example from the current repository by cloning the repository and executing the following commands:
216150

217-
```sh
218-
cd examples/angular/minimal-example
219-
npm install
151+
```bash
152+
cd examples/angular
220153
npm run start
221154
```
222155

223-
You can run the example node.js server by running:
156+
To run the example Node.js server, execute the following commands:
224157

225-
```sh
158+
```bash
226159
cd examples/server
227160
npm i
228161
npm run start
229162
```
230163

164+
> Remember to replace the **[CHANGE_ME]** values with your credentials and endpoints before executing the project.
165+
231166
[js link]: https://lyra.com/fr/doc/rest/V4.0/javascript
232167
[js themes]: https://lyra.com/fr/doc/rest/V4.0/javascript/features/themes.html
233168
[js quick start]: https://lyra.com/fr/doc/rest/V4.0/javascript/quick_start_js.html
234169
[js integration guide]: https://lyra.com/fr/doc/rest/V4.0/javascript/guide/start.html
235-
[rest api]: https://lyra.com/fr/doc/rest/V4.0/api/reference.html
170+
[rest api]: https://lyra.com/fr/doc/rest/V4.0/api/reference.html

0 commit comments

Comments
 (0)