1
|
|
|
<?php
|
2
|
|
|
|
3
|
|
|
namespace Omnipay\Spreedly\Message;
|
4
|
|
|
|
5
|
|
|
use Guzzle\Common\Exception\RuntimeException;
|
6
|
|
|
use Guzzle\Http\Exception\BadResponseException;
|
7
|
|
|
use Omnipay\Common\Helper;
|
8
|
|
|
use Omnipay\Common\Message\AbstractRequest as BaseAbstractRequest;
|
9
|
|
|
use Omnipay\Spreedly\Concerns\HasSpreedlyConfig;
|
10
|
|
|
|
11
|
|
|
abstract class AbstractRequest extends BaseAbstractRequest
|
12
|
|
|
{
|
13
|
|
|
use HasSpreedlyConfig;
|
14
|
|
|
|
15
|
|
|
/**
|
16
|
|
|
* @var string
|
17
|
|
|
*/
|
18
|
|
|
protected $endpoint = 'https://core.spreedly.com/v1/';
|
19
|
|
|
|
20
|
|
|
/**
|
21
|
|
|
* Get HTTP Method.
|
22
|
|
|
*
|
23
|
|
|
* This is nearly always POST but can be over-ridden in sub classes.
|
24
|
|
|
*
|
25
|
|
|
* @return string
|
26
|
|
|
*/
|
27
|
18 |
|
public function getHttpMethod()
|
28
|
|
|
{
|
29
|
18 |
|
return 'POST';
|
30
|
|
|
}
|
31
|
|
|
|
32
|
|
|
/**
|
33
|
|
|
* @return array
|
34
|
|
|
*/
|
35
|
36 |
|
public function getHeaders()
|
36
|
|
|
{
|
37
|
|
|
$headers = array(
|
38
|
|
|
'Content-Type' => 'application/json'
|
39
|
36 |
|
);
|
40
|
|
|
|
41
|
36 |
|
return $headers;
|
42
|
|
|
}
|
43
|
|
|
|
44
|
|
|
/**
|
45
|
|
|
* @return string
|
46
|
|
|
*/
|
47
|
|
|
abstract public function getEndpoint();
|
48
|
|
|
|
49
|
|
|
/**
|
50
|
|
|
* @param array|null $data
|
51
|
|
|
* @return \Omnipay\Common\Message\ResponseInterface|Response
|
52
|
|
|
*/
|
53
|
36 |
|
public function sendData($data)
|
54
|
|
|
{
|
55
|
36 |
|
$httpRequest = $this->httpClient->createRequest(
|
56
|
36 |
|
$this->getHttpMethod(),
|
57
|
36 |
|
$this->getEndpoint() . '.json',
|
58
|
36 |
|
$this->getHeaders(),
|
59
|
|
|
$data
|
60
|
36 |
|
);
|
61
|
|
|
|
62
|
36 |
|
$httpRequest->setAuth($this->getApiKey(), $this->getApiSecret());
|
63
|
|
|
|
64
|
|
|
try {
|
65
|
36 |
|
$httpResponse = $httpRequest->send();
|
66
|
36 |
|
} catch (BadResponseException $e) {
|
67
|
2 |
|
if (false !== strstr($e->getMessage(), '422')) {
|
68
|
|
|
return $this->createResponse([
|
69
|
|
|
'exception' => $e->getResponse()->json()
|
70
|
|
|
]);
|
71
|
|
|
}
|
72
|
2 |
|
throw $e;
|
73
|
|
|
}
|
74
|
|
|
|
75
|
34 |
|
return $this->createResponse($httpResponse->json());
|
76
|
|
|
}
|
77
|
|
|
|
78
|
|
|
/**
|
79
|
|
|
* Map data with existing parameters
|
80
|
|
|
*
|
81
|
|
|
* @param array $data
|
82
|
|
|
* @param array $map
|
83
|
|
|
* @return array
|
84
|
|
|
*/
|
85
|
24 |
|
protected function fillExistingParameters($data, $map)
|
86
|
|
|
{
|
87
|
24 |
|
foreach ($map as $key => $parameter) {
|
88
|
24 |
|
$value = null;
|
89
|
24 |
|
$method = 'get'.ucfirst(Helper::camelCase($parameter));
|
90
|
24 |
|
if (method_exists($this, $method)) {
|
91
|
24 |
|
$value = $this->$method();
|
92
|
24 |
|
} elseif ($this->parameters->has($parameter)) {
|
93
|
|
|
$value = $this->parameters->get($parameter);
|
94
|
|
|
}
|
95
|
24 |
|
if (!is_null($value)) {
|
96
|
24 |
|
$data[$key] = $value;
|
97
|
24 |
|
}
|
98
|
24 |
|
}
|
99
|
|
|
|
100
|
24 |
|
return $data;
|
101
|
|
|
}
|
102
|
|
|
|
103
|
34 |
|
protected function createResponse($data)
|
104
|
|
|
{
|
105
|
34 |
|
return $this->response = new Response($this, $data);
|
106
|
|
|
}
|
107
|
|
|
}
|
108
|
|
|
|