Files
wticreatorstudio/Modules/Subscription/Repositories/SubscriptionPlansRepository.php
T
Leonard Biano 9dc58d6fc6 Subscription Module Update
Payment codes changes and fix paypal payment failed
2024-08-29 23:48:19 +08:00

233 lines
8.0 KiB
PHP

<?php
namespace Modules\Subscription\Repositories;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Modules\Product\Entities\Product;
use Modules\Subscription\Entities\CustomerSubscription;
use Modules\Product\Repositories\ProductRepository;
use Modules\Seller\Entities\SellerProduct;
use Modules\Subscription\Entities\SubscriptionPlan;
use Modules\Subscription\Entities\SubscriptionProductMeta;
use Modules\Subscription\Services\StripePaymentService;
use Modules\Subscription\Services\PaypalPaymentService;
class SubscriptionPlansRepository
{
private $productRepository;
private $stripePayment;
private $paypalPayment;
public function __construct()
{
$this->productRepository = new ProductRepository();
$this->stripePayment = new StripePaymentService();
$this->paypalPayment = new PaypalPaymentService();
}
public function getAll() {
$plans = SubscriptionPlan::orderBy('order')->get();
foreach ($plans as $plan) {
$label = $plan->title;
if ($plan->billing_type == 'Recurring') {
if ($plan->interval == 'Custom') {
$label .= " - \${$plan->price} per {$plan->custom_interval} {$plan->custom_interval_type}";
}
else {
$label .= " - \${$plan->price} {$plan->interval}";
}
if ($plan->trial_period) {
if ($plan->trial_duration > 0) {
$label .= " with {$plan->trial_duration} days trial period";
}
if ($plan->trial_amount > 0) {
$label .= " for \${$plan->trial_amount}";
}
}
}
else if ($plan->billing_type == 'One-Time') {
if ($plan->access == 'Expire') {
$label .= " - \${$plan->price} {$plan->billing_type} Expire after {$plan->access_expire_duration} {$plan->access_expire_duration_type}";
}
else if ($plan->access == 'Fixed Expire') {
$label .= " - \${$plan->price} {$plan->billing_type} Expire on {$plan->access_expire_on}";
}
else {
$label .= " - \${$plan->price} {$plan->billing_type} {$plan->access} access";
}
}
$plan['label'] = $label;
$days = $plan->days;
$yrs = 0;
$mos = 0;
$wks = 0;
$per = null;
if ($days % 365 == 0) {
$yrs = $days / 365;
$per = $yrs == 1 ? 'year' : $yrs . ' years';
}
else if ($days % 30 == 0) {
$mos = $days / 30;
$per = $mos == 1 ? 'month' : $mos . ' months';
}
else if ($days % 7 == 0) {
$wks = $days / 7;
$per = $wks == 1 ? 'week' : $wks . ' weeks';
}
else {
$per = $days == 1 ? 'day' : $days . ' days';
}
$plan['per'] = $per;
}
return $plans;
/*return SubscriptionProductMeta::whereHas('product', function($query) {
$query->where('product_type', 3)->where('status', 1)->where('is_approved', 1)->latest();
})->get();*/
}
public function getAllPlanProducts() {
$products = SellerProduct::whereHas('product', function($query) {
$query->whereHas('plan', function($query) {
$query->where('status', 1);
});
})->get();
/*$products = Product::whereHas('plan', function($query) {
$query->where('status', 1);
})->get();*/
foreach ($products as $seller_product) {
$plan = $seller_product->product->plan;
$days = $seller_product->product->plan->days;
$yrs = 0;
$mos = 0;
$wks = 0;
$per = null;
if ($days % 365 == 0) {
$yrs = $days / 365;
$per = $yrs == 1 ? 'year' : $yrs . ' years';
}
else if ($days % 30 == 0) {
$mos = $days / 30;
$per = $mos == 1 ? 'month' : $mos . ' months';
}
else if ($days % 7 == 0) {
$wks = $days / 7;
$per = $wks == 1 ? 'week' : $wks . ' weeks';
}
else {
$per = $days == 1 ? 'day' : $days . ' days';
}
$seller_product->product->plan['per'] = $per;
}
return $products;
}
public function getAllSubscriptionPlans() {
return SubscriptionPlan::with('prices')->whereHas('prices', function ($query) {
return $query->where('payment_method', '!=', 'prepaid');
})->where('prepaid',0)->where('status',1)->orderBy('order')->get();
}
public function create($request) {
$data = array_keys_to_snake_case($request->except('_token'));
$data['image'] = $request->file('image')->store('plans','public');
$data['prepaid'] = $data['prepaid'] === 'true' ? true : false;
$data['status'] = $data['status'] === 'true' ? true : false;
if(!$data['prepaid']){
$stripeResponse = $this->stripePayment->createProduct($data);
$data['stripe_product_id'] = $stripeResponse['id'];
$paypalResponse = $this->paypalPayment->createProduct($data['title'],'SERVICE');
$data['paypal_product_id'] = $paypalResponse->id;
}
$plan = new SubscriptionPlan();
$plan->fill($data);
$plan->save();
return true;
}
public function update($request) {
$data = array_keys_to_snake_case($request->except('_token'));
$data['prepaid'] = $data['prepaid'] === 'true' ? true : false;
$data['status'] = $data['status'] === 'true' ? true : false;
$plan = SubscriptionPlan::find($data['id']);
if ($plan) {
if ($request->image) {
if(\Storage::exists($plan->image)){
\Storage::delete($plan->image);
}
$data['image'] = $request->file('image')->store('plans','public');
}
if(!$data['prepaid']){
if($plan->stripe_product_id == null){
$stripeResponse = $this->stripePayment->createProduct($data);
$data['stripe_product_id'] = $stripeResponse['id'];
//$data['stripe_price_id'] = $stripeResponse['default_price'];
} else {
$stripeResponse = $this->stripePayment->updateProduct($plan, $data);
$data['stripe_price_id'] = $stripeResponse['default_price'];
//$this->stripePayment->archivePrice($plan->stripe_price_id);
}
$paypalResponse = $this->paypalPayment->createProduct($data['title'],'SERVICE');
$data['paypal_product_id'] = $paypalResponse->id;
}
$plan->fill($data);
$plan->update();
return true;
}
return false;
}
public function delete($request) {
$plan = SubscriptionPlan::with('prices')->find($request->id);
if ($plan) {
$subscriptions = CustomerSubscription::where('plan_id', $request->id)->count();
if ($subscriptions > 0) {
return 'in_use';
} else {
foreach ($plan->prices as $key => $price) {
$this->stripePayment->archivePrice($price->stripe_price_id);
}
$this->stripePayment->archiveProduct($plan->stripe_product_id);
$plan->delete();
return true;
}
}
return false;
}
}