This commit is contained in:
Joey Kimsey
2024-12-21 16:26:05 -05:00
parent 0c2fa757dd
commit f8e75e847d
59 changed files with 861 additions and 387 deletions

View File

@ -19,6 +19,7 @@ use TheTempusProject\Bedrock\Classes\Config;
use TheTempusProject\Hermes\Functions\Route as Routes;
use TheTempusProject\Models\Memberships;
use TheTempusProject\Models\MembershipProducts as Products;
use TheTempusProject\Canary\Bin\Canary as Debug;
class Members extends Plugin {
public static $stripe;
@ -46,17 +47,17 @@ class Members extends Plugin {
'text' => '<i class="fa fa-fw fa-arrow-down"></i> Memberships',
'url' => [
[
'text' => '<i class="fa fa-fw fa-database"></i> Products',
'text' => '<i class="fa fa-fw fa-barcode"></i> Products',
'url' => '{ROOT_URL}admin/products',
],
[
'text' => '<i class="fa fa-fw fa-database"></i> Subscriptions',
'text' => '<i class="fa fa-fw fa-bag-shopping"></i> Subscriptions',
'url' => '{ROOT_URL}admin/records',
],
// [
// 'text' => '<i class="fa fa-fw fa-database"></i> Invoices',
// 'url' => '{ROOT_URL}admin/invoices',
// ],
[
'text' => '<i class="fa fa-fw fa-code"></i> Scripts',
'url' => '{ROOT_URL}admin/members',
],
],
],
];
@ -116,41 +117,52 @@ class Members extends Plugin {
];
public function __construct( $load = false ) {
if ( ! self::$loaded ) {
App::$userCPlinks[] = (object) self::$userLinks;
self::$loaded = true;
if ( ! self::$loaded && $load ) {
if ( App::$isLoggedIn ) {
App::$isMember = $this->groupHasMemberAccess( App::$activeGroup );
if ( empty( App::$isMember ) ) {
App::$isMember = $this->userHasActiveMembership( App::$activeUser->ID );
}
}
$this->filters[] = [
'name' => 'member',
'find' => '#{MEMBER}(.*?){/MEMBER}#is',
'replace' => ( App::$isMember ? '$1' : '' ),
'enabled' => true,
];
$this->filters[] = [
'name' => 'nonmember',
'find' => '#{NONMEMBER}(.*?){/NONMEMBER}#is',
'replace' => ( App::$isLoggedIn && App::$isMember == false ? '$1' : '' ),
'enabled' => true,
];
$this->filters[] = [
'name' => 'upgrade',
'find' => '#{UPGRADE}(.*?){/UPGRADE}#is',
'replace' => ( App::$isLoggedIn && ( App::$isMember === 'monthly' ) ? '$1' : '' ),
'enabled' => true,
];
}
if ( App::$isLoggedIn ) {
App::$isMember = $this->groupHasMemberAccess( App::$activeGroup );
if ( empty( App::$isMember ) ) {
App::$isMember = $this->userHasActiveMembership( App::$activeUser->ID );
parent::__construct( $load );
if ( $this->checkEnabled() && App::$isLoggedIn ) {
if ( ! self::$loaded && $load ) {
App::$userCPlinks[] = (object) self::$userLinks;
App::$topNavRightDropdown .= '<li><a href="{ROOT_URL}member/manage" class="dropdown-item"><i class="fa fa-fw fa-credit-card"></i> Subscriptions</a></li>';
}
}
$this->filters[] = [
'name' => 'member',
'find' => '#{MEMBER}(.*?){/MEMBER}#is',
'replace' => ( App::$isMember ? '$1' : '' ),
'enabled' => true,
];
$this->filters[] = [
'name' => 'nonmember',
'find' => '#{NONMEMBER}(.*?){/NONMEMBER}#is',
'replace' => ( App::$isLoggedIn && App::$isMember == false ? '$1' : '' ),
'enabled' => true,
];
$this->filters[] = [
'name' => 'upgrade',
'find' => '#{UPGRADE}(.*?){/UPGRADE}#is',
'replace' => ( App::$isLoggedIn && ( App::$isMember === 'monthly' ) ? '$1' : '' ),
'enabled' => true,
];
if ( ! self::$loaded && $load ) {
self::$loaded = true;
}
$api_key = Config::getValue( 'memberships/stripeSecret' );
if ( $api_key == 'sk_xxxxxxxxxxxxxxx' || empty($api_key) ) {
self::$stripe = false;
} else {
self::$stripe = new StripeClient( $api_key );
}
parent::__construct( $load );
}
public function groupHasMemberAccess( $activeGroup ) {
@ -169,6 +181,10 @@ class Members extends Plugin {
$products = new Products;
$product = $products->findByPriceID( $membership->subscription_price_id );
if ( empty( $product ) ) {
Debug::error('Active membership on non-existent product');
return false;
}
if ( $product->stripe_price_monthly == $membership->subscription_price_id ) {
return 'monthly';
}
@ -177,7 +193,6 @@ class Members extends Plugin {
public static function webhookSetup() {
$root = Routes::getAddress();
// $root = "https://stripe.joeykimsey.com/";
$response = self::$stripe->webhookEndpoints->create([
'enabled_events' => self::$webhookEvents,
'url' => $root . 'api/stripe/webhook',
@ -185,13 +200,66 @@ class Members extends Plugin {
return $response;
}
// public static function webhookSetup() {
// $root = Routes::getAddress();
// $root = "https://stripe.joeykimsey.com/";
// $response = self::$stripe->webhookEndpoints->create([
// 'enabled_events' => self::$webhookEvents,
// 'url' => $root . 'api/stripe/webhook',
// ]);
// return $response;
// }
public static function webhookList() {
$response = self::$stripe->webhookEndpoints->all(['limit' => 25]);
return $response;
}
public static function webhookRemove( $id ) {
$response = self::$stripe->webhookEndpoints->delete( $id, []);
return $response;
}
public static function orphanAbandon( $id ) {
$response = self::$stripe->prices->update(
$id,
['lookup_key' => ""]
);
return $response;
}
public static function findOrphans() {
$orphans = [];
// any prices with keys not represented in our local db will show as an orphan
$result = self::$stripe->prices->search([
'query' => 'lookup_key:"membership-monthly"',
]);
$products = new Products;
if ( ! empty( $result->data ) ) {
$product = $products->findByPriceID( $result->data[0]->id );
if ( empty( $product ) ) {
$data = $result->data[0];
$child = new \stdClass;
$child->price_id = $data->id;
$child->product = $data->product;
$child->lookup_key = $data->lookup_key;
$child->amount = $data->unit_amount;
$orphans[] = $child;
}
}
$result = self::$stripe->prices->search([
'query' => 'lookup_key:"membership-yearly"',
]);
if ( ! empty( $result->data ) ) {
$product = $products->findByPriceID( $result->data[0]->id );
if ( empty( $product ) ) {
$data = $result->data[0];
$child = new \stdClass;
$child->price_id = $data->id;
$child->product = $data->product;
$child->lookup_key = $data->lookup_key;
$child->amount = $data->unit_amount;
$orphans[] = $child;
}
}
return $orphans;
}
}