Skip to content

Commit

Permalink
v1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
UndercoverNL committed Jan 13, 2023
1 parent ae74bd3 commit 1b8c426
Show file tree
Hide file tree
Showing 10 changed files with 204 additions and 75 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "undercovernl/billingpackage",
"description": "Pterodactyl v2 Billing Package",
"version": "v1.2",
"version": "v1.3",
"authors": [
{
"name": "UndercoverNL",
Expand Down
16 changes: 0 additions & 16 deletions src/Billing.php

This file was deleted.

20 changes: 20 additions & 0 deletions src/Client.php
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);
}
}
11 changes: 11 additions & 0 deletions src/Service/AbstractServiceFactory.php
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)
{
//
}
}
21 changes: 21 additions & 0 deletions src/Service/CoreServiceFactory.php
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;
}
}
35 changes: 35 additions & 0 deletions src/Service/Stripe/Client.php
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;
}
}
77 changes: 77 additions & 0 deletions src/Service/Stripe/CustomerService.php
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;
}
}
39 changes: 39 additions & 0 deletions src/Service/Stripe/PaymentMethodService.php
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;
}
}
23 changes: 0 additions & 23 deletions src/Stripe/Client.php

This file was deleted.

35 changes: 0 additions & 35 deletions src/Stripe/User.php

This file was deleted.

0 comments on commit 1b8c426

Please sign in to comment.