HEX
Server: Apache
System: Linux amsngx344.inmotionhosting.com 4.18.0-553.40.1.lve.el8.x86_64 #1 SMP Wed Feb 12 18:54:57 UTC 2025 x86_64
User: raniho6 (2736)
PHP: 8.1.34
Disabled: NONE
Upload Files
File: /home/raniho6/public_html/raniworks.stream/app/Http/Controllers/XenditPaymentController.php
<?php

namespace App\Http\Controllers;

use Xendit\Xendit;
use App\Models\Plan;
use App\Models\User;
use App\Models\Order;
use App\Models\Client;
use App\Models\Coupon;
use App\Models\Invoice;
use App\Models\Project;
use App\Models\Utility;
use App\Models\UserCoupon;
use Illuminate\Http\Request;
use App\Models\InvoicePayment;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Session;

class XenditPaymentController extends Controller
{
    public function setPaymentDetail_client($invoice_id)
    {
        $invoice = Invoice::find($invoice_id);
        if (Auth::user() != null) {
            $this->user = Auth::user();
        } else {
            $this->user = Client::where('id', $invoice->client_id)->first();
        }

        $payment_setting = Utility::getPaymentSetting($this->user->currentWorkspace->id);
        $this->currancy = (isset($this->user->currentWorkspace->currency_code)) ? $this->user->currentWorkspace->currency_code : 'USD';
        $this->xendit_api_key = isset($payment_setting['xendit_api_key']) ? $payment_setting['xendit_api_key'] : '';
        $this->xendit_token = isset($payment_setting['xendit_token']) ? $payment_setting['xendit_token'] : '';
        // dd($this->currancy,
        // $this->xendit_api_key, 
        // $this->xendit_token);


    }

    public function invoicePayWithXendit(Request $request, $slug, $invoice_id)
    {
        $this->setPaymentDetail_client($invoice_id);
        $user_auth = Auth::user();
        $get_amount = $request->amount;
        $orderID = strtoupper(str_replace('.', '', uniqid('', true)));
        $invoice = Invoice::find($invoice_id);
        $client_keyword = isset($user_auth) ? (($user_auth->getGuard() == 'client') ? 'client.' : '') : '';


        try {
            if ($invoice) {
                $xendit_token = $this->xendit_token;
                $xendit_api = $this->xendit_api_key;
                $currency = $this->currancy;

                $response = ['orderId' => $orderID, 'user' => $this->user, 'get_amount' => $get_amount, 'invoice' => $invoice_id, 'currency' => $currency];

                Xendit::setApiKey($this->xendit_api_key);

                $params = [
                    'external_id' => $orderID,
                    'payer_email' => $this->user->email,
                    'description' => 'Payment for order ' . $orderID,
                    'amount' => $get_amount,
                    'callback_url' => route($client_keyword . 'invoice.xendit.status', $slug),
                    'success_redirect_url' => route($client_keyword . 'invoice.xendit.status',$slug) . '?' . http_build_query($response),
                ];
                $Xenditinvoice = \Xendit\Invoice::create($params);

                Session::put('invoicepay', $Xenditinvoice);

                return redirect($Xenditinvoice['invoice_url']);

            } else {
                return redirect()->back()->with('error', 'Invoice not found.');
            }
        } catch (\Throwable $e) {
            dd($e);
            return redirect()->back()->with('error', __($e));
        }
    }

