# ApplePay

> Wrapper for working with Apple Pay payments

[![node](https://img.shields.io/node/v/apple-pay.svg?maxAge=2592000&style=flat-square)]()[![npm version](https://img.shields.io/npm/v/apple-pay.svg?style=flat-square)](https://www.npmjs.com/package/apple-pay)

[![npm downloads](https://img.shields.io/npm/dm/apple-pay.svg?style=flat-square)](https://www.npmjs.com/package/apple-pay)[![npm](https://img.shields.io/npm/dt/apple-pay.svg?style=flat-square)](https://www.npmjs.com/package/apple-pay)

## Why ?
Simplifies interaction with native ApplePay API. Support:
- [x] Auto detect `supportsVersion` 
- [x] Filters unsupported `supportedNetworks` version based 
- [x] Static getter method `isPaymentAvailable` 

## Install

```bash
npm i -S apple-pay
```

> **Note:** This project is compatible with node v14+

## Usage

```js
import ApplePay, { STATUS_SUCCESS, STATUS_FAILURE } from 'apple-pay';

if (ApplePay.isPaymentAvailable) {
  const paymentRequest = {
    total: {
      label: 'Merchant name',
      amount: '10.0'
    },
    countryCode: 'US',
    currencyCode: 'USD',
    supportedCountries: ['US'],
    merchantCapabilities: ['supports3DS'],
    supportedNetworks: ['amex', 'discover']
  }

  const session = new ApplePay({
    onValidateMerchant,
    onPaymentAuthorized,
    paymentRequest
  });

  async function onValidateMerchant(event) {
    const body = JSON.stringify({
      validationURL: event.validationURL
    });

    const response = await fetch('you/validate/merchant/url', { body });
    const json = await response.json();

    session.completeMerchantValidation(json);
  }

  async function onPaymentAuthorized(event) {
    const body = JSON.stringify({
      paymentToken: event.payment.token
    });

    const response = await fetch('you/payment/authorized/url', { body });
    const status = response.ok ? STATUS_SUCCESS : STATUS_FAILURE;

    session.completePayment(status);
  }
}
```
