Version 5.0.0 beta 1
This commit is contained in:
1 parent
25ddeb65d6
commit
15c7beabc5
6736 files changed
+627902
-497943
No files matched your search
@@ -13,31 +13,56 @@ namespace IPS\nexus\Gateway\PayPal;
|
||||
|
||||
/* To prevent PHP errors (extending class does not exist) revealing path */
|
||||
|
||||
use DomainException;
|
||||
use IPS\DateTime;
|
||||
use IPS\Db;
|
||||
use IPS\Helpers\Form\Email;
|
||||
use IPS\Helpers\Form\Text;
|
||||
use IPS\Http\Request\Exception;
|
||||
use IPS\Http\Url;
|
||||
use IPS\Member;
|
||||
use IPS\nexus\Payout as NexusPayout;
|
||||
use IPS\Patterns\ActiveRecordIterator;
|
||||
use IPS\Request;
|
||||
use IPS\Settings;
|
||||
use IPS\Theme;
|
||||
use RuntimeException;
|
||||
use UnexpectedValueException;
|
||||
use function defined;
|
||||
|
||||
if ( !\defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
|
||||
{
|
||||
header( ( isset( $_SERVER['SERVER_PROTOCOL'] ) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
header( ( $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0' ) . ' 403 Forbidden' );
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* PayPal Pay Out Gateway
|
||||
*/
|
||||
class _Payout extends \IPS\nexus\Payout
|
||||
class Payout extends NexusPayout
|
||||
{
|
||||
/**
|
||||
* Extra HTML to display when the admin view the Payout in the ACP
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function acpHtml() : string
|
||||
{
|
||||
return Theme::i()->getTemplate( 'payouts', 'nexus' )->PayPal( $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* ACP Settings
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function settings()
|
||||
public static function settings() : array
|
||||
{
|
||||
$settings = json_decode( \IPS\Settings::i()->nexus_payout, TRUE );
|
||||
$settings = json_decode( Settings::i()->nexus_payout, TRUE );
|
||||
|
||||
$return = array();
|
||||
$return[] = new \IPS\Helpers\Form\Text( 'paypal_client_id', ( isset( $settings['PayPal']['client_id'] ) AND isset( $settings['PayPal']['client_id'] ) AND $settings['PayPal']['client_id'] ) ? $settings['PayPal']['client_id'] : '', NULL );
|
||||
$return[] = new \IPS\Helpers\Form\Text( 'paypal_secret', ( isset( $settings['PayPal']['secret'] ) AND $settings['PayPal']['secret'] ) ? $settings['PayPal']['secret'] : '', NULL );
|
||||
$return[] = new Text( 'paypal_client_id', ( isset( $settings['PayPal']['client_id'] ) AND $settings['PayPal']['client_id'] ) ? $settings['PayPal']['client_id'] : '', NULL );
|
||||
$return[] = new Text( 'paypal_secret', ( isset( $settings['PayPal']['secret'] ) AND $settings['PayPal']['secret'] ) ? $settings['PayPal']['secret'] : '', NULL );
|
||||
return $return;
|
||||
}
|
||||
|
||||
@@ -46,14 +71,14 @@ class _Payout extends \IPS\nexus\Payout
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function form()
|
||||
public static function form() : array
|
||||
{
|
||||
$return = array();
|
||||
$return[] = new \IPS\Helpers\Form\Email( 'paypal_email', \IPS\Member::loggedIn()->email, NULL, array(), function( $val )
|
||||
$return[] = new Email( 'paypal_email', Member::loggedIn()->email, NULL, array(), function( $val )
|
||||
{
|
||||
if ( !$val and \IPS\Request::i()->withdraw_method === 'PayPal' )
|
||||
if ( !$val and Request::i()->withdraw_method === 'PayPal' )
|
||||
{
|
||||
throw new \DomainException('form_required');
|
||||
throw new DomainException('form_required');
|
||||
}
|
||||
} );
|
||||
return $return;
|
||||
@@ -64,24 +89,25 @@ class _Payout extends \IPS\nexus\Payout
|
||||
*
|
||||
* @param array $values Values from form
|
||||
* @return mixed
|
||||
* @throws \DomainException
|
||||
* @throws DomainException
|
||||
*/
|
||||
public function getData( array $values )
|
||||
public function getData( array $values ) : mixed
|
||||
{
|
||||
return $values['paypal_email'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Process
|
||||
|
||||
/**
|
||||
* Process the payout
|
||||
* Return the new status for this payout record
|
||||
*
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function process()
|
||||
public function process() : string
|
||||
{
|
||||
static::checkToken();
|
||||
|
||||
$settings = json_decode( \IPS\Settings::i()->nexus_payout, true );
|
||||
$settings = json_decode( Settings::i()->nexus_payout, true );
|
||||
|
||||
$data = array(
|
||||
'items' => array(
|
||||
@@ -96,11 +122,11 @@ class _Payout extends \IPS\nexus\Payout
|
||||
),
|
||||
'sender_batch_header' => array(
|
||||
'recipient_type' => 'EMAIL',
|
||||
'sender_batch_id' => "Payout " . \IPS\DateTime::create()->rfc3339()
|
||||
'sender_batch_id' => "Payout " . DateTime::create()->rfc3339()
|
||||
)
|
||||
);
|
||||
|
||||
$response = \IPS\Http\Url::external( 'https://' . ( \IPS\NEXUS_TEST_GATEWAYS ? 'api-m.sandbox.paypal.com' : 'api.paypal.com' ) . '/v1/payments/payouts' )
|
||||
$response = Url::external( 'https://' . ( \IPS\NEXUS_TEST_GATEWAYS ? 'api-m.sandbox.paypal.com' : 'api.paypal.com' ) . '/v1/payments/payouts' )
|
||||
->request()
|
||||
->forceTls()
|
||||
->setHeaders( array(
|
||||
@@ -113,7 +139,7 @@ class _Payout extends \IPS\nexus\Payout
|
||||
|
||||
if( $response->httpResponseCode != 201 )
|
||||
{
|
||||
throw new \IPS\Http\Request\Exception( $json['message'] );
|
||||
throw new Exception( $json['message'] );
|
||||
}
|
||||
|
||||
if( isset( $json['batch_header'] ) AND $json['batch_header']['payout_batch_id'] )
|
||||
@@ -123,33 +149,34 @@ class _Payout extends \IPS\nexus\Payout
|
||||
{
|
||||
case 'DENIED':
|
||||
case 'CANCELED':
|
||||
$this->status = static::STATUS_CANCELED;
|
||||
return static::STATUS_CANCELED;
|
||||
break;
|
||||
case 'SUCCESS':
|
||||
$this->status = static::STATUS_COMPLETE;
|
||||
return static::STATUS_COMPLETE;
|
||||
break;
|
||||
default:
|
||||
$this->status = static::STATUS_PROCESSING;
|
||||
return static::STATUS_PROCESSING;
|
||||
break;
|
||||
}
|
||||
|
||||
$this->save();
|
||||
}
|
||||
|
||||
/* If we are still here, keep it at the current status */
|
||||
return $this->status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Mass Process
|
||||
*
|
||||
* @param \IPS\Patterns\ActiveRecordIterator $payouts Iterator of payouts to process
|
||||
* @param ActiveRecordIterator $payouts Iterator of payouts to process
|
||||
* @return void
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function massProcess( \IPS\Patterns\ActiveRecordIterator $payouts )
|
||||
public static function massProcess( ActiveRecordIterator $payouts ) : void
|
||||
{
|
||||
/* Make sure we check the token first so that we have proper authorization to API calls */
|
||||
static::checkToken();
|
||||
|
||||
$settings = json_decode( \IPS\Settings::i()->nexus_payout, TRUE );
|
||||
$settings = json_decode( Settings::i()->nexus_payout, TRUE );
|
||||
|
||||
/* Build a batch of payout items */
|
||||
$payoutIds = $payoutData = [];
|
||||
@@ -172,11 +199,11 @@ class _Payout extends \IPS\nexus\Payout
|
||||
'items' => $batchData,
|
||||
'sender_batch_header' => [
|
||||
'recipient_type' => 'EMAIL',
|
||||
'sender_batch_id' => "Payout-{$currency}-" . \IPS\DateTime::create()->rfc3339()
|
||||
'sender_batch_id' => "Payout-{$currency}-" . DateTime::create()->rfc3339()
|
||||
]
|
||||
];
|
||||
|
||||
$response = \IPS\Http\Url::external( 'https://' . ( \IPS\NEXUS_TEST_GATEWAYS ? 'api-m.sandbox.paypal.com' : 'api.paypal.com' ) . '/v1/payments/payouts' )
|
||||
$response = Url::external( 'https://' . ( \IPS\NEXUS_TEST_GATEWAYS ? 'api-m.sandbox.paypal.com' : 'api.paypal.com' ) . '/v1/payments/payouts' )
|
||||
->request()
|
||||
->forceTls()
|
||||
->setHeaders( [
|
||||
@@ -187,7 +214,7 @@ class _Payout extends \IPS\nexus\Payout
|
||||
|
||||
if( $response->httpResponseCode != 201 )
|
||||
{
|
||||
throw new \RuntimeException( (string) $response, $response->httpResponseCode );
|
||||
throw new RuntimeException( (string) $response, $response->httpResponseCode );
|
||||
}
|
||||
|
||||
$response = $response->decodeJson();
|
||||
@@ -212,7 +239,7 @@ class _Payout extends \IPS\nexus\Payout
|
||||
break;
|
||||
}
|
||||
|
||||
\IPS\Db::i()->update( 'nexus_payouts', $update, \IPS\Db::i()->in( 'po_id', $payoutIds[ $currency ] ) );
|
||||
Db::i()->update( 'nexus_payouts', $update, Db::i()->in( 'po_id', $payoutIds[ $currency ] ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -238,9 +265,9 @@ class _Payout extends \IPS\nexus\Payout
|
||||
/* Make sure we check the token first so that we have proper authorization to API calls */
|
||||
static::checkToken();
|
||||
|
||||
$settings = json_decode( \IPS\Settings::i()->nexus_payout, TRUE );
|
||||
$settings = json_decode( Settings::i()->nexus_payout, TRUE );
|
||||
|
||||
$response = \IPS\Http\Url::external( 'https://' . ( \IPS\NEXUS_TEST_GATEWAYS ? 'api-m.sandbox.paypal.com' : 'api.paypal.com' ) . '/v1/payments/payouts/' . $batchId )
|
||||
$response = Url::external( 'https://' . ( \IPS\NEXUS_TEST_GATEWAYS ? 'api-m.sandbox.paypal.com' : 'api.paypal.com' ) . '/v1/payments/payouts/' . $batchId )
|
||||
->request()
|
||||
->forceTls()
|
||||
->setHeaders( array(
|
||||
@@ -274,17 +301,17 @@ class _Payout extends \IPS\nexus\Payout
|
||||
* Get Token
|
||||
*
|
||||
* @return void
|
||||
* @throws \IPS\Http\Request\Exception
|
||||
* @throws \UnexpectedValueException
|
||||
* @throws Exception
|
||||
* @throws UnexpectedValueException
|
||||
*/
|
||||
protected static function checkToken()
|
||||
protected static function checkToken() : void
|
||||
{
|
||||
$payoutSettings = json_decode( \IPS\Settings::i()->nexus_payout, true );
|
||||
$payoutSettings = json_decode( Settings::i()->nexus_payout, true );
|
||||
$settings = $payoutSettings['PayPal'];
|
||||
|
||||
if ( !isset( $settings['token'] ) or $settings['token_expire'] < time() )
|
||||
{
|
||||
$response = \IPS\Http\Url::external( 'https://' . ( \IPS\NEXUS_TEST_GATEWAYS ? 'api-m.sandbox.paypal.com' : 'api.paypal.com' ) . '/v1/oauth2/token' )
|
||||
$response = Url::external( 'https://' . ( \IPS\NEXUS_TEST_GATEWAYS ? 'api-m.sandbox.paypal.com' : 'api.paypal.com' ) . '/v1/oauth2/token' )
|
||||
->request()
|
||||
->forceTls()
|
||||
->setHeaders( array(
|
||||
@@ -297,14 +324,14 @@ class _Payout extends \IPS\nexus\Payout
|
||||
|
||||
if ( !isset( $response['access_token'] ) )
|
||||
{
|
||||
throw new \UnexpectedValueException( isset( $response['error_description'] ) ? $response['error_description'] : $response );
|
||||
throw new UnexpectedValueException($response['error_description'] ?? $response);
|
||||
}
|
||||
|
||||
$settings['token'] = $response['access_token'];
|
||||
$settings['token_expire'] = ( time() + $response['expires_in'] );
|
||||
$payoutSettings['PayPal'] = $settings;
|
||||
|
||||
\IPS\Settings::i()->changeValues( array(
|
||||
Settings::i()->changeValues( array(
|
||||
'nexus_payout' => json_encode( $payoutSettings )
|
||||
) );
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user