|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Srmklive\PayPal\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Psr\Http\Message\StreamInterface; |
|
8
|
|
|
use Srmklive\PayPal\Traits\PayPalRequest as PayPalAPIRequest; |
|
9
|
|
|
|
|
10
|
|
|
class PayPal |
|
11
|
|
|
{ |
|
12
|
|
|
use PayPalAPIRequest; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* PayPal constructor. |
|
16
|
|
|
* |
|
17
|
|
|
* @param string|array $config |
|
18
|
|
|
* |
|
19
|
|
|
* @throws Exception |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct($config = '') |
|
22
|
|
|
{ |
|
23
|
|
|
// Setting PayPal API Credentials |
|
24
|
|
|
if (is_array($config)) { |
|
25
|
|
|
$this->setConfig(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
$this->httpBodyParam = 'form_params'; |
|
29
|
|
|
|
|
30
|
|
|
$this->options = []; |
|
31
|
|
|
$this->options['headers'] = [ |
|
32
|
|
|
'Accept' => 'application/json', |
|
33
|
|
|
'Accept-Language' => $this->locale, |
|
34
|
|
|
]; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Set ExpressCheckout API endpoints & options. |
|
39
|
|
|
* |
|
40
|
|
|
* @param array $credentials |
|
41
|
|
|
* |
|
42
|
|
|
* @return void |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function setOptions($credentials) |
|
45
|
|
|
{ |
|
46
|
|
|
// Setting API Endpoints |
|
47
|
|
|
if ($this->mode === 'sandbox') { |
|
48
|
|
|
$this->config['api_url'] = 'https://api.sandbox.paypal.com'; |
|
49
|
|
|
|
|
50
|
|
|
$this->config['gateway_url'] = 'https://www.sandbox.paypal.com'; |
|
51
|
|
|
$this->config['ipn_url'] = 'https://ipnpb.sandbox.paypal.com/cgi-bin/webscr'; |
|
52
|
|
|
} else { |
|
53
|
|
|
$this->config['api_url'] = 'https://api.paypal.com/'; |
|
54
|
|
|
|
|
55
|
|
|
$this->config['gateway_url'] = 'https://www.paypal.com'; |
|
56
|
|
|
$this->config['ipn_url'] = 'https://ipnpb.paypal.com/cgi-bin/webscr'; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// Adding params outside sandbox / live array |
|
60
|
|
|
$this->config['payment_action'] = $credentials['payment_action']; |
|
61
|
|
|
$this->config['notify_url'] = $credentials['notify_url']; |
|
62
|
|
|
$this->config['locale'] = $credentials['locale']; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Login through PayPal API to get access token. |
|
67
|
|
|
* |
|
68
|
|
|
* @return array|StreamInterface|string |
|
69
|
|
|
* |
|
70
|
|
|
* @throws \Throwable |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getAccessToken() |
|
73
|
|
|
{ |
|
74
|
|
|
$this->apiEndPoint = 'v1/oauth2/token?grant_type=client_credentials'; |
|
75
|
|
|
|
|
76
|
|
|
$this->options['auth'] = [$this->config['client_id'], $this->config['client_secret']]; |
|
77
|
|
|
|
|
78
|
|
|
$response = $this->doPayPalRequest(); |
|
79
|
|
|
|
|
80
|
|
|
if (isset($response["access_token"])) { |
|
81
|
|
|
$this->access_token = $response['access_token']; |
|
82
|
|
|
|
|
83
|
|
|
if (empty($this->config['app_id'])) { |
|
84
|
|
|
$this->config['app_id'] = $response['app_id']; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $response; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|