Code Monkey home page Code Monkey logo

pay-server-demos's Introduction

Google Pay Server Demos

This project demonstrates integrating Google Pay with various Payments Service Providers (PSPs), using JavaScript and Node.js. It consists of two parts: a client library that wraps each of the PSP server-side APIs, and a demo app that demonstrates end to end integration for any of the included PSPs.

If you're just interested in sample code for a particular PSP, go straight to the google-pay-psp-client/handlers directory, where you'll find server-side samples for each PSP.

Demo App

The demo app is implemented as a minimal shopping site you can use to test the end to end integration for your chosen PSP.

Setup:

  • Copy demo/config.json.example to demo/config.json and edit the config for your PSP
  • Install dependencies: npm install .
  • Run the app: node demo/app.js
  • View at http://localhost:3000

Client Library

The google-pay-psp-client directory contains a client library that wraps each of the PSP server-side APIs.

Example:

const clients = require('./google-pay-psp-client');

// PSP-specific configuration, in this case Braintree.
const config = {
  environment: 'Sandbox',
  merchantId: 'merchant id',
  publicKey: 'public key',
  privateKey: 'private key',
};

// An order requires a total, currency, and
// client-side response from the Google Pay API.
const order = {
  total: 100,
  currency: 'USD',
  paymentResponse: req.body.paymentResponse,
};

// Each PSP has a "pay" method, which takes config, order, and returns a Promise.
clients.braintree
  .pay(config, order)
  .then(response => {
    console.log(response); // PSP-specific response.
  })
  .catch(error => {
    console.error(error); // PSP-specific error.
  });

Current PSPs

Don't see your PSP here? Feel free to contribute an integration example using the steps below.

Adding a new PSP

Client Library:

Add a new JavaScript file to the google-pay-psp-client/handlers directory. It should export a single function that accepts a config and an order object (see examples of these in the "Client Library" section above). The function should return a Promise that resolves on successful payment, and rejects on failure. The order object will contain some calculated fields for the total amount:

  • order.totalInt the total amount in smallest possible units, (eg cents for USD)
  • order.totalFixed the rounded total amount (eg 2 decimal places for USD)

Demo App:

Add a new key to demo/config.json for the PSP, using the same name as the JavaScript file name added to the client library. Use this to store all configuration variables used by the PSP, at minimum containing a clientConfig key containing the gateway parameters for the PSP.

Optionally, if custom client-side integration is required, add a new JavaScript file to the demo/public/handlers directory. It should be named the same as the key added to config.json, and it will be run once the page's dom is loaded. You can then override the function names in demo/public/shop.js. See the demo/public/handlers directory for examples.

pay-server-demos's People

Contributors

stephenmcd avatar dmengelt avatar gcatanese avatar dackers86 avatar mootrichard avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.