HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux ip-172-31-42-149 5.15.0-1084-aws #91~20.04.1-Ubuntu SMP Fri May 2 07:00:04 UTC 2025 aarch64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/vhost/disk-apps/qas.sports-crowd.com/app/Http/Controllers/Api/MembershipsApiController.php
<?php

namespace App\Http\Controllers\Api;

use App\Core\Academy\Application\MembershipOrderService;
use App\Core\Academy\Application\MembershipPaymentService;
use App\Core\Membership\Application\MembershipService;
use App\Core\Payment\Application\PaymentTransactionService;
use App\Core\Payment\PaymentMethodContext;
use App\Core\Servientrega\Application\ServientregaService;
use App\Core\User\Application\UserService;
use App\Http\Controllers\Controller;
use App\Http\Controllers\WompiApiController;
use App\Models\Membership\Membership;
use App\Models\Membership\MembershipSubscriber;
use App\Models\UserPaymentSource;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class MembershipsApiController extends Controller
{
    private $paymentTransactionService;
    private $membershipPaymentService;
    private $membershipService;
    private $membershipOrderService;
    private $paymentContext;
    private $userService;
    private $servientregaService;

    public function __construct()
    {
        $this->paymentTransactionService = new PaymentTransactionService();
        $this->membershipPaymentService = new MembershipPaymentService();
        $this->membershipService = new MembershipService();
        $this->membershipOrderService = new MembershipOrderService();
        $this->userService = new UserService();
        $this->servientregaService = new ServientregaService();
    }
    public function index()
    {
        $data = Membership::where('active', true)->orderBy('id', 'desc')->paginate();
        return response()->json($data);
    }

    public function show(Request $request, Membership $membership)
    {
        return response()->json(['data' => $membership]);
    }

    public function membershipSubscribe(Request $request, Membership $membershipId)
    {
        $user = $request->user();
        $membershipSubscriber = $this->membershipService->createMembershipSubscription($membershipId, $user);
        return response()->json($membershipSubscriber);
    }

    public function createMembershipOrder(Request $request, Membership $membershipId)
    {
        $subscriberData = [];
        $billingData = $request->validate([
            'billing_first_name' => 'required',
            'billing_last_name' => 'required',
            'billing_document_type_id' => 'required',
            'billing_document' => 'required',
            'billing_phone' => 'required',
            'billing_email' => 'required',
        ]);

        if (!is_numeric($request->input('subscriber_id'))) {
            $validationRules = [
                'subscriber_id' => 'required',
                'subscriber_first_name' => 'required',
                'subscriber_last_name' => 'required',
                'subscriber_email' => '',
                'subscriber_document_type_id' => 'required',
                'subscriber_document' => 'required',
                'subscriber_phone' => 'required',
            ];
            if ($request->input('subscriber_id') == 'other') {
                $validationRules['subscriber_email'] = 'required';
            }
            $subscriberData = $request->validate($validationRules);
        } else {
            $subscriberData = $request->validate([
                'subscriber_id' => 'required'
            ]);
        }

        $user = $request->user();

        try {
            DB::beginTransaction();
            $membershipSubscriber = $this->membershipOrderService->createMembershipOrder(
                $membershipId,
                $user,
                $billingData,
                $subscriberData
            );
            DB::commit();
            return response()->json($membershipSubscriber);
        } catch (\Throwable $th) {
            DB::rollBack();
            throw $th;
        }
    }

    public function renew()
    {
        $expiredMemberships = MembershipSubscriber::where('end_date', '<', now())
            ->where('is_renewal', true)
            ->get();

        foreach ($expiredMemberships as $expiredMembership) {
            $this->membershipPayment($expiredMembership);
        }

        return 'done!';
    }

    function membershipPayment($expiredMembership)
    {
        $membershipPayment = $this->membershipService->createFromMembershipSubscription($expiredMembership);
        $this->membershipService->setAsNoRenewable($expiredMembership);

        $this->paymentContext = PaymentMethodContext::init(
            $expiredMembership->payment_transaction->gateway_payments_id
        );

        $paymentTransaction = $this->membershipPaymentService->getPaymentTransaction($membershipPayment);

        $this->paymentTransactionService->setPaymentGatewayId(
            $paymentTransaction,
            $expiredMembership->payment_transaction->gateway_payments_id
        );

        $paymentProcessorResponse = $this->paymentContext->payMembership($membershipPayment);

        $this->paymentTransactionService->setPaymentGatewayTxId(
            $membershipPayment->payment_transaction,
            $paymentProcessorResponse->paymentGatewayTxId()
        );

        $payment = $this->paymentTransactionService->buildPayment($membershipPayment->payment_transaction);
        $paymentSources = UserPaymentSource::select('payment_source')
            ->where('user_id', $payment->customer()->id())
            ->where('gateway_payment_id', $payment->paymentGatewayId())
            ->get();

        $transactionData = $paymentProcessorResponse->data();

        foreach ($paymentSources as $paymentSource) {
            $paymentRequest = new Request();
            $paymentRequest->replace([
                'reference' => $payment->reference(),
                'full_name' => $payment->customer()->fullName(),
                'email' => $payment->customer()->email(),
                'phone_number' => $payment->customer()->mobile(),
                'payment_method' => 'CARD',
                'payment_source' => $paymentSource->payment_source->id,
                'installments' => 1,
                'accept_wompi_termcons' => $transactionData['presigned_acceptance']
            ]);

            $wompiController = new WompiApiController();
            $wompiController->doPayment($paymentRequest);

            $membershipPayment->refresh();
            $this->paymentTransactionService->validatePayment($membershipPayment->payment_transaction);
        }
    }

    public function processExpired()
    {
        $expiredMemberships = MembershipSubscriber::where('end_date', '<', now())
            ->where('is_renewal', true)->get();

        foreach ($expiredMemberships as $expiredMembership) {
            $this->membershipService->revokeMembership($expiredMembership);
        }

        return 'done!';
    }

    public function getDeliveryPrice(Request $request, Membership $membership)
    {
        $lastUsedAddress = $this->userService->getLastUsedAddress($request->user());
        $deliveryPrice = $this->servientregaService->getDeliveryPrice($lastUsedAddress);
        return response()->json(['data' => $deliveryPrice]);
    }
}