1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Service\Paypal; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class BillingAgreementService |
9
|
|
|
* @package App\Service\Paypal |
10
|
|
|
*/ |
11
|
|
|
class BillingAgreementService extends IdentityService |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @param $requestBody |
15
|
|
|
* @param string $url |
16
|
|
|
* @param string $verb |
17
|
|
|
* @return array|string|null |
18
|
|
|
*/ |
19
|
|
|
public function paypalApiCall($requestBody, string $url, string $verb = 'POST') |
20
|
|
|
{ |
21
|
|
|
try { |
22
|
|
|
$accessToken = $this->getAccessToken(); |
23
|
|
|
$ch = curl_init(); |
24
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
25
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $verb); |
26
|
|
|
if ($requestBody !== null) { |
27
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestBody); |
28
|
|
|
} |
29
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
30
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [ |
31
|
|
|
'Content-Type: application/json', |
32
|
|
|
'Authorization: Bearer ' . $accessToken, |
33
|
|
|
]); |
34
|
|
|
$result = curl_exec($ch); |
35
|
|
|
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
36
|
|
|
curl_close($ch); |
37
|
|
|
} catch (Exception $e) { |
38
|
|
|
$this->logger->error('Error on PayPal::'.$url.' = ' . $e->getMessage()); |
39
|
|
|
return null; |
40
|
|
|
} |
41
|
|
|
return ([ |
42
|
|
|
'result' => $result, |
43
|
|
|
'statusCode' => $statusCode |
44
|
|
|
]); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param $requestBody |
49
|
|
|
* @return bool|string|null |
50
|
|
|
*/ |
51
|
|
|
public function createBillingAgreementToken($requestBody) |
52
|
|
|
{ |
53
|
|
|
return $this->paypalApiCall( |
54
|
|
|
$requestBody, |
55
|
|
|
'https://api.sandbox.paypal.com/v1/billing-agreements/agreement-tokens' |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param $requestBody |
61
|
|
|
* @return bool|string|null |
62
|
|
|
*/ |
63
|
|
|
public function createBillingAgreement($requestBody) |
64
|
|
|
{ |
65
|
|
|
return $this->paypalApiCall( |
66
|
|
|
$requestBody, |
67
|
|
|
'https://api.sandbox.paypal.com/v1/billing-agreements/agreements' |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param $requestBody |
73
|
|
|
* @return bool|string|null |
74
|
|
|
*/ |
75
|
|
|
public function createReferenceTransaction($requestBody) |
76
|
|
|
{ |
77
|
|
|
return $this->paypalApiCall( |
78
|
|
|
$requestBody, |
79
|
|
|
'https://api.sandbox.paypal.com/v1/payments/payment' |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $billingAgreementId |
85
|
|
|
* @return bool|string|null |
86
|
|
|
*/ |
87
|
|
|
public function deleteBillingAgreement(string $billingAgreementId) |
88
|
|
|
{ |
89
|
|
|
return $this->paypalApiCall( |
90
|
|
|
null, |
91
|
|
|
'https://api.sandbox.paypal.com/v1/billing-agreements/agreements/' . $billingAgreementId . '/cancel' |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|