/** * Returns customer credit cards if applicable * * @return \Braintree_Customer|boolean */ public function getLoggedInCustomerCards() { $applicableCards = []; $useVault = (bool) (int) $this->scopeConfig->getValue(self::CONFIG_PATH_VAULT, ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $this->sessionQuote->getStoreId()); if ($useVault) { $storedCards = false; if ($this->sessionQuote->getCustomerId()) { $customerId = $this->paymentHelper->generateCustomerId($this->sessionQuote->getCustomerId(), $this->sessionQuote->getQuote()->getCustomerEmail()); try { $storedCards = $this->braintreeCustomerAdapter->find($customerId)->creditCards; } catch (\Braintree_Exception $e) { $this->_logger->critical($e); } } if ($storedCards) { $country = $this->sessionQuote->getQuote()->getBillingAddress()->getCountryId(); $cardTypes = $this->paymentHelper->getCcAvailableCardTypes($country); $applicableCards = []; foreach ($storedCards as $card) { if (isset($cardTypes[$this->paymentHelper->getCcTypeCodeByName($card->cardType)])) { $applicableCards[] = $card; } } } } return $applicableCards; }
/** * Delete Braintree customer when Magento customer is deleted * * @param \Magento\Framework\Event\Observer $observer * @return $this */ public function execute(\Magento\Framework\Event\Observer $observer) { if (!$this->config->isActive()) { return $this; } $customer = $observer->getEvent()->getCustomer(); $customerId = $this->helper->generateCustomerId($customer->getId(), $customer->getEmail()); if ($this->vault->exists($customerId)) { $this->vault->deleteCustomer($customerId); } return $this; }
public function testGenerateCustomerId() { $result = $this->model->generateCustomerId(1, "*****@*****.**"); $this->assertEquals(md5("1" . '-' . "*****@*****.**"), $result); }
/** * @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; }
/** * Gets response from braintree api using the nonce * * @param string|null $nonce * @param array|null $options * @param array|null $billingAddress * @return $this * @throws LocalizedException * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function processNonce($nonce = null, $options = null, $billingAddress = null) { $customerId = $this->customerSession->getCustomerId(); if (!$customerId) { throw new LocalizedException(__('Invalid Customer ID provided')); } $billingCountry = null; if (is_array($billingAddress)) { $collection = $this->countryFactory->create()->addCountryCodeFilter($billingAddress['countryCodeAlpha2']); if ($collection->getSize()) { $billingCountry = $collection->getFirstItem()->getId(); } if (!$this->config->canUseForCountry($billingCountry)) { throw new LocalizedException(__('Selected payment type is not allowed for billing country.')); } } $ccType = null; if ($options) { $ccType = $options['ccType']; } if ($ccType) { $error = $this->config->canUseCcTypeForCountry($billingCountry, $ccType); if ($error) { throw new LocalizedException($error); } } $customer = $this->customerFactory->create()->load($customerId); $customerId = $this->braintreeHelper->generateCustomerId($customerId, $customer->getEmail()); if (!$this->exists($customerId)) { $customerRequest = ['id' => $customerId, 'firstName' => $billingAddress['firstName'], 'lastName' => $billingAddress['lastName'], 'email' => $this->customerSession->getCustomerDataObject()->getEmail()]; if (strlen($billingAddress['company'])) { $customerRequest['company'] = $billingAddress['company']; } $result = $this->braintreeCustomer->create($customerRequest); if (!$result->success) { throw new LocalizedException($this->errorHelper->parseBraintreeError($result)); } } //check if customerId is created on braintree $requestArray = ['customerId' => $customerId, 'paymentMethodNonce' => $nonce, 'options' => ['makeDefault' => $options['default'] == 'true' ? true : false, 'failOnDuplicatePaymentMethod' => $this->config->allowDuplicateCards() == '1' ? false : true, 'verifyCard' => $this->config->useCvv() == '1' ? true : false]]; if ($this->config->isFraudDetectionEnabled() && strlen($options['device_data']) > 0) { $requestArray['deviceData'] = $options['device_data']; } if ($options['update'] == 'true') { $token = $options['token']; unset($requestArray['customerId']); unset($requestArray['options']['failOnDuplicatePaymentMethod']); $requestArray['billingAddress'] = $billingAddress; $result = $this->braintreePaymentMethod->update($token, $requestArray); $this->debug($requestArray); $this->debug($result); } else { $result = $this->braintreePaymentMethod->create($requestArray); $this->debug($requestArray); $this->debug($result); } if (!$result->success) { throw new LocalizedException($this->errorHelper->parseBraintreeError($result)); } return $this; }