| 1 |
var packeteryLoadCheckout = function( $, settings ) { |
| 2 |
var packeteryCheckout = function( settings ) { |
| 3 |
var rateAttrValues = {}; |
| 4 |
if ( settings.savedData ) { |
| 5 |
rateAttrValues = settings.savedData; |
| 6 |
} |
| 7 |
|
| 8 |
var getPacketaWidget = function() { |
| 9 |
var $widgetDiv = $( '#shipping_method input[type="radio"]:checked' ).parent().find( '.packeta-widget' ); |
| 10 |
if ( $widgetDiv.length > 0 ) { |
| 11 |
return $widgetDiv; |
| 12 |
} |
| 13 |
|
| 14 |
return $( '.packeta-widget' ); |
| 15 |
}; |
| 16 |
|
| 17 |
var $widgetDiv = getPacketaWidget(); |
| 18 |
|
| 19 |
var getDestinationAddress = function() { |
| 20 |
var extractDestination = function( section ) { |
| 21 |
var countryValue; |
| 22 |
var $country = $( '#' + section + '_country' ); |
| 23 |
if ( $country.length >= 1 ) { |
| 24 |
countryValue = $country.val().toLowerCase(); |
| 25 |
} |
| 26 |
return { |
| 27 |
street: $( '#' + section + '_address_1' ).val(), |
| 28 |
city: $( '#' + section + '_city' ).val(), |
| 29 |
country: countryValue, |
| 30 |
postCode: $( '#' + section + '_postcode' ).val() |
| 31 |
}; |
| 32 |
}; |
| 33 |
|
| 34 |
if ( $( '#shipping_country:visible' ).length === 1 ) { |
| 35 |
return extractDestination( 'shipping' ); |
| 36 |
} else { |
| 37 |
return extractDestination( 'billing' ); |
| 38 |
} |
| 39 |
}; |
| 40 |
|
| 41 |
var getRateAttrValue = function( carrierRateId, attribute, defaultValue ) { |
| 42 |
if ( typeof rateAttrValues[ carrierRateId ] === 'undefined' || typeof rateAttrValues[ carrierRateId ][ attribute ] === 'undefined' ) { |
| 43 |
return defaultValue; |
| 44 |
} |
| 45 |
|
| 46 |
return rateAttrValues[ carrierRateId ][ attribute ]; |
| 47 |
}; |
| 48 |
|
| 49 |
var shortenShippingRateId = function( rateId ) { |
| 50 |
var rateIdArray = rateId.split(":"); |
| 51 |
return rateIdArray[rateIdArray.length - 1]; |
| 52 |
}; |
| 53 |
|
| 54 |
var getShippingRateId = function() { |
| 55 |
var $selectedRadio = $( '#shipping_method input[type="radio"]:checked' ); |
| 56 |
if ( $selectedRadio.length ) { |
| 57 |
return shortenShippingRateId( $selectedRadio.val() ); |
| 58 |
} |
| 59 |
|
| 60 |
var $selectedHiddenInput = $( '#shipping_method input[type="hidden"]' ); |
| 61 |
if ( $selectedHiddenInput.length ) { |
| 62 |
return shortenShippingRateId( $selectedHiddenInput.val() ); |
| 63 |
} |
| 64 |
|
| 65 |
return null; |
| 66 |
}; |
| 67 |
|
| 68 |
var resetWidgetInfoClasses = function() { |
| 69 |
$widgetDiv.find( '.packeta-widget-info' ).removeClass('packeta-widget-info-error').removeClass('packeta-widget-info-success'); |
| 70 |
}; |
| 71 |
|
| 72 |
var resetWidgetInfo = function() { |
| 73 |
resetWidgetInfoClasses(); |
| 74 |
$widgetDiv.find( '.packeta-widget-info' ).html(''); |
| 75 |
$widgetDiv.find( '.packeta-widget-selected-address' ).html(''); |
| 76 |
}; |
| 77 |
|
| 78 |
var showDeliveryAddress = function(carrierRateId) { |
| 79 |
resetWidgetInfoClasses(); |
| 80 |
if (getRateAttrValue( carrierRateId, settings.homeDeliveryAttrs[ 'isValidated' ].name, '0' ) === '1') { |
| 81 |
$widgetDiv.find( '.packeta-widget-selected-address' ).html( |
| 82 |
getRateAttrValue( carrierRateId, 'packetery_address_street', '' ) |
| 83 |
+ ' ' + |
| 84 |
getRateAttrValue( carrierRateId, 'packetery_address_houseNumber', '' ) |
| 85 |
+ ', ' + |
| 86 |
getRateAttrValue( carrierRateId, 'packetery_address_city', '' ) |
| 87 |
+ ', ' + |
| 88 |
getRateAttrValue( carrierRateId, 'packetery_address_postCode', '' ) |
| 89 |
); |
| 90 |
$widgetDiv.find( '.packeta-widget-info' ).addClass('packeta-widget-info-success').html(settings.translations.addressIsValidated); |
| 91 |
} else if ( 'required' === getAddressValidation( carrierRateId ) ) { |
| 92 |
$widgetDiv.find( '.packeta-widget-info' ).addClass( 'packeta-widget-info-error' ).html( settings.translations.addressIsNotValidatedAndRequiredByCarrier ); |
| 93 |
} else { |
| 94 |
$widgetDiv.find( '.packeta-widget-info' ).addClass( 'packeta-widget-info-error' ).html( settings.translations.addressIsNotValidated ); |
| 95 |
} |
| 96 |
}; |
| 97 |
|
| 98 |
var loadInfoForCarrierRate = function( carrierRateId, attrs ) { |
| 99 |
for ( var attributeKey in attrs ) { |
| 100 |
if ( !attrs.hasOwnProperty( attributeKey ) ) { |
| 101 |
continue; |
| 102 |
} |
| 103 |
|
| 104 |
var attribute = attrs[ attributeKey ].name; |
| 105 |
$( '#' + attribute ).val( getRateAttrValue( carrierRateId, attribute, '' ) ); |
| 106 |
} |
| 107 |
}; |
| 108 |
|
| 109 |
var clearInfo = function( attrs ) { |
| 110 |
for ( var carrierRateId in rateAttrValues ) { |
| 111 |
if ( !rateAttrValues.hasOwnProperty( carrierRateId ) ) { |
| 112 |
continue; |
| 113 |
} |
| 114 |
|
| 115 |
for ( var attributeKey in attrs ) { |
| 116 |
if ( !attrs.hasOwnProperty( attributeKey ) ) { |
| 117 |
continue; |
| 118 |
} |
| 119 |
|
| 120 |
var attribute = attrs[ attributeKey ].name; |
| 121 |
rateAttrValues[ carrierRateId ] = rateAttrValues[ carrierRateId ] || {}; |
| 122 |
rateAttrValues[ carrierRateId ][ attribute ] = ''; |
| 123 |
$( '#' + attribute ).val( '' ); |
| 124 |
} |
| 125 |
} |
| 126 |
$widgetDiv.find( '.packeta-widget-info' ).html( '' ); |
| 127 |
}; |
| 128 |
|
| 129 |
var resetInfo = function( attrs ) { |
| 130 |
for ( var attributeKey in attrs ) { |
| 131 |
if ( !attrs.hasOwnProperty( attributeKey ) ) { |
| 132 |
continue; |
| 133 |
} |
| 134 |
|
| 135 |
var attribute = attrs[ attributeKey ].name; |
| 136 |
$( '#' + attribute ).val( '' ); |
| 137 |
} |
| 138 |
|
| 139 |
$widgetDiv.find( '.packeta-widget-info' ).html( '' ); |
| 140 |
}; |
| 141 |
|
| 142 |
var getAddressValidation = function( carrierRateId ) { |
| 143 |
return settings.carrierConfig[ carrierRateId ][ 'address_validation' ]; |
| 144 |
}; |
| 145 |
|
| 146 |
var hasCarrierConfig = function( carrierRateId ) { |
| 147 |
return typeof settings.carrierConfig[ carrierRateId ] !== 'undefined'; |
| 148 |
}; |
| 149 |
|
| 150 |
var hasPickupPoints = function( carrierRateId ) { |
| 151 |
if ( !hasCarrierConfig( carrierRateId ) ) { |
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
return parseInt( settings.carrierConfig[ carrierRateId ][ 'is_pickup_points' ] ) === 1; |
| 156 |
}; |
| 157 |
|
| 158 |
var hasHomeDelivery = function( carrierRateId ) { |
| 159 |
if ( !hasCarrierConfig( carrierRateId ) ) { |
| 160 |
return false; |
| 161 |
} |
| 162 |
|
| 163 |
return !hasPickupPoints( carrierRateId ); |
| 164 |
}; |
| 165 |
|
| 166 |
var fillHiddenField = function( carrierRateId, name, addressFieldValue ) { |
| 167 |
$( '#' + name ).val( addressFieldValue ); |
| 168 |
rateAttrValues[ carrierRateId ] = rateAttrValues[ carrierRateId ] || {}; |
| 169 |
rateAttrValues[ carrierRateId ][ name ] = addressFieldValue; |
| 170 |
}; |
| 171 |
|
| 172 |
var updateWidgetButtonVisibility = function( carrierRateId, useAutoOpen ) { |
| 173 |
$widgetDiv = getPacketaWidget(); |
| 174 |
$( '.packeta-widget' ).addClass( 'packetery-hidden' ); |
| 175 |
var $widgetButtonRow = $( '.packetery-widget-button-table-row' ); |
| 176 |
$widgetButtonRow.addClass( 'packetery-hidden' ); |
| 177 |
resetInfo( settings.pickupPointAttrs ); // clear active hidden field values |
| 178 |
resetInfo( settings.homeDeliveryAttrs ); |
| 179 |
resetWidgetInfo(); |
| 180 |
|
| 181 |
if ( !hasCarrierConfig( carrierRateId ) ) { |
| 182 |
return; |
| 183 |
} |
| 184 |
|
| 185 |
var _hasPickupPoints = hasPickupPoints( carrierRateId ), |
| 186 |
_hasHomeDelivery = !_hasPickupPoints; |
| 187 |
|
| 188 |
if ( _hasPickupPoints ) { |
| 189 |
loadInfoForCarrierRate( carrierRateId, settings.pickupPointAttrs ); |
| 190 |
$widgetDiv.find( '.packeta-widget-info' ).html( getRateAttrValue( carrierRateId, 'packetery_point_name', '' ) ); |
| 191 |
$widgetDiv.find( 'button' ).html( settings.translations.choosePickupPoint ); |
| 192 |
$widgetButtonRow.removeClass( 'packetery-hidden' ); |
| 193 |
$widgetDiv.removeClass( 'packetery-hidden' ); |
| 194 |
} |
| 195 |
|
| 196 |
if ( |
| 197 |
useAutoOpen && |
| 198 |
_hasPickupPoints && |
| 199 |
settings.widgetAutoOpen && |
| 200 |
$('iframe#packeta-widget').length === 0 && |
| 201 |
!isPickupPointChosen($widgetDiv) |
| 202 |
) { |
| 203 |
$('.packeta-widget-button').click(); |
| 204 |
} |
| 205 |
|
| 206 |
if ( _hasHomeDelivery && 'none' === getAddressValidation( carrierRateId ) ) { |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
if ( _hasHomeDelivery ) { |
| 211 |
loadInfoForCarrierRate( carrierRateId, settings.homeDeliveryAttrs ); |
| 212 |
showDeliveryAddress( carrierRateId ); |
| 213 |
$widgetDiv.find( 'button' ).html( settings.translations.chooseAddress ); |
| 214 |
$widgetButtonRow.removeClass( 'packetery-hidden' ); |
| 215 |
$widgetDiv.removeClass( 'packetery-hidden' ); |
| 216 |
} |
| 217 |
}; |
| 218 |
|
| 219 |
var isPickupPointChosen = function ($widgetDiv) { |
| 220 |
var $pickupPointInfo = $widgetDiv.find('.packeta-widget-info'); |
| 221 |
if ($pickupPointInfo.length === 0) { |
| 222 |
return false; |
| 223 |
} |
| 224 |
return $pickupPointInfo.html() !== ''; |
| 225 |
} |
| 226 |
|
| 227 |
updateWidgetButtonVisibility( getShippingRateId(), true ); |
| 228 |
|
| 229 |
var currentPaymentMethod = $('#payment input[type="radio"]:checked').val(); |
| 230 |
var checkPaymentChange = function() { |
| 231 |
var targetPaymentMethod = $('#payment input[type="radio"]:checked').val(); |
| 232 |
if ( currentPaymentMethod === targetPaymentMethod ) { |
| 233 |
return; |
| 234 |
} |
| 235 |
|
| 236 |
currentPaymentMethod = targetPaymentMethod; |
| 237 |
jQuery('body').trigger('update_checkout'); |
| 238 |
}; |
| 239 |
|
| 240 |
$( document ).on( 'change', '#payment input[type="radio"]', checkPaymentChange ); |
| 241 |
|
| 242 |
var initialDestinationAddress = getDestinationAddress(); |
| 243 |
var initialCarrierRateId = getShippingRateId(); |
| 244 |
$( document ).on( 'updated_checkout', function () { |
| 245 |
$widgetDiv = getPacketaWidget(); |
| 246 |
var destinationAddress = getDestinationAddress(); |
| 247 |
var carrierRateId = getShippingRateId(); |
| 248 |
if ( carrierRateId === null ) { |
| 249 |
carrierRateId = initialCarrierRateId; |
| 250 |
} |
| 251 |
|
| 252 |
var clearSavedData = false; |
| 253 |
if ( hasPickupPoints( initialCarrierRateId ) ) { |
| 254 |
if ( initialDestinationAddress.country !== destinationAddress.country ) { |
| 255 |
clearSavedData = true; |
| 256 |
} |
| 257 |
} else if ( hasHomeDelivery( initialCarrierRateId ) ) { |
| 258 |
if ( |
| 259 |
initialDestinationAddress.country !== destinationAddress.country || |
| 260 |
initialDestinationAddress.street !== destinationAddress.street || |
| 261 |
initialDestinationAddress.city !== destinationAddress.city || |
| 262 |
initialDestinationAddress.postCode !== destinationAddress.postCode |
| 263 |
) { |
| 264 |
clearSavedData = true; |
| 265 |
} |
| 266 |
} |
| 267 |
if ( clearSavedData === true ) { |
| 268 |
clearInfo( settings.pickupPointAttrs ); |
| 269 |
clearInfo( settings.homeDeliveryAttrs ); |
| 270 |
|
| 271 |
$.post( |
| 272 |
settings.removeSavedDataUrl, {} |
| 273 |
).fail( function ( xhr, status, error ) { |
| 274 |
console.log( 'Packeta: Failed to remove saved selected pickup point or validated address data: ' + error ); |
| 275 |
} ); |
| 276 |
|
| 277 |
resetWidgetInfo(); |
| 278 |
} |
| 279 |
|
| 280 |
initialDestinationAddress = destinationAddress; |
| 281 |
initialCarrierRateId = carrierRateId; |
| 282 |
settings.country = destinationAddress.country; |
| 283 |
updateWidgetButtonVisibility( carrierRateId, true ); |
| 284 |
checkPaymentChange(); // If Packeta shipping method with COD is selected, then switch to non-COD shipping method does not trigger payment method input change. |
| 285 |
} ); |
| 286 |
|
| 287 |
$( document ).on( 'change', '#shipping_method input[type="radio"], #shipping_method input[type="hidden"]', function() { |
| 288 |
updateWidgetButtonVisibility( this.value, true ); |
| 289 |
} ); |
| 290 |
|
| 291 |
$( document ).on( 'ajaxStop', function() { |
| 292 |
updateWidgetButtonVisibility( getShippingRateId(), false ); |
| 293 |
|
| 294 |
setTimeout( function() { |
| 295 |
updateWidgetButtonVisibility( getShippingRateId(), false ); |
| 296 |
}, 1000 ); |
| 297 |
} ); |
| 298 |
|
| 299 |
var fillHiddenFields = function( carrierRateId, data, target ) { |
| 300 |
for ( var attrKey in data ) { |
| 301 |
if ( !data.hasOwnProperty( attrKey ) ) { |
| 302 |
continue; |
| 303 |
} |
| 304 |
|
| 305 |
if ( false === data[ attrKey ].isWidgetResultField ) { |
| 306 |
continue; |
| 307 |
} |
| 308 |
|
| 309 |
var widgetField = data[ attrKey ].widgetResultField || attrKey; |
| 310 |
var addressFieldValue = target[ widgetField ]; |
| 311 |
|
| 312 |
fillHiddenField( carrierRateId, data[ attrKey ].name, addressFieldValue ); |
| 313 |
} |
| 314 |
}; |
| 315 |
|
| 316 |
var stringifyOptions = function (widgetOptions) { |
| 317 |
var widgetOptionsArray = []; |
| 318 |
for (const property in widgetOptions) { |
| 319 |
if (!widgetOptions.hasOwnProperty(property)) { |
| 320 |
continue; |
| 321 |
} |
| 322 |
var propertyValue; |
| 323 |
if (typeof widgetOptions[property] === 'object') { |
| 324 |
propertyValue = stringifyOptions(widgetOptions[property]); |
| 325 |
} else { |
| 326 |
propertyValue = widgetOptions[property]; |
| 327 |
} |
| 328 |
widgetOptionsArray.push(property + ': ' + propertyValue); |
| 329 |
} |
| 330 |
return widgetOptionsArray.join(', '); |
| 331 |
}; |
| 332 |
|
| 333 |
$( document ).on( 'click', '.packeta-widget-button', function( e ) { |
| 334 |
e.preventDefault(); |
| 335 |
|
| 336 |
var widgetOptions = { |
| 337 |
country: settings.country, |
| 338 |
language: settings.language |
| 339 |
}; |
| 340 |
|
| 341 |
var carrierRateId = getShippingRateId(); |
| 342 |
if ( hasHomeDelivery( carrierRateId ) ) { |
| 343 |
widgetOptions.layout = 'hd'; |
| 344 |
widgetOptions.appIdentity = settings.appIdentity; |
| 345 |
|
| 346 |
var destinationAddress = getDestinationAddress(); |
| 347 |
widgetOptions.street = destinationAddress.street; |
| 348 |
widgetOptions.city = destinationAddress.city; |
| 349 |
widgetOptions.postcode = destinationAddress.postCode; |
| 350 |
widgetOptions.carrierId = settings.carrierConfig[ carrierRateId ][ 'id' ]; |
| 351 |
|
| 352 |
console.log('Address widget options: apiKey: ' + settings.packeteryApiKey + ', ' + stringifyOptions(widgetOptions)); |
| 353 |
Packeta.Widget.pick( settings.packeteryApiKey, function( result ) { |
| 354 |
resetWidgetInfo(); |
| 355 |
showDeliveryAddress( carrierRateId ); |
| 356 |
|
| 357 |
if ( !result ) { |
| 358 |
resetWidgetInfoClasses(); |
| 359 |
$widgetDiv.find( '.packeta-widget-info' ).addClass('packeta-widget-info-error').html( settings.translations.addressValidationIsOutOfOrder ); |
| 360 |
return; |
| 361 |
} |
| 362 |
|
| 363 |
if ( !result.address ) { |
| 364 |
return; // Widget was closed. |
| 365 |
} |
| 366 |
|
| 367 |
var selectedAddress = result.address; |
| 368 |
|
| 369 |
if ( selectedAddress.country !== widgetOptions.country ) { |
| 370 |
resetWidgetInfoClasses(); |
| 371 |
$widgetDiv.find( '.packeta-widget-info' ).addClass('packeta-widget-info-error').html( settings.translations.invalidAddressCountrySelected ); |
| 372 |
return; |
| 373 |
} |
| 374 |
|
| 375 |
// todo save selected address to shipping address |
| 376 |
|
| 377 |
fillHiddenField( carrierRateId, settings.homeDeliveryAttrs[ 'isValidated' ].name, '1' ); |
| 378 |
fillHiddenFields( carrierRateId, settings.homeDeliveryAttrs, selectedAddress ); |
| 379 |
showDeliveryAddress( carrierRateId ); |
| 380 |
|
| 381 |
var addressDataToSave = rateAttrValues[ carrierRateId ]; |
| 382 |
addressDataToSave.packetery_rate_id = carrierRateId; |
| 383 |
$.post( |
| 384 |
settings.saveValidatedAddressUrl, |
| 385 |
addressDataToSave |
| 386 |
).fail( function ( xhr, status, error ) { |
| 387 |
console.log( 'Packeta: Failed to save validated address data: ' + error ); |
| 388 |
} ); |
| 389 |
}, widgetOptions ); |
| 390 |
} |
| 391 |
|
| 392 |
if ( hasPickupPoints( carrierRateId ) ) { |
| 393 |
widgetOptions.appIdentity = settings.appIdentity; |
| 394 |
widgetOptions.weight = settings.weight; |
| 395 |
widgetOptions.defaultPrice = settings.carrierConfig[ carrierRateId ].defaultPrice; |
| 396 |
widgetOptions.defaultCurrency = settings.carrierConfig[ carrierRateId ].defaultCurrency; |
| 397 |
if ( settings.carrierConfig[ carrierRateId ].carriers ) { |
| 398 |
widgetOptions.carriers = settings.carrierConfig[ carrierRateId ].carriers; |
| 399 |
} |
| 400 |
if ( settings.carrierConfig[ carrierRateId ].vendors ) { |
| 401 |
widgetOptions.vendors = settings.carrierConfig[ carrierRateId ].vendors; |
| 402 |
} |
| 403 |
|
| 404 |
if ( settings.isAgeVerificationRequired ) { |
| 405 |
widgetOptions.livePickupPoint = true; // Pickup points with real person only. |
| 406 |
} |
| 407 |
|
| 408 |
console.log('Pickup point widget options: apiKey: ' + settings.packeteryApiKey + ', ' + stringifyOptions(widgetOptions)); |
| 409 |
Packeta.Widget.pick( settings.packeteryApiKey, function( pickupPoint ) { |
| 410 |
if ( pickupPoint == null ) { |
| 411 |
return; |
| 412 |
} |
| 413 |
resetWidgetInfo(); |
| 414 |
|
| 415 |
fillHiddenFields( carrierRateId, settings.pickupPointAttrs, pickupPoint ); |
| 416 |
$widgetDiv.find( '.packeta-widget-info' ).html( pickupPoint.name ); |
| 417 |
|
| 418 |
var pickupPointDataToSave = rateAttrValues[ carrierRateId ]; |
| 419 |
pickupPointDataToSave.packetery_rate_id = carrierRateId; |
| 420 |
$.post( |
| 421 |
settings.saveSelectedPickupPointUrl, |
| 422 |
pickupPointDataToSave |
| 423 |
).fail( function ( xhr, status, error ) { |
| 424 |
console.log( 'Packeta: Failed to save pickup point data: ' + error ); |
| 425 |
} ); |
| 426 |
}, widgetOptions ); |
| 427 |
} |
| 428 |
} ); |
| 429 |
}; |
| 430 |
|
| 431 |
var dependencies = []; |
| 432 |
dependencies.push( $.getScript( "https://widget.packeta.com/v6/www/js/library.js" ) ); |
| 433 |
|
| 434 |
dependencies.push( |
| 435 |
$.Deferred( function( deferred ) { |
| 436 |
$( deferred.resolve ); // wait for DOM to be loaded |
| 437 |
} ) |
| 438 |
); |
| 439 |
|
| 440 |
$.when.apply( null, dependencies ).done( function() { |
| 441 |
packeteryCheckout( settings ); |
| 442 |
} ); |
| 443 |
}; |
| 444 |
|
| 445 |
packeteryLoadCheckout( jQuery, packeteryCheckoutSettings ); |
| 446 |
|