UnknownSec Bypass
403
:
/
mnt
/
lmsestudio-instance-vol002
/
lms_fc9b9b93c3b2
/
app
/
Services
/
Hires
/ [
drwxr-xr-x
]
Menu
Upload
Mass depes
Mass delete
Terminal
Info server
About
name :
HireServices.php
<?php namespace EstudioLMS\Services\Hires; use EstudioLMS\Cart\Cart; use EstudioLMS\Events\FreeDone; use EstudioLMS\Events\HotmartDone; use EstudioLMS\Events\PagseguroDone; use EstudioLMS\Events\PaypalDone; use EstudioLMS\Exceptions\Handler; use EstudioLMS\Helpers\Helpers; use EstudioLMS\Repositories\Coupon\CouponRepository; use EstudioLMS\Repositories\Courses\Course\CourseRepository; use EstudioLMS\Repositories\Environment\HiredCourseRepository; use EstudioLMS\Repositories\Financial\HiringInterface; use EstudioLMS\Services\Admin\ConfigurationServices; use Illuminate\Contracts\Auth\Guard; use Omnipay\Common\GatewayFactory; use PHPSC\PagSeguro\Client\PagSeguroException; use PHPSC\PagSeguro\Customer\Address; use PHPSC\PagSeguro\Items\Item; use PHPSC\PagSeguro\Purchases\Transactions\Locator; use PHPSC\PagSeguro\Requests\Checkout\CheckoutService; use PHPSC\PagSeguro\Shipping\Shipping; use Symfony\Component\HttpFoundation\Session\SessionInterface; /** * Class HireServices * @package EstudioLMS\Services\Hires */ class HireServices { /** * @var HiredCourseRepository */ private $hiredCourse; /** * @var Guard */ private $auth; /** * @var SessionInterface */ private $session; /** * @var CourseRepository */ private $course; /** * @var Cart */ private $cart; /** * @var Locator */ private $locator; /** * @var ConfigurationServices */ private $configurationServices; /** * @var HiringInterface */ private $hiring; /** * @var CouponRepository */ private $couponRepository; /** * HireServices constructor. * @param HiredCourseRepository $hiredCourse * @param SessionInterface $session * @param Guard $auth * @param CourseRepository $course * @param Cart $cart * @param Locator $locator * @param ConfigurationServices $configurationServices * @param HiringInterface $hiring * @param CouponRepository $couponRepository */ public function __construct( HiredCourseRepository $hiredCourse, SessionInterface $session, Guard $auth, CourseRepository $course, Cart $cart, Locator $locator, ConfigurationServices $configurationServices, HiringInterface $hiring, CouponRepository $couponRepository ) { $this->hiredCourse = $hiredCourse; $this->auth = $auth; $this->session = $session; $this->course = $course; $this->cart = $cart; $this->locator = $locator; $this->configurationServices = $configurationServices; $this->hiring = $hiring; $this->couponRepository = $couponRepository; } /** * @param $data * @return array|string[] */ public function freeCourse($data, $assureFree = true) { if ($this->session->has('cart')) { $cart = $this->session->get('cart'); } else { $cart = $this->cart; } $course = $this->course->with(['plans.duration'])->find($data['course_id']); if ($course->is_free == 0 && $assureFree) { return ['success' => 'free_false', 'mensagem' => 'Este curso não é gratuíto!']; } $planId = (int)$course->plans->first()->id; $planDuration = (int)$course->plans->first()->duration->duration; $alreadyHired = $this->hiredCourse ->orderBy('created_at', 'DESC') ->findWhere( [ [ 'user_id', '=', $this->auth->user()['id'] ], [ 'course_id', '=', $data['course_id'] ] ] ) ->first(); $renew = false; if (count($alreadyHired)) { if ($alreadyHired['status'] == 10) { $renew = true; } else { return ['success' => 'free_false', 'mensagem' => 'Curso já contratado!']; } } $startDate = date('Y-m-d H:i:s'); $addDate = '+' . $planDuration . ' months'; $endDate = date('Y-m-d 23:59:59', strtotime($addDate, strtotime($startDate))); $header = [ 'user_id' => $this->auth->user()['id'], 'gateway_id' => 'FreeCourse', 'payment_code' => uniqid(""), 'gross_amount' => $cart->count() > 0 ? !empty($cart->getGrossAmount()) ? $cart->getGrossAmount() : 0 : 0, 'fee_amount' => 0, 'net_amount' => 0, 'coupon_title' => $cart->count() > 0 ? !empty($cart->get('coupon_name')) ? $cart->get('coupon_name') : null : null, 'coupon_code' => $cart->count() > 0 ? !empty($cart->get('coupon_code')) ? $cart->get('coupon_code') : null : null, 'coupon_discount' => $cart->count() > 0 ? !empty($cart->get('coupon_discount')) ? $cart->get('coupon_discount') : null : null, 'discount_amount' => $cart->count() > 0 ? !empty($cart->getDiscountAmount()) ? $cart->getDiscountAmount() : 0 : 0, 'status' => 3, 'course_id' => $data['course_id'], 'plan_id' => $data['select_plan'], 'start' => $startDate, 'end' => $endDate ]; $createHiring = $this->hiring->create($header); $hiredCourse = [ 'user_id' => $this->auth->user()['id'], 'course_id' => $data['course_id'], 'plan_id' => $data['select_plan'], 'hirings_id' => $createHiring['id'], 'status' => 3, 'start' => $startDate, 'end' => $endDate, 'is_free' => true ]; if ($renew) { $this->hiredCourse->update($hiredCourse, $alreadyHired->id); } else { $this->hiredCourse->create($hiredCourse); } $hiredCourse = $this->hiring ->with(['user', 'plan.duration', 'course']) ->find($createHiring['id']); \Event::fire(new FreeDone($hiredCourse)); return ['success' => true, 'mensagem' => '']; } /** * @param $data * @return array|string[] */ public function hotmart($data) { $enrollLimite = \Session::get('planLimite.plan_users_limit'); $studendHasActiveEnroll = false; $enrolled = $this->hiredCourse->activeUsers(); $purchases = ['APPROVED', 'COMPLETE']; $waitings = ['PRINTED_BILLET', 'BILLET_PRINTED', 'PROCESSING_TRANSACTION', 'WAITING_PAYMENT', 'UNDER_ANALISYS', 'STARTED']; $cancels = ['CANCELLED', 'CHARGEBACK', 'EXPIRED', 'REFUNDED', 'DISPUTE']; $exists = $this->hiring->findWhere([ ['payment_code', '=', $data['postback']['data']['purchase']['transaction']] ])->first(); $event = $data['postback']['data']['purchase']['status']; $type = in_array($event, $purchases) || in_array($event, $waitings); $overLimit = false; if ($enrollLimite > $enrolled || $studendHasActiveEnroll) { $status = $this->hotmartStatus($event); } else { $status = 7; $overLimit = true; } $course = $this->course->with(['plans.duration'])->find($data['course_id']); $plan = null; foreach ($course->plans as $row) { if ($row->pivot->default) { $plan = $row; } } $planId = (int)$plan->id; $planDuration = (int)$plan->duration->duration; if (!$exists && $type) { $alreadyHired = $this->hiredCourse ->orderBy('created_at', 'DESC') ->findWhere( [ [ 'user_id', '=', $data['student_id'] ], [ 'course_id', '=', $data['course_id'] ] ] ) ->first(); $renew = false; if (count($alreadyHired)) { $renew = true; } $startDate = date('Y-m-d H:i:s'); $addDate = '+' . $planDuration . ' months'; $endDate = date('Y-m-d 23:59:59', strtotime($addDate, strtotime($startDate))); $fee = 0; $net = 0; foreach ($data['postback']['data']['commissions'] as $commission) { if ($commission['source'] == 'MARKETPLACE') { $fee = $commission['value']; } elseif ($commission['source'] == 'PRODUCER') { $net = $commission['value']; } } $header = [ 'user_id' => $data['student_id'], 'gateway_id' => 'Hotmart', 'payment_code' => $data['postback']['data']['purchase']['transaction'], 'gross_amount' => $data['postback']['data']['purchase']['full_price']['value'], 'fee_amount' => $fee, 'net_amount' => $net, 'status' => $status, 'course_id' => $data['course_id'], 'plan_id' => $planId, 'start' => $startDate, 'end' => $endDate, 'payment_method' => $data['postback']['data']['purchase']['payment']['type'], 'installments' => $data['postback']['data']['purchase']['payment']['installments_number'] ?? 1, ]; $createHiring = $this->hiring->create($header); $hiredCourse = [ 'user_id' => $data['student_id'], 'course_id' => $data['course_id'], 'plan_id' => $planId, 'hirings_id' => $createHiring['id'], 'status' => $status, 'start' => $startDate, 'end' => $endDate, 'is_free' => false ]; if ($renew) { $this->hiredCourse->update($hiredCourse, $alreadyHired->id); } else { $this->hiredCourse->create($hiredCourse); } $hiredCourse = $this->hiring ->with(['user', 'plan.duration', 'course']) ->find($createHiring['id']); $hiredCourse['over_limit'] = $overLimit; \Event::fire(new HotmartDone($hiredCourse)); return ['success' => true, 'mensagem' => '']; } elseif ($exists && $type) { $alreadyHired = $this->hiredCourse ->orderBy('created_at', 'DESC') ->findWhere( [ [ 'user_id', '=', $data['student_id'] ], [ 'course_id', '=', $data['course_id'] ] ] ) ->first(); $startDate = date('Y-m-d H:i:s'); $addDate = '+' . $planDuration . ' months'; $endDate = date('Y-m-d 23:59:59', strtotime($addDate, strtotime($startDate))); $hiredCourse = [ 'user_id' => $data['student_id'], 'course_id' => $data['course_id'], 'plan_id' => $planId, 'hirings_id' => $alreadyHired->hirings_id, 'status' => $status, 'start' => $startDate, 'end' => $endDate, 'is_free' => false ]; $this->hiredCourse->update($hiredCourse, $alreadyHired->id); $hiredCourse = $this->hiring ->with(['user', 'plan.duration', 'course']) ->find($alreadyHired->hirings_id); $hiredCourse['over_limit'] = $overLimit; \Event::fire(new HotmartDone($hiredCourse)); return ['success' => true, 'mensagem' => '']; } elseif ($exists && in_array($event, $cancels)) { $alreadyHired = $this->hiredCourse ->orderBy('created_at', 'DESC') ->findWhere( [ [ 'user_id', '=', $data['student_id'] ], [ 'course_id', '=', $data['course_id'] ] ] ) ->first(); $exists->status = $status; $exists->save(); $alreadyHired->status = $status; $alreadyHired->save(); return ['success' => true, 'mensagem' => '']; } else { return ['success' => false, 'mensagem' => '']; } } /** * @param CheckoutService $checkoutService * @return string */ public function checkoutPagSeguro(CheckoutService $checkoutService) { $checkout = $checkoutService->createCheckoutBuilder(); $cart = $this->session->get('cart'); $itemName = str_slug($cart->get('name'), ' '); $lenItemName = strlen($itemName); $planName = ' - ' . str_slug($cart->get('plan')['name']); $lenPlanName = strlen($planName); $lenDesc = 99; $coupon = ''; $lenCoupon = 0; if (!empty($cart->get('coupon_code'))) { $getCoupon = $this->couponRepository->findByField('code', $cart->get('coupon_code'))->first(); $coupon = ' @$' . $getCoupon->id; $lenCoupon = strlen($coupon); } $planCode = ' @@' . $cart->get('plan_id'); $lenPlanCode = strlen($planCode); $itemLen = $lenDesc - ($lenPlanName + $lenCoupon + $lenPlanCode); if ($lenItemName > $itemLen) { $itemName = substr($itemName, 0, $itemLen); } $itemName = $itemName . $planName . $planCode . $coupon; $checkout->addItem( new Item( $cart->get('course_id'), $itemName, (($cart->get('price') + $cart->get('extra_amount')) - $cart->get('discount')), 1, $cart->get('shipping_price') ) ); $redirectUrl = \URL::to('/') . '/checkout/result/wait'; $notificationtUrl = \URL::to('/') . '/gateway/pagseguro'; $checkout->setRedirectTo($redirectUrl); $checkout->setNotificationURL($notificationtUrl); $shippingCode = 3; $hasShipping = $this->course->with(['material'])->find($cart->get('course_id'))->material->published; if ($hasShipping) { $addr = $this->auth->user()['address']; $address = new Address($addr->state, $addr->city, $addr->zip_code, $addr->neighborhood, $addr->street, $addr->number, $addr->complement); $shipping = new Shipping($shippingCode, $address, $cart->get('shipping_price')); $checkout->setShipping($shipping); } $checkout->setReference($this->auth->user()['email']); try { $response = $checkoutService->checkout($checkout->getCheckout()); } catch (PagSeguroException $p) { \Log::info($p->getMessage()); $cart->destroy(); $this->session->set('cart', $cart); return false; } catch (\Exception $e) { app(Handler::class)->report($e); $cart->destroy(); $this->session->set('cart', $cart); return false; } return $response->getRedirectionUrl(); } /** * @param $transaction_id * @return array */ public function completePagseguro($transaction_id) { $cart = $this->session->get('cart'); $data = $this->locator->getByCode($transaction_id); $hasShipping = false; $shippingCode = 0; $shippingCost = 0.00; /** @var Item $item */ $item = $data->getItems()->get(0); $course = $this->course->with(['plans', 'plans.duration', 'material'])->find($item->getId()); $couponCode = null; $coupon = null; $itemDescription = $item->getDescription(); if (strstr($itemDescription, '@$')) { $couponCode = intval(substr(strstr($itemDescription, '@$'), 2)); $coupon = $this->couponRepository->find($couponCode); $planId = intval(Helpers::getStringBetween($itemDescription, '@@', '@$')); } else { $planId = trim(substr(strstr($itemDescription, '@@'), 2)); } $plan = $course['plans']->where('id', $planId)->first(); $startDate = date('Y-m-d'); $addDate = '+' . $plan['duration']['duration'] . ' months'; $endDate = date('Y-m-d 23:59:59', strtotime($addDate, strtotime($startDate))); $header = [ 'user_id' => $this->auth->user()['id'], 'gateway_id' => 'PagSeguro', 'payment_code' => $data->getDetails()->getCode(), 'gross_amount' => $cart->getGrossAmount(), 'discount_amount' => $cart->getDiscountAmount(), 'fee_amount' => $data->getPayment()->getFeeAmount(), 'shipping_amount' => $shippingCost, 'extra_amount' => $cart->getExtraAmount(), 'net_amount' => $cart->getGrossAmount() - ($cart->getDiscountAmount() + $data->getPayment()->getFeeAmount()), 'shipping_type' => $shippingCode, 'status' => \GatHelper::translateGatewayStatus( 'PagSeguro', $data->getDetails()->getStatus() ), 'course_id' => $item->getId(), 'plan_id' => $planId, 'start' => $startDate, 'end' => $endDate ]; if (!is_null($coupon)) { $header['coupon_title'] = $coupon->title; $header['coupon_code'] = $coupon->code; $header['coupon_discount'] = $coupon->discount; } $createHiring = $this->hiring->create($header); /* Cursos contratados do Aluno */ $hiredCourse = [ 'user_id' => $this->auth->user()['id'], 'course_id' => $item->getId(), 'plan_id' => $planId, 'hirings_id' => $createHiring['id'], 'status' => \GatHelper::translateGatewayStatus( 'PagSeguro', $data->getDetails()->getStatus() ), 'start' => $startDate, 'end' => $endDate, 'is_free' => false ]; $renew = $this->hiredCourse->findWhere([ ['user_id', '=', $this->auth->user()['id']], ['course_id', '=', $cart->get('course_id')] ])->first(); if ($renew) { $this->hiredCourse->update($hiredCourse, $renew['id']); } else { $this->hiredCourse->create($hiredCourse); } $hiring = $this->hiring ->with(['user', 'plan.duration', 'course']) ->findByField('payment_code', $data->getDetails()->getCode()) ->first(); \Event::fire(new PagseguroDone('waiting', $hiring)); $courseData = [ 'course_id' => $cart->get('course_id'), 'plan_id' => $cart->get('plan_id') ]; if (!is_null($cart)) { $cart->destroy(); $this->session->set('cart', $cart); } return $courseData; } /** * @return mixed */ public function checkoutPaypal() { $config = $this->configurationServices->paypalConfiguration(); $gatewayFactory = new GatewayFactory; $gateway = $gatewayFactory->create('PayPal_Express'); $gateway->setUsername($config['email']); $gateway->setPassword($config['password']); $gateway->setSignature($config['token']); $gateway->setTestMode($config['sandbox'] == 1); $cart = $this->session->get('cart'); $paypal_setitems[] = [ 'name' => $cart->get('name'), 'quantity' => 1, 'price' => ($cart->get('price') + $cart->get('extra_amount')) ]; if ($cart->getDiscountAmount() > 0) { $paypal_setitems[] = [ 'name' => 'Cupom de Desconto', 'quantity' => 1, 'price' => $cart->getDiscountAmount() * -1 ]; } $response = $gateway->purchase( [ 'amount' => (($cart->getGrossAmount() + $cart->getShippingAmount()) - $cart->getDiscountAmount()), 'returnUrl' => route('checkout.paypal.success'), 'cancelUrl' => route('cart.list'), 'currency' => 'BRL', ]) ->setItems($paypal_setitems) ->setShippingAmount($cart->getShippingAmount()) ->send(); return $response; } /** * @return string|null */ public function completePayPal() { $gatewayFactory = new GatewayFactory; $gateway = $gatewayFactory->create('PayPal_Express'); $config = $this->configurationServices->paypalConfiguration(); $gateway->setUsername($config['email']); $gateway->setPassword($config['password']); $gateway->setSignature($config['token']); $gateway->setTestMode($config['sandbox'] == 1); $cart = $this->session->get('cart'); $paypal_setitems[] = [ 'name' => $cart->get('name'), 'quantity' => 1, 'price' => ($cart->get('price') + $cart->get('extra_amount')) ]; if ($cart->getDiscountAmount() > 0) { $paypal_setitems[] = [ 'name' => 'Cupom de Desconto', 'quantity' => 1, 'price' => $cart->getDiscountAmount() * -1 ]; } $response = $gateway->completePurchase( [ 'amount' => (($cart->getGrossAmount() + $cart->getShippingAmount()) - $cart->getDiscountAmount()), 'returnUrl' => route('checkout.paypal.success'), 'cancelUrl' => route('cart.list'), 'currency' => 'BRL', ]) ->setShippingAmount($cart->getShippingAmount()) ->setItems($paypal_setitems) ->send(); $paypalResponse = $response->getData(); // this is the raw response object $errorMsg = null; if (isset($paypalResponse['PAYMENTINFO_0_ACK']) && $paypalResponse['PAYMENTINFO_0_ACK'] === 'Success') { $course = $this->course->with(['plans', 'plans.duration', 'material'])->find($cart->get('course_id')); $planId = $cart->get('plan_id'); $plan = $course['plans']->where('id', (int)$planId)->first(); $startDate = date('Y-m-d'); $addDate = '+' . $plan['duration']['duration'] . ' months'; $endDate = date('Y-m-d 23:59:59', strtotime($addDate, strtotime($startDate))); $paymentCode = $paypalResponse['PAYMENTINFO_0_TRANSACTIONID'] ?? $paypalResponse['TRANSACTIONID']; $feeAmount = $paypalResponse['PAYMENTINFO_0_FEEAMT'] ?? $paypalResponse['FEEAMT']; $paymentStatus = $paypalResponse['PAYMENTINFO_0_PAYMENTSTATUS'] ?? $paypalResponse['PAYMENTSTATUS']; $header = [ 'user_id' => $this->auth->user()['id'], 'gateway_id' => 'PayPal', 'payment_code' => $paymentCode, 'gross_amount' => $cart->getGrossAmount() + $cart->getShippingAmount(), 'discount_amount' => $cart->getDiscountAmount(), 'fee_amount' => $feeAmount, 'shipping_amount' => $cart->getShippingAmount(), 'extra_amount' => $cart->getExtraAmount(), 'net_amount' => $cart->getGrossAmount() - ($cart->getDiscountAmount() + $feeAmount), 'shipping_type' => $cart->getShippingCode(), 'status' => \GatHelper::translateGatewayStatus( 'PayPal', $paymentStatus ), 'course_id' => $cart->get('course_id'), 'plan_id' => $planId, 'start' => $startDate, 'end' => $endDate, 'coupon_title' => $cart->get('coupon_name'), 'coupon_code' => $cart->get('coupon_code'), 'coupon_discount' => $cart->get('coupon_discount') ]; $createHiring = $this->hiring->create($header); /* Cursos contratados do Aluno */ $hiredCourse = [ 'user_id' => $this->auth->user()['id'], 'course_id' => $cart->get('course_id'), 'plan_id' => $cart->get('plan_id'), 'hirings_id' => $createHiring['id'], 'status' => \GatHelper::translateGatewayStatus( 'PayPal', $paymentStatus ), 'start' => $startDate, 'end' => $endDate, 'is_free' => false ]; $renew = $this->hiredCourse->findWhere([ ['user_id', '=', $this->auth->user()['id']], ['course_id', '=', $cart->get('course_id')] ])->first(); if ($renew) { $this->hiredCourse->update($hiredCourse, $renew['id']); } else { $this->hiredCourse->create($hiredCourse); } $hiredCourse = $this->hiring ->with(['user', 'plan.duration', 'course']) ->findByField('payment_code', $paymentCode) ->first(); \Event::fire(new PaypalDone('aproved', $hiredCourse)); $cart->destroy(); $this->session->set('cart', $cart); } else { $deniedInfo = [ 'status' => 7, 'user' => [ 'email' => $this->auth->user()->email, 'name' => $this->auth->user()->name ] ]; \Event::fire(new PaypalDone('denied', $deniedInfo)); $cart->destroy(); $this->session->set('cart', $cart); $errorMsg = $paypalResponse['L_ERRORCODE0'] . ' - ' . $paypalResponse['L_LONGMESSAGE0']; } return $errorMsg; } /** * Verifica se um curso do carrinho está ativo na conta do usuário logado; * Caso o curso esteja ativo, o mesmo é removido do carrinho, evitando que o usuário compre um curso que já está comprado e ativo. * @param $cart * @return array */ public function checkHiredCourses($cart) { $result = []; $hasCourse = $this->hiredCourse->findWhere( [ [ 'user_id', '=', $this->auth->user()['id'] ], [ 'course_id', '=', $cart->get('course_id') ], [ 'end', '>', date('Y-m-d H:i:s') ], [ 'status', '<=', 4 ] ] ); if (count($hasCourse->all()) > 0) { $result[0] = ['id' => $cart->get('course_id'), 'course' => $cart->get('name')]; $cart->destroy(); $this->session->set('cart', $cart); } return $result; } /** * @param $event * @return int */ public function hotmartStatus($event) { switch ($event) { case 'PRINTED_BILLET': $status = 1; break; case 'BILLET_PRINTED': $status = 1; break; case 'PROCESSING_TRANSACTION': $status = 1; break; case 'WAITING_PAYMENT': $status = 1; break; case 'STARTED': $status = 1; break; case 'UNDER_ANALISYS': $status = 2; break; case 'APPROVED': $status = 3; break; case 'COMPLETE': $status = 3; break; case 'DISPUTE': $status = 5; break; case 'CANCELLED': $status = 7; break; case 'CHARGEBACK': $status = 7; break; case 'EXPIRED': $status = 7; break; case 'REFUNDED': $status = 7; break; default : $status = 0; break; } return $status; } }
Copyright © 2026 - UnknownSec