# payplug/3.1.0/assets/js/payplug-apple-pay-product.js

PayPlug for WooCommerce (Official), version 3.1.0. 299 lines.

- Page: https://pluginprobe.com/plugins/payplug/3.1.0/code/assets/js/payplug-apple-pay-product.js
- Raw: https://pluginprobe.com/plugins/payplug/3.1.0/raw/assets/js/payplug-apple-pay-product.js
- Modified: 2026-09-25T08:27:34+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/payplug/3.1.0/code/assets/js/payplug-apple-pay-product.js#L10-L20`.

```javascript
/* global window, apple_pay_params */
(function($){
	var $apple_pay_button = $('apple-pay-button')
	var session = null;
	var is_product = apple_pay_params.is_product;
	var apple_pay = {
		init: function () {
			if(!is_product){
				return;
			}

			apple_pay.updateOrderTotal();
			$('apple-pay-button').on('click', apple_pay.ProcessCheckout)
		},
		updateOrderTotal: function(){
			jQuery.post(
				apple_pay_params.ajax_url_applepay_get_order_totals

			).done(function(results){
				if(results.success){
					apple_pay_params.total = results.data;
					apple_pay.getShippings();
				} else {
					$apple_pay_button.remove();
				}

			}).fail( function() {
				$apple_pay_button.remove();

			});
		},
		getShippings: function(){
			jQuery.post(
				apple_pay_params.ajax_url_applepay_get_shippings

			).done(function(results){
				if(results.data.length === 0 || apple_pay_params.is_virtual){
					//$apple_pay_button.remove();
					apple_pay_params.carriers = [];
					return;
				}

				apple_pay_params.carriers = results.data;
			}).fail( function() {
				$apple_pay_button.remove();

			})
		},
		ProcessCheckout: function (e) {
			e.preventDefault();
			e.stopImmediatePropagation();

			jQuery.post({
				'url' : apple_pay_params.ajax_url_applepay_empty_cart,
                'async' : false,
			}).done(function(results){
			    product_informations = {
					'product_id': jQuery("button[name=add-to-cart]").val(),
					'product_quantity': jQuery("input[name=quantity]").val()
				}
			    // We manage variations
				if (jQuery("table.variations").length != 0) {
				    product_informations['product_variation_id'] = jQuery("input[name=variation_id]").val();
				}

				jQuery.post({
					'url' : apple_pay_params.ajax_url_applepay_add_to_cart,
                    'async' : false,
				    'data' : product_informations
				}).done(function(results) {
					if (results.success === false) {
						apple_pay.showError(results.data.msg, "error");
						$apple_pay_button.remove();
						return;
					}
					apple_pay_params.total = results.data.total;
				}).fail( function() {
					$apple_pay_button.remove();
				});
			}).fail( function() {
				$apple_pay_button.remove();
			});

			apple_pay.CreateSession();
			apple_pay.CancelOrder();
			apple_pay.PaymentCompleted();

			//loading layer
			jQuery('.woocommerce').block({ message: null, overlayCSS: { background: '#fff', opacity: 0.6 } })

			jQuery.post(
				apple_pay_params.ajax_url_place_order_with_dummy_data

			).done(function(results){
				if(results.success === false){
					apple_pay.showError(results.data.msg, "error");
					apple_pay.handle_process_error(results.data.order_id);
					return;
				}
				apple_pay.OrderPaymentCreated(results);
			});

		},
		OrderPaymentCreated: function (response) {
			if ('success' !== response.payment_data.result) {
				var error_messages = response.messages || '';
				apple_pay.CancelOrder(error_messages);
			}

			apple_pay.BeginSession(response)
		},
		CreateSession: function () {
			var isVirtual = !!apple_pay_params.is_virtual;
			var cartShipping = parseFloat(apple_pay_params.cart_shipping) || 0;

			const request = {
				"countryCode": apple_pay_params.countryCode,
				"currencyCode": apple_pay_params.currencyCode,
				"merchantCapabilities": [
					"supports3DS"
				],
				"supportedNetworks": [
					"cartesBancaires",
					"visa",
					"masterCard"
				],
            	"supportedTypes": [
					"debit",
					"credit"
				],
				"total": {
					"label": "Apple Pay",
					"type": "final",
					"amount": parseFloat(apple_pay_params.total) + cartShipping
				},
				'applicationData': btoa(JSON.stringify({
					'apple_pay_domain': apple_pay_params.apple_pay_domain
				})),
				'requiredBillingContactFields' : [
					'postalAddress',
					'name',
				],
				'requiredShippingContactFields' : isVirtual
					? ['name', 'phone', 'email']
					: ['postalAddress', 'name', 'phone', 'email'],
			};

			if (!isVirtual) {
				request['shippingMethods'] = apple_pay_params.carriers;
			}

			session = new ApplePaySession(4, request);

		},
		BeginSession: function (response) {
			session.payment_id = response.payment_data.payment_id
			session.order_id = response.order_id
			session.cancel_url = response.payment_data.cancel_url
			session.return_url = response.payment_data.return_url

			apple_pay.MerchantValidated(session, response.payment_data.merchant_session);

			session.amount = parseFloat(apple_pay_params.total/100) * 100;
			session.onshippingmethodselected = event => {
				const shippingMethod = event.shippingMethod;
				session.shippingMethod = shippingMethod.identifier;

				const baseTotal = apple_pay_params.total ;
				let currentShippingCost = shippingMethod.amount;

				const newTotalAmount = parseFloat(baseTotal) + parseFloat(currentShippingCost);
				session.amount = parseFloat(newTotalAmount / 100) * 100;

				const update = {
					newTotal: {
						label: 'Apple Pay',
						amount: parseFloat(newTotalAmount)
					},
					newLineItems: [
						{
							label: shippingMethod.label,
							type: 'final',
							amount: currentShippingCost
						}
					]
				};

				session.completeShippingMethodSelection(update);
			};
			session.begin();
		},
		MerchantValidated: function(session, merchant_session) {

			session.onvalidatemerchant = event => {
				try {
					session.completeMerchantValidation(merchant_session);
				} catch (err) {
					apple_pay.CancelOrder();
				}
			}
		},
		PaymentCompleted: function () {
			session.onpaymentauthorized = event => {
				let data = event.payment;

				jQuery.post({
					'url' : apple_pay_params.ajax_url_update_applepay_order,
					'data' : {
						'order_id' : session.order_id,
						'shipping' : data.shippingContact,
						'billing' : data.billingContact,
						'shipping_method' : session.shippingMethod
					}
				}).done(function (response) {
					jQuery.ajax({
						url: apple_pay_params.ajax_url_update_applepay_payment,
						type: 'post',
						data: {
							'action': 'applepay_update_payment',
							'post_type': 'POST',
							'payment_id': session.payment_id,
							'payment_token': event.payment.token,
							'order_id': session.order_id,
							'amount': session.amount
						},
						dataType: 'json',
						success:function(res) {
							jQuery('.woocommerce').unblock();
							var apple_pay_Session_status = ApplePaySession.STATUS_SUCCESS;

							if (res.success !== true) {
								apple_pay_Session_status = ApplePaySession.STATUS_FAILURE;
								apple_pay.CancelOrder('Unfortunately your order cannot be processed as the originating bank/merchant has declined your transaction. Please attempt your purchase again.')
							}
							session.completePayment({"status": apple_pay_Session_status});
							window.location = session.return_url
						},
						error: function(err){
							apple_pay.CancelOrder('Unfortunately your order cannot be processed as the originating bank/merchant has declined your transaction. Please attempt your purchase again.')
						},
					});

				}).fail(function (response) {
					apple_pay.CancelOrder('Unfortunately your order cannot be processed as the originating bank/merchant has declined your transaction. Please attempt your purchase again.');
				})
				$('apple-pay-button').addClass("isDisabled")

			}
		},
		CancelOrder: function (message) {
			session.oncancel = event => {
				$('apple-pay-button').removeClass("isDisabled");
				apple_pay.cancel_order_request(session.order_id, session.payment_id);

			}
		},
		handle_process_error: function (order_id = null) {
			$('apple-pay-button').removeClass("isDisabled");
			apple_pay.cancel_order_request(order_id, null);
		},
		cancel_order_request: function(order_id, payment_id = null){
			jQuery.post({
				url : apple_pay_params.ajax_url_applepay_cancel_order,
				data : {
					'order_id' : order_id,
					'payment_id' : payment_id
				}
			}).done(function (response) {
				apple_pay.showError(response.data.message, "info");
				apple_pay.updateOrderTotal();
			})
		},
		showError: function (message="", type = "info") {
			jQuery('.woocommerce').unblock()
			let notices = jQuery('.woocommerce-notices-wrapper')
			jQuery('<div id="apple-pay-cart-notice"></div>')
				.addClass('woocommerce-'+type)
				.html(message)
				.prependTo(notices)
			jQuery('html , body').animate({
				scrollTop: (notices.offset().top - 100)
			}, 500)
			jQuery('.woocommerce-notices-wrapper').fadeIn();
			setTimeout(function() {
				notices.fadeOut('slow', function() {
					jQuery('.woocommerce-notices-wrapper #apple-pay-cart-notice').remove();
				});
			}, 5000);
		}
	}

	apple_pay.init();

	jQuery( 'body' ).on( 'updated_cart_totals', function() {
		apple_pay.updateOrderTotal();
	})

})(jQuery)

```
