Skip to main content

Payment flow with Apple Pay

Last updated: 10-Oct-2023
Rate this article:

Overview

The following payment method is specific to Apple devices.

Availability

Available for all 2Checkout merchants (2Sell, 2Subscribe, 2Monetize & 4Enterprise) using all or any of our ordering engines (hosted shopping cart, ConvertPlus, and InLine).

For more information on availability, see the main documentation page for Apple Pay here.

Requirements

Shoppers can use any device that supports Apple Pay and is located in one of the selected list of countries.

For more information on requirements, see the main documentation page for Apple Pay here.

Activation

For activation, you need to have Apple Pay enabled on your 2Checkout account. Contact the Merchant Support team in order to enable this. After 2Checkout sets up our domain, we will provide you with a domain verification file.

Host your domain verification file at the following path on our server: https://[DOMAIN_NAME]/.well-known/apple-developer-merchantid-domain-association.

Setting up Apple Pay

To set up Apple Pay, you need to build the frontend component and to initialize the session using the startApplePaySession method providing the “validationUrl” to the payload. Once the token is received from the frontend component, it has to be sent to the decryptApplePayData endpoint in the 2Checkout API. This will return a data response from Apple Pay that needs to be used in the placeOrder call.

Frontend component

In order to set up an Apple Pay session in the shopper’s browser, the following steps are needed:

  1. Include the Apple Pay library.

    <script src=https://applepay.cdn-apple.com/jsapi/v1/apple-pay-sdk.js></script>
  2. Add the Apple Pay button.

    <div class="apple-pay-button apple-pay-button-black" onclick="onApplePayButtonClicked()"></div>
    <style>
        .apple-pay-button {
            display: inline-block;
            -webkit-appearance: -apple-pay-button;
            -apple-pay-button-type: buy; /* Use any supported button type. */
        }
        .apple-pay-button-black {
            -apple-pay-button-style: black;
        }
        .apple-pay-button-white {
            -apple-pay-button-style: white;
        }
        .apple-pay-button-white-with-line {
            -apple-pay-button-style: white-outline;
        }
    </style>
    
  3. Set up the Apple Pay function and create a session instance.

    function onApplePayButtonClicked() { 
        // Ensure browser supports Apple Pay
        if (!ApplePaySession) {
            return;
        }
    
        // Define ApplePayPaymentRequest
       /* Define the country code and currency being sent to the library, as well as 
       the list of supported networks and the descriptor and amount of the transaction.*/
        const request = {
            "countryCode": "US",
            "currencyCode": "USD",
            "merchantCapabilities": [
                "supports3DS"
            ],
            "supportedNetworks": [
                "visa",
                "masterCard",
                "amex",
                "discover"
            ],
            "total": {
                "label": "Example Transaction",
                "type": "final",
                "amount": "0.01"
            }
        };
    
        // Create ApplePaySession
        const session = new ApplePaySession(3, request);
    
        session.onvalidatemerchant = async event => {
            // Call your own server to request a new merchant session (call 2checkout API /rest/6.0/payments/startapplepaysession)
            fetch("/startApplePay.php")
                .then(res => res.json()) // Parse response as JSON.
                .then(merchantSession => {
                    response = JSON.parse(merchantSession.ApplePaySessionData.response);
                    session.completeMerchantValidation(response);
                })
                .catch(err => {
                  console.error("Error fetching merchant session", err);
                });
        };
    
        session.onpaymentmethodselected = event => {
            // Define ApplePayPaymentMethodUpdate based on the selected payment method.
            // No updates or errors are needed, pass an empty object.
            const update = {
                newTotal: {
                    label: "Demo (Card is not charged)",
                    type: "final",
                    amount: "0.01"
                }
            }
            session.completePaymentMethodSelection(update);
        };
    
        session.onshippingmethodselected = event => {
            // Define ApplePayShippingMethodUpdate based on the selected shipping method.
            // No updates or errors are needed, pass an empty object. 
            const update = {
                newTotal: {
                    label: "Demo (Card is not charged)",
                    type: "final",
                    amount: "0.01"
                }
            }
            session.completeShippingMethodSelection(update);
        };
    
        session.onshippingcontactselected = event => {
            // Define ApplePayShippingContactUpdate based on the selected shipping contact.
            const update = {
                newTotal: {
                    label: "Demo (Card is not charged)",
                    type: "final",
                    amount: "0.01"
                }
            }
            session.completeShippingContactSelection(update);
        };
    
        session.onpaymentauthorized = event => {
            // Call your own server to request the Apply Pay token decryption and pass the decoded token to the place order call (call 2checkout APIs /rest/6.0/payments/decryptapplepaydata and /rest/6.0/orders/)
            // Define ApplePayPaymentAuthorizationResult based on the place order response
            payload = {
                token: event.payment.token,
            };
            fetch("/finishApplePay.php", {
                method: "POST",
                headers: {"Content-Type": "application/json"},
                body: JSON.stringify(payload),
            }).then((res) => res.json())
                .then((res) => {
                    if (res.Status.includes('AUTHRECEIVED', 'COMPLETE')) {
                      session.completePayment(session.STATUS_SUCCESS);
                    }
                    console.log("INFO - RECEIVED ECOM RESPONSE: ", res.Status);
                    })
                    .catch((err) => console.log(err));
        };
    
        session.oncancel = event => {
            // Payment cancelled by WebKit
        };
        session.begin();
    }
    

