Your IP : 216.73.216.220


Current Path : /var/www/html/app/code/FlipsDigital/HappyMind/Controller/Order/
Upload File :
Current File : /var/www/html/app/code/FlipsDigital/HappyMind/Controller/Order/Notification.php

<?php
namespace FlipsDigital\HappyMind\Controller\Order;
use FlipsDigital\HktGateway\Controller\Auth\Jwt;

class Notification extends \Magento\Framework\App\Action\Action
{
    private $orderRepository;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \FlipsDigital\HktGateway\Model\PaymentLogFactory $paymentLogFactory,
        \FlipsDigital\CustomerTextbookList\Model\CustomerTextbookListFactory $customerTextbookListFactory,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
    )
    {
        $this->_paymentLogFactory = $paymentLogFactory;
        $this->_ctlFactory = $customerTextbookListFactory;
        $this->_scopeConfig = $scopeConfig;
        $this->_orderRepository = $orderRepository;
        parent::__construct($context);
    }

    public function execute()
    {
        $customer_textbook_id = $this->getRequest()->getParam('id');
        // $textbook_id = '';
        $magento_order_id = $this->getRequest()->getParam('magento_order_id');
        $objPaymentLog = $this->_paymentLogFactory->create();
        $logData = [];
        $logData['create_at'] = date('Y-m-d H:i:s');

        if(isset($_SERVER['HTTP_X_HUB_SIGNATURE'])){

            $base64encodedsecret = $this->_scopeConfig->getValue('payment_setting/app_api/app_secret', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
            $postdata = file_get_contents("php://input");
            if ( hash_equals('sha512=' . hash_hmac('sha512', preg_replace("/\s+/", "", $postdata), base64_decode($base64encodedsecret)), $_SERVER['HTTP_X_HUB_SIGNATURE'] )) {
            
                $data = json_decode($postdata, true);
                $logData['identifier'] = 'Order Updated: '.$data['payload']['orderId'];
                $logData['magento_order_id'] = $magento_order_id;
                $logData['request_data'] = $postdata;

                // Update Order
                $arrCTLData = [];
                $objCTL = $this->_ctlFactory->create();
                $objCustomerTextbookList = $objCTL->load($customer_textbook_id);
                $arrCTLData = $objCustomerTextbookList->getData();
                $arrCTLData['order_id'] = $magento_order_id;
                $objCustomerTextbookList->setData($arrCTLData);
                $objCustomerTextbookList->save();

                // Update Magento Order Status
                $completeStatus = \Magento\Sales\Model\Order::STATE_COMPLETE;
                $order = $this->_orderRepository->get($magento_order_id);
                $order->setStatus($completeStatus)->setState($completeStatus);
                $this->_orderRepository->save($order);
            } else {
                $logData['identifier'] = 'Failed: signature invalid';
            }
        }else{
            $logData['identifier'] = 'Failed: header invalid';
        }
        $objPaymentLog->setData($logData);
        $objPaymentLog->save();


        // // $queryData = [
        // //     'id' => 1,
        // //     'textbook' => 2,
        // // ];
        // // $queryData['magento_order_id'] = 3;
        // // echo $this->_buildUrl('*/*/notification', $queryData);
        // // $post = $this->getRequest()->getPostValue();
        // $customer_textbook_id = '18';
        // $textbook_id = '';
        // $magento_order_id = '35';
        // $payload = '{"payload":{"orderId":"6e08ce87-00b8-46e1-a06eb1da00559f95","currency":"HKD","capturedChargeTotal":0.1,"transactions":[{"orderId":"6e08ce87-00b8-46e1-a06e-b1da00559f95","merchantTransactionId":"BTU-TEST00088","transactionId":"1718234981836259328","currency":"HKD","chargeTotal":0.1,"transactionType":"SALE","transactionState":"COMPLETED","transactionTimestamp":"2020-07-29T08:33:06.000+0000","submissionComponent":"API","paymentUrl":"https://mapi.alipay.com/gateway.do?_input_charset=UTF-8&app_pay=Y&body=Order202007291633058281554675&currency=HKD&notify_url=http%3A%2F%2Fdevmain.bindo.io%2Fgateway%2Fv2%2Fstores%2F7102%2Fasync_callback%2Forder%2F1718234981198725120%2Ftransaction%2F1718234981836259328&order_create=1596011585733&out_trade_no=1718234981836259328&partner=2088721222565561&payment_inst=ALIPAYHK&product_code=NEW_WAP_OVERSEAS_SELLER&return_url=https%3A%2F%2Fdevmain.bindo.io%2Fgateway%2Fv2%2Fstores%2F7102%2Freturn&secondary_merchant_id=7102&secondary_merchant_industry=5812&secondary_merchant_name=HKT+Payment+Limited&service=create_forex_trade_wap&sign=Iqjn1eDO4hjFPRYElIkR342w2DTDliVyMEZTAFQEkVtekv5xs0DjJIuZ2KyGmEFTQwDfDZs4zRdJcDbzoQzkAGdvEhTix5ET0E4pZHYRXQ3Hk2I9L5FPuJ4Cc6x1PKbjqsJ3%2Fx15G0MS7utXkvgtWeK0YrO8FsNEK7vlRNVE1NI%3D&sign_type=RSA&subject=HKT+Payment+Limited&timeout_rule=4m&total_fee=0.1","paymentExpireTimestamp":"2020-07-29T08:37:05.308+0000","notificationUrl":"https://webhook.site/a1d29d9c-dfc1-4cf8-a75e41086eee523c","paymentType":"ALIPAYHK","paymentRrn":"2020072922001325831428225231","paymentBrn":"31363972","createTimestamp":"2020-07-29T08:33:06.092+0000","updateTimestamp":"2020-07-29T08:33:44.153+0000"}]},"payloadType":"Order","timestamp":"2020-07-29T08:33:44.179+0000"}';
        // // $payload = '{"payload":{"orderId":"6e08ce87-00b8-46e1-a06eb1da00559f95","currency":"HKD","capturedChargeTotal":0.1,"transactions":[{"orderId":"6e08ce87-00b8-46e1-a06e-b1da00559f95","merchantTransactionId":"BTU-TEST00088","transactionId":"1718234981836259328","currency":"HKD","chargeTotal":0.1,"transactionType":"SALE","transactionState":"COMPLETED","transactionTimestamp":"2020-07-29T08:33:06.000+0000","submissionComponent":"API","paymentUrl":"https://mapi.alipay.com/gateway.do?_input_charset=UTF-8&app_pay=Y&body=Order202007291633058281554675&currency=HKD&notify_url=http%3A%2F%2Fdevmain.bindo.io%2Fgateway%2Fv2%2Fstores%2F7102%2Fasync_callback%2Forder%2F1718234981198725120%2Ftransaction%2F1718234981836259328&order_create=1596011585733&out_trade_no=1718234981836259328&partner=2088721222565561&payment_inst=ALIPAYHK&product_code=NEW_WAP_OVERSEAS_SELLER&return_url=https%3A%2F%2Fdevmain.bindo.io%2Fgateway%2Fv2%2Fstores%2F7102%2Freturn&secondary_merchant_id=7102&secondary_merchant_industry=5812&secondary_merchant_name=HKT+Payment+Limited&service=create_forex_trade_wap&sign=Iqjn1eDO4hjFPRYElIkR342w2DTDliVyMEZTAFQEkVtekv5xs0DjJIuZ2KyGmEFTQwDfDZs4zRdJcDbzoQzkAGdvEhTix5ET0E4pZHYRXQ3Hk2I9L5FPuJ4Cc6x1PKbjqsJ3%2Fx15G0MS7utXkvgtWeK0YrO8FsNEK7vlRNVE1NI%3D&sign_type=RSA&subject=HKT+Payment+Limited&timeout_rule=4m&total_fee=0.1","paymentExpireTimestamp":"2020-07-29T08:37:05.308+0000","notificationUrl":"https://webhook.site/a1d29d9c-dfc1-4cf8-a75e41086eee523c","paymentType":"ALIPAYHK","paymentRrn":"2020072922001325831428225231","paymentBrn":"31363972","createTimestamp":"2020-07-29T08:33:06.092+0000","updateTimestamp":"2020-07-29T08:33:44.153+0000"}]},"payloadType":"Order","timestamp":"2020-07-29T08:33:44.179+0000"}';
        // $data = json_decode($payload);

        // // Log data
        // $arrData = [];
        // $objPaymentLog = $this->_paymentLogFactory->create();
        // $arrData['identifier'] = 'Order Updated_'.$data->payload->orderId;
        // $arrData['create_at'] = date('Y-m-d H:i:s');
        // $arrData['magento_order_id'] = $magento_order_id;
        // $arrData['request_data'] = $payload;
        // $objPaymentLog->setData($arrData);
        // $objPaymentLog->save();

        // // Update Order
        // $arrCTLData = [];
        // $objCTL = $this->_ctlFactory->create();
        // $objCustomerTextbookList = $objCTL->load($customer_textbook_id);
        // $arrCTLData = $objCustomerTextbookList->getData();
        // $arrCTLData['order_id'] = $magento_order_id;
        // $objCustomerTextbookList->setData($arrCTLData);
        // $objCustomerTextbookList->save();

        // // return $this->resultRedirectFactory->create()->setUrl($this->_redirect->error($this->_buildUrl('*/*/success', $queryData)));
        // echo 'updated';
        exit();
    }
}