1+ import axios from "axios" ;
2+ import crypto from "crypto" ;
3+
4+ class Thepeer {
5+ constructor ( secretKey ) {
6+ this . secretKey = secretKey
7+ this . request = axios . create ( {
8+ baseURL : 'https://api.thepeer.co' ,
9+ headers : {
10+ 'x-api-key' : secretKey ,
11+ 'Accept' : 'application/json'
12+ }
13+ } ) ;
14+ }
15+
16+ validateSignature ( payload , signature ) {
17+ return signature === crypto . createHmac ( 'sha1' , this . secretKey ) . update ( payload ) . digest ( 'hex' )
18+ }
19+
20+ async indexUser ( name , identifier , email ) {
21+ await this . request . post ( "/users" , {
22+ "name" : name ,
23+ "identifier" : identifier ,
24+ "email" : email
25+ } ) . then ( ( response ) => {
26+ return response . data
27+ } ) . catch ( ( error ) => {
28+ processError ( error )
29+ } ) ;
30+ }
31+
32+ async updateUser ( reference , identifier ) {
33+ await this . request . put ( `/users/${ reference } ` , {
34+ "identifier" : identifier ,
35+ } ) . then ( ( response ) => {
36+ return response . data
37+ } ) . catch ( ( error ) => {
38+ processError ( error )
39+ } ) ;
40+ }
41+
42+ async deleteUser ( reference ) {
43+ await this . request . delete ( `/users/${ reference } ` )
44+ . then ( ( response ) => {
45+ return response . data
46+ } ) . catch ( ( error ) => {
47+ processError ( error )
48+ } ) ;
49+ }
50+
51+ async getUser ( reference ) {
52+ await this . request . get ( `/users/${ reference } ` )
53+ . then ( ( response ) => {
54+ return response . data
55+ } ) . catch ( ( error ) => {
56+ processError ( error )
57+ } ) ;
58+ }
59+
60+ async getLink ( link ) {
61+ await this . request . get ( `/link/${ link } ` )
62+ . then ( ( response ) => {
63+ return response . data
64+ } ) . catch ( ( error ) => {
65+ processError ( error )
66+ } ) ;
67+ }
68+
69+ async chargeLink ( link , amount , remark ) {
70+ await this . request . post ( `/link/${ link } /charge` , {
71+ "amount" : amount ,
72+ "remark" : remark
73+ } ) . then ( ( response ) => {
74+ return response . data
75+ } ) . catch ( ( error ) => {
76+ processError ( error )
77+ } ) ;
78+ }
79+
80+ async authorizeDirectCharge ( reference , insufficientFunds ) {
81+ await this . request . post ( `/debit/${ reference } ` , {
82+ "insufficient_funds" : insufficientFunds ,
83+ } ) . then ( ( response ) => {
84+ return response . data
85+ } ) . catch ( ( error ) => {
86+ processError ( error )
87+ } ) ;
88+ }
89+ }
90+
91+ function processError ( error ) {
92+ switch ( error . response . status ) {
93+ case 401 :
94+ throw new UnauthorizedException ( error . response . data . message )
95+ case 403 :
96+ throw new ForbiddenException ( error . response . data . message )
97+ case 404 :
98+ throw new InvalidResourceException ( error . response . data . message )
99+ case 406 :
100+ throw new NotAcceptableException ( error . response . data . message )
101+ case 503 :
102+ throw new ServiceUnavailableException ( error . response . data . message )
103+ default :
104+ throw new ServerErrorException ( error . response . data . message )
105+ }
106+ }
107+
108+ function UnauthorizedException ( message ) {
109+ return new Error ( message ) ;
110+ }
111+
112+ function ServerErrorException ( message ) {
113+ return new Error ( message ) ;
114+ }
115+
116+ function ServiceUnavailableException ( message ) {
117+ return new Error ( message ) ;
118+ }
119+
120+ function NotAcceptableException ( message ) {
121+ return new Error ( message ) ;
122+ }
123+
124+ function InvalidResourceException ( message ) {
125+ return new Error ( message ) ;
126+ }
127+
128+ function InvalidPayloadException ( message ) {
129+ return new Error ( message ) ;
130+ }
131+
132+ function ForbiddenException ( message ) {
133+ return new Error ( message ) ;
134+ }
135+
136+ UnauthorizedException . prototype = Object . create ( Error . prototype ) ;
137+ ServerErrorException . prototype = Object . create ( Error . prototype ) ;
138+ ServiceUnavailableException . prototype = Object . create ( Error . prototype ) ;
139+ NotAcceptableException . prototype = Object . create ( Error . prototype ) ;
140+ InvalidResourceException . prototype = Object . create ( Error . prototype ) ;
141+ InvalidPayloadException . prototype = Object . create ( Error . prototype ) ;
142+ ForbiddenException . prototype = Object . create ( Error . prototype ) ;
0 commit comments