Exemplo n.º 1
0
 /**
  * Check customer authentication
  *
  * @param RequestInterface $request
  * @return \Magento\Framework\Controller\Result\Redirect|\Magento\Framework\App\ResponseInterface
  */
 public function dispatch(RequestInterface $request)
 {
     $loginUrl = $this->customerUrl->getLoginUrl();
     if (!$this->customerSession->authenticate($loginUrl)) {
         $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
     }
     if (!$this->config->useVault()) {
         $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
         /** @var \Magento\Framework\Controller\Result\Redirect $resultRedirect */
         $resultRedirect = $this->resultRedirectFactory->create();
         $resultRedirect->setPath('noRoute');
         return $resultRedirect;
     }
     return parent::dispatch($request);
 }
Exemplo n.º 2
0
 /**
  * @param string $token
  * @return bool|string
  */
 public function getSavedCardType($token)
 {
     $ccType = false;
     $useCache = $this->config->useVault();
     $cachedValues = $useCache ? $this->cache->load(self::CACHE_KEY_CREDIT_CARDS) : false;
     if ($cachedValues) {
         try {
             $cachedValues = unserialize($cachedValues);
         } catch (\Exception $e) {
             $cachedValues = [];
         }
         if (array_key_exists($token, $cachedValues)) {
             return $cachedValues[$token];
         }
     }
     try {
         $creditCard = $this->braintreeCreditCard->find($token);
         $this->debug($token);
         $this->debug($creditCard);
         $ccType = $this->braintreeHelper->getCcTypeCodeByName($creditCard->cardType);
         if (!empty($cachedValues)) {
             $cachedValues = array_merge($cachedValues, [$token => $ccType]);
         } else {
             $cachedValues = [$token => $ccType];
         }
         if ($useCache) {
             $this->cache->save(serialize($cachedValues), self::CACHE_KEY_CREDIT_CARDS);
         }
     } catch (\Exception $e) {
         $this->logger->critical($e);
     }
     return $ccType;
 }
Exemplo n.º 3
0
 /**
  * @return array|void
  */
 public function getConfig()
 {
     if (!$this->config->isActive()) {
         return [];
     }
     $config = parent::getConfig();
     $clientToken = $this->config->getClientToken();
     $useVault = $this->config->useVault();
     $selectedCardToken = null;
     $storedCardOptions = [];
     if ($useVault) {
         $storedCards = $this->getStoredCards();
         if (count($storedCards) == 0) {
             $useVault = false;
         } else {
             foreach ($storedCards as $creditCard) {
                 $storedCardOptions[] = ['token' => $creditCard->token, 'maskedNumber' => $creditCard->maskedNumber . ' - ' . $creditCard->cardType, 'selected' => $creditCard->default, 'type' => $this->dataHelper->getCcTypeCodeByName($creditCard->cardType)];
                 if ($creditCard->default) {
                     $selectedCardToken = $creditCard->token;
                 }
             }
         }
     }
     $config = array_merge_recursive($config, ['payment' => ['braintree' => ['clientToken' => $clientToken, 'useVault' => $useVault, 'canSaveCard' => $this->canSaveCard(), 'show3dSecure' => $this->show3dSecure(), 'storedCards' => $storedCardOptions, 'selectedCardToken' => $selectedCardToken, 'creditCardExpMonth' => (int) $this->dataHelper->getTodayMonth(), 'creditCardExpYear' => (int) $this->dataHelper->getTodayYear(), 'countrySpecificCardTypes' => $this->config->getCountrySpecificCardTypeConfig(), 'isFraudDetectionEnabled' => $this->config->isFraudDetectionEnabled(), 'isCcDetectionEnabled' => $this->config->isCcDetectionEnabled(), 'availableCardTypes' => $this->getCcAvailableCcTypes(), 'braintreeDataJs' => $this->config->getBraintreeDataJs(), 'ajaxGenerateNonceUrl' => $this->getAjaxGenerateNonceUrl()]]]);
     return $config;
 }
Exemplo n.º 4
0
 /**
  * @return bool
  */
 public function useVault()
 {
     return (bool) $this->config->useVault();
 }
Exemplo n.º 5
0
 /**
  * @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;
 }
Exemplo n.º 6
0
 /**
  * Retrieve use of vault
  *
  * @return bool
  * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  */
 public function getUsesVault()
 {
     return $this->config->useVault();
 }