Backend component

  1. Initiate an Apple Pay session.

    Create an endpoint on your server to call to initiate an Apple Pay session. In order for a valid Apple Pay session to be created, an API call to the 2Checkout API needs to be sent, with the validationURL set to the ApplePay payment service.

    Protocol

    Endpoint

    REST POST https://api.2checkout.com/rest/6.0/payments/startapplepaysession
    JSON-RPC startApplePaySession
    SOAP startApplePaySession

    startApplePaySession payload

    {
       "validationURL":"https://apple-pay-gateway.apple.com/paymentservices/startSession"
    }
  2. Decrypt the Apple Pay token.

    Once the token is received from the frontend component, it has to be sent to the decryptApplePayData endpoint in the 2Checkout API. This will return a data response from Apple Pay that needs to be used in the placeOrder call.

    Protocol

    Endpoint

    REST POST api.2checkout.com/rest/6.0/payments/decryptapplepaydata
    JSON-RPC decryptApplePayData
    SOAP decryptApplePayData

    decryptApplePayData payload

    BODY: (ApplePayData property value should be the event.payment.token from the onpaymentauthorized event)

    {
       "ApplePayData":"543a07601ed9f8b8ee226e9630a24e2a91da8fdb08e6786fa02bf628f395bb91"
    }
  3. Place the order.

    Once the Apple Pay data is received from 2Checkout, this needs to be used in the Payment Method object of the Payment Details object in the API call to place an order.

    Protocol

    Endpoint

    REST POST https://api.2checkout.com/rest/6.0/orders
    JSON-RPC placeOrder
    SOAP placeOrder
    {
       "Items":[
          {
             "Code":"5DCB30C6B0",
             "Quantity":1
          }
       ],
       "BillingDetails":{
          "Email":"example@email.com",
          "FirstName":"Customer First Name",
          "LastName":"Customer Last Name",
          "CountryCode":"US",
          "State":"California",
          "City":"San Francisco",
          "Address1":"Example Street",
          "Zip":"90210"
       },
       "PaymentDetails":{
          "Type":"APPLE_PAY",
          "Currency":"USD",
          "CustomerIP":"10.10.10.10",
          "PaymentMethod":{
             "ApplePayToken":"ApplePayDataToken"
          }
       },
       "Language":"en",
       "Country":"US",
       "CustomerIP":"10.10.10.10",
       "Source":"Website",
       "ExternalCustomerReference":"externalCustomerId",
       "Currency":"USD",
       "MachineId":"123456789"
    }
Rate this article:

Need help?

Do you have a question? If you didn’t find the answer you are looking for in our documentation, you can contact our Support teams for more information. If you have a technical issue or question, please contact us. We are happy to help.

Not yet a Verifone customer?

We’ll help you choose the right payment solution for your business, wherever you want to sell, in-person or online. Our team of experts will happily discuss your needs.

Verifone logo