1+ <?php
2+
3+ namespace Thepeer \Sdk ;
4+
5+ use GuzzleHttp \Client ;
6+ use GuzzleHttp \Exception \GuzzleException ;
7+ use Illuminate \Http \Request ;
8+ use Thepeer \Sdk \Exceptions \InvalidPayloadException ;
9+ use Thepeer \Sdk \Exceptions \InvalidReceiptException ;
10+ use Thepeer \Sdk \Exceptions \InvalidSecretKeyException ;
11+ use Thepeer \Sdk \Exceptions \InvalidSignatureException ;
12+ use Thepeer \Sdk \Exceptions \SeverErrorException ;
13+ use Thepeer \Sdk \Exceptions \UserNotFoundException ;
14+
15+ class Thepeer
16+ {
17+ /**
18+ * @var string
19+ */
20+ private $ secretKey ;
21+
22+ /**
23+ * @var Client
24+ */
25+ private $ client ;
26+
27+ /**
28+ * Thepeer constructor.
29+ * @param $secretKey
30+ */
31+ public function __construct ($ secretKey )
32+ {
33+ $ this ->secretKey = $ secretKey ;
34+ $ this ->client = new Client ([
35+ 'base_uri ' => 'https://api.thepeer.co ' ,
36+ 'headers ' => [
37+ 'x-api-key ' => $ secretKey ,
38+ 'Content-Type ' => 'application/json ' ,
39+ 'Accept ' => 'application/json '
40+ ],
41+ 'http_errors ' => false
42+ ]);
43+ }
44+
45+ public function validateSignature (Request $ payload )
46+ {
47+ $ headerSignature = $ payload ->header ('X-Thepeer-Signature ' );
48+
49+ $ calculatedSignature = hash_hmac ('sha1 ' , json_encode ($ payload ->all ()), $ this ->secretKey );
50+
51+ if ($ headerSignature === $ calculatedSignature ) {
52+ return true ;
53+ }
54+
55+ throw new InvalidSignatureException ("signature does not match " );
56+ }
57+
58+ public function getReceipt ($ receipt )
59+ {
60+ try {
61+ $ request = $ this ->client ->get ("/verify/ {$ receipt }" );
62+
63+ $ payload = json_decode ($ request ->getBody ()->getContents ());
64+
65+ if ($ request ->getStatusCode () === 401 ) {
66+ throw new InvalidSecretKeyException ($ payload ->message );
67+ } else if ($ request ->getStatusCode () === 404 ) {
68+ throw new InvalidReceiptException ($ payload ->message );
69+ } else if ($ request ->getStatusCode () === 200 ) {
70+ return $ payload ;
71+ }
72+
73+ throw new SeverErrorException ("something went wrong " );
74+ } catch (GuzzleException $ e ) {
75+ throw new SeverErrorException ($ e ->getMessage ());
76+ }
77+ }
78+
79+ public function processReceipt ($ receipt )
80+ {
81+ try {
82+ $ request = $ this ->client ->post ("/send/ {$ receipt }" );
83+
84+ if ($ request ->getStatusCode () === 401 ) {
85+ throw new InvalidSecretKeyException ("invalid secret key " );
86+ } else if ($ request ->getStatusCode () === 404 ) {
87+ throw new InvalidReceiptException ("invalid receipt " );
88+ } else if ($ request ->getStatusCode () === 200 ) {
89+ return json_decode ($ request ->getBody ()->getContents ());
90+ }
91+
92+ throw new SeverErrorException ("something went wrong " );
93+ } catch (GuzzleException $ e ) {
94+ throw new SeverErrorException ($ e ->getMessage ());
95+ }
96+ }
97+
98+ public function indexUser (string $ name , string $ email , string $ identifier )
99+ {
100+ try {
101+ $ request = $ this ->client ->post ("/users/index " , [
102+ "body " => json_encode ([
103+ 'name ' => $ name ,
104+ 'email ' => $ email ,
105+ 'identifier ' => $ identifier
106+ ])
107+ ]);
108+
109+ $ payload = json_decode ($ request ->getBody ()->getContents ());
110+
111+ if ($ request ->getStatusCode () === 401 ) {
112+ throw new InvalidSecretKeyException ($ payload ->message );
113+ } else if ($ request ->getStatusCode () === 406 ) {
114+ throw new InvalidPayloadException ($ payload ->message );
115+ } else if ($ request ->getStatusCode () === 422 ) {
116+ foreach ($ payload ->errors as $ error ) {
117+ throw new InvalidPayloadException ($ error [0 ]);
118+ }
119+ } else if ($ request ->getStatusCode () === 200 ) {
120+ return $ payload ;
121+ }
122+
123+ throw new SeverErrorException ("something went wrong " );
124+ } catch (GuzzleException $ e ) {
125+ throw new SeverErrorException ($ e ->getMessage ());
126+ }
127+ }
128+
129+ public function updateUser (string $ reference , string $ identifier )
130+ {
131+ try {
132+ $ request = $ this ->client ->put ("/users/update/ {$ reference }" , [
133+ "body " => json_encode ([
134+ 'identifier ' => $ identifier
135+ ])
136+ ]);
137+
138+ $ payload = json_decode ($ request ->getBody ()->getContents ());
139+
140+ if ($ request ->getStatusCode () === 401 ) {
141+ throw new InvalidSecretKeyException ("invalid secret key " );
142+ } else if ($ request ->getStatusCode () === 406 ) {
143+ throw new InvalidPayloadException ($ payload ->message );
144+ } else if ($ request ->getStatusCode () === 404 ) {
145+ throw new UserNotFoundException ($ payload ->message );
146+ } else if ($ request ->getStatusCode () === 422 ) {
147+ foreach ($ payload ->errors as $ error ) {
148+ throw new InvalidPayloadException ($ error [0 ]);
149+ }
150+ } else if ($ request ->getStatusCode () === 200 ) {
151+ return $ payload ;
152+ }
153+
154+ throw new SeverErrorException ("something went wrong " );
155+ } catch (GuzzleException $ e ) {
156+ throw new SeverErrorException ($ e ->getMessage ());
157+ }
158+ }
159+
160+ public function deleteUser (string $ reference )
161+ {
162+ try {
163+ $ request = $ this ->client ->delete ("/users/delete/ {$ reference }" );
164+
165+ $ payload = json_decode ($ request ->getBody ()->getContents ());
166+
167+ if ($ request ->getStatusCode () === 401 ) {
168+ throw new InvalidSecretKeyException ("invalid secret key " );
169+ } else if ($ request ->getStatusCode () === 406 ) {
170+ throw new InvalidPayloadException ($ payload ->message );
171+ } else if ($ request ->getStatusCode () === 404 ) {
172+ throw new UserNotFoundException ($ payload ->message );
173+ } else if ($ request ->getStatusCode () === 422 ) {
174+ foreach ($ payload ->errors as $ error ) {
175+ throw new InvalidPayloadException ($ error [0 ]);
176+ }
177+ } else if ($ request ->getStatusCode () === 200 ) {
178+ return true ;
179+ }
180+
181+ throw new SeverErrorException ("something went wrong " );
182+ } catch (GuzzleException $ e ) {
183+ throw new SeverErrorException ($ e ->getMessage ());
184+ }
185+ }
186+ }
0 commit comments