-
Notifications
You must be signed in to change notification settings - Fork 1
/
Api.php
128 lines (103 loc) · 3.12 KB
/
Api.php
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
namespace Payum\Paypal\Masspay\Nvp;
use Http\Message\MessageFactory;
use Payum\Core\Bridge\Spl\ArrayObject;
use Payum\Core\Exception\Http\HttpException;
use Payum\Core\Exception\InvalidArgumentException;
use Payum\Core\HttpClientInterface;
class Api
{
public const VERSION = '2.3';
public const ACK_SUCCESS = 'Success';
public const ACK_SUCCESS_WITH_WARNING = 'SuccessWithWarning';
public const ACK_FAILURE = 'Failure';
public const ACK_FAILURE_WITH_WARNING = 'FailureWithWarning';
/**
* @var HttpClientInterface
*/
protected $client;
/**
* @var MessageFactory
*/
protected $messageFactory;
/**
* @var array
*/
protected $options = [
'username' => null,
'password' => null,
'signature' => null,
];
/**
* @param HttpClientInterface|null $client
* @param MessageFactory|null $messageFactory
*/
public function __construct(array $options, HttpClientInterface $client, MessageFactory $messageFactory)
{
$options = ArrayObject::ensureArrayObject($options);
$options->defaults($this->options);
$options->validateNotEmpty([
'username',
'password',
'signature',
]);
if (! is_bool($options['sandbox'])) {
throw new InvalidArgumentException('The boolean sandbox option must be set.');
}
$this->options = $options;
$this->client = $client;
$this->messageFactory = $messageFactory;
}
/**
* @return array
*/
public function massPay(array $fields)
{
$fields['METHOD'] = 'MassPay';
$this->addVersionField($fields);
$this->addAuthorizeFields($fields);
return $this->doRequest($fields);
}
/**
* @throws HttpException
*
* @return array
*/
protected function doRequest(array $fields)
{
$headers = [
'Content-Type' => 'application/x-www-form-urlencoded',
];
$request = $this->messageFactory->createRequest('POST', $this->getApiEndpoint(), $headers, http_build_query($fields));
$response = $this->client->send($request);
if (! ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300)) {
throw HttpException::factory($request, $response);
}
$result = [];
parse_str($response->getBody()->getContents(), $result);
foreach ($result as &$value) {
$value = urldecode($value);
}
return $result;
}
/**
* @return string
*/
protected function getApiEndpoint()
{
return $this->options['sandbox'] ?
'https://api-3t.sandbox.paypal.com/nvp' :
'https://api-3t.paypal.com/nvp'
;
}
protected function addAuthorizeFields(array &$fields): void
{
$fields['PWD'] = $this->options['password'];
$fields['USER'] = $this->options['username'];
$fields['SIGNATURE'] = $this->options['signature'];
}
protected function addVersionField(array &$fields): void
{
$fields['VERSION'] = self::VERSION;
}
}