Skip to content

Repository files navigation

PGGateway

Payment SDK of payment gateways

Supported Payment Gateways

  • Braintree
  • Paypal REST API

Demo

Method 1:

Using Docker-Compose

  1. download docker-compose
  2. run docker-compose build
  3. run docker-compose up

go to http://localhost:3000 or http://localhost:3000/check-payment

Method 2:

node version: > 7.7

  1. prepare a redis server up and running, the example will look for 127.0.0.1:6379, go to examples/payment/db/db.js to change the host/port when needed
const client = redis.createClient({
    host: process.env.DEMO_REDIS || '127.0.0.1', // demo, change if need
    port: 6379  // demo port, change if need
  })
  1. run the example server
npm install
npm run start:example

go to http://localhost:3000 or http://localhost:3000/check-payment

Test Credit Cards https://developers.braintreepayments.com/guides/credit-cards/testing-go-live/php

How to use

import PGGateway, { BraintreeGateway, RestPaypalGateway } from 'pg-gateway'

/**
 * 1. init gateways to be used
 * 2. add gateways into library, init library to add centralized db writing
 * 3. add routes
 */

// 1. init gateways to be used
/**
  * braintree configs
  */
const braintree = BraintreeGateway.init({
  isSandbox: true,
  BRAINTREE_MERCHANT_ID: 'cx48wn2djqx2m4db',
  BRAINTREE_PUBLIC_KEY: '3zw4pzcmg46jbt77',
  BRAINTREE_PRIVATE_KEY: '01418a6cb6f2ab403dd8e61620934c96'
})

/**
  * rest paypal configs
  */
const restPaypal = RestPaypalGateway.init({
  isSandbox: true,
  paypalClientId: 'AbVGZ5Tc_2HKOYaIpiXPm_JHXbMECy7J7WnTyP2y4n3LsHfjwHvNE9XPHVPVHuF2qv8fVKm5qJ9U3txS',
  paypalSecret: 'EOBxs78isIeg7myFaReh0rw-eTC_y47TETI5vT5-AfMB6i1FtmwopA-OY_RV6bJTlU3yMU34IbzbI9Xv',
  returnUrl: 'http://localhost:3000/paypal/execute',
  cancelUrl: 'http://localhost:3000/',
  onPaymentCreated: Order.save,       // callback to save the pending order
  onPaymentExecuted: Order.getById    // callback to save the approved payment
})

// 2. add gateways into library
PGGateway.use(braintree)
PGGateway.use(restPaypal)

// 3. add routes
router.use(PgGateway.init({
  // centralized way to save the approved payment into db/cache
  onPaymentApproved: (gateway, order, response) => {
    return Payment.save(gateway, order, response)
  },
}))
// REST Paypal
router.post('/paypal', PgGateway.REST_PAYPAL.members.create)
router.get('/paypal/execute', PgGateway.REST_PAYPAL.members.execute)
// Braintree
router.post('/braintree/execute', PgGateway.BRAINTREE.members.execute)


app.use(router.routes())
app.use(router.allowMethods())

API Reference

PGGateway

Kind: global class

pgGateway.onPaymentInited() ⇒ object | false

Default action onPaymentInited, can be overrided using PGGateway.init method. This is also can be access thru ctx.pgGateway.onPaymentInited

Kind: instance property of PGGateway

Param Type Description
order object Order
response object Response from 3th party gateway

pgGateway.onPaymentApproved() ⇒ object | false

Default action onPaymentApproved, can be overrided using PGGateway.init method. This is also can be access thru ctx.pgGateway.onPaymentApproved

Kind: instance property of PGGateway

Param Type Description
gatewayName string Gateway name
order object Order object
response object Response from 3th party gateway

pgGateway.loadPaymentCreated() ⇒ object | false

Default action loadPaymentCreated, can be overrided using PGGateway.init method. This is also can be access thru ctx.pgGateway.loadPaymentCreated

Kind: instance property of PGGateway

Param Type Description
refId string Reference Id

pgGateway.loadPaymentApproved() ⇒ object | false

Default action loadPaymentApproved, can be overrided using PGGateway.init method. This is also can be access thru ctx.pgGateway.loadPaymentApproved

Kind: instance property of PGGateway

Param Type Description
refId string Reference Id
username string User Name

pgGateway.use() ⇒ object

Add new configured payment gateway

Kind: instance property of PGGateway
Returns: object - self

Param Type Description
name string Custom name that used to identify the gateway being added
gateway BaseGateway Payment gateway extends class BaseGateway

