Add donate and memberships
This commit is contained in:
104
app/plugins/members/models/membership_customers.php
Normal file
104
app/plugins/members/models/membership_customers.php
Normal file
@ -0,0 +1,104 @@
|
||||
<?php
|
||||
/**
|
||||
* app/plugins/members/models/membership_customers.php
|
||||
*
|
||||
* This class is used for the manipulation of the membership_customers database table.
|
||||
*
|
||||
* @package TP Members
|
||||
* @version 3.0
|
||||
* @author Joey Kimsey <Joey@thetempusproject.com>
|
||||
* @link https://TheTempusProject.com
|
||||
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
||||
*/
|
||||
namespace TheTempusProject\Models;
|
||||
|
||||
use TheTempusProject\Canary\Bin\Canary as Debug;
|
||||
use TheTempusProject\Bedrock\Functions\Check;
|
||||
use TheTempusProject\Bedrock\Functions\Sanitize;
|
||||
use TheTempusProject\Classes\DatabaseModel;
|
||||
use TheTempusProject\TheTempusProject as App;
|
||||
use TheTempusProject\Bedrock\Classes\Config;
|
||||
use TheTempusProject\Hermes\Functions\Route as Routes;
|
||||
|
||||
class MembershipCustomers extends DatabaseModel {
|
||||
public static $stripe;
|
||||
public $tableName = 'membership_customers';
|
||||
|
||||
public $databaseMatrix = [
|
||||
[ 'stripe_customer', 'varchar', '155' ],
|
||||
[ 'local_user', 'varchar', '155' ],
|
||||
// renews?
|
||||
// does this renew periodically?
|
||||
// renewal period?
|
||||
// if periodic, how frequent?
|
||||
// renewal type?
|
||||
// automatic, manual review, paid
|
||||
];
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function findByUserID( $d ) {
|
||||
$data = self::$db->get( $this->tableName, [ 'local_user', '=', $d ] );
|
||||
if ( ! $data->count() ) {
|
||||
return false;
|
||||
}
|
||||
return $data->first();
|
||||
}
|
||||
|
||||
public function findByCustomerID( $d ) {
|
||||
$data = self::$db->get( $this->tableName, [ 'stripe_customer', '=', $d ] );
|
||||
if ( ! $data->count() ) {
|
||||
return false;
|
||||
}
|
||||
return $data->first();
|
||||
}
|
||||
|
||||
public function create( $user_id ) {
|
||||
$data = self::$db->get( 'users', ['ID', '=', $user_id] );
|
||||
if ( !$data->count() ) {
|
||||
Debug::warn( "customer cannot be created, user not found" );
|
||||
return false;
|
||||
}
|
||||
$user = $data->first();
|
||||
$user_email = $user->email;
|
||||
$user_name = $user->name;
|
||||
|
||||
$api_key = Config::getValue( 'memberships/stripeSecret' );
|
||||
if ( $api_key == 'sk_xxxxxxxxxxxxxxx' || empty($api_key) ) {
|
||||
Debug::error( "No Stripe Key found" );
|
||||
return false;
|
||||
}
|
||||
self::$stripe = new \Stripe\StripeClient( $api_key );
|
||||
|
||||
$customer = self::$stripe->customers->create([
|
||||
'name' => $user_name,
|
||||
'email' => $user_email,
|
||||
]);
|
||||
|
||||
$fields = [
|
||||
'stripe_customer' => $customer->id,
|
||||
'local_user' => $user_id,
|
||||
];
|
||||
|
||||
if ( !self::$db->insert( $this->tableName, $fields ) ) {
|
||||
Debug::error( "Membership Customer: $data not added: $fields" );
|
||||
new customException( 'membershipCustomerCreate' );
|
||||
return false;
|
||||
}
|
||||
|
||||
return $customer;
|
||||
}
|
||||
|
||||
public function findOrCreate( $user_id ) {
|
||||
$user = $this->findByUserID( $user_id );
|
||||
if ( ! empty( $user ) ) {
|
||||
return $user->stripe_customer;
|
||||
}
|
||||
$user = $this->create( $user_id );
|
||||
if ( ! empty( $user ) ) {
|
||||
return $user->id;
|
||||
}
|
||||
}
|
||||
}
|
38
app/plugins/members/models/membership_invoices.php
Normal file
38
app/plugins/members/models/membership_invoices.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
/**
|
||||
* app/plugins/members/models/membership_invoices.php
|
||||
*
|
||||
* This class is used for the manipulation of the membership_invoices database table.
|
||||
*
|
||||
* @package TP Members
|
||||
* @version 3.0
|
||||
* @author Joey Kimsey <Joey@thetempusproject.com>
|
||||
* @link https://TheTempusProject.com
|
||||
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
||||
*/
|
||||
namespace TheTempusProject\Models;
|
||||
|
||||
use TheTempusProject\Canary\Bin\Canary as Debug;
|
||||
use TheTempusProject\Bedrock\Functions\Check;
|
||||
use TheTempusProject\Bedrock\Functions\Sanitize;
|
||||
use TheTempusProject\Classes\DatabaseModel;
|
||||
use TheTempusProject\TheTempusProject as App;
|
||||
|
||||
class MembershipInvoices extends DatabaseModel {
|
||||
public static $stripe;
|
||||
public $tableName = 'membership_invoices';
|
||||
|
||||
public $databaseMatrix = [
|
||||
[ 'name', 'varchar', '155' ],
|
||||
// renews?
|
||||
// does this renew periodically?
|
||||
// renewal period?
|
||||
// if periodic, how frequent?
|
||||
// renewal type?
|
||||
// automatic, manual review, paid
|
||||
];
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
}
|
||||
}
|
206
app/plugins/members/models/membership_products.php
Normal file
206
app/plugins/members/models/membership_products.php
Normal file
@ -0,0 +1,206 @@
|
||||
<?php
|
||||
/**
|
||||
* app/plugins/members/models/membership_products.php
|
||||
*
|
||||
* This class is used for the manipulation of the membership_products database table.
|
||||
*
|
||||
* @package TP Members
|
||||
* @version 3.0
|
||||
* @author Joey Kimsey <Joey@thetempusproject.com>
|
||||
* @link https://TheTempusProject.com
|
||||
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
||||
*/
|
||||
|
||||
namespace TheTempusProject\Models;
|
||||
|
||||
use TheTempusProject\Canary\Bin\Canary as Debug;
|
||||
use TheTempusProject\Bedrock\Functions\Check;
|
||||
use TheTempusProject\Bedrock\Functions\Sanitize;
|
||||
use TheTempusProject\Classes\DatabaseModel;
|
||||
use TheTempusProject\TheTempusProject as App;
|
||||
use TheTempusProject\Bedrock\Classes\Config;
|
||||
|
||||
class MembershipProducts extends DatabaseModel {
|
||||
public static $stripe;
|
||||
public $tableName = 'membership_products';
|
||||
public $databaseMatrix = [
|
||||
[ 'name', 'varchar', '128' ],
|
||||
[ 'description', 'text', '' ],
|
||||
[ 'monthly_price', 'int', '10' ], // must be int value greater than 99 IE $1.00 === 100, $4399.22 === 439922
|
||||
[ 'yearly_price', 'int', '10' ], // must be int value greater than 99 IE $1.00 === 100, $4399.22 === 439922
|
||||
[ 'stripe_product', 'varchar', '64' ], // not-required to create - generated by stripe after
|
||||
[ 'stripe_price_monthly', 'varchar', '64' ], // not-required to create - generated by stripe after
|
||||
[ 'stripe_price_yearly', 'varchar', '64' ], // not-required to create - generated by stripe after
|
||||
];
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
$api_key = Config::getValue( 'memberships/stripeSecret' );
|
||||
if ( $api_key == 'sk_xxxxxxxxxxxxxxx' || empty($api_key) ) {
|
||||
Debug::error( "No Stripe Key found" );
|
||||
} else {
|
||||
self::$stripe = new \Stripe\StripeClient( $api_key );
|
||||
}
|
||||
}
|
||||
|
||||
public function create( $name, $description, $monthly_price, $yearly_price ) {
|
||||
if ( empty( self::$stripe ) ) {
|
||||
return false;
|
||||
}
|
||||
$stripe_product = $this->createStripeProduct( $name, $description );
|
||||
$stripe_prices = $this->createStripePrices( $stripe_product->id, $monthly_price, $yearly_price );
|
||||
|
||||
$fields = [
|
||||
'name' => $name,
|
||||
'description' => $description,
|
||||
'monthly_price' => $monthly_price,
|
||||
'yearly_price' => $yearly_price,
|
||||
'stripe_product' => $stripe_product->id,
|
||||
'stripe_price_monthly' => $stripe_prices['monthly']->id,
|
||||
'stripe_price_yearly' => $stripe_prices['yearly']->id,
|
||||
];
|
||||
|
||||
if ( !self::$db->insert( $this->tableName, $fields ) ) {
|
||||
Debug::error( "Membership Product: $data not updated: $fields" );
|
||||
new customException( 'membershipProductCreate' );
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function createStripeProduct( $name, $description ) {
|
||||
if ( empty( self::$stripe ) ) {
|
||||
return false;
|
||||
}
|
||||
$product = self::$stripe->products->create([
|
||||
'name' => $name,
|
||||
'description' => $description,
|
||||
]);
|
||||
return $product;
|
||||
}
|
||||
public function createStripePrices( $product, $monthly_price, $yearly_price ) {
|
||||
$out = [];
|
||||
$out['monthly'] = $this->createStripeMonthlyPrice( $product, $monthly_price );
|
||||
$out['yearly'] = $this->createStripeYearlyPrice( $product, $yearly_price );
|
||||
return $out;
|
||||
}
|
||||
public function createStripeMonthlyPrice( $product, $monthly_price ) {
|
||||
if ( empty( self::$stripe ) ) {
|
||||
return false;
|
||||
}
|
||||
return self::$stripe->prices->create([
|
||||
'currency' => 'usd',
|
||||
'unit_amount' => $monthly_price,
|
||||
'recurring' => ['interval' => 'month'],
|
||||
'product' => $product,
|
||||
'lookup_key' => 'membership-monthly',
|
||||
]);
|
||||
}
|
||||
public function createStripeYearlyPrice( $product, $yearly_price ) {
|
||||
if ( empty( self::$stripe ) ) {
|
||||
return false;
|
||||
}
|
||||
return self::$stripe->prices->create([
|
||||
'currency' => 'usd',
|
||||
'unit_amount' => $yearly_price,
|
||||
'recurring' => ['interval' => 'year'],
|
||||
'product' => $product,
|
||||
'lookup_key' => 'membership-yearly',
|
||||
]);
|
||||
}
|
||||
|
||||
public function updateProduct( $id, $name, $description, $monthly_price, $yearly_price ) {
|
||||
$product = $this->findById( $id );
|
||||
if ( $product === false ) {
|
||||
return false;
|
||||
}
|
||||
if ( $product->monthly_price != $monthly_price ) {
|
||||
$new_monthly = $this->updateStripeMonthlyPrice( $product->stripe_price_monthly, $monthly_price );
|
||||
}
|
||||
if ( $product->yearly_price != $yearly_price ) {
|
||||
$new_yearly = $this->updateStripeYearlyPrice( $product->stripe_price_yearly, $yearly_price );
|
||||
}
|
||||
if ( ( $product->name != $name ) || ( $product->description != $description ) ) {
|
||||
$this->updateStripeProduct( $product->stripe_product, $name, $description );
|
||||
}
|
||||
|
||||
$fields = [
|
||||
'name' => $name,
|
||||
'description' => $description,
|
||||
'monthly_price' => $monthly_price,
|
||||
'yearly_price' => $yearly_price,
|
||||
'stripe_price_monthly' => $new_monthly->id,
|
||||
'stripe_price_yearly' => $new_yearly->id,
|
||||
];
|
||||
if ( !self::$db->update( $this->tableName, $id, $fields ) ) {
|
||||
new CustomException( 'membershipProductUpdate' );
|
||||
Debug::error( "membership Product: $id not updated: $fields" );
|
||||
|
||||
return false;
|
||||
}
|
||||
self::$log->admin( "Updated membership Product: $id" );
|
||||
return true;
|
||||
}
|
||||
|
||||
public function updateStripeProduct( $id, $name, $description ) {
|
||||
if ( empty( self::$stripe ) ) {
|
||||
return false;
|
||||
}
|
||||
$product = self::$stripe->products->update(
|
||||
$id,
|
||||
[
|
||||
'name' => $name,
|
||||
'description' => $description,
|
||||
]
|
||||
);
|
||||
return $product;
|
||||
}
|
||||
|
||||
public function updateStripeYearlyPrice( $product, $yearly_price ) {
|
||||
$stripe->prices->update(
|
||||
$yearly->id,
|
||||
['lookup_key' => ""]
|
||||
);
|
||||
return $this->createStripeYearlyPrice( $product, $yearly_price );
|
||||
}
|
||||
public function updateStripeMonthlyPrice( $product, $monthly_price ) {
|
||||
$stripe->prices->update(
|
||||
$monthly->id,
|
||||
['lookup_key' => ""]
|
||||
);
|
||||
return $this->createStripeMonthlyPrice( $product, $monthly_price );
|
||||
}
|
||||
|
||||
public function filter( $postArray, $params = [] ) {
|
||||
foreach ( $postArray as $instance ) {
|
||||
if ( !is_object( $instance ) ) {
|
||||
$instance = $postArray;
|
||||
$end = true;
|
||||
}
|
||||
|
||||
$instance->prettyPriceMonthly = '$' . number_format( $instance->monthly_price / 100, 2 ); // Outputs: $99.49
|
||||
$instance->prettyPriceYearly = '$' . number_format( $instance->yearly_price / 100, 2 ); // Outputs: $99.49
|
||||
$instance->prettySavings = '$' . number_format(
|
||||
( $instance->yearly_price - ( $instance->monthly_price * 12 ) ) / 100,
|
||||
2
|
||||
); // Outputs: $99.49
|
||||
|
||||
$out[] = $instance;
|
||||
if ( !empty( $end ) ) {
|
||||
$out = $out[0];
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
public function findByPriceID( $d ) {
|
||||
$data = self::$db->get( $this->tableName, [ 'stripe_price_monthly', '=', $d, 'OR', 'stripe_price_yearly', '=', $d ] );
|
||||
|
||||
if ( ! $data->count() ) {
|
||||
return false;
|
||||
}
|
||||
return $data->first();
|
||||
}
|
||||
}
|
170
app/plugins/members/models/memberships.php
Normal file
170
app/plugins/members/models/memberships.php
Normal file
@ -0,0 +1,170 @@
|
||||
<?php
|
||||
/**
|
||||
* app/plugins/members/models/membership_records.php
|
||||
*
|
||||
* This class is used for the manipulation of the membership_records database table.
|
||||
*
|
||||
* @package TP Members
|
||||
* @version 3.0
|
||||
* @author Joey Kimsey <Joey@thetempusproject.com>
|
||||
* @link https://TheTempusProject.com
|
||||
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
|
||||
*/
|
||||
namespace TheTempusProject\Models;
|
||||
|
||||
use TheTempusProject\Canary\Bin\Canary as Debug;
|
||||
use TheTempusProject\Bedrock\Functions\Check;
|
||||
use TheTempusProject\Bedrock\Functions\Sanitize;
|
||||
use TheTempusProject\Classes\DatabaseModel;
|
||||
use TheTempusProject\TheTempusProject as App;
|
||||
use TheTempusProject\Models\MembershipProducts;
|
||||
use TheTempusProject\Bedrock\Classes\Config;
|
||||
use TheTempusProject\Canary\Classes\CustomException;
|
||||
|
||||
class Memberships extends DatabaseModel {
|
||||
public static $stripe;
|
||||
public static $products;
|
||||
public $tableName = 'membership_records';
|
||||
public $databaseMatrix = [
|
||||
[ 'stripe_customer', 'varchar', '64' ],
|
||||
[ 'stripe_subscription', 'varchar', '64' ],
|
||||
[ 'subscription_price_id', 'varchar', '64' ],
|
||||
[ 'current_period_end', 'int', '10' ],
|
||||
[ 'current_period_start', 'int', '10' ],
|
||||
[ 'status', 'varchar', '64' ],
|
||||
[ 'local_user_id', 'int', '10' ],
|
||||
[ 'billing_frequency', 'varchar', '16' ],
|
||||
];
|
||||
|
||||
public function __construct() {
|
||||
parent::__construct();
|
||||
self::$products = new MembershipProducts;
|
||||
$api_key = Config::getValue( 'memberships/stripeSecret' );
|
||||
if ( $api_key == 'sk_xxxxxxxxxxxxxxx' || empty($api_key) ) {
|
||||
Debug::error( "No Stripe Key found" );
|
||||
} else {
|
||||
self::$stripe = new \Stripe\StripeClient( $api_key );
|
||||
}
|
||||
}
|
||||
|
||||
public function filter( $postArray, $params = [] ) {
|
||||
foreach ( $postArray as $instance ) {
|
||||
if ( !is_object( $instance ) ) {
|
||||
$instance = $postArray;
|
||||
$end = true;
|
||||
}
|
||||
$instance->name = self::$user->getUsername( $instance->local_user_id );
|
||||
$priceData = self::$products->findByPriceID( $instance->subscription_price_id );
|
||||
if ( $priceData ) {
|
||||
if ( $priceData->stripe_price_monthly == $instance->subscription_price_id ) {
|
||||
$price = $priceData->monthly_price;
|
||||
} else {
|
||||
$price = $priceData->yearly_price;
|
||||
}
|
||||
$instance->productName = $priceData->name;
|
||||
$instance->prettyPrice = '$' . number_format( $price / 100, 2 ); // Outputs: $99.49
|
||||
} else {
|
||||
$instance->prettyPrice = "unknown";
|
||||
$instance->productName = "unknown";
|
||||
}
|
||||
$out[] = $instance;
|
||||
if ( !empty( $end ) ) {
|
||||
$out = $out[0];
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
public function getUserSubs( $limit = null ) {
|
||||
$whereClause = ['local_user_id', '=', App::$activeUser->ID ];
|
||||
if ( empty( $limit ) ) {
|
||||
$postData = self::$db->getPaginated( $this->tableName, $whereClause );
|
||||
} else {
|
||||
$postData = self::$db->getPaginated( $this->tableName, $whereClause, 'ID', 'DESC', [0, $limit] );
|
||||
}
|
||||
if ( !$postData->count() ) {
|
||||
Debug::info( 'No user subs found.' );
|
||||
return false;
|
||||
}
|
||||
return $this->filter( $postData->results() );
|
||||
}
|
||||
|
||||
public function findByUserID( $d ) {
|
||||
$data = self::$db->get( $this->tableName, [ 'local_user_id', '=', $d ] );
|
||||
if ( ! $data->count() ) {
|
||||
return false;
|
||||
}
|
||||
return $data->first();
|
||||
}
|
||||
|
||||
public function findActiveByUserID( $d ) {
|
||||
$data = self::$db->get( $this->tableName, [ 'local_user_id', '=', $d, 'AND', 'status', '=', 'active' ] );
|
||||
if ( ! $data->count() ) {
|
||||
return false;
|
||||
}
|
||||
return $data->first();
|
||||
}
|
||||
|
||||
public function cancel( $id ) {
|
||||
$data = self::$db->get( $this->tableName, [ 'ID', '=', $id, 'AND', 'local_user_id', '=', App::$activeUser->ID, 'AND', 'status', '=', 'active' ] );
|
||||
if ( ! $data->count() ) {
|
||||
return false;
|
||||
}
|
||||
$membershipID = $data->first()->stripe_subscription;
|
||||
$out = false;
|
||||
try {
|
||||
$out = self::$stripe->subscriptions->cancel($membershipID, []);
|
||||
} catch(\Exception $e) {
|
||||
Debug::error( 'Exception' );
|
||||
Debug::v( $e );
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
||||
public function findBySubscriptionID( $d ) {
|
||||
$data = self::$db->get( $this->tableName, [ 'stripe_subscription', '=', $d ] );
|
||||
if ( ! $data->count() ) {
|
||||
return false;
|
||||
}
|
||||
return $data->first();
|
||||
}
|
||||
|
||||
public function create( $customer, $subscription, $price, $start, $end, $status, $user_id, $frequency ) {
|
||||
$fields = [
|
||||
'stripe_customer' => $customer,
|
||||
'stripe_subscription' => $subscription,
|
||||
'subscription_price_id' => $price,
|
||||
'current_period_end' => $end,
|
||||
'current_period_start' => $start,
|
||||
'status' => $status,
|
||||
'local_user_id' => $user_id,
|
||||
'billing_frequency' => $frequency,
|
||||
];
|
||||
|
||||
if ( !self::$db->insert( $this->tableName, $fields ) ) {
|
||||
Debug::error( "Memberships: not created: $fields" );
|
||||
new CustomException( 'membershipsCreate' );
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function update( $id, $start, $end, $status ) {
|
||||
$fields = [
|
||||
'current_period_end' => $end,
|
||||
'current_period_start' => $start,
|
||||
'status' => $status,
|
||||
];
|
||||
|
||||
if ( !self::$db->update( $this->tableName, $id, $fields ) ) {
|
||||
Debug::error( "Memberships: not updated: " );
|
||||
Debug::v( $fields );
|
||||
new CustomException( 'membershipsUpdate' );
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user