1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mdb\PayPal\Ipn; |
4
|
|
|
|
5
|
|
|
use Http\Client\Exception; |
6
|
|
|
use Http\Message\StreamFactory; |
7
|
|
|
use Mdb\PayPal\Ipn\Event\IpnInvalidEvent; |
8
|
|
|
use Mdb\PayPal\Ipn\Event\IpnVerificationFailureEvent; |
9
|
|
|
use Mdb\PayPal\Ipn\Event\IpnVerifiedEvent; |
10
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
11
|
|
|
|
12
|
|
|
class Listener |
13
|
|
|
{ |
14
|
|
|
const IPN_VERIFIED_EVENT = 'ipn.message.verified'; |
15
|
|
|
const IPN_INVALID_EVENT = 'ipn.message.invalid'; |
16
|
|
|
const IPN_VERIFICATION_FAILURE_EVENT = 'ipn.message.verification_failure'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var StreamFactory |
20
|
|
|
*/ |
21
|
|
|
private $streamFactory; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var Verifier |
25
|
|
|
*/ |
26
|
|
|
private $verifier; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var EventDispatcherInterface |
30
|
|
|
*/ |
31
|
|
|
private $eventDispatcher; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param StreamFactory $streamFactory |
35
|
|
|
* @param Verifier $verifier |
36
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
37
|
|
|
*/ |
38
|
|
|
public function __construct( |
39
|
|
|
StreamFactory $streamFactory, |
40
|
|
|
Verifier $verifier, |
41
|
|
|
EventDispatcherInterface $eventDispatcher |
42
|
|
|
) { |
43
|
|
|
$this->streamFactory = $streamFactory; |
44
|
|
|
$this->verifier = $verifier; |
45
|
|
|
$this->eventDispatcher = $eventDispatcher; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function listen() |
49
|
|
|
{ |
50
|
|
|
$datas = \GuzzleHttp\Psr7\parse_query($this->createStream()->getContents(), PHP_QUERY_RFC1738); |
51
|
|
|
|
52
|
|
|
try { |
53
|
|
|
$result = $this->verifier->verify($datas); |
54
|
|
|
|
55
|
|
|
if ($result) { |
56
|
|
|
$eventName = self::IPN_VERIFIED_EVENT; |
57
|
|
|
$event = new IpnVerifiedEvent($datas); |
58
|
|
|
} else { |
59
|
|
|
$eventName = self::IPN_INVALID_EVENT; |
60
|
|
|
$event = new IpnInvalidEvent($datas); |
61
|
|
|
} |
62
|
|
|
} catch (\UnexpectedValueException $e) { |
63
|
|
|
$eventName = self::IPN_VERIFICATION_FAILURE_EVENT; |
64
|
|
|
$event = new IpnVerificationFailureEvent($datas, $e->getMessage()); |
65
|
|
|
} catch (Exception $e) { |
66
|
|
|
$eventName = self::IPN_VERIFICATION_FAILURE_EVENT; |
67
|
|
|
$event = new IpnVerificationFailureEvent($datas, $e->getMessage()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$this->eventDispatcher->dispatch($eventName, $event); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param callable $listener |
75
|
|
|
*/ |
76
|
|
|
public function onVerified($listener) |
77
|
|
|
{ |
78
|
|
|
$this->eventDispatcher->addListener(self::IPN_VERIFIED_EVENT, $listener); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param callable $listener |
83
|
|
|
*/ |
84
|
|
|
public function onInvalid($listener) |
85
|
|
|
{ |
86
|
|
|
$this->eventDispatcher->addListener(self::IPN_INVALID_EVENT, $listener); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param callable $listener |
91
|
|
|
*/ |
92
|
|
|
public function onVerificationFailure($listener) |
93
|
|
|
{ |
94
|
|
|
$this->eventDispatcher->addListener(self::IPN_VERIFICATION_FAILURE_EVENT, $listener); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return \Psr\Http\Message\StreamInterface |
99
|
|
|
*/ |
100
|
|
|
protected function createStream() |
101
|
|
|
{ |
102
|
|
|
return $this->streamFactory->createStream(\GuzzleHttp\Psr7\try_fopen('php://input', 'r')); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|