-
Notifications
You must be signed in to change notification settings - Fork 0
Issue 1 private api #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PaIIadium
wants to merge
10
commits into
main
Choose a base branch
from
issue-1-private-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
642bf85
Implement PrivatBank api (transactions and cards info)
PaIIadium e1f13ea
restructuring
PaIIadium 0b08954
Merge branch 'main' of https://github.com/AlmostGreatBand/lemon-serve…
PaIIadium 997f055
style fixes
PaIIadium 364bc2c
style fixes
PaIIadium 28560e1
BankDataSource interface is implemented
PaIIadium 0e76cc5
style fixes
PaIIadium 8e18bba
Merge branch 'main' of https://github.com/AlmostGreatBand/lemon-serve…
PaIIadium ef3d01a
Refactoring
PaIIadium 2e93040
Merge branch 'main' into issue-1-private-api
PaIIadium File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| // Use IntelliSense to learn about possible attributes. | ||
| // Hover to view descriptions of existing attributes. | ||
| // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
| "version": "0.2.0", | ||
| "configurations": [ | ||
| { | ||
| "type": "pwa-node", | ||
| "request": "launch", | ||
| "name": "Launch Program", | ||
| "skipFiles": [ | ||
| "<node_internals>/**" | ||
| ], | ||
| "program": "${workspaceFolder}\\src\\main.js" | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,4 +13,3 @@ class BankDataSource { | |
| } | ||
|
|
||
| module.exports = BankDataSource; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,184 @@ | ||
| 'use strict'; | ||
|
|
||
| // TODO: Add implementation of Privat24 API Handler | ||
| const http = require('https'); | ||
| const xmlJs = require('xml-js'); | ||
| const crypto = require('crypto'); | ||
| const { fillTransactionsXml } = require('./utils/transactions'); | ||
| const { fillBalanceXml } = require('./utils/balance'); | ||
| const { | ||
| stringToDate, | ||
| dateToString, | ||
| divideIntoPeriods | ||
| } = require('./utils/dates'); | ||
|
|
||
| const BankDataSource = require('../BankDataSource'); | ||
|
|
||
| class PrivatDataSource extends BankDataSource { | ||
| constructor() { | ||
| super(); | ||
| } | ||
|
|
||
| createSignature(xml, password) { | ||
| const regexp = /(?<=<data>).*(?=<\/data)/; | ||
| const dataString = xml.match(regexp)[0]; | ||
| const md5Signature = crypto.createHash('md5') | ||
| .update(`${dataString}${password}`, 'utf8') | ||
| .digest('hex'); | ||
| const signature = crypto.createHash('sha1') | ||
| .update(md5Signature, 'utf8') | ||
| .digest('hex'); | ||
| return signature; | ||
| } | ||
|
|
||
| fillXmlWithSignature(xml, signature) { | ||
| const xmlObj = xmlJs.xml2js(xml, { compact: true }); | ||
|
|
||
| const xmlMerchant = xmlObj.request.merchant; | ||
| xmlMerchant.signature._text = signature; | ||
|
|
||
| const xmlWithSignature = xmlJs.js2xml(xmlObj, { | ||
| spaces: 0, | ||
| compact: true, | ||
| fullTagEmptyElement: true | ||
| }); | ||
| return xmlWithSignature; | ||
| } | ||
|
|
||
| handleBankResponse(cb, resolve, reject, res) { | ||
| let data = ''; | ||
| if (res.statusCode !== 200) { | ||
| const errMsg = `Server did not send data. STATUS CODE: ${res.statusCode}`; | ||
| reject(new Error(errMsg)); | ||
| } | ||
| res.setEncoding('utf8'); | ||
| res.on('data', chunk => { | ||
| data += chunk; | ||
| }); | ||
| res.on('end', () => { | ||
| cb(resolve, reject, data); | ||
| }); | ||
| res.on('error', err => reject(err)); | ||
| } | ||
|
|
||
| handleTransactionsData(resolve, reject, dataXml) { | ||
| let dataObj; | ||
| try { | ||
| dataObj = xmlJs.xml2js(dataXml, { compact: true }); | ||
| } catch (err) { | ||
| reject(new Error(err)); | ||
| return; | ||
| } | ||
|
|
||
| if (dataObj.response.data.error !== null) { | ||
| const errorMessage = dataObj.response.data.error._attributes.message; | ||
| reject(new Error(errorMessage)); | ||
| return; | ||
| } | ||
|
|
||
| const result = []; | ||
| let transactions = dataObj.response.data.info.statements.statement; | ||
| if (!Array.isArray(transactions)) transactions = [transactions]; | ||
| transactions.map(val => result.push(val._attributes)); | ||
| resolve(result); | ||
| } | ||
|
|
||
| handleBalanceData(resolve, reject, dataXml) { | ||
| let dataObj; | ||
| try { | ||
| dataObj = xmlJs.xml2js(dataXml, { compact: true }); | ||
| } catch (err) { | ||
| reject(new Error('Invalid XML')); | ||
| return; | ||
| } | ||
|
|
||
| if (dataObj.response.data.error !== null) { | ||
| const errorMessage = dataObj.response.data.error._attributes.message; | ||
| reject(new Error(errorMessage)); | ||
| return; | ||
| } | ||
| const cardBalance = dataObj.response.data.info.cardbalance; | ||
| const card = cardBalance.card; | ||
|
|
||
| const result = { | ||
| cardNumber: card.card_number._text, | ||
| currency: card.currency._text, | ||
| balance: cardBalance.balance._text, | ||
| availableBalance: cardBalance.av_balance._text, | ||
| dateBalance: cardBalance.bal_date._text, | ||
| creditLimit: cardBalance.fin_limit._text | ||
| }; | ||
| resolve(result); | ||
| } | ||
|
|
||
| getData(dataForRequest, cb, path, xml) { | ||
| const options = { | ||
| hostname: 'api.privatbank.ua', | ||
| path, | ||
| method: 'POST', | ||
| headers: | ||
| { 'Content-Type': 'application/xml; charset=UTF-8' } | ||
| }; | ||
| const req = http.request(options); | ||
|
|
||
| const password = dataForRequest.merchantPassword; | ||
| const signature = this.createSignature(xml, password); | ||
| const xmlWithSignature = this.fillXmlWithSignature(xml, signature); | ||
| req.write(xmlWithSignature); | ||
| req.end(); | ||
|
|
||
| return new Promise((resolve, reject) => { | ||
| const handler = this.handleBankResponse.bind(this, cb, resolve, reject); | ||
| req.on('response', handler); | ||
| req.on('error', err => reject(err)); | ||
| }); | ||
| } | ||
|
|
||
| async getTransactionsData(dataForTransactions) { | ||
| const startDate = stringToDate(dataForTransactions.startDate); | ||
| const endDate = stringToDate(dataForTransactions.endDate); | ||
|
|
||
| const periods = divideIntoPeriods(startDate, endDate); | ||
| const promises = []; | ||
|
|
||
| const path = '/p24api/rest_fiz'; | ||
| const xml = fillTransactionsXml(dataForTransactions); | ||
| const cb = this.handleTransactionsData.bind(this); | ||
|
|
||
| periods.forEach(period => { | ||
| const copyDataForTransactions = Object.assign({}, dataForTransactions); | ||
| copyDataForTransactions.startDate = dateToString(period[0]); | ||
| copyDataForTransactions.endDate = dateToString(period[1]); | ||
| promises.push(this.getData(copyDataForTransactions, cb, path, xml)); | ||
| }); | ||
| const transactions = await Promise.all(promises); | ||
|
|
||
| const result = []; | ||
| transactions.forEach(arr => result.push(...arr.reverse())); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| async getBalanceData(dataForBalance) { | ||
| const result = []; | ||
| const path = '/p24api/balance'; | ||
| const xml = fillBalanceXml(dataForBalance); | ||
| const cb = this.handleBalanceData.bind(this); | ||
| for (const data of dataForBalance) { | ||
| result.push(this.getData(data, cb, path, xml)); | ||
| } | ||
| return Promise.all(result); | ||
| } | ||
|
|
||
| // conf is similar to dataForTransactionsRequest | ||
| async getTransactions(card, conf) { | ||
| conf.cardNumber = card.cardNum + ''; | ||
| return this.getTransactionsData(conf); | ||
| } | ||
| /* additional method for getting balances of cards array | ||
| conf is array of objects similar to dataForBalanceRequest */ | ||
| async getBalance(conf) { | ||
| return this.getBalanceData(conf); | ||
| } | ||
| } | ||
|
|
||
| module.exports = PrivatDataSource; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| 'use strict'; | ||
|
|
||
| const PrivatDataSource = require('./privat24.js'); | ||
|
|
||
| // sample data for transactions request | ||
| const dataForTransactionsRequest = { | ||
| merchantPassword: '55x3Ft9C96yx7s1cAMO2KVn1apuDA0X6', | ||
| merchantId: 123456, | ||
| wait: 10, | ||
| test: 0, | ||
| paymentId: '', | ||
| startDate: '11.05.2020', | ||
| endDate: '23.10.2020', | ||
| cardNumber: '1234567890123456' | ||
| }; | ||
|
|
||
| // sample data for balance request | ||
| const dataForBalanceRequest = { | ||
| merchantPassword: '55x3Ft9C96yx7s1cAMO2KVn1apuDA0X6', | ||
| merchantId: 123456, | ||
| wait: 10, | ||
| test: 0, | ||
| paymentId: '', | ||
| cardNumber: '1234567890123456', | ||
| country: 'UA' | ||
| }; | ||
|
|
||
| // example of using | ||
| (async () => { | ||
| const pds = new PrivatDataSource(); | ||
| const tranData = await pds.getTransactionsData(dataForTransactionsRequest); | ||
| const balanceData = await pds.getBalanceData([dataForBalanceRequest]); | ||
| // dataForBalanceRequest is array of data objects | ||
| console.log(tranData); | ||
| console.log(balanceData); | ||
| })(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| 'use strict'; | ||
|
|
||
| const fs = require('fs'); | ||
| const xmlJs = require('xml-js'); | ||
| const balanceXML = fs.readFileSync('./xml_data/balance.xml', 'utf-8'); | ||
|
|
||
| const fillBalanceXml = dataForBalance => { | ||
| const xmlObj = xmlJs.xml2js(balanceXML, { compact: true }); | ||
|
|
||
| const xmlMerchant = xmlObj.request.merchant; | ||
| xmlMerchant.id._text = dataForBalance.merchantId; | ||
|
|
||
| const xmlData = xmlObj.request.data; | ||
| xmlData.wait._text = dataForBalance.wait; | ||
| xmlData.test._text = dataForBalance.test; | ||
|
|
||
| const xmlPayment = xmlData.payment; | ||
| xmlPayment._attributes.id = dataForBalance.paymentId; | ||
|
|
||
| const [xmlCardNumber, xmlCountry] = xmlPayment.prop; | ||
| xmlCardNumber._attributes.value = dataForBalance.cardNumber; | ||
| xmlCountry._attributes.value = dataForBalance.country; | ||
|
|
||
| const xmlWithData = xmlJs.js2xml(xmlObj, { | ||
| spaces: 0, | ||
| compact: true, | ||
| fullTagEmptyElement: true | ||
| }); | ||
| return xmlWithData; | ||
| }; | ||
|
|
||
| module.exports = { fillBalanceXml }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| 'use strict'; | ||
|
|
||
| const stringToDate = date => { | ||
| const [day, month, year] = date.split('.'); | ||
| return new Date(`${year}-${month}-${day}`); | ||
| }; | ||
|
|
||
| const dateToString = date => | ||
| `${date.getDate()}.${date.getMonth() + 1}.${date.getFullYear()}`; | ||
|
|
||
| const divideIntoPeriods = (startDate, endDate) => { | ||
| const periods = []; | ||
| const day = 1000 * 3600 * 24; | ||
| const threeMonths = day * 30 * 3; | ||
| const periodEnd = new Date(startDate.getTime() + threeMonths); | ||
| periods.push([startDate, periodEnd]); | ||
|
|
||
| while (periods[periods.length - 1][1] < endDate) { | ||
| const lastPeriodEnd = periods[periods.length - 1][1]; | ||
| const newPeriodStart = new Date(lastPeriodEnd.getTime() + day); | ||
| const newPeriodEnd = new Date(newPeriodStart.getTime() + threeMonths); | ||
| periods.push([newPeriodStart, newPeriodEnd]); | ||
| } | ||
| periods[periods.length - 1][1] = endDate; | ||
|
|
||
| return periods; | ||
| }; | ||
|
|
||
| module.exports = { stringToDate, dateToString, divideIntoPeriods }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| 'use strict'; | ||
|
|
||
| const fs = require('fs'); | ||
| const xmlJs = require('xml-js'); | ||
| const transactionsXML = fs.readFileSync('./xml_data/transactions.xml', 'utf-8'); | ||
|
|
||
| const fillTransactionsXml = dataForTransactions => { | ||
| const xmlObj = xmlJs.xml2js(transactionsXML, { compact: true }); | ||
|
|
||
| const xmlMerchant = xmlObj.request.merchant; | ||
| xmlMerchant.id._text = dataForTransactions.merchantId; | ||
|
|
||
| const xmlData = xmlObj.request.data; | ||
| xmlData.wait._text = dataForTransactions.wait; | ||
| xmlData.test._text = dataForTransactions.test; | ||
|
|
||
| const xmlPayment = xmlData.payment; | ||
| xmlPayment._attributes.id = dataForTransactions.paymentId; | ||
|
|
||
| const [xmlStartDate, xmlEndDate, xmlCardNumber] = xmlPayment.prop; | ||
| xmlStartDate._attributes.value = dataForTransactions.startDate; | ||
| xmlEndDate._attributes.value = dataForTransactions.endDate; | ||
| xmlCardNumber._attributes.value = dataForTransactions.cardNumber; | ||
|
|
||
| const xmlWithData = xmlJs.js2xml(xmlObj, | ||
| { spaces: 0, compact: true, fullTagEmptyElement: true }); | ||
| return xmlWithData; | ||
| }; | ||
|
|
||
| module.exports = { fillTransactionsXml }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <request version="1.0"> | ||
| <merchant> | ||
| <id></id> | ||
| <signature></signature> | ||
| </merchant> | ||
| <data> | ||
| <oper>cmt</oper> | ||
| <wait>0</wait> | ||
| <test>0</test> | ||
| <payment id=""> | ||
| <prop name="cardnum" value=""></prop> | ||
| <prop name="country" value=""></prop> | ||
| </payment> | ||
| </data> | ||
| </request> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <request version="1.0"> | ||
| <merchant> | ||
| <id></id> | ||
| <signature></signature> | ||
| </merchant> | ||
| <data> | ||
| <oper>cmt</oper> | ||
| <wait></wait> | ||
| <test></test> | ||
| <payment id=""> | ||
| <prop name="sd" value=""></prop> | ||
| <prop name="ed" value=""></prop> | ||
| <prop name="card" value=""></prop> | ||
| </payment> | ||
| </data> | ||
| </request> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why delete newline?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was a mistake