UnknownSec Bypass
403
:
/
mnt
/
lmsestudio-instance-vol002
/
lms_ea1addfbb834
/
app
/
Http
/
Controllers
/
Cart
/ [
drwxr-xr-x
]
Menu
Upload
Mass depes
Mass delete
Terminal
Info server
About
name :
PagarMeController.php
<?php namespace EstudioLMS\Http\Controllers\Cart; use Carbon\Carbon; use EstudioLMS\Cart\Cart; use EstudioLMS\Events\PagarMeDone; use EstudioLMS\Http\Controllers\Controller; use EstudioLMS\Repositories\Auth\CustomerInterface; use EstudioLMS\Repositories\Auth\UserRepository; use EstudioLMS\Repositories\Config\PagarmeRecipientInterface; use EstudioLMS\Repositories\Courses\Course\CourseRepository; use EstudioLMS\Repositories\Environment\HiredCourseRepository; use EstudioLMS\Repositories\Financial\PayableInterface; use EstudioLMS\Repositories\Financial\PostbackInterface; use EstudioLMS\Services\Admin\ConfigurationServices; use EstudioLMS\Services\Hires\HiringServices; use EstudioLMS\Services\PagarMeService; use Illuminate\Contracts\Auth\Guard; use Illuminate\Http\Request; use Log; use PagarMe\Sdk\ClientException; use PagarMe\Sdk\PagarMe; use Symfony\Component\HttpFoundation\Session\SessionInterface; /** * Class PagarMeController * @package EstudioLMS\Http\Controllers\Cart */ class PagarMeController extends Controller { /** * @var Cart */ private $cart; /** * @var SessionInterface */ private $session; /** * @var PagarMeService */ private $pagarMeService; /** * @var CustomerInterface */ private $customer; /** * @var CourseRepository */ private $course; /** * @var ConfigurationServices */ private $configurationServices; /** * @var UserRepository */ private $user; /** * @var HiringServices */ private $hiringServices; /** * @var PagarmeRecipientInterface */ private $pagarmeRecipient; /** * @var HiredCourseRepository */ private $hiredCourse; /** * @var Guard */ private $auth; /** * @var PayableInterface */ private $payable; /** * @var PostbackInterface */ private $postback; /** * PagarMeController constructor. * @param Cart $cart * @param SessionInterface $session * @param PagarMeService $pagarMeService * @param CustomerInterface $customer * @param CourseRepository $course * @param ConfigurationServices $configurationServices * @param UserRepository $user * @param HiringServices $hiringServices * @param PagarmeRecipientInterface $pagarmeRecipient * @param HiredCourseRepository $hiredCourse * @param Guard $auth * @param PayableInterface $payable * @param PostbackInterface $postback */ public function __construct( Cart $cart, SessionInterface $session, PagarMeService $pagarMeService, CustomerInterface $customer, CourseRepository $course, ConfigurationServices $configurationServices, UserRepository $user, HiringServices $hiringServices, PagarmeRecipientInterface $pagarmeRecipient, HiredCourseRepository $hiredCourse, Guard $auth, PayableInterface $payable, PostbackInterface $postback ) { $this->cart = $cart; $this->session = $session; $this->pagarMeService = $pagarMeService; $this->customer = $customer; $this->course = $course; $this->configurationServices = $configurationServices; $this->user = $user; $this->hiringServices = $hiringServices; $this->pagarmeRecipient = $pagarmeRecipient; $this->hiredCourse = $hiredCourse; $this->auth = $auth; $this->payable = $payable; $this->postback = $postback; } /** * @param Request $request * @return \Illuminate\Http\RedirectResponse */ public function checkoutPagarMe(Request $request) { $config = $this->configurationServices->configuration(); $cart = $this->getCart(); $data = $request->all(); $price = ($cart->getGrossAmount() - $cart->getDiscountAmount()) + $cart->getShippingAmount() + $cart->getInstallmentInterest(); if ($price < 1) { $error = 'Valor não permitido. O gateway não aceita transações menores que R$ 1,00'; $success = 'false'; return redirect()->route('checkout.result', $success) ->with('error', $error) ->with('boletoUrl', null); } $interestAmount = 0; $data['card_hash_id'] = isset($data['card_hash']) ? null : $data['card_hash_id'] ?? null; $user = $this->user->with(['customer' => function ($query) use ($data) { return $query->where('card_id', '=', $data['card_hash_id'])->first(); }, 'address' ])->find(\Auth::user()->id); if (count($user->customer) > 0) { $customerId = $user->customer[0]->customer_id; } else { $customer = $this->pagarMeService->registerCustomer($user); $customerId = $customer->getId(); $this->customer->create( [ 'user_id' => $user->id, 'customer_id' => $customerId, 'card_id' => null, 'hash' => null, 'default' => true, ] ); } $boletoUrl = null; $boletoBarCode = null; if ($data['pay_type'] == 'C') { if (!is_null($data['card_hash_id'])) { $cardId = $user->customer[0]->card_id; $cvv = \Crypt::decrypt($user->customer[0]->hash); } else { $card = $this->pagarMeService->storeCardByHashCURL($data['card_hash'], $customerId); $this->customer->setAllDefaultFalse($user->id); $newCard = $this->customer->create([ 'user_id' => $user->id, 'customer_id' => $customerId, 'card_id' => $card['id'], 'hash' => \Crypt::encrypt($data['hash']), 'default' => true ]); $cardId = $card['id']; $cvv = $data['hash']; } $courseId = $cart->get('course_id'); $course = $this->course->with(['plans', 'plans.duration', 'material'])->find($courseId); $installmentInterest = 0.00; if ($data['pay_installment'] > 1) { $noInterest = $course->gateways[0]->pivot->installments; $cartAmount = (double)$cart->get('price') + (double)$cart->get('extra_amount') + (double)$cart->get('shipping_price'); $cartAmount = $cartAmount - (double)$cart->getDiscountAmount(); $amount = strval($cartAmount * 100); $installments = $this->pagarMeService->installments($amount, $noInterest, 12, 1); $installments = json_decode(json_encode($installments), true); $installments = $installments['installments']; $installment = $installments[$data['pay_installment']]; $instalmentAmount = (double)$installment['amount'] / 100; $cartValue = ($cart->getGrossAmount() - $cart->getDiscountAmount()) + $cart->getShippingAmount(); $installmentInterest = $instalmentAmount - $cartValue; $cart->applyInstallment($installment['installment'], $installmentInterest); } $cart = $this->getCart(); $postBackUrl = null; $softDescriptor = (strlen($config->site_name) >= 13) ? substr($config->site_name, 0, 13) : $config->site_name; try { $transaction = $this->pagarMeService->cardTransaction( $cardId, $cart, null, null, $postBackUrl, $softDescriptor ); $status = $transaction->getStatus(); $error = null; if ($status == 'paid') { $this->hiringTransaction( $transaction, $cart, $status, $cardId, $installmentInterest ); $success = 'true'; $mailData = $this->hiringServices->getByPaymentCode($transaction->getId()); \Event::fire(new PagarMeDone('credit_card', $mailData)); } else { $error = $transaction->getRefuseReason(); \Event::fire(new PagarMeDone('credit_card_refused', $error)); $success = 'false'; } } catch (ClientException $e) { $error = $e->getCode() . ' - ' . $e->getMessage(); $success = 'false'; } } else { $postBackUrl = null; try { $transaction = $this->pagarMeService->boletoTransaction( $cart, null, null, $postBackUrl ); $error = null; if (!empty($transaction->getBoletoUrl())) { $status = $transaction->getStatus(); $this->hiringTransaction($transaction, $cart, $status); $success = 'true'; $mailData = $this->hiringServices->getByPaymentCode($transaction->getId()); $mailData['boleto_url'] = $transaction->getBoletoUrl(); $mailData['boleto_barcode'] = $transaction->getBoletoBarcode(); \Event::fire(new PagarMeDone('boleto', $mailData)); } else { $error = $transaction->getRefuseReason(); $success = 'false'; \Event::fire(new PagarMeDone('boleto_refused', $error)); } } catch (ClientException $e) { $error = $e->getCode() . ' - ' . $e->getMessage(); $success = 'false'; } $boletoUrl = $transaction->getBoletoUrl(); $boletoBarCode = $transaction->getBoletoBarcode(); } if (!is_null($cart)) { $cart->destroy(); $this->session->set('cart', $cart); } return redirect()->route('checkout.result', $success) ->with('error', $error) ->with('boletoUrl', $boletoUrl); } /** * Pega o carrinho de compras da sessão caso exista, se não cria um novo carrinho * * * @return mixed */ private function getCart() { if ($this->session->has('cart')) { $cart = $this->session->get('cart'); } else { $cart = $this->cart; } return $cart; } /** * @param \PagarMe\Sdk\Transaction\BoletoTransaction|\PagarMe\Sdk\Transaction\CreditCardTransaction $transaction * @param Cart $cart * @param $status * @param null $cardId * @param float $interestAmount */ public function hiringTransaction( $transaction, $cart, $status, $cardId = null, $interestAmount = 0.00 ) { $courseId = $cart->get('course_id'); $planId = $cart->get('plan_id'); $course = $this->course->with(['plans', 'plans.duration', 'material'])->find($courseId); $plan = $course['plans']->where('id', (int)$planId)->first(); $totalFeeAmount = 0; $startDate = date('Y-m-d'); $addDate = '+' . $plan['duration']['duration'] . ' months'; $endDate = date('Y-m-d 23:59:59', strtotime($addDate, strtotime($startDate))); //$endDate = date('Y-m-d ', strtotime($addDate, strtotime($startDate))); $recipientESId = config('pagar_me.PAGAR_ME_RECIPIENT_ID'); $payables = null; $counter = 0; if ($transaction->getStatus() == 'paid') { while (count($payables) <= 1) { $payables = $this->pagarMeService->getTransactionPayableValues($transaction->getId()); $counter++; if ($counter > 20) { break; } } for ($i = 0; $i <= count($payables) - 1; $i++) { if ($payables[$i]['recipient_id'] == $recipientESId) { $totalFeeAmount += $payables[$i]['amount']; } else { $totalFeeAmount += $payables[$i]['fee']; } } $totalFeeAmount = $totalFeeAmount / 100; } if ($transaction->getPaymentMethod() == 'boleto') { $installments = 1; } else { $installments = $transaction->getInstallments(); } $grossAmount = $cart->getGrossAmount(); $discountAmount = $cart->getDiscountAmount(); $netAmount = ($grossAmount - ($totalFeeAmount + $discountAmount)) + $interestAmount; $header = [ 'user_id' => $this->auth->user()['id'], 'gateway_id' => 'PagarMe', 'payment_code' => $transaction->getId(), 'payment_method' => $transaction->getPaymentMethod(), 'gross_amount' => $grossAmount, 'discount_amount' => $discountAmount, 'fee_amount' => $totalFeeAmount, 'shipping_amount' => $cart->getShippingAmount(), 'extra_amount' => $cart->getExtraAmount(), 'net_amount' => $netAmount, 'shipping_type' => $cart->getShippingCode(), 'status' => \GatHelper::translateGatewayStatus( 'PagarMe', $transaction->getStatus() ), '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'), 'installments' => $installments, 'installment_interest' => $interestAmount ]; $createHiring = $this->hiringServices->store($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( 'PagarMe', $transaction->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); } if (count($payables) > 0) { $recipient = $this->pagarmeRecipient->firstRecord(); //Ordenando o array de payables por recipient_id e payment_date $recipId = array_column($payables, 'recipient_id'); $date = array_column($payables, 'payment_date'); array_multisort($recipId, SORT_ASC, $date, SORT_ASC, $payables); //Filtrando o array de payables, para pegar apenas os registros do recebedor principal $filterBy = $recipient->pagarme_recipient_id; $arrayRecipients = array_filter($payables, function ($arr) use ($filterBy) { return ($arr['recipient_id'] == $filterBy); }); foreach ($arrayRecipients as $key => $arrayRecipient) { $tempArray = array_filter($payables, function ($arr) use ($arrayRecipient, $recipientESId) { return $arr['recipient_id'] == $recipientESId && $arr['payment_date'] == $arrayRecipient['payment_date']; }); //$key = array_keys($tempArray)[0]; $sumFeeAmount = $arrayRecipient['fee']; $netAmount = $arrayRecipient['amount'] - $arrayRecipient['fee']; $payableStatus = $arrayRecipient['status']; $paymentDate = Carbon::parse($arrayRecipient['payment_date'])->format('Y-m-d H:s:i'); $payableData = [ 'subscription_hash' => $transaction->getId(), 'payment_code' => $transaction->getId(), 'payment_type' => $transaction->getPaymentMethod(), 'payment_date' => $paymentDate, 'card_id' => $cardId, 'gross_amount' => $arrayRecipient['amount'] / 100, 'discount_amount' => 0, 'fee_amount' => $sumFeeAmount / 100, 'extra_amount' => 0, 'net_amount' => $netAmount / 100, 'payable_status_id' => $payableStatus == 'paid' ? 2 : 1 ]; $payable = $this->payable->findWhere([ ['subscription_hash', '=', $transaction->getId()], ['payment_code', '=', $transaction->getId()], ['payment_date', '=', $paymentDate], ['payable_status_id', '=', $payableData['payable_status_id']] ])->first(); if ($payable) { $payable->fill($payableData); $payable->save(); } else { $payable = $this->payable->create($payableData); } } } } /** * @param Request $request */ public function postback(Request $request) { $apiKey = config('pagar_me.PAGAR_ME_API_KEY'); $pagarMe = new PagarMe($apiKey); $signature = $request->headers->get('x-hub-signature'); $rawBody = $request->getContent(); if ($pagarMe->postback()->validateRequest($rawBody, $signature)) { $this->pagarMeService->postback($request); } else { $fillName = uniqid() . '-invalid.txt'; $newFile = fopen($fillName, 'w'); fwrite($newFile, $rawBody); fwrite($newFile, $signature); fclose($newFile); } } }
Copyright © 2026 - UnknownSec