# Accept a Wallet Payment on Web

Use our Pay Form with Tyro.js to accept Apple Pay or Google Pay payments on a web browser (or web view for Apple Pay).

## Apple Pay support

Once enabled, the Tyro Pay Form will automatically display the Apple Pay button if it is supported on the customer's device.
Apple Pay is supported for the following:

* Safari on iOS 10+
* Safari on macOS Sierra or later
* Chrome, Edge and Firefox on iOS 16+
* Chrome, Edge and Firefox on desktop, using scan to pay
* iOS Apps using WKWebView on iOS 13+


In Safari, the customer pays with the native Apple Pay payment sheet.

In Chrome, Edge and Firefox on desktop, the customer pays with scan to pay: Apple Pay shows a QR code, which the customer scans with their iPhone to complete the payment.

Outside Safari, the Apple Pay button only shows if your website's domain is [verified for Apple Pay](#1-verify-your-website-domain-for-apple-pay). If the domain isn't verified, the button doesn't show in those browsers, and the card form still works as normal. Your page's Content Security Policy must also allow Apple's sources. See [Content Security Policy](#content-security-policy).

Please refer to [Apple's documentation](https://support.apple.com/en-au/HT208531) for more information.

## Google Pay support

Once enabled, the Tyro Pay Form will automatically display the Google Pay button if it is supported on the customer's device.
Google Pay is supported on all major web browsers.

## Enabling Wallet Payments with Tyro.js

## Prerequisites

Please familiarise yourself with the guide to [Accept an Online Payment](/docs/online-payments/pay-online/integration-guide/tyro-js), as the steps outline in this document builds upon the existing integration.

### 1. Verify your website domain for Apple Pay

Attention
Tyro acts as the aggregator and processes the payments via our own Apple Developer account. Hence you do not need to create an Apple Developer account, Apple Pay Merchant ID, or certificates etc. Follow the steps below instead.

Before enabling Apple Pay, your website must be served over HTTPS and the domain/s verified. Subdomains must also be verified, for example, company.example.com and example.com each need to be verified.

* Download the [domain association file](https://docs.connect.tyro.com/.well-known/apple-developer-merchantid-domain-association) and host it at `https://<your-website-domain>/.well-known/apple-developer-merchantid-domain-association`. For example, if your domain is `https://example.com`, the file should be publicly available at `https://example.com/.well-known/apple-developer-merchantid-domain-association`. Make sure the file name is `apple-developer-merchantid-domain-association` and contains no extension, otherwise domain verification will fail.
* Once you have hosted the file, let Tyro know the domain/s you want verified and we will action this with Apple.
* All websites that use Apple Pay will need to be verified. This includes your sandbox and production environments.
* Outside Safari, Tyro.js only shows the Apple Pay button on verified domains, so an unverified domain shows no Apple Pay button in Chrome, Edge or Firefox.


### Content Security Policy

If your checkout page sets a Content Security Policy (CSP), it must allow the following sources for Apple Pay:

| Directive | Source | Used for | Required for |
|  --- | --- | --- | --- |
| `script-src` | `https://applepay.cdn-apple.com` | The Apple Pay SDK | All browsers, including Safari |
| `connect-src` | `https://smp-paymentservices.apple.com` | Checking Apple Pay availability with Apple | Browsers other than Safari |
| `frame-src` | `https://applepay.cdn-apple.com` | The scan-to-pay (QR code) window | Browsers other than Safari |
| `font-src` | `https://applepay.cdn-apple.com` | Fonts in the scan-to-pay window | Browsers other than Safari |


If these sources are not allowed:

* In browsers other than Safari, the Apple Pay button doesn't show. Card payments still work.
* If `connect-src` is allowed but `frame-src` or `font-src` is blocked, the Apple Pay button shows but the QR code doesn't open.
* Safari is not affected by the sources marked "Browsers other than Safari".
* If you use CSP reporting, blocked sources show up as violation reports.


### 2. Enable Apple Pay and Google Pay

At step 4 from [Accept an Online Payment](/docs/online-payments/pay-online/integration-guide/tyro-js#4-create-a-pay-request-and-inject-the-pay-form), modify your code to include [options](/docs/online-payments/pay-online/integration-guide/tyro-js/pay-form#config-parameters) when you create the Pay Form `tyro.createPayForm()`.

#### Updated Pay Form config


```javascript
// Javascript sample code
let paySecret;
async function init() {
    const response = await fetch("/create-order", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ items }),
    });
    paySecret = await response.json().paySecret;
    await tyro.init(paySecret);

    const payForm = tyro.createPayForm({
        options: {
            // Updated code
            applePay: {
                enabled: true,
            },
            googlePay: {
                enabled: true
            },
            creditCardForm: {
                enabled: true
            }
        }
    });
    payForm.inject("#tyro-pay-form");
}

init();
```

#### 2.1 Optional - Configure Options

You can configure [options](/docs/online-payments/pay-online/integration-guide/tyro-js/pay-form#config-parameters) when creating the Pay Form.

#### Displaying Wallet Buttons Individually

You can also display the wallet buttons on their own. For example, to display only the Apple Pay button, set `options.applePay.enabled=true` and other options to false.


```javascript
// Javascript sample code
const payForm = tyro.createPayForm({
    options: {
        applePay: {
            enabled: true,
            // optional
            totalLabel: 'Example Merchant Name'
            // optional - defaults to all networks when not provided
            supportedNetworks: ['visa', 'mastercard', 'amex', 'jcb', 'maestro']
        },
        googlePay: {
            enabled: false
        },
        creditCardForm: {
            enabled: false
        }
    }
});
```

### 3. Add the Wallet Payment Listeners

Set the listeners to handle the Wallet Payment events.

#### 3.1 Set the WalletPaymentBeginListener

Invoked when the user has clicked on one of the Wallet Payment buttons. This is called before the Wallet dialog is shown, e.g. the Apple Pay Dialog that shows the cards to use.
You would typically show a loading state on your UI during this process.


```javascript
// Javascript sample code
  payForm.setWalletPaymentBeginListener((paymentType) => {
    // show loading code here...
    if(paymentType === 'APPLE_PAY') {
        // optionally do something specific to Apple Pay
    } else if(paymentType === 'GOOGLE_PAY'){
        // optionally do something specific to Google Pay
    }
  });
```

#### 3.2 Set the WalletPaymentCancelledListener

Invoked when the Wallet Payment dialog has been closed by the user, e.g. they decide to cancel the payment before choosing a saved card.
Use this listener to hide the loading state on your UI.


```javascript
// Javascript sample code
  payForm.setWalletPaymentCancelledListener((paymentType) => {
    // hide loading code here...
    if(paymentType === 'APPLE_PAY') {
        // optionally do something specific to Apple Pay
    } else if(paymentType === 'GOOGLE_PAY'){
        // optionally do something specific to Google Pay
    }
  });
```

#### 3.3  Set the WalletPaymentCompleteListener

Invoked when the Wallet Payment is complete.

* If the [error object](/docs/online-payments/pay-online/integration-guide/tyro-js/pay-form#error-object) is defined, the transaction has failed. Inspect the [error object](/docs/online-payments/pay-online/integration-guide/tyro-js/pay-form#error-object) for more details.
* If the error object is undefined, the transaction has completed. Fetch the PayRequest and get the status to check the result.
* For more statuses see [Pay Request statuses](/docs/online-payments/pay-online/payment-scenarios/during-payment)



```javascript
// Javascript sample code

async function showPaymentResult() {
  const payRequest = await tyro.fetchPayRequest();

  switch (payRequest.status) {
    case "SUCCESS":
      return showSuccess();
    case "FAILED":
      return showFail();
    ......
  }
}

payForm.setWalletPaymentCompleteListener((paymentType, error) => {
    if(error) {
        const { type, errorMessage, errorCode, gatewayCode } = error;
        // handle errors here
            switch (type) {
            case "SERVER_VALIDATION_ERROR":
                return showServerValidationError(errorMessage, errorCode, gatewayCode);
            case "CARD_ERROR":
                return showCardError(errorMessage, errorCode, gatewayCode);
            case "SERVER_ERROR":
                return showGenericError(errorMessage, errorCode, gatewayCode);
            ......
            }
    } else {
        showPaymentResult();
    }
});
```

### 5. Configure styles

Attention
When displaying the Wallet Payment buttons, please make sure you follow the [Apple Pay](https://developer.apple.com/design/human-interface-guidelines/technologies/apple-pay/buttons-and-marks) and [Google Pay](https://developers.google.com/pay/api/web/guides/brand-guidelines) design guidelines.

To configure the Wallet Payment styles including Apple Pay and Google Pay specific styles, please see the [Style Guide](/docs/online-payments/pay-online/integration-guide/tyro-js/style-guide).

### 6. Test your integration

Attention
You must use the cards that have been added to Google Pay or Apple Pay.
Note, when using `liveMode=false`, your card will not be charged.

#### 6.1 Testing Apple Pay

* Your domain must be verified and be served over HTTPS.
* Open your integrated checkout page on Safari with an Apple Pay enabled device. Make sure you have added cards to Apple Pay. The Apple Pay button should be displayed.
* Open your integrated checkout page in Chrome or Edge on desktop. The Apple Pay button should be displayed, and clicking it should open a QR code you can scan with an iPhone that has cards added to Apple Pay.
* You should be able to successfully submit payments via the Apple Pay button.
* If the Apple Pay button doesn't show outside Safari, check that your domain is verified and that your [Content Security Policy](#content-security-policy) allows Apple's sources.


#### 6.2 Testing Google Pay

* Make sure you are logged into your Google Account and you have Google Pay enabled with cards.
* Open your integrated checkout page on Chrome with a Google Pay enabled device. The Google Pay button should be displayed.
* You should be able to successfully submit payments via the Google Pay button.