Skip to content

Commit 8fd3a45

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-43331' into MAGETWO-43348
2 parents defbdb1 + 09530b6 commit 8fd3a45

File tree

4 files changed

+119
-64
lines changed

4 files changed

+119
-64
lines changed

app/code/Magento/Paypal/Controller/Transparent/RequestSecureToken.php

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
namespace Magento\Paypal\Controller\Transparent;
77

88
use Magento\Framework\App\Action\Context;
9+
use Magento\Framework\Controller\Result\Json;
910
use Magento\Framework\Controller\Result\JsonFactory;
1011
use Magento\Framework\Controller\ResultInterface;
1112
use Magento\Framework\Session\Generic;
1213
use Magento\Framework\Session\SessionManager;
1314
use Magento\Paypal\Model\Payflow\Service\Request\SecureToken;
1415
use Magento\Paypal\Model\Payflow\Transparent;
16+
use Magento\Quote\Model\Quote;
1517

1618
/**
1719
* Class RequestSecureToken
@@ -76,19 +78,43 @@ public function __construct(
7678
*/
7779
public function execute()
7880
{
79-
$this->sessionTransparent->setQuoteId($this->sessionManager->getQuote()->getId());
81+
/** @var Quote $quote */
82+
$quote = $this->sessionManager->getQuote();
8083

81-
$token = $this->secureTokenService->requestToken($this->sessionManager->getQuote());
84+
if (!$quote or !$quote instanceof Quote) {
85+
return $this->getErrorResponse();
86+
}
8287

83-
$result = [];
84-
$result[$this->transparent->getCode()]['fields'] = $token->getData();
85-
$result['success'] = $token->getSecuretoken() ? true : false;
88+
$this->sessionTransparent->setQuoteId($quote->getId());
89+
try {
90+
$token = $this->secureTokenService->requestToken($quote);
91+
if (!$token->getData('securetoken')) {
92+
throw new \LogicException();
93+
}
8694

87-
if (!$result['success']) {
88-
$result['error'] = true;
89-
$result['error_messages'] = __('Secure Token Error. Try again.');
95+
return $this->resultJsonFactory->create()->setData(
96+
[
97+
$this->transparent->getCode() => ['fields' => $token->getData()],
98+
'success' => true,
99+
'error' => false
100+
]
101+
);
102+
} catch (\Exception $e) {
103+
return $this->getErrorResponse();
90104
}
105+
}
91106

92-
return $this->resultJsonFactory->create()->setData($result);
107+
/**
108+
* @return Json
109+
*/
110+
private function getErrorResponse()
111+
{
112+
return $this->resultJsonFactory->create()->setData(
113+
[
114+
'success' => false,
115+
'error' => true,
116+
'error_messages' => __('Your payment has been declined. Please try again.')
117+
]
118+
);
93119
}
94120
}

