Skip to content
This repository has been archived by the owner on Jul 8, 2022. It is now read-only.

Commit

Permalink
Merge pull request #3 from martincostello/ApplePay-JS-V2
Browse files Browse the repository at this point in the history
Add support for Apple Pay JS version 2
  • Loading branch information
martincostello committed Mar 27, 2017
2 parents 6421593 + da4f488 commit fc32002
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
47 changes: 42 additions & 5 deletions ApplePaySession.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
var self = {};

self.hasActiveSession = false;
self.isApplePaySetUp = true;
self.paymentsEnabled = true;
self.paymentRequest = null;
self.merchantIdentifier = "";
self.supportedVersions = [1, 2];
self.validationURL = "https://apple-pay-gateway-cert.apple.com/paymentservices/startSession";
self.version = 1;

Expand All @@ -40,6 +42,14 @@
self.merchantIdentifier = merchantIdentifier;
};

/**
* Sets whether the user has set up Apple Pay.
* @param {Boolean} isSetUp - Whether Apple Pay has been set up by the user on the device.
*/
self.setUserSetupStatus = function (isSetUp) {
self.isApplePaySetUp = isSetUp;
};

/**
* Sets the validation URL to use for merchant validation.
* @param {String} validationURL - The URL to use for merchant validation.
Expand Down Expand Up @@ -87,7 +97,7 @@
throw "Page already has an active payment session.";
}

if (version !== self.version) {
if (self.supportedVersions.indexOf(version) === -1) {
throw "\"" + version + "\" is not a supported version.";
}

Expand All @@ -100,6 +110,10 @@
var merchantCapabilities = ["supports3DS", "supportsEMV", "supportsCredit", "supportsDebit"];
var paymentNetworks = ["amex", "discover", "interac", "masterCard", "privateLabel", "visa"];

if (version > 1) {
paymentNetworks.push("jcb");
}

if (countryCodes.indexOf(paymentRequest.countryCode) === -1) {
throw "\"" + paymentRequest.countryCode + "\" is not valid country code.";
}
Expand Down Expand Up @@ -190,9 +204,9 @@
self.onCanMakePaymentsWithActiveCard = function (session, merchantIdentifier) {

var result =
self.paymentsEnabled === true &&
merchantIdentifier &&
merchantIdentifier === self.merchantIdentifier;
self.paymentsEnabled === true &&
merchantIdentifier &&
merchantIdentifier === self.merchantIdentifier;

return Promise.resolve(result);
};
Expand Down Expand Up @@ -276,14 +290,33 @@

};

/**
* Callback for ApplePaySession.openPaymentSetup().
* @param {Object} session - The current ApplePaySession.
* @param {String} merchantIdentifier - The merchant identifier passed to the function.
*/
self.onOpenPaymentSetup = function (session, merchantIdentifier) {

var result =
self.paymentsEnabled === true &&
merchantIdentifier &&
merchantIdentifier === self.merchantIdentifier;

if (result === true) {
result = self.isApplePaySetUp;
}

return Promise.resolve(result);
};

/**
* Callback for ApplePaySession.supportsVersion().
* @param {Object} session - The current ApplePaySession.
* @param {Number} version - The version passed to the function.
* @return {Boolean} The value to return from ApplePaySession.supportsVersion().
*/
self.onSupportsVersion = function (session, version) {
return version === self.version;
return self.supportedVersions.indexOf(version) !== -1;
};

return self;
Expand Down Expand Up @@ -318,6 +351,10 @@
return ApplePaySessionPolyfill.onCanMakePaymentsWithActiveCard(this, merchantIdentifier);
};

ApplePaySession.openPaymentSetup = function (merchantIdentifier) {
return ApplePaySessionPolyfill.onOpenPaymentSetup(this, merchantIdentifier);
};

ApplePaySession.supportsVersion = function (version) {
return ApplePaySessionPolyfill.onSupportsVersion(this, version);
};
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Apple Pay JS is a way of accepting Apple Pay in websites using Safari in either

This polyfill provides a way to make [```ApplePaySession```](https://developer.apple.com/reference/applepayjs/applepaysession) available for testing your implementation in browsers that would otherwise not provide support for Apple Pay JS, such as in Chrome on Windows.

The polyfill supports Apple Pay JS versions 1 and 2.

## Examples

To create a minimal working integration using the polyfill, first install the JavaScript using [bower](https://bower.io/):
Expand Down Expand Up @@ -93,6 +95,22 @@ if ("ApplePaySession" in window && ApplePaySession.canMakePayments() === true) {
}
```

### Apple Pay Set Up

If you need to test displaying the "Set Up Apple Pay" button, use the ```setUserSetupStatus(bool)``` function, as shown below, to specify that the user has not yet set up Apple Pay on the device.

```js
ApplePaySessionPolyfill.setUserSetupStatus(false);
```

By default this value is set to ```true``` so that Apple Pay is available in the polyfill.

If you need to test compatibility with devices that do not support Apple Pay set up, then delete the function from ```ApplePaySession``` before your implementation code is loaded:

```js
delete ApplePaySession.openPaymentSetup;
```

## Feedback

Any feedback or issues can be added to the [issues](https://github.com/justeat/applepayjs-polyfill/issues) for this project in GitHub.
Expand Down

0 comments on commit fc32002

Please sign in to comment.