Page History
...
Code Block | ||
---|---|---|
| ||
<script type="text/javascript"> document.addEventListener('DOMContentLoaded', function(){ if (window.ApplePaySession) { //checking payment options and displaying the Apple Pay button if (ApplePaySession.canMakePayments) { document.getElementById('apple-pay-button').style.display = 'block'; document.getElementById('apple-pay-button').addEventListener('click', applePayButtonClicked); } } else {console.log("ApplePaySession not available"); } }); function applePayButtonClicked() { const region = 'RU'; const currency = $('#currency').val();//order currency const paymentRequest = { countryCode: region.toUpperCase(), currencyCode: currency.toUpperCase(), total: { label: 'Your label', //payment name amount: $('#amount').val()//order amount }, supportedNetworks:['masterCard', 'visa'], merchantCapabilities: [ 'supports3DS' ] }; const version = window.ApplePaySession.supportsVersion(3) ? 3 : window.ApplePaySession.supportsVersion(2) ? 2 : 1; const applePaySession = new window.ApplePaySession(version, paymentRequest); console.log("start session"); //event handler to create merchant session. applePaySession.onvalidatemerchant = function (event) { console.log("onvalidatemerchant in"); var data = { validationUrl: event.validationURL }; console.log(JSON.stringify(data)); //sending a request to the merchant server, then an API request to start the session $.post("/pay/apple_pay_comm.cfm", data).then(function (result) { applePaySession.completeMerchantValidation(result); }); } // payment authorization event handler applePaySession.onpaymentauthorized = function (event) { console.log("onpaymentauthorized in"); //var email = event.payment.shippingContact.emailAddress; //if the e-mail was requested //var phone = event.payment.shippingContact.phoneNumber; //if the phone number was requested // all options are on the site https://developer.apple.com/reference/applepayjs/paymentcontact // transfer of order parameters var data = { token : event.payment.token, merchant_id : $('#merchant_id').val(), email : $('#email').val(), amount : $('#amount').val(), currency : $('#currency').val(), ordernumber : $('#ordernumber').val(), email : $('#email').val(), firstname : $('#firstname').val(), middlename : $('#middlename').val(), lastname : $('#lastname').val(), comment : $('#comment').val() }; //sending a request to the merchant server, then an API request to payment console.log(JSON.stringify(event.payment.token)); $.post("/pay/tokenpay_widget_ap.cfm", JSON.stringify(data)).then(function (result) { if (!result.hasOwnProperty('firstcode') && JSON.stringify(result.order.orderstate) == '"Approved"' && JSON.stringify(result.order.orderstate) == '"Delayed"') { applePaySession.completePayment(ApplePaySession.STATUS_SUCCESS); } else { applePaySession.completePayment(ApplePaySession.STATUS_FAILURE); } }); }; applePaySession.begin(); } </script> <style> #apple-pay-button { display: none; background-color: black; background-image: -webkit-named-image(apple-pay-logo-white); background-size: 100% 100%; background-origin: content-box; background-repeat: no-repeat; width: 100%; height: 44px; padding: 10px 0; border-radius: 10px; } </style> |