**my code is running , when i tried to pay but payment not completed issue ? **
if (window.ApplePaySession) {
var hasActiveCard = window.ApplePaySession.canMakePaymentsWithActiveCard("merchant.com.applepaydemo.honemedia");
//var paymentAmount = "20.00";
var taxRequired = false;
var paymentTaxAmount = "0.00";
var shippingRequired = false;
var paymentShippingAmount = "5.00";
var appleButton = document.querySelectorAll(".appleButton");
for (i = 0; i < appleButton.length; i++) {
appleButton[i].style.display = "block";
appleButton[i].onclick = function() {
checkout();
}
}
if(!hasActiveCard) {
for (i = 0; i < appleButton.length; i++) {
appleButton[i].classList.add("setup");
}
}
}
function checkout() {
if (!hasActiveCard) {
window.ApplePaySession.openPaymentSetup("merchant.com.applepaydemo.honemedia")
.then(function(success) {
if (success) {
// Open payment setup successful
} else {
// Open payment setup failed
}
})
.catch(function(e) {
// Open payment setup error handling
});
return;
}
var paymentData = {
countryCode: "US",
currencyCode: "USD", //CAD, JPY, EUR, CNY, INR, MXN, KRW, XBC (bitcoin)
merchantCapabilities: ["supports3DS", "supportsCredit", "supportsDebit"],
supportedNetworks: ["amex", "discover", "masterCard", "visa"],
requiredBillingContactFields: ['email'], //email or postalAddress or all
total: {
label: "Apple Pay Session Demo",
amount: "25.00"
},
lineItems: [{
label: "Subtotal",
amount: "20.00"
},{
label: "Standard Shipping",
amount: "5.00"
}],
shippingType: 'shipping', //shipping, delivery, storePickup, or servicePickup.
requiredShippingContactFields: ["postalAddress"], //postalAddress, name, phone, email or all
shippingMethods: [{
label: "Standard Shipping",
amount: "5.00",
detail: "5-10 days",
identifier: 'standard'
}, {
label: "Express Shipping",
amount: "15.00",
detail: "1-3 days",
identifier: 'express'
}]
}
var paySession = new ApplePaySession(2, paymentData);
//console.log("Supports API v2: " + window.ApplePaySession.supportsVersion(2))
paySession.oncancel = function(event) {
//console.log('ApplePaySession oncancel ' + event);
}
// Merchant Validation
paySession.onvalidatemerchant = function(event) {
var promise = performValidation(event.validationURL);
promise.then(function(merchantSession) {
paySession.completeMerchantValidation(merchantSession);
});
}
function performValidation(valURL) {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.onload = function() {
var data = JSON.parse(this.responseText);
//console.log("data returned from validate_apple_pay.php: " + data);
resolve(data);
};
xhr.onerror = reject;
xhr.open("GET", "https://honemedia.com/applepay/gateway/validate_apple_pay.php" + "?url=" + valURL);
xhr.send();
});
}
paySession.onshippingmethodselected = function(event) {
var newShippingLabel = event.shippingMethod.label;
var updatedShippingCost = event.shippingMethod.amount;
alert((Number(updatedShippingCost) + 20.00).toString())
var newTotal = {
type: "final",
label: "Apple Pay Session Demo",
amount: (Number(updatedShippingCost) + 20.00).toString()
};
var newLineItems = [{
type: 'final',
label: "Subtotal",
amount: "20.00"
}, {
type: 'final',
label: newShippingLabel,
amount: updatedShippingCost
}];
paySession.completeShippingMethodSelection(ApplePaySession.STATUS_SUCCESS, newTotal, newLineItems);
}
paySession.onpaymentauthorized = function(event) {
//console.log(event);
var paymentTransactionIdentifier = event.payment.token.transactionIdentifier;
var paymentTransactionEncryption = event.payment.token.paymentData.version; //EC_v1 - elliptic curve cryptography
var paymentTransactionData = event.payment.token.paymentData.data;
var paymentTransactionSignature = event.payment.token.paymentData.signature;
var paymentTransactionHeader = event.payment.token.paymentData.header;
var paymentEphemeralPublicKey = paymentTransactionHeader.ephemeralPublicKey;
var paymentPublicKeyHash = paymentTransactionHeader.publicKeyHash;
var paymentTransactionId = paymentTransactionHeader.transactionId;
var shippingContact = event.payment.shippingContact.emailAddress; //could be address or could be email
var billingContact = event.payment.billingContact;
var cardPayerAddress = event.payment.shippingContact.addressLines[0];
var cardPayerState = event.payment.shippingContact.administrativeArea;
var cardPayerCountry = event.payment.shippingContact.country;
var cardPayerCountryCode = event.payment.shippingContact.countryCode;
var cardPayerFirstName = event.payment.shippingContact.givenName;
var cardPayerLastName = event.payment.shippingContact.familyName;
var cardPayerCity = event.payment.shippingContact.locality;
var cardPayerPostalCode = event.payment.shippingContact.postalCode;
var cardDisplayName = event.payment.token.paymentMethod.displayName;
var cardProvider = event.payment.token.paymentMethod.network;
var cardType = event.payment.token.paymentMethod.type;
var paymentToken = event.payment.token;
var cardPayerEmailAddress = event.payment.shippingContact.emailAddress;
processPayment(paymentToken,cardPayerEmailAddress, this);
}
// All the payment data and handlers are setup - show the Payment Sheet UI
paySession.begin();
}
/* Pass the paymentResponse values to a server side script and pass the data to payment gateway service */
function processPayment(paymentToken,payerEmail,paymentSession){
//console.log("TOKEN: " + paymentToken);
var httpc = new XMLHttpRequest();
httpc.open("POST","https://honemedia.com/applepay/gateway/gateway_stripe_applepay.php", true);
httpc.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
httpc.onreadystatechange = function() {
if (httpc.readyState == 4 && httpc.status == 200) {
console.log("RESPONSE: " + httpc.responseText);
paymentSession.completePayment(httpc.responseText == "success" ? ApplePaySession.STATUS_SUCCESS : ApplePaySession.STATUS_FAILURE);
}
}
httpc.send(
('paymentAmount=' + "25.00") +
('&token=' + paymentToken) +
('&payerEmail=' + payerEmail)
);
}