Skip to content

Commit

Permalink
Use Throwable instead of XxxExceptions classes (#319)
Browse files Browse the repository at this point in the history
* Use Throwable instead of XxxExceptions classes

Use Throwable instead of handling multiple types of Exception classes repeatedly

* Update PayPalHttpClient.php

* Update PayPalHttpClient.php

* General code tidy up across the board
- Catch Throwable where possible
- Convert all thrown Exceptions to RuntimeExceptions
- Convert anonymous functions to static anonymous functions (speed improvement in some cases)
- Split some complex workflows into separate chunks to return early
- Use 'use' statements where possible to avoid having \Unnecessary\Qualifiers\Everywhere
- Use strict type checking where possible
- Remove some unnecessary brackets from some clauses
- No longer use !is_null() function just use !== null instead
  • Loading branch information
dayallnash committed Feb 10, 2020
1 parent b71b1e4 commit 403f2ca
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 240 deletions.
15 changes: 9 additions & 6 deletions src/PayPalFacadeAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Srmklive\PayPal;

use Exception;
use Srmklive\PayPal\Services\AdaptivePayments;
use Srmklive\PayPal\Services\ExpressCheckout;

Expand All @@ -18,27 +19,29 @@ class PayPalFacadeAccessor
* Get specific PayPal API provider object to use.
*
* @return ExpressCheckout|AdaptivePayments
* @throws Exception
*/
public static function getProvider()
{
if (empty(self::$provider)) {
return new ExpressCheckout();
} else {
return self::$provider;
}

return self::$provider;
}

/**
* Set specific PayPal API to use.
*
* @param string $option
* @param string $option Defaults to express_checkout
*
* @return ExpressCheckout|AdaptivePayments
* @throws Exception
*/
public static function setProvider($option = '')
public static function setProvider($option = 'express_checkout')
{
// Set default provider.
if (empty($option) || ($option != 'adaptive_payments') || ($option == 'express_checkout')) {
// Set default provider. Defaults to ExpressCheckout
if (empty($option) || $option === 'express_checkout' || $option !== 'adaptive_payments') {
self::$provider = new ExpressCheckout();
} else {
self::$provider = new AdaptivePayments();
Expand Down
4 changes: 2 additions & 2 deletions src/Providers/PayPalServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ public function register()
*/
private function registerPayPal()
{
$this->app->singleton('express_checkout', function () {
$this->app->singleton('express_checkout', static function () {
return new ExpressCheckout();
});

$this->app->singleton('adaptive_payments', function () {
$this->app->singleton('adaptive_payments', static function () {
return new AdaptivePayments();
});
}
Expand Down
51 changes: 25 additions & 26 deletions src/Services/AdaptivePayments.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@

namespace Srmklive\PayPal\Services;

use Exception;
use Psr\Http\Message\StreamInterface;
use RuntimeException;
use Srmklive\PayPal\Traits\PayPalRequest as PayPalAPIRequest;
use Throwable;

class AdaptivePayments
{
use PayPalAPIRequest;

/**
* PayPal Processor Constructor.
*
* @throws Exception
*/
public function __construct()
{
Expand All @@ -24,7 +30,7 @@ public function __construct()
*/
public function setAdaptivePaymentsOptions()
{
if ($this->mode == 'sandbox') {
if ($this->mode === 'sandbox') {
$this->config['api_url'] = 'https://svcs.sandbox.paypal.com/AdaptivePayments';
$this->config['gateway_url'] = 'https://www.sandbox.paypal.com/cgi-bin/webscr';
} else {
Expand Down Expand Up @@ -87,8 +93,8 @@ private function setPayRequestDetails($data)
'cancelUrl' => $data['cancel_url'],
'requestEnvelope' => $this->setEnvelope(),
'feesPayer' => $data['payer'],
])->filter(function ($value, $key) {
return (($key === 'feesPayer') && empty($value)) ? null : $value;
])->filter(static function ($value, $key) {
return $key === 'feesPayer' && empty($value) ? null : $value;
});
}

Expand All @@ -97,14 +103,14 @@ private function setPayRequestDetails($data)
*
* @param array $data
*
* @throws \Exception
* @throws Exception
*
* @return array
*/
public function createPayRequest($data)
{
if (empty($data['return_url']) && empty($data['cancel_url'])) {
throw new \Exception('Return & Cancel URL should be specified');
throw new RuntimeException('Return & Cancel URL should be specified');
}

$this->setPayRequestDetails($data);
Expand Down Expand Up @@ -174,15 +180,15 @@ public function getPaymentDetails($payKey)
*/
public function getRedirectUrl($option, $payKey)
{
$url = $this->config['gateway_url'].'?cmd=';
if ($option === 'approved') {
return $this->config['gateway_url'] . '?cmd=' . '_ap-payment&paykey='.$payKey;
}

if ($option == 'approved') {
$url .= '_ap-payment&paykey='.$payKey;
} elseif ($option == 'pre-approved') {
$url .= '_ap-preapproval&preapprovalkey='.$payKey;
if ($option === 'pre-approved') {
return $this->config['gateway_url'] . '?cmd=' . '_ap-preapproval&preapprovalkey='.$payKey;
}

return $url;
return $this->config['gateway_url'] . '?cmd=';
}

/**
Expand All @@ -194,7 +200,7 @@ public function getRedirectUrl($option, $payKey)
*/
private function setPaymentOptionsReceiverDetails($receivers)
{
return collect($receivers)->map(function ($receiver) {
return collect($receivers)->map(static function ($receiver) {
$item = [];

$item['receiver'] = [
Expand Down Expand Up @@ -226,9 +232,9 @@ private function createRequestPayload($method)
/**
* Perform PayPal API request & return response.
*
* @throws \Exception
* @throws Exception
*
* @return \Psr\Http\Message\StreamInterface
* @return StreamInterface
*/
private function makeHttpRequest()
{
Expand All @@ -237,23 +243,16 @@ private function makeHttpRequest()
'json' => $this->post->toArray(),
'headers' => $this->setHeaders(),
])->getBody();
} catch (\GuzzleHttp\Exception\ClientException $e) {
throw new \Exception(collect($e->getTrace())->implode('\n'));
} catch (\GuzzleHttp\Exception\ServerException $e) {
throw new \Exception(collect($e->getTrace())->implode('\n'));
} catch (\GuzzleHttp\Exception\BadResponseException $e) {
throw new \Exception(collect($e->getTrace())->implode('\n'));
} catch (Throwable $t) {
throw new RuntimeException(collect($t->getTrace())->implode('\n'));
}
}

/**
* Function To Perform PayPal API Request.
*
* @param string $method
*
* @throws \Exception
*
* @return array|mixed|\Psr\Http\Message\StreamInterface
* @param $method
* @return array|mixed
*/
private function doPayPalRequest($method)
{
Expand All @@ -264,7 +263,7 @@ private function doPayPalRequest($method)
$response = $this->makeHttpRequest();

return \GuzzleHttp\json_decode($response, true);
} catch (\Exception $e) {
} catch (Throwable $e) {
$message = $e->getMessage();
}

Expand Down

0 comments on commit 403f2ca

Please sign in to comment.