1
- # Payment form from scratch with angular-cli
1
+ # Payment form + Angular
2
2
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.
5
5
6
6
## Requirements
7
7
8
8
You need to install [ node.js LTS version] ( https://nodejs.org/en/ ) .
9
9
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
-
20
10
## Create the project
21
11
22
- First, create the angular-cli minimal-example project ( with default options) :
12
+ Start a new vue project with:
23
13
24
- ``` sh
25
- ng new minimal-example
14
+ ``` bash
15
+ npm install -g @angular/cli
16
+ ng new project-name
26
17
```
27
18
28
- Add the dependency with yarn:
19
+ More information on [ Angular docs ] ( https://angular.io/docs ) .
29
20
30
21
``` bash
31
- cd minimal-example
32
- # with npm
22
+ cd project-name
23
+ npm install
24
+ # Add the dependency to the project
33
25
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
42
28
```
43
29
44
- see the result on http://localhost:4200/
45
-
46
- ## add the payment form
30
+ ## Add the payment form
47
31
48
32
First you have to add 2 theme files:
49
33
50
34
| File | Description |
51
35
| ----------------- | ----------------------------------------------------------------------------- |
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, ... |
54
38
55
- Add them in src /index.html in the the HEAD section:
39
+ Add them in public /index.html in the the HEAD section:
56
40
57
41
``` javascript
58
42
<!-- theme and plugins . should be loaded in the HEAD section -->
59
43
< 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" >
61
45
< 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" >
63
47
< / script>
64
48
```
65
49
66
- ** note ** : Replace ** [ CHANGE_ME] ** with your credentials and end-points .
50
+ > ** NOTE: ** Replace ** [ CHANGE_ME] ** with your credentials and endpoints .
67
51
68
52
For more information about theming, take a look to [ Lyra theming documentation] [ js themes ]
69
53
70
- Change the src/app/app.component. html template to :
54
+ reate the src/app/mycomponent/mycomponent. html with :
71
55
72
56
``` html
57
+ (...)
73
58
<div class =" form" >
74
59
<h1 >{{ title }}</h1 >
75
60
<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 >
77
65
</div >
78
66
</div >
79
67
```
80
68
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:
84
70
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'
98
76
99
- ``` js
100
- import { Component , OnInit } from " @angular/core" ;
101
- import KRGlue from " @lyracom/embedded-form-glue" ;
102
77
@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' ]
106
81
})
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
+ })
114
106
.then (({ KR }) =>
115
107
KR .setFormConfig ({
116
108
/* set the minimal configuration */
117
109
formToken: formToken,
118
- " kr-language" : " en-US" /* to update initialization parameter */
110
+ ' kr-language' : ' en-US' /* to update initialization parameter */
119
111
})
120
112
)
121
113
.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*/
124
116
.then (({ KR , result }) =>
125
117
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
+ })
127
122
}
128
123
}
129
- ```
130
124
131
- ** note ** : Replace ** [ CHANGE_ME ] ** with your credentials and end-points.
125
+ ```
132
126
133
- ## your first transaction
127
+ ## Your first transaction
134
128
135
129
The payment form is up and ready, you can try to make a transaction using
136
130
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
139
133
It's because the ** formToken** you have defined using ** KR.setFormConfig** is set to ** DEMO-TOKEN-TO-BE-REPLACED** .
140
134
141
135
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 :
143
137
144
138
- [ Embedded form quick start] [ js quick start ]
145
139
- [ embedded form integration guide] [ js integration guide ]
146
140
- [ Payment REST API reference] [ rest api ]
147
141
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
- ```
173
142
174
- On the client side:
143
+ ## Payment hash verification
175
144
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 ) .
212
146
213
- ## Run it from github
147
+ ## Run this example
214
148
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 :
216
150
217
- ``` sh
218
- cd examples/angular/minimal-example
219
- npm install
151
+ ``` bash
152
+ cd examples/angular
220
153
npm run start
221
154
```
222
155
223
- You can run the example node .js server by running :
156
+ To run the example Node .js server, execute the following commands :
224
157
225
- ``` sh
158
+ ``` bash
226
159
cd examples/server
227
160
npm i
228
161
npm run start
229
162
```
230
163
164
+ > Remember to replace the ** [ CHANGE_ME] ** values with your credentials and endpoints before executing the project.
165
+
231
166
[ js link ] : https://lyra.com/fr/doc/rest/V4.0/javascript
232
167
[ js themes ] : https://lyra.com/fr/doc/rest/V4.0/javascript/features/themes.html
233
168
[ js quick start ] : https://lyra.com/fr/doc/rest/V4.0/javascript/quick_start_js.html
234
169
[ 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