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