| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\PaymentGateways\PayPalCommerce; |
| 4 |
|
| 5 |
use Give\PaymentGateways\PayPalCommerce\Models\MerchantDetail; |
| 6 |
use Give\PaymentGateways\PayPalCommerce\Repositories\Traits\HasMode; |
| 7 |
use PayPalCheckoutSdk\Core\PayPalHttpClient; |
| 8 |
use PayPalCheckoutSdk\Core\ProductionEnvironment; |
| 9 |
use PayPalCheckoutSdk\Core\SandboxEnvironment; |
| 10 |
|
| 11 |
/** |
| 12 |
* Class PayPalClient |
| 13 |
* |
| 14 |
* @package Give\PaymentGateways\PaypalCommerce |
| 15 |
* |
| 16 |
* @since 2.9.0 |
| 17 |
*/ |
| 18 |
class PayPalClient |
| 19 |
{ |
| 20 |
use HasMode; |
| 21 |
|
| 22 |
/** |
| 23 |
* Get environment. |
| 24 |
* |
| 25 |
* @sicne 2.9.0 |
| 26 |
* |
| 27 |
* @return ProductionEnvironment|SandboxEnvironment |
| 28 |
*/ |
| 29 |
public function getEnvironment() |
| 30 |
{ |
| 31 |
/* @var MerchantDetail $merchant */ |
| 32 |
$merchant = give(MerchantDetail::class); |
| 33 |
|
| 34 |
return 'sandbox' === $this->mode ? |
| 35 |
new SandboxEnvironment($merchant->clientId, $merchant->clientSecret) : |
| 36 |
new ProductionEnvironment($merchant->clientId, $merchant->clientSecret); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Get http client. |
| 41 |
* |
| 42 |
* @since 2.25.0 Add custom AuthorizationInjector. |
| 43 |
* @since 2.9.0 |
| 44 |
*/ |
| 45 |
public function getHttpClient(): PayPalHttpClient |
| 46 |
{ |
| 47 |
return new PayPalCheckoutSdk\PayPalHttpClient($this->getEnvironment()); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Get api url. |
| 52 |
* |
| 53 |
* @deprecated 2.30.1 |
| 54 |
* @since 2.9.0 |
| 55 |
* |
| 56 |
* @param string $endpoint |
| 57 |
* |
| 58 |
* @return string |
| 59 |
*/ |
| 60 |
public function getApiUrl($endpoint) |
| 61 |
{ |
| 62 |
$baseUrl = $this->getEnvironment()->baseUrl(); |
| 63 |
|
| 64 |
return "{$baseUrl}/$endpoint"; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Get PayPal homepage url. |
| 69 |
* |
| 70 |
* @since 2.9.0 |
| 71 |
* |
| 72 |
* @return string |
| 73 |
*/ |
| 74 |
public function getHomePageUrl() |
| 75 |
{ |
| 76 |
return sprintf( |
| 77 |
'https://%1$spaypal.com/', |
| 78 |
'sandbox' === $this->mode ? 'sandbox.' : '' |
| 79 |
); |
| 80 |
} |
| 81 |
} |
| 82 |
|