1

I have followed the documentation on apple website regarding apple pay.

onValidateMerchant and completeMerchantValidation work perfectly but right after that it jumps to oncancel.

I have messages at are printed to the screen within onpaymentselected and onpaymentauthorized but they are never printed.

I have added a try catch to catch any errors that pop, turns out onpaymentselect and onpaymentauthorized are run before onValidateMerchant and completeMerchantValidation.

applePayButton.addEventListener("click", function(){

const request = {
    countryCode: 'US',
    currencyCode: 'USD',
    merchantCapabilities: [
        'supports3DS'
    ],
    supportedNetworks: [
        'visa',
        'masterCard',
        'amex',
        'discover'
    ],
    lineItems: [{
            label: 'Amount',
            amount: 0.95,
        },
        {
            label: 'Tax',
            amount: 0.05,
        }
    ],
    total: {
        label: 'Total',
        amount: 10,
    }
};
var session = new ApplePaySession(10, request);
session.begin();

try{
    session.onvalidatemerchant = function(event){
        printMessage("starting session.onvalidatemerchant" + JSON.stringify(event));

        var promise = performValidation(event.validationURL);

        promise.then(function(merchantSession) {
            printMessage("Merchant Session: "+ JSON.stringify(merchantSession));
            session.completeMerchantValidation(merchantSession);
        });
    }
}
catch(error){
    printMessage("On Validate Merchant Error: " + error)
}

try{
    printMessage("onpaymentmethodselected");

    session.onpaymentmethodselected = function(event) {
        printMessage("In On Payment Method Selected");
        //var myPaymentMethod = event.paymentMethod;

        const update = {};
        session.completePaymentMethodSelection(update);
    };

}
catch(error){
    printMessage("On Payment Method Selected Error: " + error)
}

try{
    printMessage("onpaymentauthorized");
    session.onpaymentauthorized = function(event) {
        printMessage("starting session.onpaymentauthorized");

        var applePaymentToken = event.payment.token;

        printMessage("Token" + applePaymentToken);

        // Define ApplePayPaymentAuthorizationResult
        session.completePayment(session.STATUS_SUCCESS);
    };

}
catch(error){
    printMessage("On Payment Authorized Error: " + error)
}

try{
    session.oncancel = function(event) {
        printMessage("starting session.oncancel" + JSON.stringify(event));

        // Payment cancelled by WebKit
    };
}
catch(error){
    printMessage("On Cancel Error: " + error)
}

});

This is the message that comes after payment Not Complete

Session: Step 1: applePay working

Step 2: onpaymentmethodselected

Step 3: onpaymentauthorized

Step 4: starting session.onvalidatemerchant{"isTrusted":true}

Step 5: Complete Merchant Validation:

Step 6: starting session.oncancel{"isTrusted":true}

Dev Team
  • 11
  • 3

2 Answers2

0

I had the same issue, and it seems that when the onvalidatemerchant fails with this {"isTrusted":true} is because:

  • You don't have fingerprint enabled, or
  • You're not using Safari, or
  • Your call to validate on the backend is wrong somehow

If these parts are working, then you should see the Apple Pay modal and should be able to make everything work (hopefully)

Pedro Filho
  • 25
  • 1
  • 7
  • The issue I had was in the backend when I was sending my website link I was sending it as 'example.com' and it should be 'www.example.com' :) – Dev Team Mar 24 '22 at 10:10
  • They should have some sort of error message to show that something was wrong. These smoke errors aren't helpful at all – Pedro Filho Mar 25 '22 at 11:02
0

I had the same issue where the payment sheet would display then go away without a chance to authorize the transaction. I made sure the domain was verified, the certificate was valid, and that I was actually receiving a response back from the server side request to Apple. Sorting through the process of elimination I came across certain interesting observations:

  • If the payment sheet disappeared immediately, it was usually due to a configuration issue with the server side request payload (for example, the "initiativeContext" was "something.com" when I was actually using "www.something.com" in the web browser address).
  • If the payment sheet disappeared after several seconds, it was usually due to a syntax issue or error in the client side JavaScript. In my case, I was not doing session.onpaymentmethodselected and session.completepaymentmethodselection correctly. So I removed all other Apple specific JS functions except for session.onvalidatemerchant and a subsequent call to session.completeMerchantValidation passing the Apple response from the server side request. It then worked.
  • If the Apple response from the server side request was in any way (even just the casing) changed from what Apple originally sent it would not work (for instance, "epochtimestamp":1668095263534," vs "epochTimestamp":1668095263534,").

Hope that helps.

Ray
  • 1