Skip to content

Latest commit

 

History

History

react

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Payment form + React

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

Requirements

You need to install node.js LTS version.

Create the project

Start a new svelte project with:

npx create-react-app project-name

More information on react getting started.

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="~~CHANGE_ME_ENDPOINT~~/static/js/krypton-client/V4.0/ext/neon-reset.css">
<script
    src="~~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

Replace the src/App.css styles to:

h1 { margin: 40px 0 20px 0; width: 100%; text-align: center; } .container {
display: flex; justify-content: center; }

Next, update src/App.js to:

import { useState, useEffect } from 'react'
import KRGlue from '@lyracom/embedded-form-glue'

export default function MyComponent() {
  const [message, setMessage] = useState('')

  useEffect(() => {
    async function setupPaymentForm() {
      const endpoint = '~~CHANGE_ME_ENDPOINT~~'
      const publicKey = '~~CHANGE_ME_PUBLIC_KEY~~'
      let formToken = 'DEMO-TOKEN-TO-BE-REPLACED'

      try {
        const res = await fetch('http://localhost:3000/createPayment', {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({
            paymentConf: { amount: 10000, currency: 'USD' }
          })
        })
        formToken = await res.text()

        const { KR } = await KRGlue.loadLibrary(
          endpoint,
          publicKey
        ) /* Load the remote library */

        await KR.setFormConfig({
          /* set the minimal configuration */ formToken: formToken,
          'kr-language': 'en-US' /* to update initialization parameter */
        })

        await KR.renderElements(
          '#myPaymentForm'
        ) /* Render the payment form into myPaymentForm div*/
      } catch (error) {
        setMessage(error + ' (see console for more details)')
      }
    }

    setupPaymentForm()
  }, [])

  return (
    <div className="form">
      <h1>React Example</h1>
      <div className="container">
        <div id="myPaymentForm">
          <div className="kr-smart-form" kr-card-form-expanded></div>
        </div>
        <div data-test="payment-message">{message}</div>
      </div>
    </div>
  )
}

Your payment form will be added to #myPaymentForm element.

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 it from github

You can try the current example from the current github repository doing:

cd examples/react
npm install
npm run start

You can run the example node.js server by running:

cd examples/server
npm i
npm run start