app/code/Magento/Paypal/Model/Payflow/Service/Request/SecureToken.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public function __construct(
5454
*
5555
* @param Quote $quote
5656
*
57-
* @return Object
57+
* @return DataObject
58+
* @throws \Exception
5859
*/
5960
public function requestToken(Quote $quote)
6061
{

app/code/Magento/Paypal/Model/Payflowpro.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ protected function _buildPlaceRequest(DataObject $payment, $amount)
608608
/**
609609
* Return request object with basic information for gateway request
610610
*
611-
* @return Object
611+
* @return DataObject
612612
*/
613613
public function buildBasicRequest()
614614
{

app/code/Magento/Paypal/Test/Unit/Controller/Transparent/RequestSecureTokenTest.php

Lines changed: 81 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -98,32 +98,29 @@ protected function setUp()
9898
);
9999
}
100100

101-
/**
102-
* Run test execute method
103-
*
104-
* @param array $result
105-
* @param array $resultExpectation
106-
*
107-
* @dataProvider executeDataProvider
108-
*/
109-
public function testExecute(array $result, array $resultExpectation)
101+
public function testExecuteSuccess()
110102
{
111103
$quoteId = 99;
104+
$tokenFields = ['fields-1', 'fields-2', 'fields-3'];
105+
$secureToken = 'token_hash';
106+
$resultExpectation = [
107+
'transparent' => [
108+
'fields' => ['fields-1', 'fields-2', 'fields-3']
109+
],
110+
'success' => true,
111+
'error' => false
112+
];
112113

113114
$quoteMock = $this->getMockBuilder('Magento\Quote\Model\Quote')
114115
->disableOriginalConstructor()
115116
->getMock();
116117
$tokenMock = $this->getMockBuilder('Magento\Framework\DataObject')
117-
->setMethods(['getData', 'getSecuretoken'])
118118
->disableOriginalConstructor()
119119
->getMock();
120120
$jsonMock = $this->getMockBuilder('Magento\Framework\Controller\Result\Json')
121121
->disableOriginalConstructor()
122122
->getMock();
123123

124-
$this->transparentMock->expects($this->once())
125-
->method('getCode')
126-
->willReturn('transparent');
127124
$this->sessionManagerMock->expects($this->atLeastOnce())
128125
->method('getQuote')
129126
->willReturn($quoteMock);
@@ -140,12 +137,14 @@ public function testExecute(array $result, array $resultExpectation)
140137
$this->transparentMock->expects($this->once())
141138
->method('getCode')
142139
->willReturn('transparent');
143-
$tokenMock->expects($this->once())
140+
$tokenMock->expects($this->atLeastOnce())
144141
->method('getData')
145-
->willReturn($result['transparent']['fields']);
146-
$tokenMock->expects($this->once())
147-
->method('getSecuretoken')
148-
->willReturn($result['success']);
142+
->willReturnMap(
143+
[
144+
['', null, $tokenFields],
145+
['securetoken', null, $secureToken]
146+
]
147+
);
149148
$this->resultJsonFactoryMock->expects($this->once())
150149
->method('create')
151150
->willReturn($jsonMock);
@@ -157,42 +156,71 @@ public function testExecute(array $result, array $resultExpectation)
157156
$this->assertEquals($jsonMock, $this->controller->execute());
158157
}
159158

160-
/**
161-
* @return array
162-
*/
163-
public function executeDataProvider()
159+
public function testExecuteTokenRequestException()
164160
{
165-
return [
166-
[
167-
'result' => [
168-
'transparent' => [
169-
'fields' => ['fields-1', 'fields-2', 'fields-3']
170-
],
171-
'success' => 1
172-
],
173-
'result_expectation' => [
174-
'transparent' => [
175-
'fields' => ['fields-1', 'fields-2', 'fields-3']
176-
],
177-
'success' => true
178-
]
179-
],
180-
[
181-
'result' => [
182-
'transparent' => [
183-
'fields' => ['fields-1', 'fields-2', 'fields-3']
184-
],
185-
'success' => null,
186-
],
187-
'result_expectation' => [
188-
'transparent' => [
189-
'fields' => ['fields-1', 'fields-2', 'fields-3']
190-
],
191-
'success' => false,
192-
'error' => true,
193-
'error_messages' => __('Secure Token Error. Try again.')
194-
]
195-
]
161+
$quoteId = 99;
162+
$resultExpectation = [
163+
'success' => false,
164+
'error' => true,
165+
'error_messages' => __('Your payment has been declined. Please try again.')
196166
];
167+
168+
$quoteMock = $this->getMockBuilder('Magento\Quote\Model\Quote')
169+
->disableOriginalConstructor()
170+
->getMock();
171+
$jsonMock = $this->getMockBuilder('Magento\Framework\Controller\Result\Json')
172+
->disableOriginalConstructor()
173+
->getMock();
174+
175+
$this->sessionManagerMock->expects($this->atLeastOnce())
176+
->method('getQuote')
177+
->willReturn($quoteMock);
178+
$quoteMock->expects($this->once())
179+
->method('getId')
180+
->willReturn($quoteId);
181+
$this->sessionTransparentMock->expects($this->once())
182+
->method('setQuoteId')
183+
->with($quoteId);
184+
$this->secureTokenServiceMock->expects($this->once())
185+
->method('requestToken')
186+
->with($quoteMock)
187+
->willThrowException(new \Exception());
188+
$this->resultJsonFactoryMock->expects($this->once())
189+
->method('create')
190+
->willReturn($jsonMock);
191+
$jsonMock->expects($this->once())
192+
->method('setData')
193+
->with($resultExpectation)
194+
->willReturnSelf();
195+
196+
$this->assertEquals($jsonMock, $this->controller->execute());
197197
}
198+
199+
public function testExecuteEmptyQuoteError()
200+
{
201+
$resultExpectation = [
202+
'success' => false,
203+
'error' => true,
204+
'error_messages' => __('Your payment has been declined. Please try again.')
205+
];
206+
207+
$quoteMock = null;
208+
$jsonMock = $this->getMockBuilder('Magento\Framework\Controller\Result\Json')
209+
->disableOriginalConstructor()
210+
->getMock();
211+
212+
$this->sessionManagerMock->expects($this->atLeastOnce())
213+
->method('getQuote')
214+
->willReturn($quoteMock);
215+
$this->resultJsonFactoryMock->expects($this->once())
216+
->method('create')
217+
->willReturn($jsonMock);
218+
$jsonMock->expects($this->once())
219+
->method('setData')
220+
->with($resultExpectation)
221+
->willReturnSelf();
222+
223+
$this->assertEquals($jsonMock, $this->controller->execute());
224+
}
225+
198226
}

0 commit comments

Comments
 (0)