【问题标题】:How to get grandTotal in magento 2.3.3?如何在 magento 2.3.3 中获得总计?
【发布时间】:2020-04-14 13:00:41
【问题描述】:

我正在使用 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】:

    试试这个代码

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

    【讨论】:

    • 它也返回 NULL。我发现了一件有趣的事情。当我单击“下订单”按钮时显示 NULL,但是当我同时打开第二页并将产品添加到购物车(在第二页)并刷新第一页时,它会显示我的金额(在第一页)。这意味着当我在清理购物车中触发“下订单”按钮时。如何禁用它?
    • 请问您是如何创建自定义支付网关的?
    • 我已经编辑了我的问题。如果您需要更多信息,请告诉我。
    • 请检查我的回答我已经更新了。如果它解决了你的问题,请接受它。
    • 你的答案完全正确!只需尝试在此处删除echo -> ` $grandTotal = echo $order->getGrandTotal();` 一切正常!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2018-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-17
    • 1970-01-01
    • 2013-06-07
    • 1970-01-01
    相关资源
    最近更新 更多