ApplePay

ApplePay

This class represents an Apple Pay component. Instances of this class have methods for validating the merchant server and tokenizing payments.

Constructor

new ApplePay(options)

You cannot use this constructor directly. Use braintree.applePay.create instead.

Parameters:
Name Type Description
options object

Options

Source:

Methods

createPaymentRequest(paymentRequest) → {external:ApplePayPaymentRequest}

Merges a payment request with Braintree defaults to return an {external:ApplePayPaymentRequest}.

The following properties are assigned to paymentRequest if not already defined. Their default values come from the Braintree gateway.

  • countryCode
  • currencyCode
  • merchantCapabilities
  • supportedNetworks
Parameters:
Name Type Description
paymentRequest external:ApplePayPaymentRequest

The payment request details to apply on top of those from Braintree.

Source:
Example
var applePay = require('braintree-web/apple-pay');

applePay.create({client: clientInstance}, function (applePayErr, applePayInstance) {
  if (applePayErr) {
    // Handle error here
    return;
  }

  var paymentRequest = applePayInstance.createPaymentRequest({
    total: {
      label: 'My Company',
      amount: '19.99'
    }
  });

  var session = new ApplePaySession(1, paymentRequest);

  // ...

performValidation(options, callback) → {void}

Validates your merchant website, as required by ApplePaySession before payment can be authorized.

Parameters:
Name Type Description
options object

Options

Properties
Name Type Description
validationURL string

The validationURL fram an ApplePayValidateMerchantEvent.

displayName string

The canonical name for your store. Use a non-localized name. This parameter should be a UTF-8 string that is a maximum of 128 characters. The system may display this name to the user.

callback callback

The second argument, data, is the Apple Pay merchant session object. Pass the merchant session to your Apple Pay session's completeMerchantValidation method.

Source:
Example
var applePay = require('braintree-web/apple-pay');

applePay.create({client: clientInstance}, function (applePayErr, applePayInstance) {
  if (applePayErr) {
    // Handle error here
    return;
  }

  var paymentRequest = applePayInstance.createPaymentRequest({
    total: {
      label: 'My Company',
      amount: '19.99'
    }
  });
  var session = new ApplePaySession(1, paymentRequest);

  session.onvalidatemerchant = function (event) {
    applePayInstance.performValidation({
      validationURL: event.validationURL,
      displayName: 'My Great Store'
    }, function (validationErr, validationData) {
      if (validationErr) {
        console.error(validationErr);
        session.abort();
        return;
      }

      session.completeMerchantValidation(validationData);
    });
  };
});

tokenize(options, callback) → {void}

Tokenizes an Apple Pay payment. This will likely be called in your ApplePaySession's onpaymentauthorized callback.

Parameters:
Name Type Description
options object

Options

Properties
Name Type Description
token object

The payment.token property of an external:ApplePayPaymentAuthorizedEvent.

callback callback

The second argument, data, is the tokenized payload.

Source:
Example
var applePay = require('braintree-web/apple-pay');

applePay.create({client: clientInstance}, function (applePayErr, applePayInstance) {
  if (applePayErr) {
    // Handle error here
    return;
  }

  var paymentRequest = applePayInstance.createPaymentRequest({
    total: {
      label: 'My Company',
      amount: '19.99'
    }
  });
  var session = new ApplePaySession(1, paymentRequest);

  session.onpaymentauthorized = function (event) {
    applePayInstance.tokenize({
      token: event.payment.token
    }, function (tokenizeErr, tokenizedPayload) {
      if (tokenizeErr) {
        session.completePayment(ApplePaySession.STATUS_FAILURE);
        return;
      }
      session.completePayment(ApplePaySession.STATUS_SUCCESS);

      // Send the tokenizedPayload to your server here!
    });
  };

  // ...
});