pgGateway.init() ⇒ function

Return a middleware to inject pgGateway into request's context

Kind: instance property of PGGateway
Returns: function - middleware
Access: public

Param Type Description
options object Options
options.onPaymentInited function Function that will be performed when payment is inited
options.onPaymentApproved function Function that will be performed when payment is approved( or confirmed by 3th party gateway)
options.loadPaymentCreated function Function to retrive data from database for payment created
options.loadPaymentApproved function Function to retrive data from database for payment approved

Example

import PgGateway from 'pg-gateway'

PgGateway.use(new BraintreeGateway({
 ...configs
}))

// To inject pgGateway into request's context
router.use(PgGateway.init({
 onPaymentApproved: (params) => {
   //...save data into db
},
 loadPaymentApproved: (refId, userName) => {
   //...retrive data from db
}
}))

RestPaypalGateway

RestPaypalGateway - Provide middleware to handle payments

Kind: global class
Extends: BaseGateway

restPaypalGateway.init(options) ⇒ object

Single entry to create a well-configured braintree gateway

Kind: instance method of RestPaypalGateway
Returns: object - new RestPaypalGateway

Param Type Description
options object Options
options.isSandbox boolean
options.paypalClientId string
options.paypalSecret string
options.returnUrl string
options.cancelUrl string

Example

import PGGateway from 'pg-gateway'
import { RestPaypalGateway } from '..path'

PGGateway.use(RestPaypalGateway.init({
   isSandbox: true,
   paypalClientId: 'AbVGZ5Tc_2HKOYaIpiXPm_JHXbMECy7J7WnTyP2y4n3LsHfjwHvNE9XPHVPVHuF2qv8fVKm5qJ9U3txS',
   paypalSecret: 'EOBxs78isIeg7myFaReh0rw-eTC_y47TETI5vT5-AfMB6i1FtmwopA-OY_RV6bJTlU3yMU34IbzbI9Xv',
   returnUrl: 'http://localhost:3000/paypal/execute',
   cancelUrl: 'http://localhost:3000/',
   onPaymentCreated: (id, order) => { //cache the order }
   onPaymentExecuted: (key) => { //retrive the cached order}
}))

// this instance will expose 2 functions
// - create: function to create payment and send to paypal
// - execute: middleware to process payment
//
// you may access thru 
// - pgGateway.BRAINTREE.member.create or 
// - pgGateway.BRAINTREE.member.execute 

restPaypalGateway.createPayment(ctx, next) ⇒ promise

Middleware to create paypal payment

Kind: instance method of RestPaypalGateway

Param Type
ctx any
next any

restPaypalGateway.executePayment(ctx) ⇒ promise

Middleware to execute paypal payment

Kind: instance method of RestPaypalGateway

Param Type
ctx any

BraintreeGateway

BraintreeGateway - Provide middleware to handle payments

Kind: global class
Extends: BaseGateway

braintreeGateway.init(options) ⇒ object

Single entry to create a well-configured braintree gateway

Kind: instance method of BraintreeGateway
Returns: object - new BraintreeGateway

Param Type Description
options object Options
options.isSandbox boolean
options.BRAINTREE_MERCHANT_ID string
options.BRAINTREE_PUBLIC_KEY string
options.BRAINTREE_PRIVATE_KEY string

Example

import PGGateway from 'pg-gateway'
import { BraintreeGateway } from '..path'

PGGateway.use(BraintreeGateway.init({
   isSandbox: true,
   BRAINTREE_MERCHANT_ID: 'cx48wn2djqx2m4db',
   BRAINTREE_PUBLIC_KEY: '3zw4pzcmg46jbt77',
   BRAINTREE_PRIVATE_KEY: '01418a6cb6f2ab403dd8e61620934c96'
}))

// this instance will expose 2 function
// - createToken: function to create client token
// - execute: middleware to process payment
//
// you may access thru 
// - pgGateway.BRAINTREE.member.createToken or 
// - pgGateway.BRAINTREE.member.execute

braintreeGateway.createToken() ⇒ object

Middleware to create braintree client token

Kind: instance method of BraintreeGateway
Returns: object - Response from braintree.clientToken.generate()

braintreeGateway.executePayment(ctx, next)

Middleware to execute payment after receiving payment nonce from client

Kind: instance method of BraintreeGateway

Param Type
ctx Object
next function

Generate Docs

npm run docs

Test

npm run test
npm run test:watch

About

Payment gateway SDK

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages