npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

apple-pay-helper

v1.1.0

Published

A utility library for handling server side Apple Pay sessions and decrypting tokens.

Downloads

36

Readme

npm downloads npm version License: MIT

🍏 ApplePayHelper

ApplePayHelper is a comprehensive JavaScript library designed to simplify the integration of Apple Pay within your Node.js applications. It provides a seamless way to initiate Apple Pay sessions, decrypt payment tokens, and map the decrypted tokens to different payment providers. Most of the decryption logic is taken from samcorcos/apple-pay-decrypt and was modified to work with modern Node.js. I highly recommend checking his guide on how to generate the required PEM files for Apple Pay. Although mentioned Unpacked Size 45 kB in the npm package, the actual sum size of the files that are being used is around 14kb and 7kb on gzip format. That is not including the dependencies.

🌟 Features

  • Apple Pay Merchant Validation: Easily initiate Apple Pay sessions with Apple's servers for the onmerchantvalidation event.
  • Payment Token Decryption: Securely decrypt Apple Pay payment tokens using your merchant certificates once the user has authorized the payment.
  • Payment Provider Mapping: Map the decrypted payment tokens to different payment providers.

📦 Installation

You can install the apple-pay-helper library from npm:

npm install apple-pay-helper

Before You Begin

📜 You'll need to have the following files before you can get started:

  1. Merchant certificate (merchantCertOnlyPem)
  2. Merchant private key (merchantKeyOnlyPem)
  3. Payment processor private key (paymentProcessorPrivateKeyPem)

You can read more here on how to generate these files:

  • File Numbers 1,2
  • File Number 3
  • The old decrypting library that does not support modern Node.js has a good guide on how to generate the required files. You can find it here

🚀 Quick Start

Here's a quick example to get you started:

const fs = require("fs");
const ApplePayHelper = require("apple-pay-helper");

/**
 * Get the 3 required PEM files from the filesystem.
 * 1. Merchant certificate (merchantCertOnlyPem)
 * 2. Merchant private key (merchantKeyOnlyPem)
 * 3. Payment processor private key (paymentProcessorPrivateKeyPem)
 * Note: Make sure to generate these files using the instructions
 */
const merchantCertOnlyPem = fs
  .readFileSync("path/to/your-merchant-cert-file.pem")
  .toString();
const merchantKeyOnlyPem = fs
  .readFileSync("path/to/your-merchant-key-file.pem")
  .toString();
const paymentProcessorPrivateKeyPem = fs
  .readFileSync("path/to/your-payment-processor-key-file.pem")
  .toString();

const applePayConfig = {
  merchantId: "merchant.com.yourdomain.pay",
  displayName: "Your Company",
  initiativeContext: "yourdomain.com",
  version: "0008",
  initiative: "web", // web, ios, etc.
  isValidateExpirationDate: true, // neither to validate the expiration date of the payment token or not
  tokenExpirationWindow: 300000, // defaults to 5 minutes
  paymentProcessorPrivateKeyPem: paymentProcessorPrivateKeyPem,
  merchantCertOnlyPem: merchantCertOnlyPem,
  merchantKeyOnlyPem: merchantKeyOnlyPem,
};

const applePayHelper = new ApplePayHelper(applePayConfig);

/**
 * Initiate an Apple Pay session with Apple's servers. Use the response inside the 'onmerchantvalidation' event handler in the front.
 */
const appleValidationURL =
  "https://apple-pay-gateway.apple.com/paymentservices/startSession";
const response = await applePayHelper.initiateSession(appleValidationURL);

// After the user has authorized the payment, you can send the encrypted token from the front and decrypt the token like so:
const decryptedToken = await applePayHelper.decryptToken(paymentData);

// at this point you send the decrypted token to your payment processor

For a more detailed example, check out the test file

🧪 Testing

To run the tests, you'll need to have the required files mentioned above. Once you have the files, create a config.js file similar to the example.config.js file and add the paths to the file. Then run the following command to run the testing server:

npm run test

Then you can import the Postman collection and test the endpoints.

Run in Postman

📖 Resources

📝 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙌 Contributing

Contributions, issues, and feature requests are welcome! Feel free to open an issue or submit a pull request.


Happy coding! 🎉