/** * @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()); } }
/** * @param string $token * @throws LocalizedException * @return bool|string */ public function generatePaymentMethodToken($token) { $result = $this->braintreePaymentMethod->createNonce($token); if (!$result->success) { throw new LocalizedException($this->errorHelper->parseBraintreeError($result)); } return $result->paymentMethodNonce->nonce; }
/** * @expectedException \Magento\Framework\Exception\LocalizedException * @expectedExceptionMessage There was an error capturing the transaction: error. */ public function testCaptureError() { $amount = self::AUTH_AMOUNT; $paymentId = 1005; $paymentObject = $this->setupPaymentObjectForCapture($paymentId); //setup braintree error response $resultError = $this->getMockBuilder('\\Braintree_Result_Error')->disableOriginalConstructor()->getMock(); $this->errorHelperMock->expects($this->once())->method('parseBraintreeError')->with($resultError)->willReturn(new \Magento\Framework\Phrase('error')); $this->braintreeTransactionMock->expects($this->once())->method('submitForSettlement')->with(self::AUTH_TRAN_ID, $amount)->willReturn($resultError); $this->psrLoggerMock->expects($this->once())->method('critical'); $this->model->capture($paymentObject, $amount); }
/** * @expectedException \Magento\Framework\Exception\LocalizedException * @expectedExceptionMessage There was an error voiding the transaction: error. */ public function testVoidError() { $orderId = 1005; $paymentObject = $this->setupPaymentObjectForVoid($orderId); $transactions = ['1' => \Braintree_Transaction::factory(['id' => '1', 'status' => \Braintree_Transaction::SUBMITTED_FOR_SETTLEMENT])]; $this->setupTransactionIds($orderId, array_keys($transactions)); $index = 0; foreach ($transactions as $id => $transaction) { $this->braintreeTransactionMock->expects($this->at($index))->method('find')->with($id)->willReturn($transaction); $index++; } foreach (array_keys($transactions) as $id) { $resultError = $this->getMockBuilder('\\Braintree_Result_Error')->disableOriginalConstructor()->getMock(); $this->errorHelperMock->expects($this->once())->method('parseBraintreeError')->with($resultError)->willReturn(new \Magento\Framework\Phrase('error')); $this->braintreeTransactionMock->expects($this->at($index))->method('void')->with($id)->willReturn($resultError); $index++; } $this->model->void($paymentObject); }
/** * Voids transaction * * @param InfoInterface $payment * @throws LocalizedException * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function void(InfoInterface $payment) { $transactionIds = $this->getTransactionsToVoid($payment); $message = false; foreach ($transactionIds as $transactionId) { $transaction = $this->braintreeTransaction->find($transactionId); if ($transaction->status !== \Braintree_Transaction::SUBMITTED_FOR_SETTLEMENT && $transaction->status !== \Braintree_Transaction::AUTHORIZED) { throw new LocalizedException(__('Some transactions are already settled or voided and cannot be voided.')); } if ($transaction->status === \Braintree_Transaction::SUBMITTED_FOR_SETTLEMENT) { $message = __('Voided capture.'); } } $errors = ''; foreach ($transactionIds as $transactionId) { $this->_debug(['void-' . $transactionId]); $result = $this->braintreeTransaction->void($transactionId); $this->_debug($this->_convertObjToArray($result)); if (!$result->success) { $errors .= ' ' . $this->errorHelper->parseBraintreeError($result)->getText(); } elseif ($message) { $payment->setMessage($message); } } if ($errors) { throw new LocalizedException(__('There was an error voiding the transaction: %1.', $errors)); } else { $match = true; foreach ($transactionIds as $transactionId) { $collection = $this->salesTransactionCollectionFactory->create()->addFieldToFilter('parent_txn_id', ['eq' => $transactionId])->addFieldToFilter('txn_type', PaymentTransaction::TYPE_VOID); if ($collection->getSize() < 1) { $match = false; } } if ($match) { $payment->setIsTransactionClosed(true); } } return $this; }
/** * @param array $result * @param boolean $expected * @dataProvider isNonceUsedMoreThanOnceErrorDataProvider */ public function testIsNonceUsedMoreThanOnceError($result, $expected) { $resultObj = new \Braintree_Result_Error($result); $resultBool = $this->model->isNonceUsedMoreThanOnceError($resultObj); $this->assertEquals($expected, $resultBool); }