Skip to content

Commit

Permalink
MAGETWO-84639: Correctly set payment information when using paypal #1…
Browse files Browse the repository at this point in the history
  • Loading branch information
ishakhsuvarov authored Jan 3, 2018
2 parents 2cddf69 + b6437bc commit e5efbd4
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 41 deletions.
7 changes: 6 additions & 1 deletion app/code/Magento/Paypal/Model/Express.php
Original file line number Diff line number Diff line change
Expand Up @@ -669,14 +669,19 @@ public function getApi()
public function assignData(\Magento\Framework\DataObject $data)
{
parent::assignData($data);

$additionalData = $data->getData(PaymentInterface::KEY_ADDITIONAL_DATA);

if (!is_array($additionalData)) {
return $this;
}

foreach ($additionalData as $key => $value) {
// Skip extension attributes
if ($key === \Magento\Framework\Api\ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY) {
continue;
}

$this->getInfoInstance()->setAdditionalInformation($key, $value);
}
return $this;
Expand Down
11 changes: 10 additions & 1 deletion app/code/Magento/Paypal/Test/Unit/Model/ExpressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,21 @@ public function testAssignData()
{
$transportValue = 'something';

$extensionAttribute = $this->getMockForAbstractClass(
\Magento\Quote\Api\Data\PaymentExtensionInterface::class,
[],
'',
false,
false
);

$data = new DataObject(
[
PaymentInterface::KEY_ADDITIONAL_DATA => [
Express\Checkout::PAYMENT_INFO_TRANSPORT_BILLING_AGREEMENT => $transportValue,
Express\Checkout::PAYMENT_INFO_TRANSPORT_PAYER_ID => $transportValue,
Express\Checkout::PAYMENT_INFO_TRANSPORT_TOKEN => $transportValue
Express\Checkout::PAYMENT_INFO_TRANSPORT_TOKEN => $transportValue,
\Magento\Framework\Api\ExtensibleDataInterface::EXTENSION_ATTRIBUTES_KEY => $extensionAttribute
]
]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,12 @@
*/

define([
'jquery',
'Magento_Checkout/js/model/quote',
'Magento_Checkout/js/model/url-builder',
'mage/storage',
'Magento_Checkout/js/model/error-processor',
'Magento_Customer/js/model/customer',
'Magento_Checkout/js/model/full-screen-loader'
], function ($, quote, urlBuilder, storage, errorProcessor, customer, fullScreenLoader) {
'Magento_Checkout/js/action/set-payment-information'
], function (quote, setPaymentInformation) {
'use strict';

return function (messageContainer) {
var serviceUrl,
payload,
paymentData = quote.paymentMethod();

/**
* Checkout for guest and registered customer.
*/
if (!customer.isLoggedIn()) {
serviceUrl = urlBuilder.createUrl('/guest-carts/:cartId/set-payment-information', {
cartId: quote.getQuoteId()
});
payload = {
cartId: quote.getQuoteId(),
email: quote.guestEmail,
paymentMethod: paymentData
};
} else {
serviceUrl = urlBuilder.createUrl('/carts/mine/set-payment-information', {});
payload = {
cartId: quote.getQuoteId(),
paymentMethod: paymentData
};
}
fullScreenLoader.startLoader();

return storage.post(
serviceUrl, JSON.stringify(payload)
).fail(function (response) {
errorProcessor.process(response, messageContainer);
}).always(function () {
fullScreenLoader.stopLoader();
});
return setPaymentInformation(messageContainer, quote.paymentMethod());
};
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

define([
'squire'
], function (Squire) {
'use strict';

var injector = new Squire(),
mocks = {
'Magento_Checkout/js/action/place-order': jasmine.createSpy('placeOrderAction'),
'Magento_CheckoutAgreements/js/model/agreements-assigner': jasmine.createSpy('agreementsAssigner')
},
defaultContext = require.s.contexts._,
mixin,
placeOrderAction;

beforeEach(function (done) {
window.checkoutConfig = {
checkoutAgreements: {
isEnabled: true
}
};
injector.mock(mocks);
injector.require([
'Magento_CheckoutAgreements/js/model/place-order-mixin',
'Magento_Checkout/js/action/place-order'
], function (Mixin, placeOrder) {
mixin = Mixin;
placeOrderAction = placeOrder;
done();
});
});

describe('Magento_CheckoutAgreements/js/model/place-order-mixin', function () {
it('mixin is applied to Magento_Checkout/js/action/place-order', function () {
var placeOrderMixins = defaultContext.config.config.mixins['Magento_Checkout/js/action/place-order'];

expect(placeOrderMixins['Magento_CheckoutAgreements/js/model/place-order-mixin']).toBe(true);
});

it('Magento_CheckoutAgreements/js/model/agreements-assigner is called', function () {
var messageContainer = jasmine.createSpy('messageContainer'),
paymentData = {};

mixin(placeOrderAction)(paymentData, messageContainer);
expect(mocks['Magento_CheckoutAgreements/js/model/agreements-assigner'])
.toHaveBeenCalledWith(paymentData);
expect(mocks['Magento_Checkout/js/action/place-order'])
.toHaveBeenCalledWith(paymentData, messageContainer);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

define([
'squire'
], function (Squire) {
'use strict';

var injector = new Squire(),
mocks = {
'Magento_Checkout/js/action/set-payment-information': jasmine.createSpy('placeOrderAction'),
'Magento_CheckoutAgreements/js/model/agreements-assigner': jasmine.createSpy('agreementsAssigner')
},
defaultContext = require.s.contexts._,
mixin,
placeOrderAction;

beforeEach(function (done) {
window.checkoutConfig = {
checkoutAgreements: {
isEnabled: true
}
};
injector.mock(mocks);
injector.require([
'Magento_CheckoutAgreements/js/model/set-payment-information-mixin',
'Magento_Checkout/js/action/set-payment-information'
], function (Mixin, setPaymentInformation) {
mixin = Mixin;
placeOrderAction = setPaymentInformation;
done();
});
});

describe('Magento_CheckoutAgreements/js/model/set-payment-information-mixin', function () {
it('mixin is applied to Magento_Checkout/js/action/set-payment-information', function () {
var placeOrderMixins = defaultContext
.config.config.mixins['Magento_Checkout/js/action/set-payment-information'];

expect(placeOrderMixins['Magento_CheckoutAgreements/js/model/set-payment-information-mixin']).toBe(true);
});

it('Magento_CheckoutAgreements/js/model/agreements-assigner is called', function () {
var messageContainer = jasmine.createSpy('messageContainer'),
paymentData = {};

mixin(placeOrderAction)(messageContainer, paymentData);
expect(mocks['Magento_CheckoutAgreements/js/model/agreements-assigner'])
.toHaveBeenCalledWith(paymentData);
expect(mocks['Magento_Checkout/js/action/set-payment-information'])
.toHaveBeenCalledWith(messageContainer, paymentData);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,24 @@ define([

describe('paypal/js/view/payment/method-renderer/paypal-express-abstract', function () {
var injector = new Squire(),
successPromise = jasmine.createSpyObj('successPromise', ['done']),
setPaymentMock = jasmine.createSpy('set-payment-information', function () {
return successPromise;
}).and.callThrough(),
validateMock = jasmine.createSpy('validate', function () {
return true;
}).and.callThrough(),
mocks = {
'Magento_Checkout/js/model/quote': {
billingAddress: ko.observable(),
shippingAddress: ko.observable(),
paymentMethod: ko.observable(),
totals: ko.observable({})

},
'Magento_Checkout/js/action/set-payment-information': setPaymentMock,
'Magento_Checkout/js/model/payment/additional-validators': {
validate: validateMock
}
},
paypalExpressAbstract,
Expand Down Expand Up @@ -85,6 +96,23 @@ define([
}, 500);
});

it('setPaymentMethodAction is called before redirect to paypal', function () {
spyOn(paypalExpressAbstract, 'selectPaymentMethod');
paypalExpressAbstract.continueToPayPal();
expect(paypalExpressAbstract.selectPaymentMethod).toHaveBeenCalled();
expect(validateMock).toHaveBeenCalled();
expect(validateMock.calls.mostRecent()).toEqual(jasmine.objectContaining({
object: mocks['Magento_Checkout/js/model/payment/additional-validators'],
args: [],
returnValue: true
}));
expect(setPaymentMock).toHaveBeenCalled();
expect(setPaymentMock.calls.mostRecent()).toEqual(jasmine.objectContaining({
returnValue: successPromise
}));
expect(successPromise.done).toHaveBeenCalledWith(jasmine.any(Function));
});

afterAll(function (done) {
tplElement.remove();
done();
Expand Down

0 comments on commit e5efbd4

Please sign in to comment.