Skip to content

Filsommer/react-ergo-payments

 
 

Repository files navigation

This project was based on Nightowl's Connector.

React Ergo Payments

Logo


What is this library?

The React Ergo Payments library is an npm package developed to allow clients to pay/subscribe with ERG within your website. It validates and and submits the client's transaction and your website can do all sorts of fun stuff upon success. Including:

  1. Allowing users to pay/donate X ERG to a specific wallet Y
  2. Triggering a purchase request in your online store
  3. Saving the tx into your database and creating active subscriptions depending on the timestamp

Built With

Created using React.js and Nautilus.

  • For React 17 install react-ergo-payments v1.0.0
  • For React 18 install react-ergo-payments v1.0.1

This package uses WASM, which is not supported by create-react-app (CRA). Follow the guide below to install it in an existing CRA.


Pre-requisites

  • Node.js
    https://nodejs.org/
  • Nautilus^0.4.0
    https://chrome.google.com/webstore/detail/nautilus-wallet/gjlmehlldlphhljhpnlddaodbjjcchai

Installation

  • Install the package:
    npm install react-ergo-payments

If you are using CRA, this workaround is needed:

  1. npm uninstall react-scripts
  2. npm install @craco/craco
  3. npm install wasm-loader
  4. Change all config.js scripts with react-scrips to craco (E.g.: "start": "craco start")
  5. Create a config file named craco.config.js and add this:
  module.exports = {
  webpack: {
    configure: (webpackConfig) => {
      const wasmExtensionRegExp = /\.wasm$/;
      webpackConfig.resolve.extensions.push('.wasm');

      webpackConfig.module.rules.forEach((rule) => {
        (rule.oneOf || []).forEach((oneOf) => {
          if (oneOf.loader && oneOf.loader.indexOf('file-loader') >= 0) {
            oneOf.exclude.push(wasmExtensionRegExp);
          }
        });
      });

      const wasmLoader = {
        test: /\.wasm$/,
        exclude: /node_modules/,
        loaders: ['wasm-loader'],
      };

      addBeforeLoader(webpackConfig, loaderByName('file-loader'), wasmLoader);

      return webpackConfig;
    },
  },
};

Usage

  • Load the package asynchronously into the file/component you want:
  const [component, setComponent] = React.useState();

  useEffect(() => {
    loadLibrary().then(lib => {
      setComponent(lib);
    })
    return () => setComponent();
  }, []);

  async function loadLibrary() {
      return await import("react-ergo-payments");
  }
  • Finally, use the component:
{ component && <component.ErgoForm /> }

Important: Attributes

Currently, you can specify 4 attributes:

  1. receiverWalletAddress the wallet your want your clients to pay to
  2. onSuccess() call back -> this processor triggers onSuccess(txId, valueSent)
  3. price (in ERG)
  4. color (see Nightowl's Connector for examples)

So an example in your real-world store would be:

function onSuccess(txId, valueSent) {
    // do something with this info
}

{ component && 
<component.ErgoForm 
    price={priceInErg} 
    onSuccess={onSuccess} 
    color={"orange"} 
    receiverWalletAddress={"9iPnRh5gaxxxcCQjUq6cT7FaQpRDpKtu"} /> }

Warning: You need specify the receiverWalletAddress or you will send your funds to my default wallet


Contributing

I'm just venturing into blockchain waters for a short time now, so any suggestions and contributions to this open source project are greatly appreciated!


Contact

Filipe Sommer - LinkedIn

About

React package to process Ergo Payments. Currently only supports the Nautilus wallet.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 74.2%
  • CSS 20.4%
  • HTML 5.4%