PluginProbe
PayPlug for WooCommerce (Official) / 3.1.0
PayPlug for WooCommerce (Official) v3.1.0
3.1.0 3.0.0 2.18.0 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.1.0 1.10.0 1.10.1 1.2.1 1.2.10 1.2.11 1.2.2 1.2.3 1.2.4 1.2.5 All 102 releases
payplug / assets / js / payplug-apple-pay-product.js

payplug-apple-pay-product.js in PayPlug for WooCommerce (Official) 3.1.0, at assets/js/payplug-apple-pay-product.js

299 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /* global window, apple_pay_params */
2 (function($){
3 var $apple_pay_button = $('apple-pay-button')
4 var session = null;
5 var is_product = apple_pay_params.is_product;
6 var apple_pay = {
7 init: function () {
8 if(!is_product){
9 return;
10 }
11
12 apple_pay.updateOrderTotal();
13 $('apple-pay-button').on('click', apple_pay.ProcessCheckout)
14 },
15 updateOrderTotal: function(){
16 jQuery.post(
17 apple_pay_params.ajax_url_applepay_get_order_totals
18
19 ).done(function(results){
20 if(results.success){
21 apple_pay_params.total = results.data;
22 apple_pay.getShippings();
23 } else {
24 $apple_pay_button.remove();
25 }
26
27 }).fail( function() {
28 $apple_pay_button.remove();
29
30 });
31 },
32 getShippings: function(){
33 jQuery.post(
34 apple_pay_params.ajax_url_applepay_get_shippings
35
36 ).done(function(results){
37 if(results.data.length === 0 || apple_pay_params.is_virtual){
38 //$apple_pay_button.remove();
39 apple_pay_params.carriers = [];
40 return;
41 }
42
43 apple_pay_params.carriers = results.data;
44 }).fail( function() {
45 $apple_pay_button.remove();
46
47 })
48 },
49 ProcessCheckout: function (e) {
50 e.preventDefault();
51 e.stopImmediatePropagation();
52
53 jQuery.post({
54 'url' : apple_pay_params.ajax_url_applepay_empty_cart,
55 'async' : false,
56 }).done(function(results){
57 product_informations = {
58 'product_id': jQuery("button[name=add-to-cart]").val(),
59 'product_quantity': jQuery("input[name=quantity]").val()
60 }
61 // We manage variations
62 if (jQuery("table.variations").length != 0) {
63 product_informations['product_variation_id'] = jQuery("input[name=variation_id]").val();
64 }
65
66 jQuery.post({
67 'url' : apple_pay_params.ajax_url_applepay_add_to_cart,
68 'async' : false,
69 'data' : product_informations
70 }).done(function(results) {
71 if (results.success === false) {
72 apple_pay.showError(results.data.msg, "error");
73 $apple_pay_button.remove();
74 return;
75 }
76 apple_pay_params.total = results.data.total;
77 }).fail( function() {
78 $apple_pay_button.remove();
79 });
80 }).fail( function() {
81 $apple_pay_button.remove();
82 });
83
84 apple_pay.CreateSession();
85 apple_pay.CancelOrder();
86 apple_pay.PaymentCompleted();
87
88 //loading layer
89 jQuery('.woocommerce').block({ message: null, overlayCSS: { background: '#fff', opacity: 0.6 } })
90
91 jQuery.post(
92 apple_pay_params.ajax_url_place_order_with_dummy_data
93
94 ).done(function(results){
95 if(results.success === false){
96 apple_pay.showError(results.data.msg, "error");
97 apple_pay.handle_process_error(results.data.order_id);
98 return;
99 }
100 apple_pay.OrderPaymentCreated(results);
101 });
102
103 },
104 OrderPaymentCreated: function (response) {
105 if ('success' !== response.payment_data.result) {
106 var error_messages = response.messages || '';
107 apple_pay.CancelOrder(error_messages);
108 }
109
110 apple_pay.BeginSession(response)
111 },
112 CreateSession: function () {
113 var isVirtual = !!apple_pay_params.is_virtual;
114 var cartShipping = parseFloat(apple_pay_params.cart_shipping) || 0;
115
116 const request = {
117 "countryCode": apple_pay_params.countryCode,
118 "currencyCode": apple_pay_params.currencyCode,
119 "merchantCapabilities": [
120 "supports3DS"
121 ],
122 "supportedNetworks": [
123 "cartesBancaires",
124 "visa",
125 "masterCard"
126 ],
127 "supportedTypes": [
128 "debit",
129 "credit"
130 ],
131 "total": {
132 "label": "Apple Pay",
133 "type": "final",
134 "amount": parseFloat(apple_pay_params.total) + cartShipping
135 },
136 'applicationData': btoa(JSON.stringify({
137 'apple_pay_domain': apple_pay_params.apple_pay_domain
138 })),
139 'requiredBillingContactFields' : [
140 'postalAddress',
141 'name',
142 ],
143 'requiredShippingContactFields' : isVirtual
144 ? ['name', 'phone', 'email']
145 : ['postalAddress', 'name', 'phone', 'email'],
146 };
147
148 if (!isVirtual) {
149 request['shippingMethods'] = apple_pay_params.carriers;
150 }
151
152 session = new ApplePaySession(4, request);
153
154 },
155 BeginSession: function (response) {
156 session.payment_id = response.payment_data.payment_id
157 session.order_id = response.order_id
158 session.cancel_url = response.payment_data.cancel_url
159 session.return_url = response.payment_data.return_url
160
161 apple_pay.MerchantValidated(session, response.payment_data.merchant_session);
162
163 session.amount = parseFloat(apple_pay_params.total/100) * 100;
164 session.onshippingmethodselected = event => {
165 const shippingMethod = event.shippingMethod;
166 session.shippingMethod = shippingMethod.identifier;
167
168 const baseTotal = apple_pay_params.total ;
169 let currentShippingCost = shippingMethod.amount;
170
171 const newTotalAmount = parseFloat(baseTotal) + parseFloat(currentShippingCost);
172 session.amount = parseFloat(newTotalAmount / 100) * 100;
173
174 const update = {
175 newTotal: {
176 label: 'Apple Pay',
177 amount: parseFloat(newTotalAmount)
178 },
179 newLineItems: [
180 {
181 label: shippingMethod.label,
182 type: 'final',
183 amount: currentShippingCost
184 }
185 ]
186 };
187
188 session.completeShippingMethodSelection(update);
189 };
190 session.begin();
191 },
192 MerchantValidated: function(session, merchant_session) {
193
194 session.onvalidatemerchant = event => {
195 try {
196 session.completeMerchantValidation(merchant_session);
197 } catch (err) {
198 apple_pay.CancelOrder();
199 }
200 }
201 },
202 PaymentCompleted: function () {
203 session.onpaymentauthorized = event => {
204 let data = event.payment;
205
206 jQuery.post({
207 'url' : apple_pay_params.ajax_url_update_applepay_order,
208 'data' : {
209 'order_id' : session.order_id,
210 'shipping' : data.shippingContact,
211 'billing' : data.billingContact,
212 'shipping_method' : session.shippingMethod
213 }
214 }).done(function (response) {
215 jQuery.ajax({
216 url: apple_pay_params.ajax_url_update_applepay_payment,
217 type: 'post',
218 data: {
219 'action': 'applepay_update_payment',
220 'post_type': 'POST',
221 'payment_id': session.payment_id,
222 'payment_token': event.payment.token,
223 'order_id': session.order_id,
224 'amount': session.amount
225 },
226 dataType: 'json',
227 success:function(res) {
228 jQuery('.woocommerce').unblock();
229 var apple_pay_Session_status = ApplePaySession.STATUS_SUCCESS;
230
231 if (res.success !== true) {
232 apple_pay_Session_status = ApplePaySession.STATUS_FAILURE;
233 apple_pay.CancelOrder('Unfortunately your order cannot be processed as the originating bank/merchant has declined your transaction. Please attempt your purchase again.')
234 }
235 session.completePayment({"status": apple_pay_Session_status});
236 window.location = session.return_url
237 },
238 error: function(err){
239 apple_pay.CancelOrder('Unfortunately your order cannot be processed as the originating bank/merchant has declined your transaction. Please attempt your purchase again.')
240 },
241 });
242
243 }).fail(function (response) {
244 apple_pay.CancelOrder('Unfortunately your order cannot be processed as the originating bank/merchant has declined your transaction. Please attempt your purchase again.');
245 })
246 $('apple-pay-button').addClass("isDisabled")
247
248 }
249 },
250 CancelOrder: function (message) {
251 session.oncancel = event => {
252 $('apple-pay-button').removeClass("isDisabled");
253 apple_pay.cancel_order_request(session.order_id, session.payment_id);
254
255 }
256 },
257 handle_process_error: function (order_id = null) {
258 $('apple-pay-button').removeClass("isDisabled");
259 apple_pay.cancel_order_request(order_id, null);
260 },
261 cancel_order_request: function(order_id, payment_id = null){
262 jQuery.post({
263 url : apple_pay_params.ajax_url_applepay_cancel_order,
264 data : {
265 'order_id' : order_id,
266 'payment_id' : payment_id
267 }
268 }).done(function (response) {
269 apple_pay.showError(response.data.message, "info");
270 apple_pay.updateOrderTotal();
271 })
272 },
273 showError: function (message="", type = "info") {
274 jQuery('.woocommerce').unblock()
275 let notices = jQuery('.woocommerce-notices-wrapper')
276 jQuery('<div id="apple-pay-cart-notice"></div>')
277 .addClass('woocommerce-'+type)
278 .html(message)
279 .prependTo(notices)
280 jQuery('html , body').animate({
281 scrollTop: (notices.offset().top - 100)
282 }, 500)
283 jQuery('.woocommerce-notices-wrapper').fadeIn();
284 setTimeout(function() {
285 notices.fadeOut('slow', function() {
286 jQuery('.woocommerce-notices-wrapper #apple-pay-cart-notice').remove();
287 });
288 }, 5000);
289 }
290 }
291
292 apple_pay.init();
293
294 jQuery( 'body' ).on( 'updated_cart_totals', function() {
295 apple_pay.updateOrderTotal();
296 })
297
298 })(jQuery)
299