mirror of
https://github.com/Bigherollc/wticreatorstudio.git
synced 2026-10-20 21:03:27 -04:00
Subscription Module Update
Payment Code for renewal
This commit is contained in:
@@ -6,10 +6,18 @@ use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Modules\Subscription\Entities\PaymentCode;
|
||||
use Modules\Subscription\Entities\PaymentCodeAddon;
|
||||
use Modules\Subscription\Entities\CustomerSubscription;
|
||||
use Modules\Subscription\Entities\UserCredit;
|
||||
use Modules\Subscription\Entities\UserCreditHistory;
|
||||
use Modules\Customer\Services\CustomerService;
|
||||
|
||||
class PaymentCodeRepository
|
||||
{
|
||||
|
||||
public function __construct(){
|
||||
$this->customerService = new CustomerService;
|
||||
}
|
||||
|
||||
public function all() {
|
||||
return PaymentCode::with(['user','planPrice.plan','addons'])->withSum('addons', 'price')->orderBy('id')->get();
|
||||
}
|
||||
@@ -50,4 +58,100 @@ class PaymentCodeRepository
|
||||
return true;
|
||||
}
|
||||
|
||||
public function delete($id) {
|
||||
|
||||
$paymentCode = PaymentCode::find($id);
|
||||
if ($paymentCode) {
|
||||
|
||||
$paymentCode->delete();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function validate($code) {
|
||||
|
||||
$paymentCode = PaymentCode::with('planPrice.plan','addons')->where('code',$code)->where('type','renewal')->withSum('addons', 'price')->first();
|
||||
if ($paymentCode) {
|
||||
|
||||
|
||||
if($paymentCode->user_id != null) {
|
||||
return response()->json([
|
||||
'error' => 'Payment Code has been used already!'
|
||||
], 500);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Payment Code Valid',
|
||||
'subscription' => $paymentCode
|
||||
], 200);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'error' => 'Payment Code Not Found!'
|
||||
], 500);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function apply($code,$id = null) {
|
||||
|
||||
|
||||
$user_id = $id != null ? $id : auth()->user()->id;
|
||||
|
||||
$paymentCode = PaymentCode::with('planPrice.plan')->where('code',$code)->where('type','renewal')->first();
|
||||
if ($paymentCode) {
|
||||
|
||||
if($paymentCode->user_id != null) {
|
||||
return response()->json([
|
||||
'error' => 'Payment Code has been used already!'
|
||||
], 500);
|
||||
}
|
||||
|
||||
$customer_subscription = new CustomerSubscription();
|
||||
$customer_subscription->customer_id = $user_id;
|
||||
$customer_subscription->txn_id = $paymentCode->code;
|
||||
$customer_subscription->subscription_plan_price_id = $paymentCode->planPrice->id;
|
||||
$customer_subscription->payment_code_id = $paymentCode->id;
|
||||
$customer_subscription->payment_method = 'prepaid';
|
||||
$customer_subscription->payment_mode = 'All';
|
||||
$customer_subscription->status = 'active';
|
||||
$customer_subscription->payment_status = 'succeeded';
|
||||
$customer_subscription->is_paid = true;
|
||||
$customer_subscription->approved = true;
|
||||
$customer_subscription->next_payment_at = null;
|
||||
$customer_subscription->expired_at = Carbon::now()->addMonths($paymentCode->planPrice->access_expire_duration);
|
||||
$customer_subscription->last_payment_date = Carbon::now()->format('Y-m-d');
|
||||
$customer_subscription->save();
|
||||
|
||||
$paymentCode->user_id = $user_id;
|
||||
$paymentCode->save();
|
||||
|
||||
$user_credits = UserCredit::firstOrCreate(['user_id' => $user_id]);
|
||||
$before_credits = $this->customerService->credits($user_credits->user_id);
|
||||
$user_credits->increment('subscription_credits',$paymentCode->planPrice->total_credits);
|
||||
$after_credits = $this->customerService->credits($user_credits->user_id);
|
||||
|
||||
$user_credits_history = new UserCreditHistory;
|
||||
$user_credits_history->user_id = $user_id;
|
||||
$user_credits_history->subscription_credits_from = $before_credits['total_available_credits']['subscription'];
|
||||
$user_credits_history->subscription_credits_to = $after_credits['total_available_credits']['subscription'];
|
||||
$user_credits_history->alacarte_credits_from = $before_credits['total_available_credits']['alacarte'];;
|
||||
$user_credits_history->alacarte_credits_to = $after_credits['total_available_credits']['alacarte'];
|
||||
$user_credits_history->details = 'Added Total Plan Credits of '.$paymentCode->planPrice->plan->title.' (Prepaid)';
|
||||
$user_credits_history->save();
|
||||
|
||||
return response()->json([
|
||||
'message' => __('Prepaid Code successfully applied.')
|
||||
], 200);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'error' => 'Payment Code Not Found!'
|
||||
], 404);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user