Skip to content

Latest commit

 

History

History

angular

Payment form + Angular

This page explains how to create a dynamic payment form from scratch using Angular and the embedded-form-glue library.

Requirements

You need to install node.js LTS version.

Create the project

Start a new vue project with:

npm install -g @angular/cli
ng new project-name

More information on Angular docs.

cd project-name
npm install
# Add the dependency to the project
npm install --save @lyracom/embedded-form-glue
# Run the project
npm run start

Add the payment form

First you have to add 2 theme files:

File Description
neon-reset.css default style applied before the Lyra Javascript Library is loaded
neon.js theme logic, like waiting animation on submit button, ...

Add them in public/index.html in the the HEAD section:

<!-- theme and plugins. should be loaded in the HEAD section -->
<link rel="stylesheet"
href="https://~~CHANGE_ME_ENDPOINT~~/static/js/krypton-client/V4.0/ext/neon-reset.css">
<script
    src="https://~~CHANGE_ME_ENDPOINT~~/static/js/krypton-client/V4.0/ext/neon.js">
</script>

Note

Replace [CHANGE_ME] with your credentials and end-points.

For more information about theming, take a look to Lyra theming documentation

reate the src/app/mycomponent/mycomponent.html with:

(...)
<div class="form">
  <h1>{{ title }}</h1>
  <div class="container">
    <div id="myPaymentForm">
      <div class="kr-smart-form" kr-card-form-expanded></div>
    </div>
    <div data-test="payment-message">{{ message }}</div>
  </div>
</div>

Then, create the src/app/mycomponent/mycomponent.ts with:

import { HttpClient } from '@angular/common/http'
import { Component, AfterViewInit, ChangeDetectorRef } from '@angular/core'
import KRGlue from '@lyracom/embedded-form-glue'
import { firstValueFrom } from 'rxjs'

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements AfterViewInit {
  title: string = 'Angular Example'
  message: string = ''

  constructor(private http: HttpClient, private chRef: ChangeDetectorRef) {}

  ngAfterViewInit() {
    const endpoint = '~~CHANGE_ME_ENDPOINT~~'
    const publicKey = '~~CHANGE_ME_PUBLIC_KEY~~'
    let formToken = 'DEMO-TOKEN-TO-BE-REPLACED'

    const observable = this.http.post(
      'http://localhost:3000/createPayment',
      { paymentConf: { amount: 10000, currency: 'USD' } },
      { responseType: 'text' }
    )
    firstValueFrom(observable)
      .then((resp: any) => {
        formToken = resp
        return KRGlue.loadLibrary(
          endpoint,
          publicKey
        ) /* Load the remote library */
      })
      .then(({ KR }) =>
        KR.setFormConfig({
          /* set the minimal configuration */
          formToken: formToken,
          'kr-language': 'en-US' /* to update initialization parameter */
        })
      )
      .then(({ KR }) =>
        KR.renderElements('#myPaymentForm')
      ) /* Render a payment form into myPaymentForm div*/
      .catch(error => {
        this.message = error.message + ' (see console for more details)'
      })
  }
}

The first transaction

To make the first transaction, please see the first transaction guide.

Payment hash verification

To learn how to verify the payment hash, please see the payment hash verification information.

Run this example

You can try the current example from the current repository by cloning the repository and executing the following commands:

cd examples/angular
npm run start

To run the example Node.js server, execute the following commands:

cd examples/server
npm i
npm run start

Remember to replace the [CHANGE_ME] values with your credentials and endpoints before executing the project.