Apple Pay: Difference between revisions

From Barion Documentation
Jump to navigation Jump to search
(replaced existing content with new and approved)
No edit summary
Line 3: Line 3:


Learn to offer the Apple Pay button as a payment option in your Barion shop when the customer uses a supported browser (typically Safari) on an eligible device.
Learn to offer the Apple Pay button as a payment option in your Barion shop when the customer uses a supported browser (typically Safari) on an eligible device.
{{NotificationBox|title=NOTE|text=Barion currently only supports Apple Pay buttons on the web. Native iPhone app support coming soon.|color=#1993c7}}


<blockquote>This article is about directly integrating the Apple Pay button in your webshop.
<blockquote>This article is about directly integrating the Apple Pay button in your webshop.
Line 27: Line 25:
<p>It may happen that even though Barion approves your Barion shop to sell a certain item, you will not be allowed to display the Apple Pay button as a payment option for customers to pay for that item.</p></blockquote></li>
<p>It may happen that even though Barion approves your Barion shop to sell a certain item, you will not be allowed to display the Apple Pay button as a payment option for customers to pay for that item.</p></blockquote></li>
<li>an account in the Barion Wallet for each of the currencies that you’d like to accept using Apple Pay</li></ul>
<li>an account in the Barion Wallet for each of the currencies that you’d like to accept using Apple Pay</li></ul>
<span id="limitations"></span>
== Limitations ==
== Limitations ==


* you can’t yet tokenize your customer’s card details when using the Apple Pay button, so [[01-payments/03-payment-scenarios/02-set-up-subscription|recurring payments]] aren’t available
* you can’t yet tokenize your customer’s card details when using the Apple Pay button, so [[Token payment 3D Secure|recurring payments]] aren’t available
* Barion currently only supports Apple Pay button on the web, and not on native iOS apps
* regardless of the integration detailed here, the Apple Pay button will only be displayed to your customers if they comply with Apple’s requirements:
* regardless of the integration detailed here, the Apple Pay button will only be displayed to your customers if they comply with Apple’s requirements:
** they’re using [https://support.apple.com/en-us/102896 an Apple Pay-compatible device]
** they’re using [https://support.apple.com/en-us/102896 an Apple Pay-compatible device]
** they’re viewing your webshop using a browser that supports Apple Pay
** they’re viewing your webshop using a browser that supports Apple Pay
** they’re located [https://support.apple.com/en-us/102775 in a country or region where Apple Pay is available]
** they’re located [https://support.apple.com/en-us/102775 in a country or region where Apple Pay is available]
<span id="steps"></span>
== Steps ==
== Steps ==
<span id="setting-up-your-apple-prerequisites"></span>
=== Setting up your Apple prerequisites ===
=== Setting up your Apple prerequisites ===


Line 50: Line 43:
# Click the “Manage Apple Pay settings”, and then “Download domain verification file” to save the verification file for your shop’s domain.
# Click the “Manage Apple Pay settings”, and then “Download domain verification file” to save the verification file for your shop’s domain.
# Save the domain verification file that you downloaded in the content root of all the sites that your specified domains use, making sure of the following:
# Save the domain verification file that you downloaded in the content root of all the sites that your specified domains use, making sure of the following:
#* the file should send the `<mimeMap fileExtension="." mimeType="application/json" />`` MIME data;
#* the file should send the <code><mimeMap fileExtension="." mimeType="application/json" /></code> MIME data;
#* the file must be accessible externally, and not be password-protected, or behind a proxy or redirect;
#* the file must be accessible externally, and not be password-protected, or behind a proxy or redirect;
#* the sites must be served over HTTPS.
#* the sites must be served over HTTPS.
# Enter all the domains that you’d like to use Apple Pay on (into whose root you’ve downloaded the domain verification file) in the text box provided, then click “Save”.
# Enter all the domains that you’d like to use Apple Pay on (into whose root you’ve downloaded the domain verification file) in the text box provided, then click “Save”.
<span id="configuring-and-displaying-the-apple-pay-button"></span>
=== Configuring and displaying the Apple Pay button ===
=== Configuring and displaying the Apple Pay button ===


Line 65: Line 56:
</blockquote>
</blockquote>
<ol start="2" style="list-style-type: decimal;">
<ol start="2" style="list-style-type: decimal;">
<li><p>Add the <code>apple-pay-button</code> element (<code></p><div id="apple-pay-button"></div><p></code>) to your checkout page, [https://developer.apple.com/documentation/apple_pay_on_the_web/displaying_apple_pay_buttons_using_css and use the CSS included in the button script to display it].</p></li>
<li><p>Add the <code>apple-pay-button</code> element (<div id="apple-pay-button"></div>) to your checkout page, [https://developer.apple.com/documentation/apple_pay_on_the_web/displaying_apple_pay_buttons_using_css and use the CSS included in the button script to display it].</p></li>
<li><p>Start a new ApplePaySession when the checkout page loads:</p></li></ol>
<li><p>Start a new ApplePaySession when the checkout page loads:</p></li></ol>


Line 85: Line 76:


<p>If Apple can verify your Barion shop as a registered Apple Pay provider, the method checks the following:</p>
<p>If Apple can verify your Barion shop as a registered Apple Pay provider, the method checks the following:</p>
<pre> * that your customer's device is capable of using Apple Pay
</li></ol>
* that Apple Pay is enabled on your customer's device, and
<ul>
* that the customer has the details of at least one active and web payment-eligible bank card stored in their Apple Pay wallet.</pre>
  <li>that your customer's device is capable of using Apple Pay</li>
<ol start="2" style="list-style-type: decimal;">
  <li>that Apple Pay is enabled on your customer's device, and</li>
  <li>that the customer has the details of at least one active and web payment-eligible bank card stored in their Apple Pay wallet.</li>
</ul>
<ol start="4" style="list-style-type: decimal;">
<li><ol start="2" style="list-style-type: decimal;">
<li>Handle [https://developer.apple.com/documentation/apple_pay_on_the_web/paymentcredentialstatus the <code>PaymentCredentialStatus</code> object] that the method returns to make a decision about displaying the Apple Pay button.</li></ol>
<li>Handle [https://developer.apple.com/documentation/apple_pay_on_the_web/paymentcredentialstatus the <code>PaymentCredentialStatus</code> object] that the method returns to make a decision about displaying the Apple Pay button.</li></ol>
</li></ol>
</li></ol>
<span id="handling-payments"></span>
=== Handling payments ===
=== Handling payments ===


Line 98: Line 91:


<ol style="list-style-type: decimal;">
<ol style="list-style-type: decimal;">
<li><p>Set up an event handler for [https://developer.apple.com/documentation/apple_pay_on_the_web/applepaysession/1778021-onvalidatemerchant <code>ApplePaySession.onvalidatemerchant</code>] that calls [[05-api-reference/04-apple-pay-resource/validate-session|the ValidateSession Barion API endpoint]], passing it the <code>onvalidatemerchant</code> event’s <code>ValidationUrl</code> attribute.</p>
<li><p>Set up an event handler for [https://developer.apple.com/documentation/apple_pay_on_the_web/applepaysession/1778021-onvalidatemerchant <code>ApplePaySession.onvalidatemerchant</code>] that calls [[ApplePay-ValidateSession|the ValidateSession Barion API endpoint]], passing it the <code>onvalidatemerchant</code> event’s <code>ValidationUrl</code> attribute.</p>
<syntaxhighlight lang="javascript">async function validateApplePaySession(POSKey, SessionRequestUrl, ShopUrl) {
<syntaxhighlight lang="javascript">async function validateApplePaySession(POSKey, SessionRequestUrl, ShopUrl) {
   const url = "https://api.test.barion.com/v2/ApplePay/ValidateSession";
   const url = "https://api.test.barion.com/v2/ApplePay/ValidateSession";
Line 131: Line 124:
};</syntaxhighlight>
};</syntaxhighlight>
<blockquote><p>This event is triggered when the Apple Pay payment sheet is displayed to the customer.</p></blockquote></li>
<blockquote><p>This event is triggered when the Apple Pay payment sheet is displayed to the customer.</p></blockquote></li>
<li><p>Set up an event handler for [https://developer.apple.com/documentation/apple_pay_on_the_web/applepaysession/1778020-onpaymentauthorized <code>ApplePaySession.onpaymentauthorized</code>] that calls the [[05-api-reference/04-apple-pay-resource/start-payment-with-apple-token|StartPaymentWithAppleToken Barion API endpoint]], passing it the event’s following attributes:</p>
<li><p>Set up an event handler for [https://developer.apple.com/documentation/apple_pay_on_the_web/applepaysession/1778020-onpaymentauthorized <code>ApplePaySession.onpaymentauthorized</code>] that calls the [[ApplePay-StartPaymentWithAppleToken|StartPaymentWithAppleToken Barion API endpoint]], passing it the event’s following attributes:</p>
<ul>
<ul>
<li>token</li>
<li>token</li>
Line 138: Line 131:
</li></ol>
</li></ol>


<blockquote>Note that StartPaymentWithAppleToken is actually a specilized version of [[05-api-reference/01-payment-resource/01-payment-start|the payment/start API endpoint]], and so will also require all the usual parameters that you’d pass to that endpoint.
<blockquote>Note that <code>StartPaymentWithAppleToken</code> is actually a specialized version of [[Payment-Start-v2|the payment/start API endpoint]], and so will also require all the usual parameters that you’d pass to that endpoint.
</blockquote>
</blockquote>
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
Line 197: Line 190:
<ol start="3" style="list-style-type: decimal;">
<ol start="3" style="list-style-type: decimal;">
<li><p>Set up an event handler for <code>oncancel</code> to implement your business logic for when the user abandons the Apple Pay payment.</p>
<li><p>Set up an event handler for <code>oncancel</code> to implement your business logic for when the user abandons the Apple Pay payment.</p>
<p>Make sure that your event handler indicates that the session is canceled so the ApplePaySession’s completePayment method doesn’t get executed.</p>
<p>Make sure that your event handler indicates that the session is canceled so the ApplePaySession’s <code>completePayment</code> method doesn’t get executed.</p>
<syntaxhighlight lang="javascript">  applePaySession.oncancel = function (event) {
<syntaxhighlight lang="javascript">  applePaySession.oncancel = function (event) {
     // your oncancel business logic
     // your oncancel business logic
Line 206: Line 199:


<syntaxhighlight lang="javascript">  applePaySession.begin();</syntaxhighlight>
<syntaxhighlight lang="javascript">  applePaySession.begin();</syntaxhighlight>
<blockquote>This starts the merchant validation process that results in `ApplePaySession.onvalidatemerchant` being triggered.</blockquote>
<blockquote>This starts the merchant validation process that results in <code>ApplePaySession.onvalidatemerchant</code> being triggered.</blockquote>
<span id="testing-your-implementation"></span>
=== Testing your implementation ===
=== Testing your implementation ===


If you’re located in one of the countries or regions where the Apple Pay sandbox environment is available, [https://developer.apple.com/apple-pay/sandbox-testing/ use Apple’s Sandbox testing to try out your integration].
If you’re located in one of the countries or regions where the Apple Pay sandbox environment is available, [https://developer.apple.com/apple-pay/sandbox-testing/ use Apple’s Sandbox testing to try out your integration].
== Related links ==


<span id="related-links"></span>
[[ApplePay-ValidateSession|ValidateSession API endpoint]]
== Related links ==


[[05-api-reference/04-apple-pay-resource/validate-session|ValidateSession API endpoint]] [[05-api-reference/04-apple-pay-resource/start-payment-with-apple-token|StartPaymentWithAppleToken API endpoint]]
[[ApplePay-StartPaymentWithAppleToken|StartPaymentWithAppleToken API endpoint]]

Revision as of 05:59, 9 August 2024

Apple Pay integration

Learn to offer the Apple Pay button as a payment option in your Barion shop when the customer uses a supported browser (typically Safari) on an eligible device.

This article is about directly integrating the Apple Pay button in your webshop.

If you simply want to display Apple Pay as a payment option in the Barion Smart Gateway, no further configuration is necessary.

The appeal of Apple Pay is that it requires only two actions from the customer: selecting the ‘Pay with Apple Pay’ button, and confirming the payment with Touch ID or Face ID.

Another advantage to your Barion shop’s payment workflow is that customers aren’t navigated away from your checkout page when using Apple Pay.

Prerequisites

  • access to and being able to modify both your webshop’s front-end and back-end code
  • your webshop using HTTPS (having a TLS 1.1 or higher certificate installed) for all its pages that include the Apple Pay button
  • Barion Wallet with an approved Barion shop

    Note that Apple’s policies regarding acceptable products and services are more limiting than those of Barion.

    It may happen that even though Barion approves your Barion shop to sell a certain item, you will not be allowed to display the Apple Pay button as a payment option for customers to pay for that item.

  • an account in the Barion Wallet for each of the currencies that you’d like to accept using Apple Pay

Limitations

  • you can’t yet tokenize your customer’s card details when using the Apple Pay button, so recurring payments aren’t available
  • Barion currently only supports Apple Pay button on the web, and not on native iOS apps
  • regardless of the integration detailed here, the Apple Pay button will only be displayed to your customers if they comply with Apple’s requirements:

Steps

Setting up your Apple prerequisites

When using Barion’s Apple Pay API endpoints, Barion gives you an Apple developer account, and registers a Merchant ID, Payment Processing Certificate, and Merchant Identity Certificate for you as a subsidiary of Barion’s Apple Pay credentials.

Verify and register your webshop’s domain name or names:

  1. Log in to your Barion Wallet, and go to your Barion shop’s Shops>Actions>Settings screen.
  2. Click the “Manage Apple Pay settings”, and then “Download domain verification file” to save the verification file for your shop’s domain.
  3. Save the domain verification file that you downloaded in the content root of all the sites that your specified domains use, making sure of the following:
    • the file should send the <mimeMap fileExtension="." mimeType="application/json" /> MIME data;
    • the file must be accessible externally, and not be password-protected, or behind a proxy or redirect;
    • the sites must be served over HTTPS.
  4. Enter all the domains that you’d like to use Apple Pay on (into whose root you’ve downloaded the domain verification file) in the text box provided, then click “Save”.

Configuring and displaying the Apple Pay button

  1. Load the button script into your checkout page by adding the following script tag inside the page head:

    <script src="https://applepay.cdn-apple.com/jsapi/v1.1.0/apple-pay-sdk.js"></script>

This script makes an ApplePaySession available to your site front-end, among other things.

  1. Add the apple-pay-button element (

    ) to your checkout page, and use the CSS included in the button script to display it.

  2. Start a new ApplePaySession when the checkout page loads:

  const request = {
    countryCode: 'US',
    currencyCode: 'USD',
    supportedNetworks: ['visa', 'masterCard', 'amex', 'discover'],
    merchantCapabilities: ['supports3DS'],
    total: { label: 'Your Merchant Name', amount: '10.00' }
  }
  const applePlaySession = new ApplePaySession(3, request);

  // add an isSessionCanceled boolean to easily be able to cancel the session if your user abandons the in-progress payment process
  var isSessionCanceled = false;
  1. Make sure to only display the Apple Pay button to users who can use it, by have your webshop’s checkout pages do the following when they load:

    1. Call the applePaySession.canMakePayments method, passing it your Barion shop’s public ID.

    If Apple can verify your Barion shop as a registered Apple Pay provider, the method checks the following:

  • that your customer's device is capable of using Apple Pay
  • that Apple Pay is enabled on your customer's device, and
  • that the customer has the details of at least one active and web payment-eligible bank card stored in their Apple Pay wallet.
    1. Handle the PaymentCredentialStatus object that the method returns to make a decision about displaying the Apple Pay button.

Handling payments

Set up your webshop to perform these steps each time a user selects the Apple Pay button.

  1. Set up an event handler for ApplePaySession.onvalidatemerchant that calls the ValidateSession Barion API endpoint, passing it the onvalidatemerchant event’s ValidationUrl attribute.

    async function validateApplePaySession(POSKey, SessionRequestUrl, ShopUrl) {
      const url = "https://api.test.barion.com/v2/ApplePay/ValidateSession";
      const data = {
        POSKey,
        SessionRequestUrl,
        ShopUrl,
      };
    
      try {
        const response = await fetch(url, {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          body: JSON.stringify(data),
        });
    
        if (!response.ok) {
          throw new Error(`Error: ${response.status} ${response.statusText}`);
        }
    
        const result = await response.json();
        return result;
      } catch (error) {
        // handle the error according to your business logic
      }
    }
    
    applePaySession.onvalidatemerchant = function (event) {
      validateApplePaySession(POSKey, event.validationURL, ShopUrl);
    };

    This event is triggered when the Apple Pay payment sheet is displayed to the customer.

  2. Set up an event handler for ApplePaySession.onpaymentauthorized that calls the StartPaymentWithAppleToken Barion API endpoint, passing it the event’s following attributes:

    • token
    • shippingContact
    • billingContact

Note that StartPaymentWithAppleToken is actually a specialized version of the payment/start API endpoint, and so will also require all the usual parameters that you’d pass to that endpoint.

async function startApplePayPayment(
  POSKey,
  ShippingContact,
  BillingContact,
  Token,
  /** include other required and optional Payment/Start params */
  PaymentStartProperties
) {
  const url =
    &quot;https://api.test.barion.com/v2/ApplePay/StartPaymentWithAppleToken&quot;;
  const data = {
    POSKey,
    ShippingContact,
    BillingContact,
    Token,
    ...PaymentStartProperties,
  };

  try {
    const response = await fetch(url, {
      method: &quot;POST&quot;,
      headers: {
        &quot;Content-Type&quot;: &quot;application/json&quot;,
      },
      body: JSON.stringify(data),
    });

    if (!response.ok) {
      throw new Error(`Error: ${response.status} ${response.statusText}`);
    }

    const result = await response.json();
    return result;
  } catch (error) {
    // handle the error according to your business logic
  }
}

const PaymentStartProperties = {
  PaymentType: &quot;Immediate&quot;,
};

applePaySession.onpaymentauthorized = function (event) {
  startApplePayPayment(
    POSKey,
    event.paymentInfo.shippingContent,
    event.paymentInfo.billingContact,
    event.paymentInfo.Token,
    PaymentStartProperties
  );
};

This event is triggered when the customer has authorized the Apple Pay payment using their Touch ID or Face ID.

  1. Set up an event handler for oncancel to implement your business logic for when the user abandons the Apple Pay payment.

    Make sure that your event handler indicates that the session is canceled so the ApplePaySession’s completePayment method doesn’t get executed.

      applePaySession.oncancel = function (event) {
        // your oncancel business logic
    
        isSessionCanceled = true;
      };
  2. Call ApplePaySession.begin to kick off the payment process.

  applePaySession.begin();

This starts the merchant validation process that results in ApplePaySession.onvalidatemerchant being triggered.

Testing your implementation

If you’re located in one of the countries or regions where the Apple Pay sandbox environment is available, use Apple’s Sandbox testing to try out your integration.

Related links

ValidateSession API endpoint

StartPaymentWithAppleToken API endpoint