|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Service\Paypal; |
|
4
|
|
|
|
|
5
|
|
|
use App\Service\SettingsService; |
|
6
|
|
|
use PayPal\Auth\OAuthTokenCredential; |
|
7
|
|
|
use PayPal\Rest\ApiContext; |
|
8
|
|
|
use PayPalHttp\HttpRequest; |
|
9
|
|
|
use Psr\Log\LoggerInterface; |
|
10
|
|
|
use Exception; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class AbstractPaypalService |
|
14
|
|
|
* @package App\Service\Paypal |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class AbstractPaypalService |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $clientId; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $clientSecret; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var ApiContext |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $apiContext; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var LoggerInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $logger; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var SessionService |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $sessionService; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var SettingsService |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $settingsService; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* PaypalService constructor. |
|
50
|
|
|
* @param string $clientId |
|
51
|
|
|
* @param string $clientSecret |
|
52
|
|
|
* @param LoggerInterface $logger |
|
53
|
|
|
* @param SessionService $sessionService |
|
54
|
|
|
* @param SettingsService $settingsService |
|
55
|
|
|
*/ |
|
56
|
|
|
public function __construct( |
|
57
|
|
|
string $clientId, |
|
58
|
|
|
string $clientSecret, |
|
59
|
|
|
LoggerInterface $logger, |
|
60
|
|
|
SessionService $sessionService, |
|
61
|
|
|
SettingsService $settingsService |
|
62
|
|
|
) { |
|
63
|
|
|
$sessionClientId = $sessionService->session->get('PAYPAL_SDK_CLIENT_ID'); |
|
64
|
|
|
$sessionClientSecret = $sessionService->session->get('PAYPAL_SDK_CLIENT_SECRET'); |
|
65
|
|
|
$sessionSDKExtra = $sessionService->session->get('PAYPAL_SDK_EXTRA'); |
|
|
|
|
|
|
66
|
|
|
$this->clientId = $sessionClientId ?? $clientId; |
|
67
|
|
|
$this->clientSecret = $sessionClientSecret ?? $clientSecret; |
|
68
|
|
|
$this->logger = $logger; |
|
69
|
|
|
$apiContext = new ApiContext(new OAuthTokenCredential($this->clientId, $this->clientSecret)); |
|
70
|
|
|
$apiContext->setConfig(['mode' => 'sandbox']); |
|
71
|
|
|
$this->apiContext = $apiContext; |
|
72
|
|
|
$this->settingsService = $settingsService; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param $requestBody |
|
77
|
|
|
* @param string $accessToken |
|
78
|
|
|
* @param string $url |
|
79
|
|
|
* @param array $inputHeaders |
|
80
|
|
|
* @param string $verb |
|
81
|
|
|
* @return array|string|null |
|
82
|
|
|
*/ |
|
83
|
|
|
public function paypalApiCall( |
|
84
|
|
|
string $accessToken, |
|
85
|
|
|
$requestBody, |
|
86
|
|
|
string $url, |
|
87
|
|
|
array $inputHeaders = [], |
|
88
|
|
|
string $verb = 'POST' |
|
89
|
|
|
) { |
|
90
|
|
|
try { |
|
91
|
|
|
$headers = array_merge($inputHeaders, [ |
|
92
|
|
|
'Content-Type: application/json', |
|
93
|
|
|
'Authorization: Bearer ' . $accessToken, |
|
94
|
|
|
]); |
|
95
|
|
|
$ch = curl_init(); |
|
96
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
|
97
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $verb); |
|
98
|
|
|
if ($requestBody !== null) { |
|
99
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody); |
|
100
|
|
|
} |
|
101
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
102
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
|
103
|
|
|
$result = curl_exec($ch); |
|
104
|
|
|
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
|
105
|
|
|
curl_close($ch); |
|
106
|
|
|
} catch (Exception $e) { |
|
107
|
|
|
$this->logger->error('Error on PayPal::'.$url.' = ' . $e->getMessage()); |
|
108
|
|
|
return null; |
|
109
|
|
|
} |
|
110
|
|
|
return ([ |
|
111
|
|
|
'result' => $result, |
|
112
|
|
|
'statusCode' => $statusCode |
|
113
|
|
|
]); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Add Negative testing Settings header to the given request |
|
118
|
|
|
* @param HttpRequest $request |
|
119
|
|
|
* @return void |
|
120
|
|
|
*/ |
|
121
|
|
|
public function addNegativeTestingSetting(HttpRequest $request) |
|
122
|
|
|
{ |
|
123
|
|
|
$negativeTesting = $this->settingsService->getSetting('settings-merchant-negative-testing'); |
|
124
|
|
|
if ($negativeTesting !== null) { |
|
125
|
|
|
$request->headers['PayPal-Mock-Response'] = json_encode( |
|
126
|
|
|
['mock_application_codes' => $negativeTesting] |
|
127
|
|
|
); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|