Skip to content

Commit 60854ad

Browse files
author
Alexander Akimov
authored
Merge pull request #1771 from magento-plankton/MAGETWO-84321
Plankton bugfixes
2 parents d9ebeee + 62f6b4c commit 60854ad

File tree

5 files changed

+290
-142
lines changed

5 files changed

+290
-142
lines changed

app/code/Magento/InstantPurchase/Model/PlaceOrder.php

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
use Magento\InstantPurchase\Model\QuoteManagement\QuoteCreation;
1414
use Magento\InstantPurchase\Model\QuoteManagement\QuoteFilling;
1515
use Magento\InstantPurchase\Model\QuoteManagement\ShippingConfiguration;
16+
use Magento\Quote\Api\CartRepositoryInterface;
1617
use Magento\Store\Model\Store;
18+
use \Throwable;
1719

1820
/**
1921
* Place an order using instant purchase option.
@@ -22,6 +24,11 @@
2224
*/
2325
class PlaceOrder
2426
{
27+
/**
28+
* @var CartRepositoryInterface
29+
*/
30+
private $quoteRepository;
31+
2532
/**
2633
* @var QuoteCreation
2734
*/
@@ -49,19 +56,22 @@ class PlaceOrder
4956

5057
/**
5158
* PlaceOrder constructor.
59+
* @param CartRepositoryInterface $quoteRepository
5260
* @param QuoteCreation $quoteCreation
5361
* @param QuoteFilling $quoteFilling
5462
* @param ShippingConfiguration $shippingConfiguration
5563
* @param PaymentConfiguration $paymentConfiguration
5664
* @param Purchase $purchase
5765
*/
5866
public function __construct(
67+
CartRepositoryInterface $quoteRepository,
5968
QuoteCreation $quoteCreation,
6069
QuoteFilling $quoteFilling,
6170
ShippingConfiguration $shippingConfiguration,
6271
PaymentConfiguration $paymentConfiguration,
6372
Purchase $purchase
6473
) {
74+
$this->quoteRepository = $quoteRepository;
6575
$this->quoteCreation = $quoteCreation;
6676
$this->quoteFilling = $quoteFilling;
6777
$this->shippingConfiguration = $shippingConfiguration;
@@ -79,6 +89,7 @@ public function __construct(
7989
* @param array $productRequest
8090
* @return int order identifier
8191
* @throws LocalizedException if order can not be placed.
92+
* @throws Throwable if unpredictable error occurred.
8293
*/
8394
public function placeOrder(
8495
Store $store,
@@ -98,17 +109,28 @@ public function placeOrder(
98109
$product,
99110
$productRequest
100111
);
101-
$quote = $this->shippingConfiguration->configureShippingMethod(
102-
$quote,
103-
$instantPurchaseOption->getShippingMethod()
104-
);
105-
$quote = $this->paymentConfiguration->configurePayment(
106-
$quote,
107-
$instantPurchaseOption->getPaymentToken()
108-
);
109-
$orderId = $this->purchase->purchase(
110-
$quote
111-
);
112-
return $orderId;
112+
113+
$quote->collectTotals();
114+
$this->quoteRepository->save($quote);
115+
$quote = $this->quoteRepository->get($quote->getId());
116+
117+
try {
118+
$quote = $this->shippingConfiguration->configureShippingMethod(
119+
$quote,
120+
$instantPurchaseOption->getShippingMethod()
121+
);
122+
$quote = $this->paymentConfiguration->configurePayment(
123+
$quote,
124+
$instantPurchaseOption->getPaymentToken()
125+
);
126+
$orderId = $this->purchase->purchase(
127+
$quote
128+
);
129+
return $orderId;
130+
} catch (Throwable $e) {
131+
$quote->setIsActive(false);
132+
$this->quoteRepository->save($quote);
133+
throw $e;
134+
}
113135
}
114136
}
Lines changed: 105 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,115 @@
1-
/*jshint browser:true*/
2-
/*global define*/
3-
define(
4-
[
5-
'uiComponent',
6-
'ko',
7-
'Magento_Ui/js/modal/confirm',
8-
'jquery',
9-
'Magento_Customer/js/customer-data',
10-
'mage/url',
11-
'mage/template',
12-
'jquery/ui',
13-
'mage/translate'
14-
], function (
15-
Component,
16-
ko,
17-
confirm,
18-
$,
19-
customerData,
20-
urlBuilder,
21-
mageTemplate
22-
) {
23-
'use strict';
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
define([
6+
'ko',
7+
'jquery',
8+
'underscore',
9+
'uiComponent',
10+
'Magento_Ui/js/modal/confirm',
11+
'Magento_Customer/js/customer-data',
12+
'mage/url',
13+
'mage/template',
14+
'mage/translate',
15+
'text!Magento_InstantPurchase/template/confirmation.html',
16+
'mage/validation'
17+
], function (ko, $, _, Component, confirm, customerData, urlBuilder, mageTemplate, $t, confirmationTemplate) {
18+
'use strict';
2419

25-
return Component.extend({
26-
showButton: ko.observable(false),
27-
paymentToken: ko.observable(null),
28-
shippingAddress: ko.observable(null),
29-
billingAddress: ko.observable(null),
30-
shippingMethod: ko.observable(null),
31-
defaults: {
32-
template: 'Magento_InstantPurchase/instant-purchase',
33-
buttonText: $.mage.__('Instant Purchase'),
34-
purchaseUrl: urlBuilder.build('instantpurchase/button/placeOrder')
35-
},
36-
options: {
37-
message: $.mage.__('Are you sure you want to place order and pay?'),
38-
formSelector: '#product_addtocart_form',
39-
confirmTemplate: '<p class="message"><%- data.message %></p>' +
40-
'<strong>' + $.mage.__('Shipping Address') + ':</strong>' +
41-
'<p><%- data.shippingAddress().summary %></p>' +
42-
'<strong>' + $.mage.__('Billing Address') + ':</strong>' +
43-
'<p><%- data.billingAddress().summary %></p>' +
44-
'<strong>' + $.mage.__('Payment Method') + ':</strong>\n' +
45-
'<p><%- data.paymentToken().summary %></p>' +
46-
'<strong>' + $.mage.__('Shipping Method') + ':</strong>\n' +
47-
'<p><%- data.shippingMethod().summary %></p>'
48-
},
20+
return Component.extend({
21+
defaults: {
22+
template: 'Magento_InstantPurchase/instant-purchase',
23+
buttonText: $t('Instant Purchase'),
24+
purchaseUrl: urlBuilder.build('instantpurchase/button/placeOrder'),
25+
showButton: false,
26+
paymentToken: null,
27+
shippingAddress: null,
28+
billingAddress: null,
29+
shippingMethod: null,
30+
productFormSelector: '#product_addtocart_form',
31+
confirmationTitle: $t('Instant Purchase Confirmation'),
32+
confirmationData: {
33+
message: $t('Are you sure you want to place order and pay?'),
34+
shippingAddressTitle: $t('Shipping Address'),
35+
billingAddressTitle: $t('Billing Address'),
36+
paymentMethodTitle: $t('Payment Method'),
37+
shippingMethodTitle: $t('Shipping Address')
38+
}
39+
},
4940

50-
/** @inheritdoc */
51-
initialize: function () {
52-
var self = this,
53-
data = customerData.get('instant-purchase')();
41+
/** @inheritdoc */
42+
initialize: function () {
43+
var instantPurchase = customerData.get('instant-purchase');
5444

55-
this._super();
45+
this._super();
5646

57-
self.showButton(data.available);
58-
self.paymentToken(data.paymentToken);
59-
self.shippingAddress(data.shippingAddress);
60-
self.billingAddress(data.billingAddress);
61-
self.shippingMethod(data.shippingMethod);
62-
},
47+
this.setPurchaseData(instantPurchase());
48+
instantPurchase.subscribe(this.setPurchaseData, this);
49+
},
6350

64-
/**
65-
* Confirmation method
66-
*/
67-
instantPurchase: function () {
68-
var self = this,
69-
form = $(self.options.formSelector),
70-
confirmTemplate = mageTemplate(this.options.confirmTemplate);
51+
/** @inheritdoc */
52+
initObservable: function () {
53+
this._super()
54+
.observe('showButton paymentToken shippingAddress billingAddress shippingMethod');
7155

72-
if (!(form.validation() && form.validation('isValid'))) {
73-
return;
74-
}
56+
return this;
57+
},
7558

76-
confirm({
77-
title: $.mage.__('Instant Purchase Confirmation'),
78-
content: confirmTemplate(
79-
{
80-
data: {
81-
message: self.options.message,
82-
paymentToken: self.paymentToken,
83-
shippingAddress: self.shippingAddress,
84-
billingAddress: self.billingAddress,
85-
shippingMethod: self.shippingMethod
86-
}
87-
}
88-
),
89-
actions: {
90-
/** @inheritdoc */
91-
confirm: function () {
92-
$.ajax({
93-
url: self.purchaseUrl,
94-
data: form.serialize(),
95-
type: 'post',
96-
dataType: 'json',
59+
/**
60+
* Set data from customerData.
61+
*
62+
* @param {Object} data
63+
*/
64+
setPurchaseData: function (data) {
65+
this.showButton(data.available);
66+
this.paymentToken(data.paymentToken);
67+
this.shippingAddress(data.shippingAddress);
68+
this.billingAddress(data.billingAddress);
69+
this.shippingMethod(data.shippingMethod);
70+
},
9771

98-
/** Show loader before send */
99-
beforeSend: function () {
100-
$('body').trigger('processStart');
101-
}
102-
}).always(function () {
103-
$('body').trigger('processStop');
104-
});
105-
}
106-
}
72+
/**
73+
* Confirmation method
74+
*/
75+
instantPurchase: function () {
76+
var form = $(this.productFormSelector),
77+
confirmTemplate = mageTemplate(confirmationTemplate),
78+
confirmData = _.extend({}, this.confirmationData, {
79+
paymentToken: this.paymentToken().summary,
80+
shippingAddress: this.shippingAddress().summary,
81+
billingAddress: this.billingAddress().summary,
82+
shippingMethod: this.shippingMethod().summary
10783
});
84+
85+
if (!(form.validation() && form.validation('isValid'))) {
86+
return;
10887
}
109-
});
110-
}
111-
);
88+
89+
confirm({
90+
title: this.confirmationTitle,
91+
content: confirmTemplate({
92+
data: confirmData
93+
}),
94+
actions: {
95+
/** @inheritdoc */
96+
confirm: function () {
97+
$.ajax({
98+
url: this.purchaseUrl,
99+
data: form.serialize(),
100+
type: 'post',
101+
dataType: 'json',
102+
103+
/** Show loader before send */
104+
beforeSend: function () {
105+
$('body').trigger('processStart');
106+
}
107+
}).always(function () {
108+
$('body').trigger('processStop');
109+
});
110+
}.bind(this)
111+
}
112+
});
113+
}
114+
});
115+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!--
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
-->
7+
<p class="message"><%- data.message %></p>
8+
<strong><%- data.shippingAddressTitle %>:</strong>
9+
<p><%- data.shippingAddress %></p>
10+
<strong><%- data.billingAddressTitle %>:</strong>
11+
<p><%- data.billingAddress %></p>
12+
<strong><%- data.paymentMethodTitle %>:</strong>
13+
<p><%- data.paymentToken %></p>
14+
<strong><%- data.shippingMethodTitle %>:</strong>
15+
<p><%- data.shippingMethod %></p>
Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
1-
<!-- ko if: showButton -->
2-
<button type="button"
3-
class="action primary instant-purchase"
4-
data-bind="click: instantPurchase, attr:{title: buttonText}">
5-
<span data-bind="text: buttonText"></span>
6-
</button>
7-
<!-- ko if: paymentToken -->
8-
<input type="hidden"
9-
name="instant_purchase_payment_token"
10-
data-bind="value: paymentToken().publicHash">
11-
<!-- /ko -->
12-
<!-- ko if: shippingAddress -->
13-
<input type="hidden"
14-
name="instant_purchase_shipping_address"
15-
data-bind="value: shippingAddress().id">
16-
<!-- /ko -->
17-
<!-- ko if: billingAddress -->
18-
<input type="hidden"
19-
name="instant_purchase_billing_address"
20-
data-bind="value: billingAddress().id">
21-
<!-- ko if: shippingMethod -->
22-
<input type="hidden"
23-
name="instant_purchase_carrier"
24-
data-bind="value: shippingMethod().carrier">
25-
<input type="hidden"
26-
name="instant_purchase_shipping"
27-
data-bind="value: shippingMethod().method">
28-
<!-- /ko -->
29-
<!-- /ko -->
1+
<!--
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
-->
7+
<if args="showButton()">
8+
<button type="button"
9+
class="action primary instant-purchase"
10+
click="instantPurchase"
11+
attr="title: $t(buttonText)">
12+
<span translate="buttonText" />
13+
</button>
14+
<input if="paymentToken()"
15+
type="hidden"
16+
name="instant_purchase_payment_token"
17+
ko-value="paymentToken().publicHash" />
18+
<input if="shippingAddress()"
19+
type="hidden"
20+
name="instant_purchase_shipping_address"
21+
ko-value="shippingAddress().id" />
22+
<input if="billingAddress()"
23+
type="hidden"
24+
name="instant_purchase_billing_address"
25+
ko-value="billingAddress().id" />
26+
<if args="shippingMethod()">
27+
<input type="hidden"
28+
name="instant_purchase_carrier"
29+
ko-value="shippingMethod().carrier" />
30+
<input type="hidden"
31+
name="instant_purchase_shipping"
32+
ko-value="shippingMethod().method" />
33+
</if>
34+
</if>

0 commit comments

Comments
 (0)