Skip to content

Commit

Permalink
Merge pull request #3326 from magento-mpi/mpi-forwardport-1010
Browse files Browse the repository at this point in the history
[MPI]-forwardport-1010
  • Loading branch information
joanhe authored Oct 30, 2018
2 parents 77af5d6 + de0d090 commit fdf16cb
Show file tree
Hide file tree
Showing 19 changed files with 614 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public function build(array $buildSubject)

$payment = $paymentDO->getPayment();
$data = $payment->getAdditionalInformation();
// the payment token could be stored only if a customer checks the Vault flow on storefront
// see https://developers.braintreepayments.com/guides/paypal/vault/javascript/v2#invoking-the-vault-flow
if (!empty($data[VaultConfigProvider::IS_ACTIVE_CODE])) {
$result[self::$optionsKey] = [
self::$storeInVaultOnSuccess => true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
namespace Magento\Braintree\Gateway\Request;

use Magento\Braintree\Gateway\SubjectReader;
use Magento\Framework\Exception\LocalizedException;
use Magento\Payment\Gateway\Command\CommandException;
use Magento\Payment\Gateway\Request\BuilderInterface;
use Magento\Payment\Helper\Formatter;

Expand Down Expand Up @@ -41,6 +43,9 @@ public function build(array $buildSubject)
$payment = $paymentDO->getPayment();
$extensionAttributes = $payment->getExtensionAttributes();
$paymentToken = $extensionAttributes->getVaultPaymentToken();
if ($paymentToken === null) {
throw new CommandException(__('The Payment Token is not available to perform the request.'));
}
return [
'amount' => $this->formatPrice($this->subjectReader->readAmount($buildSubject)),
'paymentMethodToken' => $paymentToken->getGatewayToken()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private function updateBillingAddress(Quote $quote, array $details)
{
$billingAddress = $quote->getBillingAddress();

if ($this->config->isRequiredBillingAddress()) {
if ($this->config->isRequiredBillingAddress() && !empty($details['billingAddress'])) {
$this->updateAddressData($billingAddress, $details['billingAddress']);
} else {
$this->updateAddressData($billingAddress, $details['shippingAddress']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Braintree\Test\Unit\Gateway\Request;

use Magento\Braintree\Gateway\SubjectReader;
use Magento\Braintree\Gateway\Request\VaultCaptureDataBuilder;
use Magento\Braintree\Gateway\SubjectReader;
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
use Magento\Sales\Api\Data\OrderPaymentExtension;
use Magento\Sales\Model\Order\Payment;
Expand All @@ -26,47 +28,46 @@ class VaultCaptureDataBuilderTest extends \PHPUnit\Framework\TestCase
/**
* @var PaymentDataObjectInterface|MockObject
*/
private $paymentDOMock;
private $paymentDO;

/**
* @var Payment|MockObject
*/
private $paymentMock;
private $payment;

/**
* @var SubjectReader|\PHPUnit_Framework_MockObject_MockObject
* @var SubjectReader|MockObject
*/
private $subjectReaderMock;
private $subjectReader;

/**
* @inheritdoc
*/
protected function setUp()
protected function setUp(): void
{
$this->paymentDOMock = $this->createMock(PaymentDataObjectInterface::class);
$this->paymentMock = $this->getMockBuilder(Payment::class)
$this->paymentDO = $this->createMock(PaymentDataObjectInterface::class);
$this->payment = $this->getMockBuilder(Payment::class)
->disableOriginalConstructor()
->getMock();
$this->paymentDOMock->expects(static::once())
->method('getPayment')
->willReturn($this->paymentMock);
$this->paymentDO->method('getPayment')
->willReturn($this->payment);

$this->subjectReaderMock = $this->getMockBuilder(SubjectReader::class)
$this->subjectReader = $this->getMockBuilder(SubjectReader::class)
->disableOriginalConstructor()
->getMock();

$this->builder = new VaultCaptureDataBuilder($this->subjectReaderMock);
$this->builder = new VaultCaptureDataBuilder($this->subjectReader);
}

/**
* \Magento\Braintree\Gateway\Request\VaultCaptureDataBuilder::build
* Checks the result after builder execution.
*/
public function testBuild()
public function testBuild(): void
{
$amount = 30.00;
$token = '5tfm4c';
$buildSubject = [
'payment' => $this->paymentDOMock,
'payment' => $this->paymentDO,
'amount' => $amount,
];

Expand All @@ -75,36 +76,68 @@ public function testBuild()
'paymentMethodToken' => $token,
];

$this->subjectReaderMock->expects(self::once())
->method('readPayment')
$this->subjectReader->method('readPayment')
->with($buildSubject)
->willReturn($this->paymentDOMock);
$this->subjectReaderMock->expects(self::once())
->method('readAmount')
->willReturn($this->paymentDO);
$this->subjectReader->method('readAmount')
->with($buildSubject)
->willReturn($amount);

$paymentExtensionMock = $this->getMockBuilder(OrderPaymentExtension::class)
/** @var OrderPaymentExtension|MockObject $paymentExtension */
$paymentExtension = $this->getMockBuilder(OrderPaymentExtension::class)
->setMethods(['getVaultPaymentToken'])
->disableOriginalConstructor()
->getMockForAbstractClass();

$paymentTokenMock = $this->getMockBuilder(PaymentToken::class)
/** @var PaymentToken|MockObject $paymentToken */
$paymentToken = $this->getMockBuilder(PaymentToken::class)
->disableOriginalConstructor()
->getMock();

$paymentExtensionMock->expects(static::once())
->method('getVaultPaymentToken')
->willReturn($paymentTokenMock);
$this->paymentMock->expects(static::once())
->method('getExtensionAttributes')
->willReturn($paymentExtensionMock);
$paymentExtension->method('getVaultPaymentToken')
->willReturn($paymentToken);
$this->payment->method('getExtensionAttributes')
->willReturn($paymentExtension);

$paymentTokenMock->expects(static::once())
->method('getGatewayToken')
$paymentToken->method('getGatewayToken')
->willReturn($token);

$result = $this->builder->build($buildSubject);
self::assertEquals($expected, $result);
}

/**
* Checks a builder execution if Payment Token doesn't exist.
*
* @expectedException \Magento\Payment\Gateway\Command\CommandException
* @expectedExceptionMessage The Payment Token is not available to perform the request.
*/
public function testBuildWithoutPaymentToken(): void
{
$amount = 30.00;
$buildSubject = [
'payment' => $this->paymentDO,
'amount' => $amount,
];

$this->subjectReader->method('readPayment')
->with($buildSubject)
->willReturn($this->paymentDO);
$this->subjectReader->method('readAmount')
->with($buildSubject)
->willReturn($amount);

/** @var OrderPaymentExtension|MockObject $paymentExtension */
$paymentExtension = $this->getMockBuilder(OrderPaymentExtension::class)
->setMethods(['getVaultPaymentToken'])
->disableOriginalConstructor()
->getMockForAbstractClass();

$this->payment->method('getExtensionAttributes')
->willReturn($paymentExtension);
$paymentExtension->method('getVaultPaymentToken')
->willReturn(null);

$this->builder->build($buildSubject);
}
}
Loading

0 comments on commit fdf16cb

Please sign in to comment.