PageSourceSearch

https://mlf.no/wp-content/plugins/woo-vipps/payment/js/vipps.js

js mlf.no collected 2026-10-08 10:45:34 UTC 11,132 bytes, 265 lines download raw bytes

1/*
2
3This file is part of the plugin Pay with Vipps and MobilePay for WooCommerce
4Copyright (c) 2019 WP-Hosting AS
5
6MIT License
7
8Copyright (c) 2019 WP-Hosting AS
9
10Permission is hereby granted, free of charge, to any person obtaining a copy
11of this software and associated documentation files (the "Software"), to deal
12in the Software without restriction, including without limitation the rights
13to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14copies of the Software, and to permit persons to whom the Software is
15furnished to do so, subject to the following conditions:
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26SOFTWARE.
27
28*/
29
30// Hook vippsInit to woocommerce/product-collection render event. LP 29.11.2024
31document.body.addEventListener('wc-blocks_product_list_rendered',
32  function (event) {
33    document.body.dispatchEvent(new Event('vippsInit'));
34  }
35);
36
37jQuery( document ).ready( function() {
38
39 // This is for WooCommerce Product Bundles, which potentially have *several* variant-products or configurable products, so
40 // the standard Woo hooks don't apply. Luckily, we have other, simpler events to use.
41 // For this to work, we also have to force the compat-mode thing on. IOK 2020-04-21
42 jQuery('.cart.bundle_data').on('woocommerce-product-bundle-hide', function (e, variant) {
43     jQuery('form .button.single-product.vipps-buy-now').removeClass('variation-found');
44     jQuery('form .button.single-product.vipps-buy-now').attr('disabled','disabled');
45     jQuery('form .button.single-product.vipps-buy-now').addClass('disabled');
46     // We don't know why the button is hidden, so ensure we use compatibility-mode IOK 2020-04-21
47     jQuery('.button.single-product.vipps-buy-now').addClass('compat-mode');
48     removeErrorMessages();
49 });
50 jQuery('.cart.bundle_data').on('woocommerce-product-bundle-show', function (e, variant) {
51     jQuery('form .button.single-product.vipps-buy-now').addClass('variation-found');
52     jQuery('form .button.single-product.vipps-buy-now').removeAttr('disabled');
53     jQuery('form .button.single-product.vipps-buy-now').removeClass('disabled');
54     removeErrorMessages();
55 });
56 
57 // Hooks for the 'buy now with vipps' button on product pages etc
58 jQuery('body').on('found_variation', function (e,variation) {
59
60   var purchasable=true;
61   if ( ! variation.is_purchasable || ! variation.is_in_stock || ! variation.variation_is_visible ) {
62     purchasable = false;
63   }
64   jQuery('form .button.single-product.vipps-buy-now').addClass('variation-found');
65   if (purchasable) {
66    jQuery('form .button.single-product.vipps-buy-now').removeAttr('disabled');
67    jQuery('form .button.single-product.vipps-buy-now').removeClass('disabled');
68    removeErrorMessages();
69   } else {
70    jQuery('form .button.single-product.vipps-buy-now').attr('disabled','disabled');
71    jQuery('form .button.single-product.vipps-buy-now').addClass('disabled');
72    removeErrorMessages();
73   }
74 });
75 jQuery('body').on('reset_data', function () {
76    jQuery('form .button.single-product.vipps-buy-now').removeClass('variation-found');
77    jQuery('form .button.single-product.vipps-buy-now').attr('disabled','disabled');
78    jQuery('form .button.single-product.vipps-buy-now').addClass('disabled');
79    removeErrorMessages();
80 });
81 // If this is triggered, somebody just loaded a variation form, so we need to redo the button init scripts
82 jQuery('body').on('wc_variation_form', function () {
83	 vippsInit();
84 });
85 // Allow other guys to do this too
86 jQuery('body').on('vippsInit', function () {
87  vippsInit(); 
88 });
89
90
91 // Using ajax, get a session and a key and redirect to the "buy single product using express checkout" page.
92 function buySingleProduct (event) {
93   event.preventDefault(); 
94
95   var element = jQuery(this);
96   if (jQuery('body').hasClass('processing')) return; // Trying to stop doublecclickers. IOK 2018-09-27
97
98   if (jQuery(element).hasClass('disabled'))  {
99     if (typeof wc_add_to_cart_variation_params != 'undefined') { 
100        if (jQuery(element).hasClass('variation-found'))  {
101          window.alert(wc_add_to_cart_variation_params.i18n_unavailable_text);
102        } else {
103          window.alert(wc_add_to_cart_variation_params.i18n_make_a_selection_text);
104        }
105     } else {
106	     console.log("Missing the wc_add_to_cart_variation_params");
107     }
108     return false;
109   }
110
111   jQuery('body').addClass('processing');
112   removeErrorMessages();
113   jQuery(element).attr('disabled','disabled');
114   jQuery(element).attr('inactive','inactive');
115   jQuery(element).addClass('disabled');
116   jQuery(element).addClass('loading');
117
118   if (typeof wp !== 'undefined' && typeof wp.hooks !== 'undefined') {
119      wp.hooks.doAction('vippsBuySingleProduct', element, event);
120   }
121
122   var compatMode = jQuery(element).hasClass('compat-mode');
123   if (typeof wp !== 'undefined' && typeof wp.hooks !== 'undefined') {
124      compatMode = wp.hooks.applyFilters('vippsBuySingleProductCompatMode', compatMode,  element, event);
125   }
126
127   //  In compatibility mode, we delegate to the existing buy now button instead of doing the logic
128   //  ourselves. This allows more filters and actions in existing plugins to run. IOK 2019-02-26
129   if (compatMode) {
130       var form =   jQuery(element).closest('form');
131       var otherbutton =  form.find('.single_add_to_cart_button').first(); 
132       var compatAction = function () {
133           form.prepend('<input type=hidden id="vipps_compat_mode" name="vipps_compat_mode" value="1">');
134           if (otherbutton.length>0) otherbutton.click();
135        }
136       // If your theme or product is weird enough, you may need this
137       if (typeof wp !== 'undefined' && typeof wp.hooks !== 'undefined') {
138         compatAction = wp.hooks.applyFilters('vippsBuySingleProductCompatModeAction', compatAction, element, event);
139       }
140       compatAction();
141       return false;
142   }
143
144   var data  =  {};
145   if (element.data('product_id') || element.data('product_sku')) {
146     data = element.data();
147   } else {
148     var form = element.closest('form.cart');
149     if (!form) {
150       jQuery(element).removeClass('disabled');
151       jQuery(element).removeClass('loading');
152       jQuery(element).removeAttr('disabled');
153       jQuery(element).removeAttr('inactive');
154       jQuery('body').removeClass('processing');
155       addErrorMessage('Cannot add product - unknown error');
156       return false;
157    }
158    // Initialize with the entire content of the form if we have it.
159    var serialized = form.serializeArray();
160    for (var i=0;i<serialized.length;i++) {
161        if (serialized[i]['name'] != 'add-to-cart') {
162            data[serialized[i]['name']] = serialized[i]['value'];
163        }
164    }
165    var prodid = jQuery(form).find('input[name="product_id"]');
166    var varid = jQuery(form).find('input[name="variation_id"]');
167    var quantity = jQuery(form).find('input[name="quantity"]');
168    data['quantity'] = (quantity.length>0) ? quantity.val() : 1;
169    data['product_id'] = (prodid.length>0) ? prodid.val() : 0;
170    data['variation_id'] = (varid.length>0) ? varid.val() : 0;
171    // Earlier versions, no variation:
172    if (prodid.length == 0) {
173      prodid = jQuery(form).find('button[name="add-to-cart"]');  
174      if (prodid.length > 0) {
175         data['product_id'] =  prodid.val();
176      } else {
177         // Some weird themes are even more incompatible
178         prodid = jQuery(form).find('button[data-product_id]');
179         data['product_id'] = prodid.length > 0 ? prodid.data('product_id') : 0;
180     }
181    }
182   }
183   // Finally, create a hook for even weirder themes.
184   data = wp.hooks.applyFilters('vippsBuySingleProductData', data,  element, event);
185   data['action'] = 'vipps_buy_single_product';
186
187   jQuery.ajax(VippsConfig['vippsajaxurl'], {
188    "method": "POST",
189    "data":data,
190    "cache":false,
191    "dataType": "json",
192    "error": function (xhr, statustext, error) {
193      console.log("Error creating express checkout:" + statustext + " " + error);
194      addErrorMessage(error, element);
195      jQuery(element).prop('disabled',true);
196      jQuery(element).prop('inactive',true);
197      jQuery('body').removeClass('processing');
198    },
199    "success": function (result, statustext, xhr) {
200     if (result["ok"]) {
201       console.log("We created the order!");
202       /* In case user presses the back button */
203       setTimeout(function () {
204          jQuery(element).removeClass('disabled');
205          jQuery(element).removeClass('loading');
206          jQuery(element).removeAttr('disabled');
207          jQuery(element).removeAttr('inactive');
208          jQuery('body').removeClass('processing');
209          }
210       , 1500);
211       window.location.assign(result["url"]);
212     } else {
213       console.log("Failure!");
214       jQuery(element).removeClass('disabled');
215       jQuery(element).removeClass('loading');
216       jQuery(element).removeAttr('disabled');
217       jQuery(element).removeAttr('inactive');
218       addErrorMessage(result['msg'],element);
219       jQuery('body').removeClass('processing');
220     }
221    },
222    "timeout": 0
223   });
224
225  }
226
227
228
229 // Remove old error messages
230 function removeErrorMessages () {
231   jQuery('.woocommerce-error.vipps-error').fadeOut(300, function () {  jQuery(this).remove(); });
232   jQuery(document).trigger('woo-vipps-remove-errors');
233   if (typeof wp !== 'undefined' && typeof wp.hooks !== 'undefined') {
234      wp.hooks.doAction('vippsRemoveErrorMessages');
235   }
236 }
237
238 // And add new ones
239 function addErrorMessage(msg,element) {
240   // Allow developers to customize error message by hiding vipps-default-error-message and hooking woo-vipps-error-message <messsage>,<element>
241   var msg = "<p><ul class='woocommerce-error vipps-error vipps-default-error-message vipps-buy-now-error'><li>
241"+msg+"!</li></ul></p>";
242   if (typeof wp !== 'undefined' && typeof wp.hooks !== 'undefined') {
243      msg = wp.hooks.applyFilters('vippsErrorMessage',msg, element);
244   }
245   jQuery(document).trigger('woo-vipps-error-message',[msg, element]);
246   if (typeof wp !== 'undefined' && typeof wp.hooks !== 'undefined') {
247      wp.hooks.doAction('vippsAddErrorMessage', msg, element);
248   }
249
250   jQuery(msg).hide().insertAfter(element).fadeIn(300);
251   jQuery('.woocommerce-error.vipps-error').click(removeErrorMessages);
252 }
253
254 // Hooks for the button itself
255 function vippsInit() {
256   jQuery('.button.single-product.vipps-buy-now:not(.initialized)').click(buySingleProduct);
257   jQuery('.button.single-product.vipps-buy-now').addClass('initialized');
258   if (typeof wp !== 'undefined' && typeof wp.hooks !== 'undefined') {
259      wp.hooks.doAction('vippsInit');
260   }
261 }
262 
263 vippsInit();
264});
265

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.