/** * Executes the controller action and asserts non exception logic */ public function testExecuteLocalizedException() { $phrase = new \Magento\Framework\Phrase('some error'); $objectManager = new ObjectManager($this); $this->vault->expects($this->once())->method('processNonce')->willThrowException(new LocalizedException($phrase)); $this->resultJson->expects($this->once())->method('setData')->with(['success' => false, 'error_message' => 'some error']); $this->resultFactory->expects($this->once())->method('create')->willReturn($this->resultJson); $this->messageManager->expects($this->once())->method('addError')->with($phrase); $notification = $objectManager->getObject('Magento\\Braintree\\Controller\\Creditcard\\AjaxSave', ['request' => $this->request, 'resultFactory' => $this->resultFactory, 'vault' => $this->vault, 'messageManager' => $this->messageManager]); $this->assertSame($this->resultJson, $notification->execute()); }
/** * 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 controller action and asserts non exception logic */ public function testExecuteInternalLocalizedException() { $phrase = new \Magento\Framework\Phrase('Something went wrong while processing.'); $objectManager = new ObjectManager($this); $this->vault->expects($this->once())->method('generatePaymentMethodToken')->willThrowException(new LocalizedException($phrase)); $this->resultJson->expects($this->once())->method('setData')->with(['success' => false, 'error_message' => 'Something went wrong while processing.']); $this->resultFactory->expects($this->once())->method('create')->willReturn($this->resultJson); $this->messageManager->expects($this->once())->method('addError')->with($phrase); $this->request->expects($this->any())->method('getParam')->willReturn(true); /** @var \Magento\Braintree\Controller\Creditcard\Generate $controller */ $controller = $objectManager->getObject('Magento\\Braintree\\Controller\\Creditcard\\Generate', ['request' => $this->request, 'resultFactory' => $this->resultFactory, 'vault' => $this->vault, 'messageManager' => $this->messageManager]); $this->assertSame($this->resultJson, $controller->executeInternal()); }
/** * @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()); } }
protected function setupAuthorizeRequest(array $configData, $vault, $registry, $existingCustomer, 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; if ($existingCustomer !== null) { $this->vaultMock->expects($this->once())->method('exists')->with(self::CUSTOMER_ID)->willReturn($existingCustomer); if ($existingCustomer) { unset($expectedRequestAttribs['customer']); } } if (!empty($vault['canSaveCard'])) { $this->vaultMock->expects($this->once())->method('canSaveCard')->with(self::AUTH_CC_LAST_4)->willReturn($vault['canSaveCard']); } if (array_key_exists('registry', $registry)) { $this->registryMock->expects($this->once())->method('registry')->with(PaymentMethod::REGISTER_NAME)->willReturn($registry['registry']); } if (array_key_exists('register', $registry)) { $this->registryMock->expects($this->once())->method('register')->with(PaymentMethod::REGISTER_NAME, true); } return $expectedRequestAttribs; }
/** * @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)); }
/** * Returns applicable stored cards * * @return array */ public function getStoredCards() { $storedCards = $this->vault->currentCustomerStoredCards(); $country = $this->checkoutSession->getQuote()->getBillingAddress()->getCountryId(); $cardTypes = $this->config->getApplicableCardTypes($country); $applicableCards = []; foreach ($storedCards as $card) { if (in_array($this->dataHelper->getCcTypeCodeByName($card->cardType), $cardTypes)) { $applicableCards[] = $card; } } return $applicableCards; }
/** * Executes the controller action and asserts failed deletion */ public function testExecuteSaveFail() { $objectManager = new ObjectManagerHelper($this); $phrase = new \Magento\Framework\Phrase('There was error deleting the credit card'); $this->vault->expects($this->once())->method('deleteCard')->willReturn(false); $this->request->expects($this->any())->method('getParam')->willReturn('token'); $this->resultRedirectFactory->expects($this->once())->method('create')->willReturn($this->resultRedirect); /** * @var \Magento\Framework\Message\ManagerInterface $messageManager */ $messageManager = $this->getMockBuilder('\\Magento\\Framework\\Message\\ManagerInterface')->getMock(); $messageManager->expects($this->any())->method('addError')->with($phrase); $notification = $objectManager->getObject('Magento\\Braintree\\Controller\\Creditcard\\DeleteConfirm', ['request' => $this->request, 'resultRedirectFactory' => $this->resultRedirectFactory, 'vault' => $this->vault, 'messageManager' => $messageManager]); $this->assertSame($this->resultRedirect, $notification->execute()); }
/** * Executes the controller action and asserts with redirects for non existing logic */ public function testExecuteNonExistingTokenRedirect() { $objectManager = new ObjectManagerHelper($this); $phrase = new \Magento\Framework\Phrase('Credit card does not exist'); $this->vault->expects($this->once())->method('storedCard')->willReturn(false); $this->request->expects($this->any())->method('getParam')->willReturn('token'); $this->resultRedirectFactory->expects($this->once())->method('create')->willReturn($this->resultRedirect); $this->resultPageFactory->expects($this->never())->method('create')->willReturn($this->resultPage); $this->resultRedirect->expects($this->once())->method('setPath')->willReturnSelf(); /** * @var \Magento\Framework\Message\ManagerInterface $messageManager */ $messageManager = $this->getMockBuilder('\\Magento\\Framework\\Message\\ManagerInterface')->getMock(); $messageManager->expects($this->once())->method('addError')->with($phrase); $notification = $objectManager->getObject('Magento\\Braintree\\Controller\\Creditcard\\Edit', ['request' => $this->request, 'resultPageFactory' => $this->resultPageFactory, 'resultRedirectFactory' => $this->resultRedirectFactory, 'vault' => $this->vault, 'messageManager' => $messageManager]); $this->assertSame($this->resultRedirect, $notification->execute()); }
/** * @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()); }
/** * @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; }
/** * Retrieve current stored cards * * @return array */ public function getCurrentCustomerStoredCards() { return $this->vault->currentCustomerStoredCards(); }
public function testGetCurrentCustomerStoredCards() { $cards = ['card']; $this->vaultMock->expects($this->once())->method('currentCustomerStoredCards')->willReturn($cards); $this->assertEquals($cards, $this->block->getCurrentCustomerStoredCards()); }