Skip to content

Commit db7a51c

Browse files
committed
better example.
readme bugfixes version 1.0.5
1 parent 6369ab0 commit db7a51c

File tree

4 files changed

+138
-10
lines changed

4 files changed

+138
-10
lines changed

README.md

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,97 @@
1-
# pay.ir node.js typescript library
1+
# pay.ir node.js typescript module
2+
3+
## what is
4+
5+
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.
6+
7+
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.
8+
so even if you don't use the typescript to write your app the improvements must be clear.
9+
10+
## improvements
11+
12+
- typings. get assist from IDEs when writing your code.
13+
- better networking. this library uses [Axios](https://github.com/axios/axios) for networking and error handling is much more to my liking.
14+
- smaller code due to better error handling
15+
16+
## Installing
17+
18+
first you need to pass your API key to the constructor.
19+
20+
Using npm:
21+
22+
```bash
23+
npm install axios
24+
```
25+
26+
## Example
27+
28+
import library using
29+
30+
```javascript
31+
const Pay = require('payir-typescript');
32+
```
33+
34+
or:
35+
36+
```javascript
37+
import PayIrTypescript from 'payir-typescript';
38+
```
39+
40+
then pass the API key to the constructor
41+
42+
```javascript
43+
const pay = new PayIrTypescript(APIKEY);
44+
```
45+
46+
now you can use the following methods on the pay instance
47+
48+
### Send
49+
50+
this method accepts payment parameters as an object and returns a promise.
51+
52+
```javascript
53+
pay
54+
.send({
55+
amount: AMOUNT,
56+
redirect: REDIRECT_URL
57+
})
58+
.then(item => {
59+
const val = <string>item;
60+
console.log(val);
61+
})
62+
.catch(e => console.log(e));
63+
```
64+
65+
or using `async/await` we can have
66+
67+
```javascript
68+
const payment = await pay.send({
69+
amount: AMOUNT,
70+
redirect: REDIRECT_URL
71+
});
72+
```
73+
74+
full list of parameters accepted are listed in [pay.ir](https://pay.ir/docs/gateway/).
75+
76+
### Verify
77+
78+
this method accepts verify parameters as an object and returns a promise.
79+
80+
```javascript
81+
pay
82+
.verify({
83+
token: 'token'
84+
})
85+
.then(item => console.log(item))
86+
.catch(e => console.log(e));
87+
```
88+
89+
or using `async/await` we can have
90+
91+
```javascript
92+
const payment = await pay.verify({
93+
token: 'token'
94+
});
95+
```
96+
97+
full list of parameters accepted are listed in [pay.ir](https://pay.ir/docs/gateway/).

lib/example.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,20 @@ import PayIrTypescript from './index';
22

33
const pay = new PayIrTypescript('testa');
44

5-
pay.send({
6-
amount: 1000,
7-
redirect: 'https://www.typescriptlang.org/docs/handbook/modules.html'
8-
});
5+
pay
6+
.send({
7+
amount: 1000,
8+
redirect: 'https://www.typescriptlang.org/docs/handbook/modules.html'
9+
})
10+
.then(item => {
11+
const val = <string>item;
12+
console.log(val);
13+
})
14+
.catch(e => console.log(e));
15+
16+
pay
17+
.verify({
18+
token: 'token'
19+
})
20+
.then(item => console.log(item))
21+
.catch(e => console.log(e));

lib/index.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,25 @@ interface ISendArguments {
99
}
1010

1111
interface IVerifyArguments {
12-
token: number;
12+
token: number | string;
1313
}
1414

15+
interface ISendSuccessResponse {
16+
status: number;
17+
token: string;
18+
}
19+
20+
interface IVerifyResponse {
21+
status: number;
22+
amount: string;
23+
transId: number;
24+
factorNumber?: any;
25+
mobile: string;
26+
description: string;
27+
cardNumber: string;
28+
traceNumber: string;
29+
message: string;
30+
}
1531
/**
1632
* this class proved all of your required functions to make a successful payment using pay.ir gateway
1733
*/
@@ -57,7 +73,10 @@ export default class PayIrTypescript {
5773
/**
5874
* get payment url
5975
*/
60-
public send = (args: ISendArguments, getRedirect: boolean = false) => {
76+
public send = (
77+
args: ISendArguments,
78+
getRedirect: boolean = false
79+
): Promise<string | ISendSuccessResponse> => {
6180
return new Promise((resolve, reject) => {
6281
if (typeof args.amount !== 'number' || args.amount < 1000) {
6382
throw new Error(
@@ -88,7 +107,7 @@ export default class PayIrTypescript {
88107
/**
89108
* Verify successful payment token
90109
*/
91-
public verify = (args: IVerifyArguments) => {
110+
public verify = (args: IVerifyArguments): Promise<IVerifyResponse> => {
92111
return new Promise((resolve, reject) => {
93112
if (typeof args.token !== 'number') {
94113
reject(new Error('token must be a number'));

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "payir-typescript",
3-
"version": "1.0.0",
3+
"version": "1.0.5",
44
"description": "",
55
"main": "build/lib/index.js",
66
"types": "build/lib/index.d.ts",
@@ -34,7 +34,7 @@
3434
},
3535
"homepage": "https://github.com/electather/payir-typescript",
3636
"files": [
37-
"lib/**/*"
37+
"build/lib/**/*"
3838
],
3939
"devDependencies": {
4040
"@types/axios": "^0.14.0",

0 commit comments

Comments
 (0)