Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
apiphoomchu committed Aug 7, 2023
0 parents commit afd26a8
Show file tree
Hide file tree
Showing 18 changed files with 4,654 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
21 changes: 21 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
workflow_dispatch:

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Install dependencies
run: yarn
- name: Run tests
run: yarn test

2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
build/
8 changes: 8 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
src
.vscode
.prettierrc
.github
tsconfig.json
jest.config.js
yarn.lock
build/tsconfig.tsbuildinfo
6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"trailingComma": "es5",
"tabWidth": 2,
"semi": true,
"singleQuote": true
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.formatOnSave": true
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Apiphoom Chuenchompoo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
88 changes: 88 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# use-omise

A React hook for collecting card or payment source details for [Omise](https://www.omise.co/) 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:

```jsx
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.

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

## What the use-omise hook returns

```jsx
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](https://github.com/omise/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
7 changes: 7 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
};
48 changes: 48 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "use-omise",
"version": "1.2.1",
"description": "A React hook for Omise payments",
"author": "Chris Vibert",
"license": "MIT",
"main": "build/index.js",
"types": "build/index.d.ts",
"typings": "build/index.d.ts",
"files": [
"/build"
],
"scripts": {
"build": "rm -rf build && tsc",
"prepublishOnly": "yarn test && yarn build",
"preversion": "yarn test",
"test": "jest"
},
"repository": {
"type": "git",
"url": "https://github.com/cpv123/use-omise"
},
"keywords": [
"react",
"omise"
],
"devDependencies": {
"@testing-library/react-hooks": "^3.7.0",
"@types/jest": "^26.0.19",
"@types/react": "^17.0.0",
"husky": "^4.3.6",
"jest": "^26.6.3",
"prettier": "^2.2.1",
"pretty-quick": "^3.1.0",
"react": "^17.0.1",
"react-test-renderer": "^17.0.1",
"ts-jest": "^26.4.4",
"typescript": "^4.1.3"
},
"peerDependencies": {
"react": "^16.8.0 || ^17.0.0"
},
"husky": {
"hooks": {
"pre-commit": "pretty-quick --staged"
}
}
}
Loading

0 comments on commit afd26a8

Please sign in to comment.