|
1
|
|
|
var ORO_ORDER_EMBED_API = {}; |
|
2
|
|
|
|
|
3
|
|
|
define(function(require) { |
|
4
|
|
|
'use strict'; |
|
5
|
|
|
|
|
6
|
|
|
var PlaceOrderComponent; |
|
7
|
|
|
var BaseComponent = require('oroui/js/app/components/base/component'); |
|
8
|
|
|
var PlaceOrderView = require('oromagento/js/app/views/place-order-view'); |
|
9
|
|
|
var $ = require('jquery'); |
|
10
|
|
|
var _ = require('underscore'); |
|
11
|
|
|
var mediator = require('oroui/js/mediator'); |
|
12
|
|
|
var widgetManager = require('oroui/js/widget-manager'); |
|
13
|
|
|
var messenger = require('oroui/js/messenger'); |
|
14
|
|
|
var __ = require('orotranslation/js/translator'); |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @export oromagento/js/app/components/place-order-component |
|
18
|
|
|
*/ |
|
19
|
|
|
PlaceOrderComponent = BaseComponent.extend({ |
|
20
|
|
|
optionNames: BaseComponent.prototype.optionNames.concat([ |
|
21
|
|
|
'wid', 'errorMessage', 'cartSyncURL', 'customerSyncURL' |
|
22
|
|
|
]), |
|
23
|
|
|
|
|
24
|
|
|
modalWidgetAlias: 'transaction-dialog', |
|
25
|
|
|
|
|
26
|
|
|
messageTemplate: _.template('<%= message %> <a href="<%= url %>" class="order-link"><%= urlLabel %></a> '), |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @inheritDoc |
|
30
|
|
|
*/ |
|
31
|
|
|
constructor: function PlaceOrderComponent() { |
|
32
|
|
|
PlaceOrderComponent.__super__.constructor.apply(this, arguments); |
|
33
|
|
|
}, |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @inheritDoc |
|
37
|
|
|
*/ |
|
38
|
|
|
initialize: function(options) { |
|
39
|
|
|
PlaceOrderComponent.__super__.initialize.apply(this, arguments); |
|
40
|
|
|
|
|
41
|
|
|
// handle error |
|
42
|
|
|
if (this.errorMessage) { |
|
43
|
|
|
this._handleError(); |
|
44
|
|
|
|
|
45
|
|
|
return; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
this.view = new PlaceOrderView({ |
|
49
|
|
|
el: options._sourceElement.find('>iframe') |
|
50
|
|
|
}); |
|
51
|
|
|
|
|
52
|
|
|
widgetManager.getWidgetInstance(this.wid, this.onWidgetLoad.bind(this)); |
|
53
|
|
|
|
|
54
|
|
|
this._setOroOrderEmbedApi(); |
|
55
|
|
|
}, |
|
56
|
|
|
|
|
57
|
|
|
onWidgetLoad: function(widget) { |
|
58
|
|
|
widget._showLoading(); |
|
59
|
|
|
this.listenToOnce(this.view, 'frameLoaded', widget._hideLoading.bind(widget)); |
|
60
|
|
|
}, |
|
61
|
|
|
|
|
62
|
|
|
onOroOrderEmbedApiError: function() { |
|
63
|
|
|
messenger.notificationFlashMessage('error', __('oro.magento.external_error')); |
|
64
|
|
|
widgetManager.getWidgetInstanceByAlias(this.modalWidgetAlias, function(widget) { |
|
65
|
|
|
widget.remove(); |
|
66
|
|
|
}); |
|
67
|
|
|
}, |
|
68
|
|
|
|
|
69
|
|
|
onOroOrderEmbedApiSuccess: function() { |
|
70
|
|
|
var performMessage = messenger.notificationFlashMessage( |
|
71
|
|
|
'warning', __('oro.magento.performing_synchronization') |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
|
|
widgetManager.getWidgetInstanceByAlias(this.modalWidgetAlias, function(widget) { |
|
75
|
|
|
widget.trigger('formSave'); |
|
76
|
|
|
if (this.cartSyncURL) { |
|
77
|
|
|
this._doCartSync(widget, performMessage); |
|
78
|
|
|
} else if (this.customerSyncURL) { |
|
79
|
|
|
this._doCustomerSync(widget, performMessage); |
|
80
|
|
|
} |
|
81
|
|
|
}.bind(this)); |
|
82
|
|
|
}, |
|
83
|
|
|
|
|
84
|
|
|
_handleError: function() { |
|
85
|
|
|
messenger.notificationFlashMessage('error', this.errorMessage); |
|
86
|
|
|
widgetManager.getWidgetInstanceByAlias(this.modalWidgetAlias, function(widget) { |
|
87
|
|
|
widget.remove(); |
|
88
|
|
|
}); |
|
89
|
|
|
}, |
|
90
|
|
|
|
|
91
|
|
|
_showMessage: function(data) { |
|
92
|
|
|
var message = data.message; |
|
93
|
|
|
|
|
94
|
|
|
if (data.statusType === 'success') { |
|
95
|
|
|
message = this.messageTemplate({ |
|
96
|
|
|
message: data.message, |
|
97
|
|
|
url: data.url, |
|
98
|
|
|
urlLabel: __('oro.magento.view_order') |
|
99
|
|
|
}); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
messenger.notificationFlashMessage(data.statusType, message); |
|
103
|
|
|
}, |
|
104
|
|
|
|
|
105
|
|
|
_chooseMessage: function(data) { |
|
106
|
|
|
if (mediator.execute('isInAction')) { |
|
107
|
|
|
mediator.once('page:afterChange', function() { |
|
108
|
|
|
this._showMessage(data); |
|
109
|
|
|
}.bind(this)); |
|
110
|
|
|
} else { |
|
111
|
|
|
this._showMessage(data); |
|
112
|
|
|
} |
|
113
|
|
|
}, |
|
114
|
|
|
|
|
115
|
|
|
_setOroOrderEmbedApi: function() { |
|
116
|
|
|
ORO_ORDER_EMBED_API.success = this.onOroOrderEmbedApiSuccess.bind(this); |
|
117
|
|
|
ORO_ORDER_EMBED_API.error = this.onOroOrderEmbedApiError.bind(this); |
|
118
|
|
|
}, |
|
119
|
|
|
|
|
120
|
|
|
_doCustomerSync: function(widget, performMessage) { |
|
121
|
|
|
$.ajax({ |
|
122
|
|
|
dataType: 'json', |
|
123
|
|
|
url: this.customerSyncURL, |
|
124
|
|
|
beforeSend: function() { |
|
125
|
|
|
widget.remove(); |
|
126
|
|
|
}, |
|
127
|
|
|
success: function(data) { |
|
128
|
|
|
mediator.trigger('datagrid:doReset:magento-customer-orders-widget-grid'); |
|
129
|
|
|
this._chooseMessage(data); |
|
130
|
|
|
}.bind(this), |
|
131
|
|
|
errorHandlerMessage: __('oro.magento.external_error') |
|
132
|
|
|
}).always(performMessage.close); |
|
133
|
|
|
}, |
|
134
|
|
|
|
|
135
|
|
|
_doCartSync: function(widget, performMessage) { |
|
136
|
|
|
$.ajax({ |
|
137
|
|
|
dataType: 'json', |
|
138
|
|
|
url: this.cartSyncURL, |
|
139
|
|
|
beforeSend: function() { |
|
140
|
|
|
widget.remove(); |
|
141
|
|
|
mediator.execute('showLoading'); |
|
142
|
|
|
}, |
|
143
|
|
|
success: function(data) { |
|
144
|
|
|
if (mediator.execute('isInAction')) { |
|
145
|
|
|
mediator.once('page:afterChange', function() { |
|
146
|
|
|
this._showMessage(data); |
|
147
|
|
|
}.bind(this)); |
|
148
|
|
|
} else { |
|
149
|
|
|
mediator.trigger('datagrid:doReset:magento-customer-orders-widget-grid'); |
|
150
|
|
|
this._showMessage(data); |
|
151
|
|
|
mediator.execute('refreshPage'); |
|
152
|
|
|
} |
|
153
|
|
|
}.bind(this) |
|
154
|
|
|
}).always(performMessage.close); |
|
155
|
|
|
} |
|
156
|
|
|
}); |
|
157
|
|
|
|
|
158
|
|
|
return PlaceOrderComponent; |
|
159
|
|
|
}); |
|
160
|
|
|
|