    public function getInvoicePaymentStatus(Request $request,$slug)
    {
        $session = Session::get('invoicepay');
        $invoiceId = $request->invoice;
        $this->setPaymentDetail_client($invoiceId);
        $user_auth = Auth::user();
        $client_keyword = isset($user_auth) ? (($user_auth->getGuard() == 'client') ? 'client.' : '') : '';
        $amount = $request->get_amount;
    //    dd($session,$request->all(),$this->user,$slug);

       if (!empty($invoiceId)) {
        $invoice = Invoice::find($invoiceId);

        if (!empty($invoice)) {
            $currentWorkspace = Utility::getWorkspaceBySlug($slug);
            $invoice_payment = new InvoicePayment();
            $invoice_payment->order_id = $request->orderId;
            $invoice_payment->invoice_id = $invoiceId;
            $invoice_payment->currency = isset($currentWorkspace->currency_code) ? $currentWorkspace->currency_code : 'USD';
            $invoice_payment->amount = $amount;
            $invoice_payment->payment_type = 'Xendit';
            $invoice_payment->receipt = '';
            $invoice_payment->client_id = $this->user->id;
            $invoice_payment->txn_id = '';
            $invoice_payment->payment_status = 'approved';
            $invoice_payment->save();


            if (($invoice->getDueAmount() - $invoice_payment->amount) == 0) {
                $invoice->status = 'paid';
                $invoice->save();
            } else {

                $invoice->status = 'partialy paid';
                $invoice->save();
            }


            $user1 = $currentWorkspace->id;
            $settings = Utility::getPaymentSetting($user1);
            $total_amount = $invoice->getDueAmounts($invoice->id);
            $client = Client::find($invoice->client_id);
            $project_name = Project::where('id', $invoice->project_id)->first();
            
            $uArr = [
                // 'user_name' => $user->name,
                'project_name' => $project_name->name,
                'company_name' => User::find($project_name->created_by)->name,
                'invoice_id' => Utility::invoiceNumberFormat($invoice->id),
                'client_name' => $client->name,
                'total_amount' => $total_amount,
                'paid_amount' => $request->amount,
            ];
            if (isset($settings['invoicest_notificaation']) && $settings['invoicest_notificaation'] == 1) {
                Utility::send_slack_msg('Invoice Status Updated', $user1, $uArr);
            }

            if (isset($settings['telegram_invoicest_notificaation']) && $settings['telegram_invoicest_notificaation'] == 1) {
                Utility::send_telegram_msg('Invoice Status Updated', $uArr, $user1);
            }

            //webhook
            $module = 'Invoice Status Updated';
            $webhook = Utility::webhookSetting($module, $user1);

            // $webhook=  Utility::webhookSetting($module);
            if ($webhook) {
                $parameter = json_encode($invoice);
                // 1 parameter is  URL , 2 parameter is data , 3 parameter is method
                $status = Utility::WebhookCall($webhook['url'], $parameter, $webhook['method']);
                // if($status == true)
                // {
                //     return redirect()->back()->with('success', __('Payment added Successfully!'));
                // }
                // else
                // {
                //     return redirect()->back()->with('error', __('Webhook call failed.'));
                // }
            }
            if (Auth::check()) {
                return redirect()->route(
                    $client_keyword.'invoices.show',
                    [
                        $slug,
                        $invoice->id,
                    ]
                )->with('success', __('Payment added Successfully'));
            } else {
                return redirect()->route('pay.invoice', [$slug, \Illuminate\Support\Facades\Crypt::encrypt($invoice->id)])->with('success', __('Payment added Successfully'));
            }


        } else {
            if (\Auth::check()) {
                return redirect()->route(
                    $client_keyword . 'invoices.show',
                    [
                        $slug,
                        $invoiceId,
                    ]
                )->with('error', __('Invoice not found.'));
            } else {
                return redirect()->route('pay.invoice', [$slug, \Illuminate\Support\Facades\Crypt::encrypt($invoiceId)])->with('error', __('Invoice not found!'));
            }
        }

    } else {
        if (\Auth::check()) {
            return redirect()->route(
                $client_keyword . 'invoices.show',
                [
                    $slug,
                    $invoiceId,
                ]
            )->with('error', __('Invoice not found.'));
        } else {
            return redirect()->route('pay.invoice', [$slug, \Illuminate\Support\Facades\Crypt::encrypt($invoiceId)])->with('error', __('Invoice not found!'));
        }

    }
    }
}