-
Notifications
You must be signed in to change notification settings - Fork 5
/
Api.php
88 lines (69 loc) · 2.22 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
<?php
namespace Payum\Paypal\Ipn;
use Http\Message\MessageFactory;
use Payum\Core\Exception\Http\HttpException;
use Payum\Core\Exception\InvalidArgumentException;
use Payum\Core\HttpClientInterface;
/**
* @link https://www.x.com/developers/paypal/documentation-tools/ipn/integration-guide/IPNIntro
*/
class Api
{
/**
* It sends back if the message originated with PayPal.
*/
public const NOTIFY_VERIFIED = 'VERIFIED';
/**
* if there is any discrepancy with what was originally sent
*/
public const NOTIFY_INVALID = 'INVALID';
public const CMD_NOTIFY_VALIDATE = '_notify-validate';
/**
* @var HttpClientInterface
*/
protected $client;
/**
* @var MessageFactory
*/
protected $messageFactory;
/**
* @var array
*/
protected $options;
public function __construct(array $options, HttpClientInterface $client, MessageFactory $messageFactory)
{
$this->client = $client;
$this->messageFactory = $messageFactory;
$this->options = $options;
if (! (isset($this->options['sandbox']) && is_bool($this->options['sandbox']))) {
throw new InvalidArgumentException('The boolean sandbox option must be set.');
}
}
/**
* @return string
*/
public function notifyValidate(array $fields)
{
$fields['cmd'] = self::CMD_NOTIFY_VALIDATE;
$headers = [
'Content-Type' => 'application/x-www-form-urlencoded',
];
$request = $this->messageFactory->createRequest('POST', $this->getIpnEndpoint(), $headers, http_build_query($fields));
$response = $this->client->send($request);
if (! ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300)) {
throw HttpException::factory($request, $response);
}
$result = $response->getBody()->getContents();
return self::NOTIFY_VERIFIED === $result ? self::NOTIFY_VERIFIED : self::NOTIFY_INVALID;
}
/**
* @return string
*/
public function getIpnEndpoint()
{
return $this->options['sandbox'] ?
'https://www.sandbox.paypal.com/cgi-bin/webscr' :
'https://www.paypal.com/cgi-bin/webscr'
;
}
}