1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Vipps exception. |
5
|
|
|
* |
6
|
|
|
* Provides and handles vipps exception. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Vipps\Exceptions; |
10
|
|
|
|
11
|
|
|
use Psr\Http\Message\ResponseInterface; |
12
|
|
|
use Vipps\Model\Error\ErrorInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class VippsException |
16
|
|
|
* @package Vipps\Exceptions |
17
|
|
|
*/ |
18
|
|
|
class VippsException extends \Exception |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Initiate code description array. |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
static public $codes = [ |
26
|
|
|
01 => "Provided credentials doesn't match", |
27
|
|
|
21 => "Reference Order ID is not valid", |
28
|
|
|
22 => "Reference Order ID is not in valid state", |
29
|
|
|
31 => "Merchant is blocked because of {}", |
30
|
|
|
32 => "Receiving limit of merchant has exceeded", |
31
|
|
|
33 => "Number of payment requests has been exceeded", |
32
|
|
|
34 => "Unique constraint violation of the order id", |
33
|
|
|
35 => "Requested Order not found", |
34
|
|
|
36 => "Merchant agreement not signed", |
35
|
|
|
37 => "Merchant not available or deactivated or blocked", |
36
|
|
|
41 => "User don’t have a valid card", |
37
|
|
|
42 => "Refused by issuer bank", |
38
|
|
|
43 => "Refused by issuer bank because of invalid amount", |
39
|
|
|
44 => "Refused by issuer because of expired card", |
40
|
|
|
45 => "Reservation failed for some unknown reason", |
41
|
|
|
51 => "Can't cancel already captured order", |
42
|
|
|
52 => "Cancellation failed", |
43
|
|
|
53 => "Can’t cancel order which is not reserved yet", |
44
|
|
|
61 => "Captured amount exceeds the reserved amount ordered", |
45
|
|
|
62 => "Can't capture cancelled order", |
46
|
|
|
63 => "Capture failed for some unknown reason, please use Get Payment Details API to know the exact status", |
47
|
|
|
71 => "Cant refund more than captured amount", |
48
|
|
|
72 => "Cant refund for reserved order, use cancellation API for the", |
49
|
|
|
73 => "Can't refund on cancelled order", |
50
|
|
|
74 => "Refund failed during debit from merchant account", |
51
|
|
|
81 => "User Not registered with vipps", |
52
|
|
|
82 => "User App Version is not supported", |
53
|
|
|
91 => "Transaction is not allowed", |
54
|
|
|
92 => "Transaction already processed", |
55
|
|
|
98 => "Too many concurrent requests", |
56
|
|
|
99 => "Internal error", |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Error group. |
61
|
|
|
* |
62
|
|
|
* @var null |
63
|
|
|
*/ |
64
|
|
|
protected $errorGroup; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Error code. |
68
|
|
|
* |
69
|
|
|
* @var int |
70
|
|
|
*/ |
71
|
|
|
protected $errorCode = 0; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Error message. |
75
|
|
|
* |
76
|
|
|
* @var null |
77
|
|
|
*/ |
78
|
|
|
protected $errorMessage; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* VippsException constructor. |
82
|
|
|
* |
83
|
|
|
* @param string $message |
84
|
|
|
* @param int $code |
85
|
|
|
* @param \Exception|null $previous |
86
|
|
|
*/ |
87
|
|
|
public function __construct($message = '', $code = 0, \Exception $previous = null) |
88
|
|
|
{ |
89
|
|
|
if ($previous instanceof VippsException) { |
90
|
|
|
$this->errorCode = $previous->getErrorCode(); |
91
|
|
|
$this->errorGroup = $previous->getErrorGroup(); |
92
|
|
|
$this->errorMessage = $previous->getErrorMessage(); |
93
|
|
|
} |
94
|
|
|
parent::__construct($message, $code, $previous); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return mixed |
99
|
|
|
*/ |
100
|
|
|
public function getErrorCode() |
101
|
|
|
{ |
102
|
|
|
return $this->errorCode; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return mixed |
107
|
|
|
*/ |
108
|
|
|
public function getErrorGroup() |
109
|
|
|
{ |
110
|
|
|
return $this->errorGroup; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return mixed |
115
|
|
|
*/ |
116
|
|
|
public function getErrorMessage() |
117
|
|
|
{ |
118
|
|
|
return $this->errorMessage; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Create new Exception from Response. |
123
|
|
|
* |
124
|
|
|
* @param $response |
125
|
|
|
* @return self|null |
126
|
|
|
*/ |
127
|
|
|
public static function createFromResponse(ResponseInterface $response) |
128
|
|
|
{ |
129
|
|
|
|
130
|
|
|
$content = $response->getBody()->getContents(); |
131
|
|
|
|
132
|
|
|
// @todo: Match one of the error types. |
133
|
|
|
|
134
|
|
|
// @todo: Check if error. |
135
|
|
|
if (!($content instanceof ErrorInterface)) { |
136
|
|
|
// Rewind content pointer. |
137
|
|
|
$response->getBody()->rewind(); |
138
|
|
|
return null; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
// @todo: Create Exception of correct type. |
142
|
|
|
|
143
|
|
|
return new static(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param $code |
148
|
|
|
* @return null |
149
|
|
|
*/ |
150
|
|
|
public static function getErrorCodeDescription($code = 0) |
151
|
|
|
{ |
152
|
|
|
if (isset(self::$codes[$code])) { |
153
|
|
|
return self::$codes[$code]; |
154
|
|
|
} |
155
|
|
|
return null; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|