-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ae74bd3
commit 1b8c426
Showing
10 changed files
with
204 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace UndercoverNL; | ||
|
||
class StripeClient | ||
{ | ||
/** | ||
* @var \Stripe\Service\CoreServiceFactory | ||
*/ | ||
private $coreServiceFactory; | ||
|
||
public function __get($name) | ||
{ | ||
if (null === $this->coreServiceFactory) { | ||
$this->coreServiceFactory = new \UndercoverNL\Service\CoreServiceFactory($this); | ||
} | ||
|
||
return $this->coreServiceFactory->__get($name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace UndercoverNL\Service; | ||
|
||
abstract class AbstractServiceFactory | ||
{ | ||
public function __get($name) | ||
{ | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace UndercoverNL\Service; | ||
|
||
class CoreServiceFactory extends AbstractServiceFactory | ||
{ | ||
/** | ||
* @var array<string, string> | ||
*/ | ||
private static $classMap = [ | ||
'stripe' => [ | ||
'customers' => Stripe\CustomerService::class, | ||
'paymentMethods' => Stripe\CustomerService::class, | ||
], | ||
]; | ||
|
||
protected function getServiceClass($name) | ||
{ | ||
return \array_key_exists($name, self::$classMap) ? self::$classMap[$name] : null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace UndercoverNL\Service\Stripe; | ||
|
||
use Stripe\StripeClient; | ||
use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface; | ||
|
||
class Client { | ||
/** | ||
* @var \Pterodactyl\Contracts\Repository\SettingsRepositoryInterface | ||
*/ | ||
protected $settings; | ||
|
||
/** | ||
* @var \Stripe\StripeClient | ||
*/ | ||
protected $client; | ||
|
||
/** | ||
* @var \CustomerService | ||
*/ | ||
protected $customers; | ||
|
||
/** | ||
* Client constructor. | ||
*/ | ||
public function __construct(SettingsRepositoryInterface $settings) | ||
{ | ||
$this->settings = $settings; | ||
$this->customers = $customers; | ||
|
||
|
||
$this->client = $this->settings->get('settings::billing::stripe_enabled') && $this->settings->get('settings::billing::stripe_secret') ? new StripeClient($this->settings->get('settings::billing::stripe_secret')) : null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace UndercoverNL\Service\Stripe; | ||
|
||
use Stripe\Customer; | ||
use Stripe\Collection; | ||
use Pterodactyl\Models\User; | ||
|
||
class CustomerService extends Client { | ||
/** | ||
* Creates a new Stripe customer or updates an existing Stripe customer. | ||
* | ||
* @param User|array $user | ||
* | ||
* @return Customer|null | ||
*/ | ||
public function createOrUpdateUser(User|array $user): Customer|null | ||
{ | ||
if ($this->settings->get('settings::billing::stripe_enabled') && $this->settings->get('settings::billing::stripe_secret')) { | ||
$data = [ | ||
'email' => $user['email'], | ||
'name' => $user['first_name'] . ' ' . $user['last_name'], | ||
'phone' => $user['phone'], | ||
'address' => [ | ||
'city' => $user['city'], | ||
'country' => $user['country'], | ||
'line1' => $user['address_1'], | ||
'line2' => $user['address_2'] ?? null, | ||
'postal_code' => $user['postal_code'], | ||
'state' => $user['state'], | ||
], | ||
]; | ||
|
||
if ($user->stripe_id) { | ||
$response = $this->client->customers->update( | ||
$user->stripe_id, | ||
$data, | ||
); | ||
} else { | ||
$response = $this->client->customers->create($data); | ||
} | ||
} else { | ||
$response = null; | ||
} | ||
|
||
return $response; | ||
} | ||
|
||
/** | ||
* Receive the payment methods of a Stripe customer. | ||
* | ||
* @param User $user | ||
* | ||
* @return Collection|null | ||
*/ | ||
public function paymentMethods(User $user): Collection|null | ||
{ | ||
if ($this->settings->get('settings::billing::stripe_enabled') && $this->settings->get('settings::billing::stripe_secret')) { | ||
if (!$user->stripe_id) { | ||
$response = $this->createOrUpdateUser($user); | ||
|
||
$user->update([ | ||
'stripe_id' => $response['id'], | ||
]); | ||
} | ||
|
||
$response = $this->client->customers->allPaymentMethods( | ||
$user->stripe_id, | ||
['type' => 'card'] | ||
); | ||
} else { | ||
$response = null; | ||
} | ||
|
||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace UndercoverNL\Service\Stripe; | ||
|
||
use Stripe\Collection; | ||
use Pterodactyl\Models\User; | ||
|
||
class PaymentMethodService extends Client { | ||
|
||
/** | ||
* Attach an created payment method to a Stripe customer. | ||
* | ||
* @param User $user | ||
* @param string $id | ||
* | ||
* @return Collection|null | ||
*/ | ||
public function addPaymentMethod(User $user, string $id): Collection|null | ||
{ | ||
if ($this->settings->get('settings::billing::stripe_enabled') && $this->settings->get('settings::billing::stripe_secret')) { | ||
if (!$user->stripe_id) { | ||
$response = $this->customers->createOrUpdateUser($user); | ||
|
||
$user->update([ | ||
'stripe_id' => $response['id'], | ||
]); | ||
} | ||
|
||
$response = $this->client->paymentMethods->attach( | ||
$user->stripe_id, | ||
['type' => 'card'] | ||
); | ||
} else { | ||
$response = null; | ||
} | ||
|
||
return $response; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.