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