1 <?php
2 3 4
5 namespace Team3\PayU\Communication\Sender;
6
7 use Buzz\Client\ClientInterface;
8 use Buzz\Message\RequestInterface;
9 use Buzz\Message\Response;
10 use Psr\Log\LoggerInterface;
11 use Team3\PayU\Configuration\Credentials\CredentialsInterface;
12 use Team3\PayU\Communication\ClientException;
13
14 15 16 17 18 19
20 class Sender implements SenderInterface
21 {
22 23 24
25 private $curlClient;
26
27 28 29
30 private $logger;
31
32 33 34 35
36 public function __construct(
37 ClientInterface $curlClient,
38 LoggerInterface $logger
39 ) {
40 $this->curlClient = $curlClient;
41 $this->logger = $logger;
42 }
43
44 45 46 47 48 49 50
51 public function send(
52 RequestInterface $request,
53 CredentialsInterface $credentials
54 ) {
55 $response = new Response();
56
57 try {
58 $this
59 ->curlClient
60 ->send(
61 $request,
62 $response,
63 $this->getOptions($credentials)
64 );
65 } catch (\Exception $exception) {
66 $this->logException($exception);
67 throw new ClientException(
68 sprintf(
69 'Exception %s occurred with message: "%s"',
70 get_class($exception),
71 $exception->getMessage()
72 ),
73 $exception->getCode(),
74 $exception
75 );
76 }
77
78 return $response;
79 }
80
81 82 83 84 85
86 private function getOptions(CredentialsInterface $credentials)
87 {
88 return [
89 CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
90 CURLOPT_USERPWD => sprintf(
91 '%s:%s',
92 $credentials->getMerchantPosId(),
93 $credentials->getMerchantPosId()
94 ),
95 CURLOPT_SSLVERSION => 1,
96 CURLOPT_SSL_CIPHER_LIST => $credentials->getEncryptionProtocols(),
97 CURLOPT_SSL_VERIFYHOST => 2,
98 CURLOPT_SSL_VERIFYPEER => false,
99 CURLOPT_ENCODING => 'gzip',
100 CURLOPT_FOLLOWLOCATION => false,
101 ];
102 }
103
104 105 106
107 private function logException(\Exception $exception)
108 {
109 $this
110 ->logger
111 ->error(sprintf(
112 '%s was thrown with message %s',
113 get_class($exception),
114 $exception->getMessage()
115 ));
116 }
117 }
118