diff --git a/.DS_Store b/.DS_Store index e2f14ea..29907bb 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/README.md b/README.md index 18f1b42..bd53ee5 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Using npm: ```bash -$ npm i -s kkiapay +$ npm i -s kkiapay/nodejs-sdk ``` @@ -30,14 +30,18 @@ Using cdn: ## Example -Request to retrieve transactions - ```js -kkiapay.transaction({ transactionId:"xxxxxx",privatekey:"xxxxxxx",publickey:"xxxxxxx",secretkey:"xxxxxxx"}). +// setup your api key (https://www.kkiapay.me) +//initialize kkiapay in production environnment +const k = kkiapay({privatekey:"xxxxxxx",publickey:"xxxxxxx",secretkey:"xxxxxxx"}) +//initialize kkiapay in sandbox environnment +const k = kkiapay({privatekey:"xxxxxxx",publickey:"xxxxxxx",secretkey:"xxxxxxx",sandbox:true}) +// Request to retrieve transactions +k.verify("transactionId"). then((response) => { //handle response }). catch((error) => { //handle error }) -``` \ No newline at end of file +``` diff --git a/lib/.DS_Store b/lib/.DS_Store new file mode 100644 index 0000000..5b1d078 Binary files /dev/null and b/lib/.DS_Store differ diff --git a/lib/function.js b/lib/function.js index 144d9a8..59719b2 100644 --- a/lib/function.js +++ b/lib/function.js @@ -6,10 +6,6 @@ const validateOptions=(options)=>{ throw new Error("parameter must be a object"); } - if(!(options.hasOwnProperty("transactionId"))) - { - throw new Error("transactionId is required"); - } if(!(options.hasOwnProperty("privatekey"))) { diff --git a/lib/http.js b/lib/http.js index 21feb09..16936eb 100644 --- a/lib/http.js +++ b/lib/http.js @@ -1,8 +1,11 @@ const axios = require('axios'); -const opts = require('./opts') +// const opts = require('./opts') const http = axios.create({ - baseURL: opts.baseURL + // baseURL: opts.baseURL, + headers: { + 'Content-Type': 'application/json' + } }) module.exports = http \ No newline at end of file diff --git a/lib/index.js b/lib/index.js index d6489a8..22a520d 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,3 +1,2 @@ -module.exports = { - transaction: require('./transaction'), -} \ No newline at end of file +const transaction = require('./transaction') +module.exports = transaction \ No newline at end of file diff --git a/lib/opts.js b/lib/opts.js index d8d91a7..0ed1d12 100644 --- a/lib/opts.js +++ b/lib/opts.js @@ -1,4 +1,3 @@ module.exports={ - baseURL: "https://api.kkiapay.me", transactionEndpoint: '/api/v1/transactions/status', -} \ No newline at end of file +} diff --git a/lib/transaction/index.js b/lib/transaction/index.js index eea1f80..4f5e85e 100644 --- a/lib/transaction/index.js +++ b/lib/transaction/index.js @@ -3,20 +3,39 @@ const opts = require('../opts') const { validateOptions }=require("../function") -module.exports = async function(options){ - const { transactionId,publickey,privatekey,secretkey } = validateOptions(options); - const headers = { - "Content-Type": "application/json", - "x-api-key": publickey // todo doit être change après que la police de contrôle soit activé - }; - return http.post(opts.transactionEndpoint, - {transactionId}, - {headers}) +module.exports = function (config) { + validateOptions(config) + http.defaults.headers.common['x-api-key'] = config.publickey; + http.defaults.headers.common['x-secret-api-key'] = config.secretkey; + http.defaults.headers.common['x-private-api-key'] = config.privatekey; + http.defaults.baseURL = config.sandbox ? "https://api-sandbox.kkiapay.me" : "https://api.kkiapay.me" + + return features +} - .then((response) => { - return response.data - }).catch((error) => { - throw new Error("Transaction Not Found"); + +const features={ + + verify: async (transactionId) => { + + return http.post(opts.transactionEndpoint, + {transactionId}) + + .then((response) => { + return response.data + }).catch((error) => { + console.log(error.response.data) + + if(error.response.status===4003) + { + throw new Error(error.response.data.reason) + } + else + { + throw new Error("Transaction Not Found") + } }) + + } } \ No newline at end of file diff --git a/sample/index.js b/sample/index.js index 1890489..0ea5279 100644 --- a/sample/index.js +++ b/sample/index.js @@ -1,7 +1,11 @@ -const kkiapay=require("../index") +const kkiapay=require("../lib") + + +let k=kkiapay({publickey:"f774e960812811e99cf7e73f32bcec42",privatekey:"process.env.privatekey",secretkey:"process.env.secretkey",sandbox:true}) + +k.verify("O_S9_rdlX"). +then(e=>console.log(e)). +catch(err=>console.log(err)) -kkiapay.transaction({ transactionId:"RUYnpqsSQ1",privatekey:process.env.privatekey,publickey:process.env.publickey,secretkey:process.env.secretkey}). -then(response => console.log(response)). -catch(err => console.log(err)) diff --git a/tests/function.test.js b/tests/function.test.js index 806ad90..28c16f0 100644 --- a/tests/function.test.js +++ b/tests/function.test.js @@ -37,11 +37,6 @@ describe('Validation of transaction parameters',() => { ).toThrow(); }); - it('must trow exception when transactionId is not specify', () => { - expect(() => - validateOptions(omit('transactionId')) - ).toThrow(); - }); it('must trow exception when transactionId is not specify', () => { expect(validateOptions(omit(''))).toMatchObject(options) diff --git a/tests/transaction.test.js b/tests/transaction.test.js index 6e1602d..67d74f5 100644 --- a/tests/transaction.test.js +++ b/tests/transaction.test.js @@ -1,5 +1,11 @@ require ("babel-polyfill"); -const transaction = require('../lib/transaction'); +const options = { + privatekey:"12345", + publickey:"1234", + secretkey:"1234", + transactionId:"123456" +}; +const transaction = require('../lib/transaction')(options).verify; const changeStatus = require('./__Mock__/http') @@ -9,6 +15,7 @@ describe("transaction verify test",() => { changeStatus(true); transaction({ transactionId:"RUYnpqsSQ1",privatekey:process.env.privatekey,publickey:process.env.publickey,secretkey:process.env.secretkey}) .then((data) => { + console.log(data) expect(data).toMatchObject({}) done() })