Files
wticreatorstudio/Modules/Subscription/Http/Controllers/SubscribersController.php
Fritz Ramirez 10d0c477c8 Initial commit
2024-02-12 22:54:20 -05:00

426 lines
18 KiB
PHP

<?php
namespace Modules\Subscription\Http\Controllers;
use Brian2694\Toastr\Facades\Toastr;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Customer\Repositories\CustomerRepository;
use Modules\Customer\Services\CustomerService;
use Modules\Subscription\Entities\SubscriptionSetting;
use Modules\Subscription\Entities\CustomerSubscription;
use Modules\Subscription\Entities\UserCredit;
use Modules\Subscription\Services\PaypalPaymentService;
use App\Traits\Accounts;
use App\Traits\SendMail;
use Carbon\Carbon;
use Exception;
class SubscribersController extends Controller
{
use Accounts, SendMail;
private $stripePayment;
private $paypalPayment;
protected $customerService;
public function __construct(CustomerService $customerService)
{
$credential = getPaymentInfoViaSellerId(1, 4);
$this->stripePayment = new \Stripe\StripeClient([
"api_key" => @$credential->perameter_3,
"stripe_version" => "2022-11-15"
]);
$this->paypalPayment = new PaypalPaymentService();
$this->customerService = $customerService;
}
public function index() {
return view('subscription::subscribers.index');
}
public function allCustomers(Request $request) {
if (isModuleActive('RegistrationFormSteps')) {
$subscriptions = CustomerSubscription::with(['user','planPrice'])->latest()->get();
$subscribers_data = [];
foreach ($subscriptions as $subscription) {
$payment_details = '';
if ($subscription->planPrice->billing_type == 'Recurring') {
$price = $subscription->planPrice->price;
$price_monthly = $subscription->planPrice->price / $subscription->planPrice->custom_interval;
if ($subscription->planPrice->interval == 'Custom') {
if($subscription->payment_mode == 'Monthly'){
$payment_details .= "\$".number_format($price_monthly,2)." / monthly for {$subscription->planPrice->custom_interval} Months";
} else {
$payment_details .= "\$".number_format($price,2)." for {$subscription->planPrice->custom_interval} {$subscription->planPrice->custom_interval_type}";
}
} else {
$payment_details .= "\${$subscription->planPrice->price} {$subscription->planPrice->interval}";
}
if($subscription->discount != null){
$duration = $subscription->discount->coupon->duration;
$coupon_payment_mode = $subscription->discount->coupon->payment_mode;
$discount = $subscription->discount->coupon->discount;
if ($subscription->discount->coupon->type == 'Amount') {
$discount_label = "<span class=\"text-success\">Save \${$discount}</span>";
$discounted_price_monthly = number_format($price_monthly - $discount,2);
$discounted_price_all = number_format($price - $discount,2);
} else {
$discount_label = "<span class=\"text-success\">Save \${$discount}%</span>";
$discounted_price_monthly = number_format($price_monthly - (($discount / 100) * $price_monthly),2);
$discounted_price_all = $subscription->discount->coupon->duration == 'Once' ? number_format($price - (($discount / 100) * $price_monthly),2) : number_format($price - (($discount / 100) * $price),2);
}
$payment_details .= "<br> (<span class=\"badge bg-info\">{$subscription->discount->coupon->code}</span>";
if ($subscription->planPrice->payment == 'monthly' ) {
$payment_details .= $duration == 'Once' ? " - \${$discounted_price_monthly} 1st month only)" : " | \${$discounted_price_monthly} per month)";
} else {
$payment_details .= " - \${$discounted_price_all} for {$subscription->planPrice->custom_interval} Months ";
}
}
} else if ($subscription->planPrice->billing_type == 'One-Time') {
if($subscription->payment_status == 'uncaptured'){
$payment_details .= 'Expires on '. Carbon::parse($subscription->created_at)->addDays(7)->format('M d, Y').' if not yet authorize / captured';
} else {
if($subscription->planPrice->access_expire_duration_type == 'Days'){
$payment_details .= "\${$subscription->planPrice->price} {$subscription->planPrice->billing_type} {$subscription->planPrice->access} access, will expire on ".Carbon::parse($subscription->created_at)->addDays($subscription->planPrice->access_expire_duration)->format('M d, Y');
} else if($subscription->planPrice->access_expire_duration_type == 'Weeks'){
$payment_details .= "\${$subscription->planPrice->price} {$subscription->planPrice->billing_type} {$subscription->planPrice->access} access, will expire on ".Carbon::parse($subscription->created_at)->addWeeks($subscription->planPrice->access_expire_duration)->format('M d, Y');
} else if($subscription->planPrice->access_expire_duration_type == 'Months'){
$payment_details .= "\${$subscription->planPrice->price} {$subscription->planPrice->billing_type} {$subscription->planPrice->access} access, will expire on ".Carbon::parse($subscription->created_at)->addMonths($subscription->planPrice->access_expire_duration)->format('M d, Y');
} else if($subscription->planPrice->access_expire_duration_type == 'Years'){
$payment_details .= "\${$subscription->planPrice->price} {$subscription->planPrice->billing_type} {$subscription->planPrice->access} access, will expire on ".Carbon::parse($subscription->created_at)->addYears($subscription->planPrice->access_expire_duration)->format('M d, Y');
}
}
}
$data = [
'id' => $subscription->id,
'user' => [
'id' => $subscription->user->id,
'first_name' => $subscription->user->first_name,
'last_name' => $subscription->user->last_name,
'email' => $subscription->user->email,
'avatar' => $subscription->user->avatar,
'company_details' => $subscription->user->company_details,
],
'plan' => [
'title' => $subscription->planPrice->plan->title,
'image_url' => $subscription->planPrice->plan->image_url,
'price' => $subscription->planPrice->price,
'billing_type' => $subscription->planPrice->billing_type,
'interval' => $subscription->planPrice->interval,
'custom_interval' => $subscription->planPrice->custom_interval,
'custom_interval_type' => $subscription->planPrice->custom_interval_type,
],
'payment_details' => $payment_details,
'status' => $subscription->status == 'trialing' ? 'Waiting for approval' : $subscription->status,
'approved' => $subscription->approved,
'payment_method' => $subscription->payment_method,
'payment_mode' => $subscription->payment_mode,
'payment_status' => $subscription->payment_status,
'pov' => $subscription->pov,
'live_stream' => $subscription->live_stream,
'last_payment_date' => Carbon::parse($subscription->last_payment_date)->format('M j, Y'),
'next_payment_date' => Carbon::parse($subscription->next_payment_at)->format('M j, Y'),
'created_at' => Carbon::parse($subscription->created_at)->format('j M Y, g:i a'),
];
if($subscription->discount != null){
$data['discount'] = [
'code' => $subscription->discount->coupon->code,
'type' => $subscription->discount->coupon->type,
'discount' => $subscription->discount->coupon->discount,
'duration' => $subscription->discount->coupon->duration,
];
}
array_push($subscribers_data, $data);
// $subscription->plan_title = $subscription->planPrice->plan->title;
// $subscription->payment_details = $payment_details;
// $subscription->transaction_date = Carbon::parse($subscription->created_at)->format('M d, Y');
}
return response()->json([
'subscribers' => $subscribers_data
]);
}
if (isModuleActive('Customer')) {
$customer_service = new CustomerService(new CustomerRepository());
$customers = $customer_service->getAll()->where('is_active',1)->get();
return response()->json([
'customers' => $customers
]);
}
return response()->json([
'message' => 'Customer module not enabled.'
], 400);
}
public function approveCustomer(Request $request) {
try{
$subscription = CustomerSubscription::find($request->id);
if($subscription){
if($subscription->approved){
return response()->json([
'message' => 'Customer all ready approved!'
], 422);
}
//if no active subscription
$trial_end = 'now';
//lets check first if theres active subscription
$active_subscription = CustomerSubscription::where('customer_id',$subscription->customer_id)->where('status','active')->where('payment_status','succeeded')->first();
if($active_subscription) {
if($active_subscription->payment_method == 'stripe'){
// $response = $this->stripePayment->subscriptions->cancel(
// $active_subscription->txn_id,
// []
// );
// if ($response['status'] != "canceled") {
// return response()->json([
// 'message' => 'Subscription cannot be cancelled!'
// ], 500);
// }
// if($subscription->planPrice->subscription_plan_id == $active_subscription->planPrice->subscription_plan_id || $subscription->planPrice->payment == 'upfront'){
// $trial_end = $response['current_period_end'];
// }
} else if($active_subscription->payment_method == 'paypal'){
$response = $this->paypalPayment->cancelSubscription($active_subscription->txn_id);
if (!$response) {
return response()->json([
'message' => 'Subscription cannot be cancelled!'
], 500);
}
if($subscription->planPrice->subscription_plan_id == $active_subscription->planPrice->subscription_plan_id || $subscription->planPrice->payment == 'upfront' && $active_subscription->expired_at != null){
$trial_end = $active_subscription->expired_at->timestamp;
}
}
$active_subscription->status = 'cancelled';
$active_subscription->payment_status = 'cancelled';
$active_subscription->save();
}
if($subscription->payment_method == 'stripe'){
$response = $this->stripePayment->subscriptions->update($subscription->txn_id, [
'trial_end' => $trial_end
]);
if($trial_end != 'now' || $response['status'] == "active"){
$this->sendSubscriptionUpdate($subscription->user,'Approved');
$subscription->status = 'active';
$subscription->payment_status = 'succeeded';
$subscription->approved = 1;
} else {
$subscription->status = $response['status'];
$subscription->payment_status = 'failed';
}
} else if($subscription->payment_method == 'paypal'){
$response = $this->paypalPayment->activateSubscription($subscription->txn_id);
if(isset($response->details[0]->description)){
$details = $response->details[0]->description;
$subscription->message = $details;
$subscription->payment_status = 'failed';
$subscription->save();
return response()->json([
'message' => $details
], 422);
} else {
$this->sendSubscriptionUpdate($subscription->user,'Approved');
$subscription->status = 'active';
$subscription->payment_status = 'succeeded';
$subscription->approved = 1;
}
}
$subscription->save();
return response()->json([
'message' => 'Customer successfully approved!'
]);
}
} catch(Exception $e){
return response()->json([
'message' => $e->getMessage()
], 422);
}
}
public function rejectCustomer(Request $request) {
try{
$subscription = CustomerSubscription::find($request->id);
if($subscription){
if ($subscription->planPrice->billing_type == 'Recurring') {
$response = $this->stripePayment->subscriptions->cancel(
$subscription->txn_id,
[]
);
if ($response['status'] != "canceled") {
return response()->json([
'message' => 'Subscription cannot be cancelled!'
], 200);
}
}
$this->sendSubscriptionUpdate($subscription->user,'Rejected');
$subscription->status = 'rejected';
$subscription->payment_status = 'cancelled';
$subscription->update();
return response()->json([
'message' => 'Subscription successfully rejected'
]);
}
} catch(Exception $e){
return response()->json([
'message' => $e->getMessage()
], 422);
}
}
public function capturePayment(Request $request) {
try{
$subscription = CustomerSubscription::find($request->id);
if($subscription){
$response = $this->stripePayment->charges->capture(
$subscription->txn_id,
[]
);
if ($response['status'] == "succeeded") {
$subscription->payment_status = 'succeeded';
$subscription->is_paid = true;
$subscription->save();
return response()->json([
'message' => 'Payment successfully captured'
]);
}
}
} catch(Exception $e){
return response()->json([
'message' => $e->getMessage()
], 422);
}
}
public function cancelSubscription(Request $request) {
try{
$subscription = CustomerSubscription::find($request->id);
if($subscription){
if ($subscription->planPrice->billing_type == 'Recurring') {
$response = $this->stripePayment->subscriptions->cancel(
$subscription->txn_id,
[]
);
if ($response['status'] != "canceled") {
return response()->json([
'message' => 'Subscription cannot be cancelled!'
], 200);
}
}
$subscription->pov = 0;
$subscription->live_stream = 0;
$subscription->status = 'cancelled';
$subscription->payment_status = 'cancelled';
$subscription->update();
return response()->json([
'message' => 'Subscription successfully cancelled'
]);
}
} catch(Exception $e){
return response()->json([
'message' => $e->getMessage()
], 422);
}
}
}