@bananobay/accept-banano-client
TypeScript icon, indicating that this package has built-in type declarations

2.0.3 • Public • Published

accept-banano-client

Build Status Coverage Status npm (scoped) npm bundle size (scoped) GitHub

Payment gateway for BANANO

accept-banano-client is a JavaScript package that helps you to communicate with accept-banano for receiving BANANO payments easily in your client-side applications.

Installation

via NPM

npm install @accept-banano/client

yarn add @accept-banano/client

ES Modules / TypeScript

import * as acceptBanano from '@accept-banano/client'

CommonJS

const acceptBanano = require('@accept-banano/client')

Directly in Browser, as a UMD module

After the accept-banano-client script is loaded there will be a global variable called acceptBanano, which you can access via window.acceptBanano

<html>
  <head>
    ...
    <script src="https://unpkg.com/@accept-banano/client@2"></script>
  </head>
  ...
</html>

Usage

Creating a Payment Session

To be able to initiate the payment process, you must create a new payment session.

// 1- create a new payment session
type CreateSessionParams = {
  apiHost: string // host of your Accept BANANO server, without protocol
  pollInterval?: number // time interval (ms) to re-check for verification of a payment (default: 3s)
  debug?: boolean // enables debug mode and prints some useful stuff to console
}

const session = acceptBanano.createSession({
  apiHost: 'accept-nano-demo.put.io',
})

// 2- register event listeners to shape-up your logic based on session events.
type PaymentSessionEvents = {
  start: () => void
  end: (error: PaymentError | null, payment: AcceptBananoPayment | null) => void
}

session.on('start', () => {
  myApp.paymentStarted()
})

session.on('end', (error, payment) => {
  if (error) {
    return myApp.paymentFailed({ reason: error.reason })
  }

  return myApp.paymentSucceeded({
    amount: payment.amount,
    state: payment.state,
  })
})

Presenting the Payment Overlay

After creating your session and attaching the event listeners, you can follow one of those options to proceed with the payment flow.

Option 1: Create a Payment Through Client

If you want to create and verify an accept-banano payment in your client application, you can use this option.

After the payment is created, accept-banano-client will automatically proceed to the verification step.

type CreatePaymentParams = {
  amount: string // stringified number
  currency: 'BANANO' | 'USD'
  state?: string // payload to share between your client and server, will be embedded into the payment object
}

session.createPayment({
  amount: '1',
  currency: 'USD',
  state: '{userId:7}',
})

Option 2: Verify a Payment Through Client

If you create an accept-banano payment in another context (such as your application's backend), you can use this option to perform the verification in your client application.

type VerifyPaymentParams = {
  token: string // the Accept BANANO payment token created in your backend application
}

session.verifyPayment({ token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9' })

Contributing

  • Please open an issue if you have a question or suggestion.
  • Don't create a PR before discussing it first.

Who is using accept-nano-client in production?

  • Me

Please send a PR to list your site if accept-banano is helping you to receive BANANO payments.

Readme

Keywords

none

Package Sidebar

Install

npm i @bananobay/accept-banano-client

Weekly Downloads

0

Version

2.0.3

License

MIT

Unpacked Size

1.4 MB

Total Files

55

Last publish

Collaborators

  • mconstant