1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sebdesign\VivaPayments; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
6
|
|
|
use Illuminate\Support\ServiceProvider; |
7
|
|
|
|
8
|
|
|
class VivaPaymentsServiceProvider extends ServiceProvider |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Indicates if loading of the provider is deferred. |
12
|
|
|
* |
13
|
|
|
* @var bool |
14
|
|
|
*/ |
15
|
|
|
protected $defer = true; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Register the application services. |
19
|
|
|
* |
20
|
|
|
* @return void |
21
|
|
|
*/ |
22
|
|
|
public function register() |
23
|
|
|
{ |
24
|
|
|
$this->mergeConfigFrom( |
25
|
|
|
__DIR__.'/../config/services.php', 'services' |
26
|
|
|
); |
27
|
|
|
|
28
|
|
|
$this->app->singleton(Client::class, function ($app) { |
29
|
|
|
return new Client($this->buildGuzzleClient($app)); |
30
|
|
|
}); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Build the Guzzlehttp client. |
35
|
|
|
* |
36
|
|
|
* @param \Illuminate\Contracts\Foundation\Application $app |
37
|
|
|
* @return GuzzleHttp\Client |
38
|
|
|
*/ |
39
|
|
|
protected function buildGuzzleClient($app) |
40
|
|
|
{ |
41
|
|
|
$config = $app['config']->get('services.viva'); |
42
|
|
|
|
43
|
|
|
return new GuzzleClient([ |
44
|
|
|
'base_uri' => $this->getUrl($config['environment']), |
45
|
|
|
'curl' => $this->curlDoesntUseNss() |
46
|
|
|
? [CURLOPT_SSL_CIPHER_LIST => 'TLSv1'] |
47
|
|
|
: [], |
48
|
|
|
\GuzzleHttp\RequestOptions::AUTH => [ |
49
|
|
|
$config['merchant_id'], |
50
|
|
|
$config['api_key'], |
51
|
|
|
], |
52
|
|
|
]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Check if cURL doens't use NSS. |
57
|
|
|
* |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
protected function curlDoesntUseNss() |
61
|
|
|
{ |
62
|
|
|
$curl = curl_version(); |
63
|
|
|
|
64
|
|
|
return ! preg_match('/NSS/', $curl['ssl_version']); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the URL based on the environment. |
69
|
|
|
* |
70
|
|
|
* @param string $environment |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
protected function getUrl($environment) |
74
|
|
|
{ |
75
|
|
|
if ($environment === 'production') { |
76
|
|
|
return Client::PRODUCTION_URL; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if ($environment === 'demo') { |
80
|
|
|
return Client::DEMO_URL; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
throw new \InvalidArgumentException('The Viva Payments environment must be demo or production.'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the services provided by the provider. |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
public function provides() |
92
|
|
|
{ |
93
|
|
|
return [Client::class]; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|