/** * 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; }
/** * Executes the dispatch override */ public function testDispatchNotLoggedIn() { $objectManager = new ObjectManagerHelper($this); $this->resultRedirectFactory->expects($this->any())->method('create')->willReturn($this->resultRedirect); $this->resultRedirect->expects($this->any())->method('setPath')->willReturnSelf(); $this->customerSession->expects($this->any())->method('authenticate')->willReturn(false); $this->config->expects($this->any())->method('useVault')->willReturn(true); /** * @var \Magento\Framework\App\RequestInterface $requestInterface */ $requestInterface = $this->getMockBuilder('\\Magento\\Framework\\App\\Request\\Http')->disableOriginalConstructor()->getMock(); $notification = $objectManager->getObject('Magento\\Braintree\\Controller\\MyCreditCards', ['customerSession' => $this->customerSession, 'resultRedirectFactory' => $this->resultRedirectFactory, 'config' => $this->config, 'customerUrl' => $this->customerUrl, 'response' => $this->_response]); $this->assertSame($this->_response, $notification->dispatch($requestInterface)); }
/** * @dataProvider processBraintreePaymentSkipDataProvider */ public function testProcessBraintreePaymentSkip($config) { $index = 0; foreach ($config as $key => $value) { $this->configMock->expects($this->at($index))->method('getConfigData')->with($key)->willReturn($value); $index++; } $paymentObj = new \Magento\Framework\DataObject(['method' => PaymentMethod::METHOD_CODE]); $orderMock = $this->getMockBuilder('\\Magento\\Sales\\Model\\Order')->disableOriginalConstructor()->getMock(); $orderMock->expects($this->once())->method('getPayment')->willReturn($paymentObj); $orderMock->expects($this->once())->method('canInvoice')->willReturn(true); $observer = new \Magento\Framework\Event\Observer(['event' => new \Magento\Framework\DataObject(['shipment' => new \Magento\Framework\DataObject(['order' => $orderMock])])]); $this->transactionFactoryMock->expects($this->never())->method('create'); $this->assertEquals($this->processBraintreePaymentObserver, $this->processBraintreePaymentObserver->execute($observer)); }
/** * 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); }
/** * @dataProvider getCustomerEmptyDataProvider */ public function testGetCustomerEmpty($useVault, $isLoggedIn) { $this->configMock->expects($this->any())->method('useVault')->willReturn($useVault); $this->customerSessionMock->expects($this->any())->method('isLoggedIn')->willReturn($isLoggedIn); $this->assertEquals('', $this->block->currentCustomerName()); $this->assertEquals('', $this->block->currentCustomerLastName()); }
/** * @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; }
/** * @dataProvider getCountrySpecificCardTypeConfigDataProvider */ public function testGetCountrySpecificCardTypeConfig($countryCardType, $expectedResult) { $prefix = 'payment/braintree/'; $valueMap = [[$prefix . Cc::KEY_COUNTRY_CREDIT_CARD, ScopeInterface::SCOPE_STORE, null, $countryCardType]]; $this->scopeConfigMock->expects($this->any())->method('getValue')->willReturnMap($valueMap); $this->assertEquals($expectedResult, $this->model->getCountrySpecificCardTypeConfig()); }
/** * @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; }
/** * @param string $params * @dataProvider dataProcessNonce */ public function testProcessNonceException($params = null, $exceptionMessage = null) { $this->customerSessionMock->expects($this->any())->method('getCustomerId')->willReturn($params['customerId']); $countryCollectionMock = $this->getMockBuilder('\\Magento\\Directory\\Model\\Resource\\Country\\Collection')->disableOriginalConstructor()->getMock(); $countryCollectionMock->expects($this->any())->method('addCountryCodeFilter')->willReturn($countryCollectionMock); $this->countryFactoryMock->expects($this->any())->method('create')->willReturn($countryCollectionMock); $this->configMock->expects($this->any())->method('canUseForCountry')->willReturn($params['canUseForCountry']); $this->configMock->expects($this->any())->method('canUseCcTypeForCountry')->willReturn($params['canUseCcTypeForCountry']); $this->helperMock->expects($this->any())->method('generateCustomerId')->willReturn($params['customerId']); $this->customerMock->expects($this->any())->method('load')->willReturn($this->customerMock); $this->customerFactoryMock->expects($this->any())->method('create')->willReturn($this->customerMock); if (is_object($params['paymentMethodObj'])) { if (!$params['optionsArray']['update']) { $this->braintreePaymentMethodMock->expects($this->once())->method('create')->willReturn($params['paymentMethodObj']); } else { $this->braintreePaymentMethodMock->expects($this->once())->method('update')->willReturn($params['paymentMethodObj']); } if (!$params['paymentMethodObj']->success) { $this->errorHelperMock->expects($this->once())->method('parseBraintreeError')->willReturn(new \Magento\Framework\Phrase($exceptionMessage)); } else { $this->errorHelperMock->expects($this->never())->method('parseBraintreeError'); } } try { $this->model->processNonce($params['nonce'], $params['optionsArray'], $params['billingAddress']); } catch (\Magento\Framework\Exception\LocalizedException $e) { $this->assertEquals($exceptionMessage, $e->getMessage()); } }
public function testGetConfigData() { $field = 'configFieldName'; $storeId = '2'; $configValue = 'configValue'; $this->configMock->expects($this->once())->method('getConfigData')->with($field, $storeId)->willReturn($configValue); $this->assertEquals($configValue, $this->model->getConfigData($field, $storeId)); }
/** * Loads country collection * * @return \Magento\Directory\Model\ResourceModel\Country\Collection */ public function getCountryCollection() { $collection = $this->getData('country_collection'); if ($collection == null) { $restrictedCountriesByBraintree = $this->countrySource->getRestrictedCountries(); $collection = $this->_countryCollectionFactory->create()->addFieldToFilter('country_id', ['nin' => $restrictedCountriesByBraintree])->loadByStore(); foreach ($collection as $item) { $countryCode = $item->getData('country_id'); if (!$this->config->canUseForCountry($countryCode)) { $restrictedCountriesByBraintree[] = $item->getData('country_id'); } } $collection = $this->_countryCollectionFactory->create()->addFieldToFilter('country_id', ['nin' => $restrictedCountriesByBraintree])->loadByStore(); $this->setData('country_collection', $collection); } return $collection; }
/** * @param bool $isActive * @param bool $isExistingCustomer * @param bool $deleteCustomer * @dataProvider deleteBraintreeCustomerDataProvider */ public function testDeleteBraintreeCustomer($isActive, $isExistingCustomer, $deleteCustomer) { $braintreeCustoemrId = 'braintreeCustomerId'; $customerId = '10002'; $customerEmail = '*****@*****.**'; $this->configMock->expects($this->once())->method('isActive')->willReturn($isActive); $customer = new \Magento\Framework\DataObject(['id' => $customerId, 'email' => $customerEmail]); $this->helperMock->expects($this->any())->method('generateCustomerId')->with($customerId, $customerEmail)->willReturn($braintreeCustoemrId); $observer = new \Magento\Framework\Event\Observer(['event' => new \Magento\Framework\DataObject(['customer' => $customer])]); $this->vaultMock->expects($this->any())->method('exists')->with($braintreeCustoemrId)->willReturn($isExistingCustomer); if ($deleteCustomer) { $this->vaultMock->expects($this->once())->method('deleteCustomer')->with($braintreeCustoemrId); } else { $this->vaultMock->expects($this->never())->method('deleteCustomer'); } $this->assertEquals($this->deleteBraintreeCustomerObserver, $this->deleteBraintreeCustomerObserver->execute($observer)); }
/** * @param array $data * @param array $expected * @dataProvider getCcAvailableCardTypesDataProvider */ public function testGetCcAvailableCardTypes($data, $expected) { $this->braintreeCcConfig->expects($this->any())->method('getConfigData')->willReturn($data['cctypes']); $this->braintreeCcConfig->expects($this->any())->method('getCountrySpecificCardTypeConfig')->willReturn($data['cctypesCountrySpecific']); $this->braintreeCcConfig->expects($this->any())->method('getApplicableCardTypes')->willReturn($data['applicableCards']); $this->paymentConfig->expects($this->any())->method('getCcTypes')->willReturn($data['ccTypes']); $result = $this->model->getCcAvailableCardTypes($data['country']); $this->assertEquals($expected, $result); }
protected function setupAuthorizeRequest(array $configData, array $paymentInfo, array $expectedRequestAttributes, $paymentObject, $storeId, $amount) { //setup general payment and order information $transactionRequest = $this->setupPaymentObject($paymentObject, $storeId); //setup config options foreach ($configData as $methodName => $value) { $this->configMock->expects($this->any())->method($methodName)->willReturn($value); } //setup payment info instance $this->infoInstanceMock->expects($this->any())->method('getAdditionalInformation')->willReturnMap($paymentInfo); $this->model->setInfoInstance($this->infoInstanceMock); $expectedRequestAttribs = array_merge($transactionRequest, $expectedRequestAttributes); $expectedRequestAttribs['amount'] = $amount; return $expectedRequestAttribs; }
/** * @dataProvider getConfigDataProvider */ public function testGetConfig($configData, $vaultData, $tokenNonceMap, $expectedResult) { foreach ($configData as $key => $value) { $this->configMock->expects($this->any())->method($key)->willReturn($value); } foreach ($vaultData as $key => $value) { $this->vaultMock->expects($this->any())->method($key)->willReturn($value); } $this->vaultMock->expects($this->any())->method('generatePaymentMethodToken')->willReturnMap($tokenNonceMap); $cardTypeMap = [['Visa', 'VI'], ['Master Card', 'MA'], ['American Express', 'AE'], ['Discover Card', 'DI']]; $this->helperMock->expects($this->any())->method('getCcTypeCodeByName')->willReturnMap($cardTypeMap); $this->helperMock->expects($this->once())->method('getTodayMonth')->willReturn(self::TODAY_MONTH); $this->helperMock->expects($this->once())->method('getTodayYear')->willReturn(self::TODAY_YEAR); $this->helperMock->expects($this->once())->method('getCcAvailableCardTypes')->willReturn($this->availableCardTypes); $this->customerSessionMock->expects($this->any())->method('isLoggedIn')->willReturn(true); $this->urlBuilderMock->expects($this->once())->method('getUrl')->with('braintree/creditcard/generate')->willReturn(self::PAYMENT_NONCE_GENERATION_URL); $this->assertEquals($expectedResult, $this->model->getConfig()); }
/** * @dataProvider getConfigDataProvider */ public function testGetConfig($configData, $vaultData, $tokenNonceMap, $expectedResult) { foreach ($configData as $key => $value) { $this->configMock->expects($this->any())->method($key)->willReturn($value); } foreach ($vaultData as $key => $value) { $this->vaultMock->expects($this->any())->method($key)->willReturn($value); } $this->vaultMock->expects($this->any())->method('generatePaymentMethodToken')->willReturnMap($tokenNonceMap); $quoteMock = $this->getMockBuilder('\\Magento\\Quote\\Model\\Quote')->disableOriginalConstructor()->getMock(); $quoteMock->expects($this->once())->method('getBillingAddress')->willReturn(new \Magento\Framework\Object(['country_id' => 'US'])); $this->checkoutSessionMock->expects($this->once())->method('getQuote')->willReturn($quoteMock); $cardTypeMap = [['Visa', 'VI'], ['Master Card', 'MA'], ['American Express', 'AE'], ['Discover Card', 'DI']]; $this->helperMock->expects($this->any())->method('getCcTypeCodeByName')->willReturnMap($cardTypeMap); $this->helperMock->expects($this->once())->method('getTodayMonth')->willReturn(self::TODAY_MONTH); $this->helperMock->expects($this->once())->method('getTodayYear')->willReturn(self::TODAY_YEAR); $this->helperMock->expects($this->once())->method('getCcAvailableCardTypes')->willReturn($this->availableCardTypes); $this->customerSessionMock->expects($this->any())->method('isLoggedIn')->willReturn(true); $this->urlBuilderMock->expects($this->once())->method('getUrl')->with('braintree/creditcard/generate')->willReturn(self::PAYMENT_NONCE_GENERATION_URL); $this->assertEquals($expectedResult, $this->model->getConfig()); }
/** * Retrieve available credit card types as associative array code & title * * @param string $country * @return array * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function getCcAvailableCardTypes($country = null) { $types = array_flip(explode(',', $this->braintreeCcConfig->getConfigData('cctypes'))); $mergedArray = []; if (is_array($types)) { foreach (array_keys($types) as $type) { $types[$type] = $this->getCcTypeNameByCode($type); } } //merge options then filter by a specific country $countrySpecificTypes = $this->braintreeCcConfig->getCountrySpecificCardTypeConfig(); //include all country specific in types if (is_array($countrySpecificTypes)) { foreach ($countrySpecificTypes as $countryArray) { foreach ($countryArray as $ccType) { $types[$ccType] = $this->getCcTypeNameByCode($ccType); } } } //preserve the same credit card order $allTypes = $this->getCcTypes(); if (is_array($allTypes)) { foreach ($allTypes as $ccTypeCode => $ccTypeName) { if (array_key_exists($ccTypeCode, $types)) { $mergedArray[$ccTypeCode] = $ccTypeName; } } } if ($country) { $countrySpecificTypesApplicable = $this->braintreeCcConfig->getApplicableCardTypes($country); if (!empty($countrySpecificTypesApplicable)) { foreach (array_keys($mergedArray) as $code) { if (!in_array($code, $countrySpecificTypesApplicable)) { unset($mergedArray[$code]); } } } } return $mergedArray; }
/** * @return string */ public function getClientToken() { return $this->config->getClientToken(); }
/** * If it's configured to capture on each shipment * * @return bool */ private function shouldInvoice() { $flag = $this->config->getConfigData(self::CONFIG_PATH_PAYMENT_ACTION) == \Magento\Payment\Model\Method\AbstractMethod::ACTION_AUTHORIZE && $this->config->getConfigData(self::CONFIG_PATH_CAPTURE_ACTION) == PaymentMethod::CAPTURE_ON_SHIPMENT; return $flag; }
/** * Retrieve information from payment configuration * * @param string $field * @param int|string|null|\Magento\Store\Model\Store $storeId * * @return mixed */ public function getConfigData($field, $storeId = null) { if ('order_place_redirect_url' === $field) { return $this->getOrderPlaceRedirectUrl(); } return $this->config->getConfigData($field, $storeId); }
/** * Retrieve applicable credit card types * * @return array */ public function getCcApplicableTypes() { return $this->config->getApplicableCardTypes(null); }