如何在magento 2.3.3中获得grandTotal?

问题描述 投票:0回答:1

我正在使用magento 2.3.3,并且正在尝试创建自定义付款网关。当我尝试从carttotal获取grandTotal值时,发生了问题。我尝试了很多不同的代码。有人可以帮我弄这个吗?作为示例,我将展示一些我尝试过的解决方案。

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $cart = $objectManager->get('\Magento\Sales\Model\Order'); $grandTotal = $cart->getGrandTotal();

此代码将返回NULL以及其他所有内容。任何帮助将不胜感激。

我创建了控制器文件,该文件将客户重定向到付款网关表单。工作正常。但是我必须汇款。这是代码的一部分。

 public function execute(){
 $back_url = $this->urlBuilder->getUrl('tarlanpay/redirect/callback');
    $store_id = $this->getStoreId();
    $order_id = $this->_checkoutSession->getData('last_order_id'); 
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $cart = $objectManager->get('\Magento\Checkout\Model\Cart');
    $grandTotal = $cart->getQuote()->getGrandTotal();
    $test_mode = $this->scopeConfig->getValue('payment/tarlanpay/test_mode', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    $merchant_id = $this->scopeConfig->getValue('payment/tarlanpay/merchant_id', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    $encrypted_key = $this->scopeConfig->getValue('payment/tarlanpay/secret_key', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    $secret_key = $this->_encryptor->decrypt($encrypted_key);
    // var_dump($grandTotal) ;die(); // it is debugging

    $post_data = [
            'reference_id' => $store_id.'-'.$order_id,
            'amount' => $grandTotal,
            'description' => 'magento 2',
            'merchant_id' => $merchant_id,
            'secret_key' => $secret_key,
            'is_test' => $test_mode,
            'back_url' => $back_url,
            'request_url' => 'http://magento2'
    ];
   $post_data['secret_key'] = password_hash($store_id.'-'.$order_id.$secret_key, PASSWORD_BCRYPT, ['cost' => 10]);

    $ctp_url = 'there is an API.examlpe.com';

    $curl = curl_init($ctp_url);

    curl_setopt($curl, CURLOPT_HTTPHEADER, array (
     'Accept: application/json'
    ));
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_FORBID_REUSE, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);

    $response = curl_exec($curl);

    $decoded_response = json_decode($response, true);

    $form_action_url = $decoded_response['data']['redirect_url']; //it's an info that came from payment gateway
    $array_data = array (
    'action' =>$form_action_url,
    'fields' => 'success'
    );
    $result = $this->resultJsonFactory->create();
    return $result->setData($array_data);
}
magento return-value payment-gateway
1个回答
0
投票

尝试此代码

 public function execute(){
 $back_url = $this->urlBuilder->getUrl('tarlanpay/redirect/callback');
    $store_id = $this->getStoreId();
    $order_id = $this->_checkoutSession->getData('last_order_id'); 
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $order = $objectManager->create('Magento\Sales\Api\Data\OrderInterface')->load($order_id);
    $grandTotal = echo $order->getGrandTotal();
    $test_mode = $this->scopeConfig->getValue('payment/tarlanpay/test_mode', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    $merchant_id = $this->scopeConfig->getValue('payment/tarlanpay/merchant_id', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    $encrypted_key = $this->scopeConfig->getValue('payment/tarlanpay/secret_key', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    $secret_key = $this->_encryptor->decrypt($encrypted_key);
    // var_dump($grandTotal) ;die(); // it is debugging

    $post_data = [
            'reference_id' => $store_id.'-'.$order_id,
            'amount' => $grandTotal,
            'description' => 'magento 2',
            'merchant_id' => $merchant_id,
            'secret_key' => $secret_key,
            'is_test' => $test_mode,
            'back_url' => $back_url,
            'request_url' => 'http://magento2'
    ];
   $post_data['secret_key'] = password_hash($store_id.'-'.$order_id.$secret_key, PASSWORD_BCRYPT, ['cost' => 10]);

    $ctp_url = 'there is an API.examlpe.com';

    $curl = curl_init($ctp_url);

    curl_setopt($curl, CURLOPT_HTTPHEADER, array (
     'Accept: application/json'
    ));
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_FORBID_REUSE, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);

    $response = curl_exec($curl);

    $decoded_response = json_decode($response, true);

    $form_action_url = $decoded_response['data']['redirect_url']; //it's an info that came from payment gateway
    $array_data = array (
    'action' =>$form_action_url,
    'fields' => 'success'
    );
    $result = $this->resultJsonFactory->create();
    return $result->setData($array_data);
}
© www.soinside.com 2019 - 2024. All rights reserved.