Skip to content
This repository has been archived by the owner on Oct 9, 2020. It is now read-only.

Commit

Permalink
Add Order payment command
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderC committed Jan 24, 2019
1 parent c228958 commit 903b313
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 48 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ CLI Tool:

```bash
alexanderc@MacBook-Pro:~/$ pt.md --help
pt.md [command]
pt.md

Commands:
pt.md configure Configure pt.md client
pt.md list List current deliveries
pt.md pay [id] Pay for an order

Options:
--version Show version number [boolean]
Expand Down Expand Up @@ -65,7 +66,6 @@ total = 1;
items = [
{
"isPaid": false,
"paymentURI": "https://pt.md/BeneficiarZone/TransactionJournal/CardPayOrder?id=123456",
"rowNum": 1,
"orderID": 123456,
"orderDate": "2019-01-01T07:00:00.007Z",
Expand All @@ -91,6 +91,12 @@ items = [
}
]
*/

// @TODO will be improved in future
const item = items[0];
const paymentHTML = await pt.paymentPage(item.orderID);

// ...save payment HTML to file and open it with a browser to get redirected to payment itself
```

## Development
Expand Down
60 changes: 52 additions & 8 deletions cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ const Configstore = require('configstore');
const { prompt } = require('enquirer');
const chalk = require('chalk');
const Table = require('terminal-table');
const terminalLink = require('terminal-link');
const yargs = require('yargs');
const tempfile = require('tempfile');
const fs = require('fs-extra');
const opn = require('opn');
const pkg = require('../package.json');
const Pt = require('../src/index');
const debug = require('../src/helper/debug')(__filename);
Expand Down Expand Up @@ -46,7 +48,7 @@ function display(items) {

table.push(
[
'Paid', 'Id', 'Order Date', 'Status', 'Cell', 'Pickup Interval', 'Shop', 'Goods Name', 'Delivery Company', 'Actions',
'Paid', 'Id', 'Order Date', 'Status', 'Cell', 'Pickup Interval', 'Shop', 'Goods Name', 'Delivery Company',
].map(col => chalk.gray(col)),
);

Expand All @@ -71,12 +73,6 @@ function display(items) {
deliveryCompanyName,
];

if (!isPaid) {
row.push(terminalLink('Pay', item.paymentURI));
} else {
row.push(chalk.blue('N/A'));
}

table.push(row);
}

Expand Down Expand Up @@ -174,6 +170,54 @@ yargs // eslint-disable-line
}
},
)
.command(
'pay [id]',
'Pay for an order',
(yargs) => { // eslint-disable-line
return yargs
.positional('id', {
describe: 'Order ID to pay',
})
.require('id', true);
},
async (argv) => {
const conf = config();
const {
id,
} = argv;

const pt = Pt.create();

try {
debug(`Authorize with username:${conf.get('username')} and password:${conf.get('password') ? 'YES' : 'N/A'}`);

await pt.authenticate(conf.get('username'), conf.get('password'));
} catch (err) {
error(
err,
'Configure your pt.md client using "pt.md configure" command.',
);
}

try {
const paymentPageFile = tempfile('.html');

debug(`Fetching payment HTML for order "${id}" to "${paymentPageFile}"`);

await fs.outputFile(paymentPageFile, await pt.paymentPage(id)); // eslint-disable-line

await opn(`file://${paymentPageFile}`, { wait: false });

setTimeout(async () => {
debug(`Removing temporary file: "${paymentPageFile}"`);

await fs.remove(paymentPageFile);
}, 3000); // @TODO figure out a smarter way
} catch (err) {
error(err, `It might be that the order "${id}" is missing.`);
}
},
)
.command({ command: '*', handler() { yargs.showHelp(); } })
.help('h')
.alias('h', 'help')
Expand Down
82 changes: 59 additions & 23 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@
"debug": "^4.1.0",
"deepmerge": "^2.2.1",
"enquirer": "^2.3.0",
"fs-extra": "^7.0.1",
"open": "0.0.5",
"opn": "^5.4.0",
"qs": "^6.6.0",
"terminal-link": "^1.2.0",
"tempfile": "^2.0.0",
"terminal-table": "0.0.12",
"yargs": "^12.0.5"
}
Expand Down
16 changes: 2 additions & 14 deletions src/entity/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,13 @@ class Item extends Base {
return this.invoicePaymentStateID === 12990;
}

/**
* Get payment URI
* @todo Decide to return payment URL anyway
*/
get paymentURI() {
if (this.isPaid) {
return null;
}

return `https://pt.md/BeneficiarZone/TransactionJournal/CardPayOrder?id=${this.orderID}`;
}

/**
* @inheritdoc
*/
toJSON() {
const { isPaid, paymentURI } = this;
const { isPaid } = this;

return Object.assign({ isPaid, paymentURI }, super.toJSON());
return Object.assign({ isPaid }, super.toJSON());
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ class Pt {
return response.data;
}

/**
* Get payment page HTML
* @param {string} orderID
*/
async paymentPage(orderID) {
const paymentPage = await this.client.request('paymentPage');

const response = await paymentPage.send({ id: orderID });

return response.data;
}

/**
* Create an instance of Pt
*/
Expand Down
8 changes: 8 additions & 0 deletions src/transport/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,12 @@ module.exports = {
return response;
},
}),
paymentPage: endpoint({
method: 'get',
url: '/BeneficiarZone/TransactionJournal/CardPayOrder',
withCredentials: true,
params: {
id: null,
},
}),
};

0 comments on commit 903b313

Please sign in to comment.