|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ProtoneMedia\LaravelPaddle\Api; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Http; |
|
6
|
|
|
use Illuminate\Support\Facades\Validator; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
|
|
9
|
|
|
class Request |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Paddle API Endpoint. |
|
13
|
|
|
*/ |
|
14
|
|
|
const API_ENDPOINT = "https://vendors.paddle.com/api"; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Method options. |
|
18
|
|
|
*/ |
|
19
|
|
|
const METHOD_GET = 'get'; |
|
20
|
|
|
const METHOD_POST = 'post'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $uri; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
private $data = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
private $rules = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
private $method; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Creates an instance with the URI and data. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $uri |
|
46
|
|
|
* @param array $data |
|
47
|
|
|
* @param array $rules |
|
48
|
|
|
* @param string $method |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct(string $uri, array $data = [], array $rules = [], string $method = self::METHOD_POST) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->uri = $uri; |
|
53
|
|
|
$this->rules = $rules; |
|
54
|
|
|
$this->method = $method; |
|
55
|
|
|
|
|
56
|
|
|
$this->fill($data); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Formats the URL to send the request to. |
|
61
|
|
|
* |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
|
|
public function url(): string |
|
65
|
|
|
{ |
|
66
|
|
|
return static::API_ENDPOINT . $this->uri; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Validates the data with the rules. |
|
71
|
|
|
* |
|
72
|
|
|
* @return $this |
|
73
|
|
|
* @throws \ProtoneMedia\LaravelPaddle\Api\InvalidDataException |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function validateData() |
|
76
|
|
|
{ |
|
77
|
|
|
tap(Validator::make($this->data, $this->rules), function ($validator) { |
|
78
|
|
|
if ($validator->passes()) { |
|
79
|
|
|
return; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
throw InvalidDataException::fromValidator($validator); |
|
83
|
|
|
}); |
|
84
|
|
|
|
|
85
|
|
|
return $this; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Sends the data payload to the uri and returns to decoded response. |
|
90
|
|
|
* |
|
91
|
|
|
* @return mixed |
|
92
|
|
|
* |
|
93
|
|
|
* @throws \ProtoneMedia\LaravelPaddle\Api\PaddleApiException |
|
94
|
|
|
*/ |
|
95
|
|
|
public function send() |
|
96
|
|
|
{ |
|
97
|
|
|
$this->validateData(); |
|
98
|
|
|
|
|
99
|
|
|
$method = $this->method; |
|
100
|
|
|
|
|
101
|
|
|
$data = $this->getData() + ['vendor_id' => config('paddle.vendor_id')]; |
|
102
|
|
|
|
|
103
|
|
|
if ($method === static::METHOD_POST) { |
|
104
|
|
|
$data['vendor_auth_code'] = config('paddle.vendor_auth_code'); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$url = $this->url(); |
|
108
|
|
|
|
|
109
|
|
|
if (config('paddle.sandbox_environment')) { |
|
110
|
|
|
$url = str_replace('https://', 'https://sandbox-', $url); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
$response = Http::asForm()->$method($url, $data); |
|
114
|
|
|
|
|
115
|
|
|
if (!$response->successful()) { |
|
116
|
|
|
throw PaddleApiException::unsuccessfulStatus($response->status()); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$json = $response->json(); |
|
120
|
|
|
|
|
121
|
|
|
if (array_key_exists('success', $json) && !$json['success']) { |
|
122
|
|
|
throw PaddleApiException::fromResponse($json); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $json['response'] ?? $json; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Getter for the set data. |
|
130
|
|
|
* |
|
131
|
|
|
* @return array |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getData(): array |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->data; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Loops through the array to set all data attributes. |
|
140
|
|
|
* |
|
141
|
|
|
* @param array $data |
|
142
|
|
|
* @return $this |
|
143
|
|
|
*/ |
|
144
|
|
|
private function fill(array $data) |
|
145
|
|
|
{ |
|
146
|
|
|
foreach ($data as $key => $value) { |
|
147
|
|
|
$key = lcfirst(Str::studly($key)); |
|
148
|
|
|
|
|
149
|
|
|
$this->$key($value); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Setter for the data. |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $key |
|
157
|
|
|
* @param mixed $value |
|
158
|
|
|
* @return $this |
|
159
|
|
|
*/ |
|
160
|
|
|
protected function setAttribute(string $key, $value = null) |
|
161
|
|
|
{ |
|
162
|
|
|
$this->data[Str::snake($key)] = $value; |
|
163
|
|
|
|
|
164
|
|
|
return $this; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* To make the request fluent. |
|
169
|
|
|
* |
|
170
|
|
|
* @param string $method |
|
171
|
|
|
* @param array $parameters |
|
172
|
|
|
* @return $this |
|
173
|
|
|
*/ |
|
174
|
|
|
public function __call($method, $parameters) |
|
175
|
|
|
{ |
|
176
|
|
|
return $this->setAttribute($method, $parameters[0] ?? null); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|