From db7a51c716b03d1935d8e5f9a55794ff6ef24bf9 Mon Sep 17 00:00:00 2001 From: Omid Astaraki Date: Fri, 12 Apr 2019 19:12:00 +0430 Subject: [PATCH] better example. readme bugfixes version 1.0.5 --- README.md | 98 +++++++++++++++++++++++++++++++++++++++++++++++++- lib/example.ts | 21 ++++++++--- lib/index.ts | 25 +++++++++++-- package.json | 4 +-- 4 files changed, 138 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 0f485f7..2450001 100644 --- a/README.md +++ b/README.md @@ -1 +1,97 @@ -# pay.ir node.js typescript library +# pay.ir node.js typescript module + +## what is + +By using this package, you'll be able to work with Pay.ir REST Api in Node.js (Back-End) without any problem! This package is usable for all Node.js Frameworks such as Express.js, Hapi.js, Sails.js, Adonis.js or others. + +This package is written in Typescript and targets ES6. the main difference between this package and [official](https://github.com/erfansahaf/payir) one is the typings and readability. I rewrote everything from scratch and added the much needed improvements to the original package. +so even if you don't use the typescript to write your app the improvements must be clear. + +## improvements + +- typings. get assist from IDEs when writing your code. +- better networking. this library uses [Axios](https://github.com/axios/axios) for networking and error handling is much more to my liking. +- smaller code due to better error handling + +## Installing + +first you need to pass your API key to the constructor. + +Using npm: + +```bash +npm install axios +``` + +## Example + +import library using + +```javascript +const Pay = require('payir-typescript'); +``` + +or: + +```javascript +import PayIrTypescript from 'payir-typescript'; +``` + +then pass the API key to the constructor + +```javascript +const pay = new PayIrTypescript(APIKEY); +``` + +now you can use the following methods on the pay instance + +### Send + +this method accepts payment parameters as an object and returns a promise. + +```javascript +pay + .send({ + amount: AMOUNT, + redirect: REDIRECT_URL + }) + .then(item => { + const val = item; + console.log(val); + }) + .catch(e => console.log(e)); +``` + +or using `async/await` we can have + +```javascript +const payment = await pay.send({ + amount: AMOUNT, + redirect: REDIRECT_URL +}); +``` + +full list of parameters accepted are listed in [pay.ir](https://pay.ir/docs/gateway/). + +### Verify + +this method accepts verify parameters as an object and returns a promise. + +```javascript +pay + .verify({ + token: 'token' + }) + .then(item => console.log(item)) + .catch(e => console.log(e)); +``` + +or using `async/await` we can have + +```javascript +const payment = await pay.verify({ + token: 'token' +}); +``` + +full list of parameters accepted are listed in [pay.ir](https://pay.ir/docs/gateway/). diff --git a/lib/example.ts b/lib/example.ts index 8cc8db0..747eb51 100644 --- a/lib/example.ts +++ b/lib/example.ts @@ -2,7 +2,20 @@ import PayIrTypescript from './index'; const pay = new PayIrTypescript('testa'); -pay.send({ - amount: 1000, - redirect: 'https://www.typescriptlang.org/docs/handbook/modules.html' -}); +pay + .send({ + amount: 1000, + redirect: 'https://www.typescriptlang.org/docs/handbook/modules.html' + }) + .then(item => { + const val = item; + console.log(val); + }) + .catch(e => console.log(e)); + +pay + .verify({ + token: 'token' + }) + .then(item => console.log(item)) + .catch(e => console.log(e)); diff --git a/lib/index.ts b/lib/index.ts index 2fcc2f4..77b1bc0 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -9,9 +9,25 @@ interface ISendArguments { } interface IVerifyArguments { - token: number; + token: number | string; } +interface ISendSuccessResponse { + status: number; + token: string; +} + +interface IVerifyResponse { + status: number; + amount: string; + transId: number; + factorNumber?: any; + mobile: string; + description: string; + cardNumber: string; + traceNumber: string; + message: string; +} /** * this class proved all of your required functions to make a successful payment using pay.ir gateway */ @@ -57,7 +73,10 @@ export default class PayIrTypescript { /** * get payment url */ - public send = (args: ISendArguments, getRedirect: boolean = false) => { + public send = ( + args: ISendArguments, + getRedirect: boolean = false + ): Promise => { return new Promise((resolve, reject) => { if (typeof args.amount !== 'number' || args.amount < 1000) { throw new Error( @@ -88,7 +107,7 @@ export default class PayIrTypescript { /** * Verify successful payment token */ - public verify = (args: IVerifyArguments) => { + public verify = (args: IVerifyArguments): Promise => { return new Promise((resolve, reject) => { if (typeof args.token !== 'number') { reject(new Error('token must be a number')); diff --git a/package.json b/package.json index a7df674..8202618 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "payir-typescript", - "version": "1.0.0", + "version": "1.0.5", "description": "", "main": "build/lib/index.js", "types": "build/lib/index.d.ts", @@ -34,7 +34,7 @@ }, "homepage": "https://github.com/electather/payir-typescript", "files": [ - "lib/**/*" + "build/lib/**/*" ], "devDependencies": { "@types/axios": "^0.14.0",