PluginProbe
Packeta / 1.4
Packeta v1.4
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / public / checkout.js

checkout.js in Packeta 1.4, at public/checkout.js

340 lines 11.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 var packeteryLoadCheckout = function( $, settings ) {
2 var packeteryCheckout = function( settings ) {
3 var rateAttrValues = {};
4
5 var getPacketaWidget = function() {
6 var $widgetDiv = $( '#shipping_method input[type="radio"]:checked' ).parent().find( '.packeta-widget' );
7 if ( $widgetDiv.length > 0 ) {
8 return $widgetDiv;
9 }
10
11 return $( '.packeta-widget' );
12 };
13
14 var $widgetDiv = getPacketaWidget();
15
16 var getDestinationAddress = function() {
17 var extractDestination = function( section ) {
18 return {
19 street: $( '#' + section + '_address_1' ).val(),
20 city: $( '#' + section + '_city' ).val(),
21 country: $( '#' + section + '_country' ).val().toLowerCase(),
22 postCode: $( '#' + section + '_postcode' ).val()
23 };
24 };
25
26 if ( $( '#shipping_country:visible' ).length === 1 ) {
27 return extractDestination( 'shipping' );
28 } else {
29 return extractDestination( 'billing' );
30 }
31 };
32
33 var getRateAttrValue = function( carrierRateId, attribute, defaultValue ) {
34 if ( typeof rateAttrValues[ carrierRateId ] === 'undefined' || typeof rateAttrValues[ carrierRateId ][ attribute ] === 'undefined' ) {
35 return defaultValue;
36 }
37
38 return rateAttrValues[ carrierRateId ][ attribute ];
39 }
40
41 var shortenShippingRateId = function( rateId ) {
42 var rateIdArray = rateId.split(":");
43 return rateIdArray[rateIdArray.length - 1];
44 };
45
46 var getShippingRateId = function() {
47 var $selectedRadio = $( '#shipping_method input[type="radio"]:checked' );
48 if ( $selectedRadio.length ) {
49 return shortenShippingRateId( $selectedRadio.val() );
50 }
51
52 return shortenShippingRateId( $( '#shipping_method input[type="hidden"]' ).val() );
53 };
54
55 var resetWidgetInfoClasses = function() {
56 $widgetDiv.find( '.packeta-widget-info' ).removeClass('packeta-widget-info-error').removeClass('packeta-widget-info-success');
57 };
58
59 var resetWidgetInfo = function() {
60 resetWidgetInfoClasses();
61 $widgetDiv.find( '.packeta-widget-info' ).html('');
62 $widgetDiv.find( '.packeta-widget-selected-address' ).html('');
63 };
64
65 var showDeliveryAddress = function(carrierRateId) {
66 resetWidgetInfoClasses();
67 if (getRateAttrValue( carrierRateId, settings.homeDeliveryAttrs[ 'isValidated' ].name, '0' ) === '1') {
68 $widgetDiv.find( '.packeta-widget-selected-address' ).html(
69 getRateAttrValue( carrierRateId, 'packetery_address_street', '' )
70 + ' ' +
71 getRateAttrValue( carrierRateId, 'packetery_address_houseNumber', '' )
72 + ', ' +
73 getRateAttrValue( carrierRateId, 'packetery_address_city', '' )
74 + ', ' +
75 getRateAttrValue( carrierRateId, 'packetery_address_postCode', '' )
76 );
77 $widgetDiv.find( '.packeta-widget-info' ).addClass('packeta-widget-info-success').html(settings.translations.addressIsValidated);
78 } else if ( 'required' === getAddressValidation( carrierRateId ) ) {
79 $widgetDiv.find( '.packeta-widget-info' ).addClass( 'packeta-widget-info-error' ).html( settings.translations.addressIsNotValidatedAndRequiredByCarrier );
80 } else {
81 $widgetDiv.find( '.packeta-widget-info' ).addClass( 'packeta-widget-info-error' ).html( settings.translations.addressIsNotValidated );
82 }
83 };
84
85 var loadInfoForCarrierRate = function( carrierRateId, attrs ) {
86 for ( var attributeKey in attrs ) {
87 if ( !attrs.hasOwnProperty( attributeKey ) ) {
88 continue;
89 }
90
91 var attribute = attrs[ attributeKey ].name;
92 $( '#' + attribute ).val( getRateAttrValue( carrierRateId, attribute, '' ) );
93 }
94 };
95
96 var clearInfo = function( attrs ) {
97 for ( var carrierRateId in rateAttrValues ) {
98 if ( !rateAttrValues.hasOwnProperty( carrierRateId ) ) {
99 continue;
100 }
101
102 for ( var attributeKey in attrs ) {
103 if ( !attrs.hasOwnProperty( attributeKey ) ) {
104 continue;
105 }
106
107 var attribute = attrs[ attributeKey ].name;
108 rateAttrValues[ carrierRateId ] = rateAttrValues[ carrierRateId ] || {};
109 rateAttrValues[ carrierRateId ][ attribute ] = '';
110 $( '#' + attribute ).val( '' );
111 }
112 }
113 $widgetDiv.find( '.packeta-widget-info' ).html( '' );
114 };
115
116 var resetInfo = function( attrs ) {
117 for ( var attributeKey in attrs ) {
118 if ( !attrs.hasOwnProperty( attributeKey ) ) {
119 continue;
120 }
121
122 var attribute = attrs[ attributeKey ].name;
123 $( '#' + attribute ).val( '' );
124 }
125
126 $widgetDiv.find( '.packeta-widget-info' ).html( '' );
127 };
128
129 var getAddressValidation = function( carrierRateId ) {
130 return settings.carrierConfig[ carrierRateId ][ 'address_validation' ];
131 };
132
133 var hasCarrierConfig = function( carrierRateId ) {
134 return typeof settings.carrierConfig[ carrierRateId ] !== 'undefined';
135 };
136
137 var hasPickupPoints = function( carrierRateId ) {
138 if ( !hasCarrierConfig( carrierRateId ) ) {
139 return false;
140 }
141
142 return parseInt( settings.carrierConfig[ carrierRateId ][ 'is_pickup_points' ] ) === 1;
143 };
144
145 var hasHomeDelivery = function( carrierRateId ) {
146 if ( !hasCarrierConfig( carrierRateId ) ) {
147 return false;
148 }
149
150 return !hasPickupPoints( carrierRateId );
151 };
152
153 var updateWidgetButtonVisibility = function( carrierRateId ) {
154 $widgetDiv = getPacketaWidget();
155 $( '.packeta-widget' ).addClass( 'packetery-hidden' );
156 var $widgetButtonRow = $( '.packetery-widget-button-table-row' );
157 $widgetButtonRow.addClass( 'packetery-hidden' );
158 resetInfo( settings.pickupPointAttrs ); // clear active hidden field values
159 resetInfo( settings.homeDeliveryAttrs );
160 resetWidgetInfo();
161
162 if ( !hasCarrierConfig( carrierRateId ) ) {
163 return;
164 }
165
166 var _hasPickupPoints = hasPickupPoints( carrierRateId ),
167 _hasHomeDelivery = !_hasPickupPoints;
168
169 if ( _hasPickupPoints ) {
170 loadInfoForCarrierRate( carrierRateId, settings.pickupPointAttrs );
171 $widgetDiv.find( '.packeta-widget-info' ).html( getRateAttrValue( carrierRateId, 'packetery_point_name', '' ) );
172 $widgetDiv.find( 'button' ).html( settings.translations.choosePickupPoint );
173 $widgetButtonRow.removeClass( 'packetery-hidden' );
174 $widgetDiv.removeClass( 'packetery-hidden' );
175 }
176
177 if ( _hasHomeDelivery && 'none' === getAddressValidation( carrierRateId ) ) {
178 return;
179 }
180
181 if ( _hasHomeDelivery ) {
182 loadInfoForCarrierRate( carrierRateId, settings.homeDeliveryAttrs );
183 showDeliveryAddress( carrierRateId );
184 $widgetDiv.find( 'button' ).html( settings.translations.chooseAddress );
185 $widgetButtonRow.removeClass( 'packetery-hidden' );
186 $widgetDiv.removeClass( 'packetery-hidden' );
187 }
188 };
189
190 updateWidgetButtonVisibility( getShippingRateId() );
191
192 var currentPaymentMethod = $('#payment input[type="radio"]:checked').val();
193 var checkPaymentChange = function() {
194 var targetPaymentMethod = $('#payment input[type="radio"]:checked').val();
195 if ( currentPaymentMethod === targetPaymentMethod ) {
196 return;
197 }
198
199 currentPaymentMethod = targetPaymentMethod;
200 jQuery('body').trigger('update_checkout');
201 };
202
203 $(document).on('change', '#payment input[type="radio"]', checkPaymentChange);
204 $( document ).on( 'updated_checkout', function() {
205 $widgetDiv = getPacketaWidget();
206 var destinationAddress = getDestinationAddress();
207 if ( destinationAddress.country !== settings.country ) {
208 clearInfo( settings.pickupPointAttrs );
209 clearInfo( settings.homeDeliveryAttrs );
210 resetWidgetInfo();
211 settings.country = destinationAddress.country;
212 }
213
214 updateWidgetButtonVisibility( getShippingRateId() );
215 checkPaymentChange(); // If Packeta shipping method with COD is selected, then switch to non-COD shipping method does not trigger payment method input change.
216 } );
217
218 $( document ).on( 'change', '#shipping_method input[type="radio"], #shipping_method input[type="hidden"]', function() {
219 updateWidgetButtonVisibility( this.value );
220 } );
221
222 $( document ).on( 'ajaxStop', function() {
223 updateWidgetButtonVisibility( getShippingRateId() );
224
225 setTimeout( function() {
226 updateWidgetButtonVisibility( getShippingRateId() );
227 }, 1000 );
228 } );
229
230 var fillHiddenField = function( carrierRateId, name, addressFieldValue ) {
231 $( '#' + name ).val( addressFieldValue );
232 rateAttrValues[ carrierRateId ] = rateAttrValues[ carrierRateId ] || {};
233 rateAttrValues[ carrierRateId ][ name ] = addressFieldValue;
234 };
235
236 var fillHiddenFields = function( carrierRateId, data, target ) {
237 for ( var attrKey in data ) {
238 if ( !data.hasOwnProperty( attrKey ) ) {
239 continue;
240 }
241
242 if ( false === data[ attrKey ].isWidgetResultField ) {
243 continue;
244 }
245
246 var widgetField = data[ attrKey ].widgetResultField || attrKey;
247 var addressFieldValue = target[ widgetField ];
248
249 fillHiddenField( carrierRateId, data[ attrKey ].name, addressFieldValue );
250 }
251 };
252
253 $( document ).on( 'click', '.packeta-widget-button', function( e ) {
254 e.preventDefault();
255
256 var widgetOptions = {
257 country: settings.country,
258 language: settings.language
259 };
260
261 var carrierRateId = getShippingRateId();
262 if ( hasHomeDelivery( carrierRateId ) ) {
263 widgetOptions.layout = 'hd';
264 widgetOptions.appIdentity = settings.appIdentity;
265
266 var destinationAddress = getDestinationAddress();
267 widgetOptions.street = destinationAddress.street;
268 widgetOptions.city = destinationAddress.city;
269 widgetOptions.postcode = destinationAddress.postCode;
270 widgetOptions.carrierId = settings.carrierConfig[ carrierRateId ][ 'id' ];
271
272 PacketaHD.Widget.pick( settings.packeteryApiKey, function( result ) {
273 resetWidgetInfo();
274 showDeliveryAddress( carrierRateId );
275
276 if ( !result ) {
277 resetWidgetInfoClasses();
278 $widgetDiv.find( '.packeta-widget-info' ).addClass('packeta-widget-info-error').html( settings.translations.addressValidationIsOutOfOrder );
279 return;
280 }
281
282 if ( !result.address ) {
283 return; // Widget was closed.
284 }
285
286 var selectedAddress = result.address;
287
288 if ( selectedAddress.country !== widgetOptions.country ) {
289 resetWidgetInfoClasses();
290 $widgetDiv.find( '.packeta-widget-info' ).addClass('packeta-widget-info-error').html( settings.translations.invalidAddressCountrySelected );
291 return;
292 }
293
294 // todo save selected address to shipping address
295
296 fillHiddenField( carrierRateId, settings.homeDeliveryAttrs[ 'isValidated' ].name, '1' );
297 fillHiddenFields( carrierRateId, settings.homeDeliveryAttrs, selectedAddress );
298 showDeliveryAddress( carrierRateId );
299 }, widgetOptions );
300 }
301
302 if ( hasPickupPoints( carrierRateId ) ) {
303 widgetOptions.appIdentity = settings.appIdentity;
304 widgetOptions.weight = settings.weight;
305 widgetOptions.carriers = settings.carrierConfig[ carrierRateId ].carriers;
306
307 if ( settings.isAgeVerificationRequired ) {
308 widgetOptions.livePickupPoint = true; // Pickup points with real person only.
309 }
310
311 Packeta.Widget.pick( settings.packeteryApiKey, function( pickupPoint ) {
312 if ( pickupPoint == null ) {
313 return;
314 }
315 resetWidgetInfo();
316
317 fillHiddenFields( carrierRateId, settings.pickupPointAttrs, pickupPoint );
318 $widgetDiv.find( '.packeta-widget-info' ).html( pickupPoint.name );
319 }, widgetOptions );
320 }
321 } );
322 };
323
324 var dependencies = [];
325 dependencies.push( $.getScript( "https://widget.packeta.com/v6/www/js/library.js" ) );
326 dependencies.push( $.getScript( "https://hd.widget.packeta.com/www/js/library-hd.js" ) );
327
328 dependencies.push(
329 $.Deferred( function( deferred ) {
330 $( deferred.resolve ); // wait for DOM to be loaded
331 } )
332 );
333
334 $.when.apply( null, dependencies ).done( function() {
335 packeteryCheckout( settings );
336 } );
337 };
338
339 packeteryLoadCheckout( jQuery, packeteryCheckoutSettings );
340