Skip to content

Commit

Permalink
Merge pull request #87 from magento-vanilla/PR-MPI-VANILLA
Browse files Browse the repository at this point in the history
[Vanilla + MPI] Bugfix
  • Loading branch information
dvoskoboinikov committed Jun 7, 2016
2 parents 3fbefa4 + 716ad67 commit 658b520
Show file tree
Hide file tree
Showing 74 changed files with 974 additions and 580 deletions.
3 changes: 2 additions & 1 deletion app/code/Magento/Backend/Model/Session/Quote.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,9 @@ public function getQuote()
if ($this->getStoreId()) {
if (!$this->getQuoteId()) {
$this->_quote->setCustomerGroupId($this->groupManagement->getDefaultGroup()->getId());
$this->_quote->setIsActive(false);
$this->_quote->setStoreId($this->getStoreId());

$this->quoteRepository->save($this->_quote);
$this->setQuoteId($this->_quote->getId());
$this->_quote = $this->quoteRepository->get($this->getQuoteId(), [$this->getStoreId()]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ public function testGetQuoteWithoutQuoteId()
'',
false
);

$this->quoteRepositoryMock->expects($this->once())->method('get')->willReturn($quoteMock);
$cartInterfaceMock->expects($this->once())
->method('setCustomerGroupId')
Expand All @@ -357,7 +358,6 @@ public function testGetQuoteWithoutQuoteId()
$quoteMock->expects($this->once())
->method('setIsSuperMode')
->with(true);

$this->assertEquals($quoteMock, $this->quote->getQuote());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ require([
showHour: false,
showMinute: false,
serverTimezoneSeconds: <?php echo (int) $block->getStoreTimestamp(); ?>,
serverTimezoneOffset: <?php echo (int) $block->getTimezoneOffsetSeconds(); ?>,
yearRange: '<?php /* @escapeNotVerified */ echo $block->getYearRange() ?>'
}
});
Expand Down
28 changes: 17 additions & 11 deletions app/code/Magento/Braintree/Controller/Paypal/Review.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Magento\Framework\Controller\ResultFactory;
use Magento\Braintree\Gateway\Config\PayPal\Config;
use Magento\Braintree\Model\Paypal\Helper\QuoteUpdater;
use Magento\Framework\Exception\LocalizedException;

/**
* Class Review
Expand All @@ -21,6 +22,11 @@ class Review extends AbstractAction
*/
private $quoteUpdater;

/**
* @var string
*/
private static $paymentMethodNonce = 'payment_method_nonce';

/**
* Constructor
*
Expand Down Expand Up @@ -52,13 +58,16 @@ public function execute()

try {
$this->validateQuote($quote);
$this->validateRequestData($requestData);

$this->quoteUpdater->execute(
$requestData['nonce'],
$requestData['details'],
$quote
);
if ($this->validateRequestData($requestData)) {
$this->quoteUpdater->execute(
$requestData['nonce'],
$requestData['details'],
$quote
);
} elseif (!$quote->getPayment()->getAdditionalInformation(self::$paymentMethodNonce)) {
throw new LocalizedException(__('We can\'t initialize checkout.'));
}

/** @var \Magento\Framework\View\Result\Page $resultPage */
$resultPage = $this->resultFactory->create(ResultFactory::TYPE_PAGE);
Expand All @@ -82,13 +91,10 @@ public function execute()

/**
* @param array $requestData
* @return void
* @throws \InvalidArgumentException
* @return boolean
*/
private function validateRequestData(array $requestData)
{
if (empty($requestData['nonce']) || empty($requestData['details'])) {
throw new \InvalidArgumentException('Data of request cannot be empty.');
}
return !empty($requestData['nonce']) && !empty($requestData['details']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public function testExecuteException()

$quoteMock->expects(self::once())
->method('getItemsCount')
->willReturn(1);
->willReturn(0);

$this->requestMock->expects(self::once())
->method('getPostValue')
Expand All @@ -188,7 +188,54 @@ public function testExecuteException()
->method('addExceptionMessage')
->with(
self::isInstanceOf('\InvalidArgumentException'),
'Data of request cannot be empty.'
'We can\'t initialize checkout.'
);

$this->resultFactoryMock->expects(self::once())
->method('create')
->with(ResultFactory::TYPE_REDIRECT)
->willReturn($resultRedirectMock);

$resultRedirectMock->expects(self::once())
->method('setPath')
->with('checkout/cart')
->willReturnSelf();

self::assertEquals($this->review->execute(), $resultRedirectMock);
}

public function testExecuteExceptionPaymentWithoutNonce()
{
$result = '{}';
$quoteMock = $this->getQuoteMock();
$resultRedirectMock = $this->getResultRedirectMock();

$quoteMock->expects(self::once())
->method('getItemsCount')
->willReturn(1);

$paymentMock = $this->getMockBuilder(\Magento\Quote\Model\Quote\Payment::class)
->disableOriginalConstructor()
->getMock();

$quoteMock->expects(self::once())
->method('getPayment')
->willReturn($paymentMock);

$this->requestMock->expects(self::once())
->method('getPostValue')
->with('result', '{}')
->willReturn($result);

$this->checkoutSessionMock->expects(self::once())
->method('getQuote')
->willReturn($quoteMock);

$this->messageManagerMock->expects(self::once())
->method('addExceptionMessage')
->with(
self::isInstanceOf(\Magento\Framework\Exception\LocalizedException::class),
'We can\'t initialize checkout.'
);

$this->resultFactoryMock->expects(self::once())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ $config = [
data-locale="<?php /* @noEscape */ echo $block->getLocale(); ?>"
data-amount="<?php /* @noEscape */ echo $block->getAmount(); ?>"
id="<?php /* @noEscape */ echo $id; ?>"
class="action-braintree-paypal-logo">
class="action-braintree-paypal-logo" disabled>
<img class="braintree-paypal-button-hidden"
src="https://checkout.paypal.com/pwpp/2.17.6/images/pay-with-paypal.png"
alt="Pay with PayPal"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
define(
[
'rjsResolver',
'uiRegistry',
'uiComponent',
'underscore',
Expand All @@ -13,6 +14,7 @@ define(
'domReady!'
],
function (
resolver,
registry,
Component,
_,
Expand Down Expand Up @@ -47,8 +49,10 @@ define(
* @param {Object} integration
*/
onReady: function (integration) {
registry.set(this.integrationName, integration);
$('#' + this.id).removeAttr('disabled');
resolver(function () {
registry.set(this.integrationName, integration);
$('#' + this.id).removeAttr('disabled');
}, this);
},

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ define([
return {
apiClient: null,
config: {},
checkout: null,

/**
* Get Braintree api client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,11 @@ define(
/**
* Device data initialization
*
* @param {Object} braintreeInstance
* @param {Object} checkout
*/
onReady: function (braintreeInstance) {
this.additionalData['device_data'] = braintreeInstance.deviceData;
onReady: function (checkout) {
braintree.checkout = checkout;
this.additionalData['device_data'] = checkout.deviceData;
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ define([
], function ($, _, Component, Braintree, quote, fullScreenLoader, additionalValidators) {
'use strict';

var checkout;

return Component.extend({
defaults: {
template: 'Magento_Braintree/payment/paypal',
Expand All @@ -33,10 +31,10 @@ define([

/**
* Triggers when widget is loaded
* @param {Object} integration
* @param {Object} checkout
*/
onReady: function (integration) {
checkout = integration;
onReady: function (checkout) {
Braintree.checkout = checkout;
this.enableButton();
},

Expand Down Expand Up @@ -144,25 +142,49 @@ define([
this.paymentMethodNonce = paymentMethodNonce;
},

/**
* Update quote billing address
* @param {Object}customer
* @param {Object}address
*/
setBillingAddress: function (customer, address) {
var billingAddress = {
street: [address.streetAddress],
city: address.locality,
regionCode: address.region,
postcode: address.postalCode,
countryId: address.countryCodeAlpha2,
firstname: customer.firstName,
lastname: customer.lastName,
telephone: customer.phone
};

quote.billingAddress(billingAddress);
},

/**
* Prepare data to place order
* @param {Object} data
*/
beforePlaceOrder: function (data) {
this.setPaymentMethodNonce(data.nonce);

if (quote.billingAddress() === null && typeof data.details.billingAddress !== 'undefined') {
this.setBillingAddress(data.details, data.details.billingAddress);
}
this.placeOrder();
},

/**
* Re-init PayPal Auth Flow
*/
reInitPayPal: function () {
if (!checkout) {
return;
if (Braintree.checkout) {
Braintree.checkout.teardown(function () {
Braintree.checkout = null;
});
}
checkout.teardown(function () {
checkout = null;
});

this.disableButton();
this.clientConfig.paypal.amount = this.grandTotalAmount;

Expand All @@ -175,7 +197,7 @@ define([
*/
payWithPayPal: function () {
if (additionalValidators.validate()) {
checkout.paypal.initAuthFlow();
Braintree.checkout.paypal.initAuthFlow();
}
},

Expand All @@ -200,8 +222,7 @@ define([
* @returns {Object}
*/
getPayPalConfig: function () {
var address = quote.shippingAddress(),
totals = quote.totals(),
var totals = quote.totals(),
config = {};

config.paypal = {
Expand All @@ -212,16 +233,6 @@ define([
currency: totals['base_currency_code'],
locale: this.getLocale(),
enableShippingAddress: true,
shippingAddressOverride: {
recipientName: address.firstname + ' ' + address.lastname,
streetAddress: address.street[0],
locality: address.city,
countryCodeAlpha2: address.countryId,
postalCode: address.postcode,
region: address.regionCode,
phone: address.telephone,
editable: this.isAllowOverrideShippingAddress()
},

/**
* Triggers on any Braintree error
Expand All @@ -238,13 +249,39 @@ define([
}
};

config.paypal.shippingAddressOverride = this.getShippingAddress();

if (this.getMerchantName()) {
config.paypal.displayName = this.getMerchantName();
}

return config;
},

/**
* Get shipping address
* @returns {Object}
*/
getShippingAddress: function () {
var address = quote.shippingAddress();

if (address.postcode === null) {

return {};
}

return {
recipientName: address.firstname + ' ' + address.lastname,
streetAddress: address.street[0],
locality: address.city,
countryCodeAlpha2: address.countryId,
postalCode: address.postcode,
region: address.regionCode,
phone: address.telephone,
editable: this.isAllowOverrideShippingAddress()
};
},

/**
* Get merchant name
* @returns {String}
Expand Down Expand Up @@ -279,6 +316,8 @@ define([
* Disable submit button
*/
disableButton: function () {
// stop any previous shown loaders
fullScreenLoader.stopLoader();
fullScreenLoader.startLoader();
$('[data-button="place"]').attr('disabled', 'disabled');
},
Expand Down
Loading

0 comments on commit 658b520

Please sign in to comment.