Skip to content

Commit

Permalink
better example.
Browse files Browse the repository at this point in the history
readme
bugfixes
version 1.0.5
  • Loading branch information
omidas committed Apr 12, 2019
1 parent 6369ab0 commit db7a51c
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 10 deletions.
98 changes: 97 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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 = <string>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/).
21 changes: 17 additions & 4 deletions lib/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <string>item;
console.log(val);
})
.catch(e => console.log(e));

pay
.verify({
token: 'token'
})
.then(item => console.log(item))
.catch(e => console.log(e));
25 changes: 22 additions & 3 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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<string | ISendSuccessResponse> => {
return new Promise((resolve, reject) => {
if (typeof args.amount !== 'number' || args.amount < 1000) {
throw new Error(
Expand Down Expand Up @@ -88,7 +107,7 @@ export default class PayIrTypescript {
/**
* Verify successful payment token
*/
public verify = (args: IVerifyArguments) => {
public verify = (args: IVerifyArguments): Promise<IVerifyResponse> => {
return new Promise((resolve, reject) => {
if (typeof args.token !== 'number') {
reject(new Error('token must be a number'));
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -34,7 +34,7 @@
},
"homepage": "https://github.com/electather/payir-typescript",
"files": [
"lib/**/*"
"build/lib/**/*"
],
"devDependencies": {
"@types/axios": "^0.14.0",
Expand Down

0 comments on commit db7a51c

Please sign in to comment.