/**
  * @param InfoInterface $payment
  * @param string|null $token
  * @return array
  * @throws LocalizedException
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  * @SuppressWarnings(PHPMD.NPathComplexity)
  */
 protected function populateAuthorizeRequest(InfoInterface $payment, $token)
 {
     /** @var \Magento\Sales\Api\Data\OrderInterface $order */
     $order = $payment->getOrder();
     $orderId = $order->getIncrementId();
     $billing = $order->getBillingAddress();
     $shipping = $order->getShippingAddress();
     $transactionParams = ['channel' => $this->getChannel(), 'orderId' => $orderId, 'customer' => ['firstName' => $billing->getFirstname(), 'lastName' => $billing->getLastname(), 'company' => $billing->getCompany(), 'phone' => $billing->getTelephone(), 'fax' => $billing->getFax(), 'email' => $order->getCustomerEmail()]];
     $customerId = $this->braintreeHelper->generateCustomerId($order->getCustomerId(), $order->getCustomerEmail());
     $merchantAccountId = $this->config->getMerchantAccountId();
     if ($merchantAccountId) {
         $transactionParams['merchantAccountId'] = $merchantAccountId;
     }
     if (!$this->isTokenAllowed()) {
         $token = null;
     } elseif (!$token) {
         $token = $this->getInfoInstance()->getAdditionalInformation('cc_token');
     }
     if ($token) {
         $transactionParams['paymentMethodToken'] = $token;
         $transactionParams['customerId'] = $customerId;
     } elseif ($this->getInfoInstance()->getAdditionalInformation('payment_method_nonce')) {
         $transactionParams['paymentMethodNonce'] = $this->getInfoInstance()->getAdditionalInformation('payment_method_nonce');
         if ($this->isPaymentMethodNonceForCc()) {
             if ($order->getCustomerId() && $this->config->useVault()) {
                 if ($this->getInfoInstance()->getAdditionalInformation('store_in_vault')) {
                     $last4 = $this->getInfoInstance()->getAdditionalInformation('cc_last4');
                     if ($this->shouldSaveCard($last4)) {
                         $transactionParams['options']['storeInVaultOnSuccess'] = true;
                     }
                 } else {
                     $transactionParams['options']['storeInVault'] = false;
                 }
                 if ($this->vault->exists($customerId)) {
                     $transactionParams['customerId'] = $customerId;
                     //TODO: How can we update customer information?
                     unset($transactionParams['customer']);
                 } else {
                     $transactionParams['customer']['id'] = $customerId;
                 }
             }
             $transactionParams['creditCard'] = ['cardholderName' => $billing->getFirstname() . ' ' . $billing->getLastname()];
         }
         $transactionParams['billing'] = $this->toBraintreeAddress($billing);
         $transactionParams['shipping'] = $this->toBraintreeAddress($shipping);
         $transactionParams['options']['addBillingAddressToPaymentMethod'] = true;
     } else {
         throw new LocalizedException(__('Incomplete payment information.'));
     }
     if ($this->verify3dSecure()) {
         $transactionParams['options']['three_d_secure'] = ['required' => true];
         if ($token && $this->getInfoInstance()->getAdditionalInformation('payment_method_nonce')) {
             $transactionParams['paymentMethodNonce'] = $this->getInfoInstance()->getAdditionalInformation('payment_method_nonce');
             unset($transactionParams['paymentMethodToken']);
         }
     }
     if ($this->config->isFraudProtectionEnabled() && strlen($this->getInfoInstance()->getAdditionalInformation('device_data')) > 0) {
         $transactionParams['deviceData'] = $this->getInfoInstance()->getAdditionalInformation('device_data');
     }
     return $transactionParams;
 }