• Stars
    star
    343
  • Rank 119,283 (Top 3 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 7 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

ReactJS library for implementing paystack payment gateway

react-paystack

This is a react library for implementing paystack payment gateway

Demo

Demo

Get Started

This React library provides a wrapper to add Paystack Payments to your React application

Install

npm install react-paystack --save

or with yarn

yarn add react-paystack

Usage

This library can be implemented into any react application in 3 different ways:

  1. By using hooks provided by the library
  2. By using a button provided by the library
  3. By using a context consumer provided by the library

Note that all 3 implementations produce the same results.

1. Using the paystack hook

  import React from 'react';
  import logo from './logo.svg';
  import { usePaystackPayment } from 'react-paystack';
  import './App.css';
  
  const config = {
      reference: (new Date()).getTime().toString(),
      email: "[email protected]",
      amount: 20000, //Amount is in the country's lowest currency. E.g Kobo, so 20000 kobo = N200
      publicKey: 'pk_test_dsdfghuytfd2345678gvxxxxxxxxxx',
  };
  
  // you can call this function anything
  const onSuccess = (reference) => {
    // Implementation for whatever you want to do with reference and after success call.
    console.log(reference);
  };

  // you can call this function anything
  const onClose = () => {
    // implementation for  whatever you want to do when the Paystack dialog closed.
    console.log('closed')
  }

  const PaystackHookExample = () => {
      const initializePayment = usePaystackPayment(config);
      return (
        <div>
            <button onClick={() => {
                initializePayment(onSuccess, onClose)
            }}>Paystack Hooks Implementation</button>
        </div>
      );
  };
  
  function App() {
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <p>
            Edit <code>src/App.js</code> and save to reload.
          </p>
          <a
            className="App-link"
            href="https://reactjs.org"
            target="_blank"
            rel="noopener noreferrer"
          >
            Learn React
          </a>
        </header>
        <PaystackHookExample />
      </div>
    );
  }
  
  export default App;

2. Using the paystack button

  import React from 'react';
  import logo from './logo.svg';
  import { PaystackButton } from 'react-paystack';
  import './App.css';
  
  const config = {
    reference: (new Date()).getTime().toString(),
    email: "[email protected]",
    amount: 20000, //Amount is in the country's lowest currency. E.g Kobo, so 20000 kobo = N200
    publicKey: 'pk_test_dsdfghuytfd2345678gvxxxxxxxxxx',
  };
  
  function App() {
    // you can call this function anything
    const handlePaystackSuccessAction = (reference) => {
      // Implementation for whatever you want to do with reference and after success call.
      console.log(reference);
    };

    // you can call this function anything
    const handlePaystackCloseAction = () => {
      // implementation for  whatever you want to do when the Paystack dialog closed.
      console.log('closed')
    }

    const componentProps = {
        ...config,
        text: 'Paystack Button Implementation',
        onSuccess: (reference) => handlePaystackSuccessAction(reference),
        onClose: handlePaystackCloseAction,
    };

    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <p>
            Edit <code>src/App.js</code> and save to reload.
          </p>
          <a
            className="App-link"
            href="https://reactjs.org"
            target="_blank"
            rel="noopener noreferrer"
          >
            Learn React
          </a>
        </header>
        <PaystackButton {...componentProps} />
      </div>
    );
  }
  
  export default App;

3. using the Paystack consumer

import React from 'react';
import logo from './logo.svg';
import { PaystackConsumer } from 'react-paystack';
import './App.css';
  
  const config = {
      reference: (new Date()).getTime().toString(),
      email: "[email protected]",
      amount: 20000, //Amount is in the country's lowest currency. E.g Kobo, so 20000 kobo = N200
      publicKey: 'pk_test_dsdfghuytfd2345678gvxxxxxxxxxx',
  };
  
  // you can call this function anything
  const handleSuccess = (reference) => {
    // Implementation for whatever you want to do with reference and after success call.
    console.log(reference);
  };

  // you can call this function anything
  const handleClose = () => {
    // implementation for  whatever you want to do when the Paystack dialog closed.
    console.log('closed')
  }

  function App() {
      const componentProps = {
          ...config,
          text: 'Paystack Button Implementation',
          onSuccess: (reference) => handleSuccess(reference),
          onClose: handleClose
      };
  
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <p>
            Edit <code>src/App.js</code> and save to reload.
          </p>
          <a
            className="App-link"
            href="https://reactjs.org"
            target="_blank"
            rel="noopener noreferrer"
          >
            Learn React
          </a>
        </header>
        <PaystackConsumer {...componentProps} >
          {({initializePayment}) => <button onClick={() => initializePayment(handleSuccess, handleClose)}>Paystack Consumer Implementation</button>}
        </PaystackConsumer>
      </div>
    );
  }
  
  export default App;

Please checkout Paystack Documentation for other available options you can add to the tag

Deployment

REMEMBER TO CHANGE THE KEY WHEN DEPLOYING ON A LIVE/PRODUCTION SYSTEM

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b feature-name
  3. Commit your changes: git commit -am 'Some commit message'
  4. Push to the branch: git push origin feature-name
  5. Submit a pull request 😉😉

How can I thank you?

Why not star the github repo? I'd love the attention! Why not share the link for this repository on Twitter or Any Social Media? Spread the word!

Don't forget to follow me on twitter!

Thanks! Olusegun Ayeni.

License

This project is licensed under the MIT License - see the LICENSE.md file for details

More Repositories

1

adonisjs-hackathon-starter

A boilerplate for AdonisJS web framework
JavaScript
149
star
2

vue-paystack

Paystack Vue Plugin for Vue 2.X
Vue
117
star
3

react-ravepayment

ReactJS library for implementing RavePay payment gateway
TypeScript
38
star
4

vue-ravepayment

RavePay Vue Plugin for Vue 2.X
JavaScript
33
star
5

DEV-TV

DevTV Written With Laravel
JavaScript
25
star
6

microapps-donation

A single page application that allows people to donate money.
JavaScript
23
star
7

mernmap

Mernmap Open Source Application Built With The MERN(Mongodb , ExpressJS, ReactJS, NodeJS)
CSS
10
star
8

yourstube

A Simpler Clone of Youtube. Built with the MEAN Stack
JavaScript
7
star
9

hackathon-starter

Hackathon Starter Written With Laravel(Extended Version)
PHP
5
star
10

github-searcher

Github-searcher Open Source Application Built With The Mean Stack
JavaScript
5
star
11

jusibe-java-lib

JAVA Client Library for Jusibe SMS REST API Service
Java
4
star
12

adonis-social-ii

JavaScript
3
star
13

eHealthSystems

Solution to eHealth System Africa Interview
JavaScript
3
star
14

angularjs-ravepayment

RavePay Angular Plugin for Angular 1.x
HTML
3
star
15

angular4-ravepayment

TypeScript
3
star
16

AllyProviders

A Collection of Providers for AdonisJS Ally
JavaScript
2
star
17

typescript-ravepayment

RavePay Library for Typescript
TypeScript
2
star
18

adonisjs-restful-api

JavaScript
2
star
19

java-dumb-passwords

Guard your users against entering dumb passwords in your java or JavaEE apps
Java
2
star
20

vendor-system-api

TypeScript
1
star
21

adonis-social-i

JavaScript
1
star
22

okra-reactjs

TypeScript
1
star
23

MovieFind

A Simple Movie Info Finder
JavaScript
1
star