forked from yabacon/paystack-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponse.php
More file actions
69 lines (60 loc) · 1.82 KB
/
Response.php
File metadata and controls
69 lines (60 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
namespace Yabacon\Paystack\Http;
use \Yabacon\Paystack\Exception\ApiException;
class Response
{
public $okay;
public $body;
public $forApi;
public $messages = [];
private function parsePaystackResponse()
{
$resp = \json_decode($this->body);
if ($resp === null || !property_exists($resp, 'status') || !$resp->status) {
throw new ApiException(
"Paystack Request failed with response: '" .
$this->messageFromApiJson($resp)."'",
$resp
);
}
return $resp;
}
private function messageFromApiJson($resp)
{
$message = $this->body;
if ($resp !== null) {
if (property_exists($resp, 'message')) {
$message = $resp->message;
}
if (property_exists($resp, 'errors') && ($resp->errors instanceof \stdClass)) {
$message .= "\nErrors:\n";
foreach ($resp->errors as $field => $errors) {
$message .= "\t" . $field . ":\n";
foreach ($errors as $_unused => $error) {
$message .= "\t\t" . $error->rule . ": ";
$message .= $error->message . "\n";
}
}
}
}
return $message;
}
private function implodedMessages()
{
return implode("\n\n", $this->messages);
}
public function wrapUp()
{
if ($this->okay && $this->forApi) {
return $this->parsePaystackResponse();
}
if (!$this->okay && $this->forApi) {
throw new \Exception($this->implodedMessages());
}
if ($this->okay) {
return $this->body;
}
error_log($this->implodedMessages());
return false;
}
}