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