Skip to content

Commit

Permalink
simplified, and more verbose, sdk invokation
Browse files Browse the repository at this point in the history
  • Loading branch information
jgermade committed Apr 9, 2019
1 parent 65cf753 commit b0cd62b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 38 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ node install @aplazame/node --save
Full example in: [Github](https://github.com/aplazame/node-sdk/tree/master/example)

``` js
var Aplazame = require('@aplazame/node')

var aplazame = new Aplazame('merchant_private_key', false)
var aplazame = require('@aplazame/node')({
access_token: 'merchant_private_key',
is_sandbox: true,
})

aplazame.post('/checkout', {
merchant: {...},
Expand Down
11 changes: 6 additions & 5 deletions example/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ require('dotenv').config()
var fs = require('fs'),
express = require('express'),
bodyParser = require('body-parser'),
Aplazame = require('../sdk')
ejs = require('ejs')

var app = express(),
ejs = require('ejs'),
index_html = fs.readFileSync('example/index.html', 'utf8')
index_html = fs.readFileSync('example/index.html', 'utf8'),
apz = require('../sdk')({
access_token: process.env.PRIVATE_KEY,
is_sandbox: true,
})

console.log('PUBLIC_KEY', process.env.PUBLIC_KEY)
console.log('PRIVATE_KEY', process.env.PRIVATE_KEY)

var apz = new Aplazame(process.env.PRIVATE_KEY, true)

app.use(bodyParser.json())

app.use('/static', express.static('example/static'))
Expand Down
63 changes: 33 additions & 30 deletions sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,36 +56,39 @@ function makeRequest (_url, data, options) {
})
}

function Aplazame (access_token, is_sandbox) {

if( !access_token ) throw new Error('access_token missing')
function aplazameHandler (access_token, is_sandbox) {

var apz = {}

;['get', 'delete'].forEach(function (method) {
apz[method] = function (path, options) {
options = options || {}
options.method = method
options.access_token = access_token
options.is_sandbox = is_sandbox
return makeRequest(api_origin + path, null, options).then(function (res) {
return res.data
})
}
})

;['post', 'put', 'patch'].forEach(function (method) {
apz[method] = function (path, data, options) {
options = options || {}
options.method = method
options.access_token = access_token
options.is_sandbox = is_sandbox
return makeRequest(api_origin + path, data, options).then(function (res) {
return res.data
})
}
})

this.access_token = access_token
this.is_sandbox = Boolean(is_sandbox)
return apz
}

;['get', 'delete'].forEach(function (method) {
Aplazame.prototype[method] = function (path, options) {
options = options || {}
options.method = method
options.access_token = this.access_token
options.is_sandbox = this.is_sandbox
return makeRequest(api_origin + path, null, options).then(function (res) {
return res.data
})
}
})

;['post', 'put', 'patch'].forEach(function (method) {
Aplazame.prototype[method] = function (path, data, options) {
options = options || {}
options.method = method
options.access_token = this.access_token
options.is_sandbox = this.is_sandbox
return makeRequest(api_origin + path, data, options).then(function (res) {
return res.data
})
}
})

module.exports = Aplazame
module.exports = function aplazameSDK (options = {}) {
if( !options.access_token ) throw new Error('access_token missing')

return aplazameHandler(options.access_token, options.is_sandbox)
}

0 comments on commit b0cd62b

Please sign in to comment.