如何在 magento 2.3.3 中获得总计?

How to get grandTotal in magento 2.3.3?

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

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

此代码 returns 与其他所有内容一样为 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);
}

试试这个代码

 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 = $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);
}