Giter VIP home page Giter VIP logo

use-omise's Introduction

use-omise

A React hook for collecting card or payment source details for Omise payment gateway.

Credit/debit card payments require 'tokens' which can generated by the createTokenPromise function. Non-card payments require 'sources' which can be generated by the createSourcePromise function.

Installation

Install with npm

npm install use-omise

or with yarn

yarn add use-omise

And make sure you also have React installed at version 16.8 or above.

How to use

Creating a token for debit/credit card payments:

function PaymentForm() {
  const { loading, createTokenPromise } = useOmise({
    publicKey: 'YOUR-OMISE-PUBLIC-KEY',
  });

  if (loading) return <div>Loading OmiseJS...</div>;

  const handleSubmit = async (cardFormValues) => {
    try {
      const token = await createTokenPromise('card', cardFormValues);
      // Send the token to your server to create a charge
    } catch (error) {
      // Handle error on the UI
    }
  };

  return <CreditCardForm handleSubmit={handleSubmit} />;
}

The cardFormValues object will hold the details of the card to be charged, e.g.

{
  name: "Example card holder",
  number: "4242424242424242",
  security_code: "111",
  expiration_month: "06",
  expiration_year: "2020"
}

What the use-omise hook returns

const {
  loading,
  createTokenPromise,
  createSourcePromise,
  createToken,
  createSource,
  checkCreateTokenError,
} = useOmise({ publicKey: 'YOUR-OMISE-PUBLIC-KEY' });

Note: It is recommended that you use the createTokenPromise and createSourcePromise functions for creating tokens and sources - this allows you to use async/await and promise chaining syntax rather than callbacks. The promisified token function also uses the checkCreateTokenError helper function internally to check for all possible errors.

Value Type Description
loading boolean Indicates if the omise.js script is currently loading
createTokenPromise function A 'promisified' version of the createToken function
createSourcePromise function A 'promisified' version of the createSource function
createToken function The original Omise createToken function in callback format
createSource function The original Omise createSource function in callback format
checkCreateTokenError function A helper function to check if the createToken has returned an error

How it works

  1. Loads the Omise.js script. By default it will use the primary CDN (Singapore) but the secondary CDN (Japan) can also be used
  2. Once loaded, it will initialise Omise by setting the public key that you provide
  3. Returns you the functions needed to create tokens/source which can then be used to make charges on the server

use-omise's People

Contributors

cpv123 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

use-omise's Issues

useOmise hook doesn't return CreateTokenFunction.

Hello, when I try to destructure the result from useOmise hook and get the createTokenPromise function the result is null the first time, after I refresh the page few times(not specific times, sometimes 1 time, sometime 2-3-4) I get the needed result(which is the function).
Screenshot 2023-01-27 at 16 34 36
Screenshot 2023-01-27 at 16 34 47
Screenshot 2023-01-27 at 16 35 01

createTokenPromise function cannot use on mobile phone in Next.js

I try to use createTokenPromise in Next.js On window OS or macOS it work properly. But when I use on Mobile phone (Safari, Chrome) it return "de is not a function. (In 'de("card",t)', 'de' is null)" when I was submitted . I don't know why previously I also have a problem with Omise.createToken and then I try your libs . thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.