Payment SDK of payment gateways
- Braintree
- Paypal REST API
Using Docker-Compose
- download docker-compose
- run
docker-compose build - run
docker-compose up
go to http://localhost:3000 or http://localhost:3000/check-payment
node version: > 7.7
- prepare a redis server up and running, the example will look for 127.0.0.1:6379, go to
examples/payment/db/db.jsto 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
})- 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
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())
-
- .onPaymentInited() ⇒
object|false - .onPaymentApproved() ⇒
object|false - .loadPaymentCreated() ⇒
object|false - .loadPaymentApproved() ⇒
object|false - .use() ⇒
object - .init() ⇒
function
- .onPaymentInited() ⇒
-
RestPaypalGateway ⇐
BaseGateway- .init(options) ⇒
object - .createPayment(ctx, next) ⇒
promise - .executePayment(ctx) ⇒
promise
- .init(options) ⇒
-
BraintreeGateway ⇐
BaseGateway- .init(options) ⇒
object - .createToken() ⇒
object - .executePayment(ctx, next)
- .init(options) ⇒
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 |
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 |
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 |
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 |
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 |
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 - Provide middleware to handle payments
Kind: global class
Extends: BaseGateway
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 Middleware to create paypal payment
Kind: instance method of RestPaypalGateway
| Param | Type |
|---|---|
| ctx | any |
| next | any |
Middleware to execute paypal payment
Kind: instance method of RestPaypalGateway
| Param | Type |
|---|---|
| ctx | any |
BraintreeGateway - Provide middleware to handle payments
Kind: global class
Extends: BaseGateway
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.executeMiddleware to create braintree client token
Kind: instance method of BraintreeGateway
Returns: object - Response from braintree.clientToken.generate()
Middleware to execute payment after receiving payment nonce from client
Kind: instance method of BraintreeGateway
| Param | Type |
|---|---|
| ctx | Object |
| next | function |
npm run docs
npm run test
npm run test:watch