Skip to content

Commit a4f7063

Browse files
committed
payments: Ya.Kassa
1 parent e44f253 commit a4f7063

18 files changed

Lines changed: 270 additions & 40 deletions

File tree

handlers/donate/controller/newOrder.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
const payments = require('payments');
2+
const getPaymentMethods = require('../lib/getPaymentMethods');
3+
24
var OrderTemplate = payments.OrderTemplate;
35

46
exports.get = function*() {
@@ -19,7 +21,7 @@ exports.get = function*() {
1921
this.locals.sitetoolbar = true;
2022
this.locals.title = orderTemplate.title;
2123

22-
this.locals.paymentMethods = require('../lib/paymentMethods');
24+
this.locals.paymentMethods = yield* getPaymentMethods.call(this);
2325

2426
this.body = this.render('newOrder');
2527
};

handlers/donate/controller/orders.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ var OrderTemplate = payments.OrderTemplate;
44
var Transaction = payments.Transaction;
55
var assert = require('assert');
66
const getOrderInfo = payments.getOrderInfo;
7+
const getPaymentMethods = require('../lib/getPaymentMethods');
8+
79

810
// Existing order page
911
exports.get = function*() {
@@ -21,7 +23,7 @@ exports.get = function*() {
2123

2224
this.locals.user = this.req.user;
2325

24-
this.locals.paymentMethods = require('../lib/paymentMethods');
26+
this.locals.paymentMethods = yield* getPaymentMethods.call(this);
2527

2628
this.locals.paymentMethodShowDefaultCurrency = true;
2729

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const payments = require('payments');
2+
3+
var methodsEnabled = ['webmoney', 'yandexmoney', 'paypal', 'payanyway', 'interkassa', 'banksimple', 'banksimpleua'];
4+
5+
module.exports = function*() {
6+
7+
var paymentMethods = {};
8+
9+
methodsEnabled.forEach(function(key) {
10+
paymentMethods[key] = payments.methods[key].info;
11+
});
12+
13+
if (this.user && this.user.isAdmin) {
14+
paymentMethods.yakassa = payments.methods.yakassa.info;
15+
}
16+
17+
return paymentMethods;
18+
};

handlers/donate/lib/paymentMethods.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

handlers/payments/interkassa/templates/form.jade

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ form(method="POST" name="payment" action="https://sci.interkassa.com/" accept-ch
44
input(type="hidden",name="ik_pm_no",value=number)
55
input(type="hidden",name="ik_cur",value=currency)
66
input(type="hidden",name="ik_am",value=amount)
7-
input(type="hidden",name="ik_desc",value="Оплата по счёту #{number}")
7+
input(type="hidden",name="ik_desc",value="Оплата по счёту " + number)
88
input(type="submit",value="Оплатить")

handlers/payments/payanyway/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ exports.createTransaction = function*(order) {
1010

1111
var transaction = new Transaction({
1212
order: order._id,
13-
currency: 'RUB',
14-
amount: order.convertAmount('RUB'),
13+
currency: 'RUB',
14+
amount: order.convertAmount('RUB'),
1515
status: Transaction.STATUS_PENDING,
1616
paymentMethod: path.basename(__dirname)
1717
});

handlers/payments/webmoney/test/.jshintrc

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
'use strict';
2+
3+
const yakassaConfig = require('config').payments.modules.yakassa;
4+
const mongoose = require('mongoose');
5+
const Order = require('../../models/order');
6+
const Transaction = require('../../models/transaction');
7+
const md5 = require('MD5');
8+
9+
// ONLY ACCESSED from YAKASSA SERVER
10+
exports.check = function* (next) {
11+
12+
yield* this.loadTransaction('orderNumber', {skipOwnerCheck: true});
13+
14+
if (!checkSignature(this.request.body)) {
15+
this.log.debug("wrong signature");
16+
this.body = respond('checkOrderResponse', {
17+
code: 1,
18+
invoiceId: this.request.body.invoiceId
19+
});
20+
return;
21+
}
22+
23+
yield this.transaction.logRequest('check', this.request);
24+
25+
this.log.debug("check");
26+
27+
28+
if (this.transaction.status != Transaction.STATUS_PENDING ||
29+
this.transaction.amount != this.request.body.orderSumAmount ||
30+
this.request.body.shopId != yakassaConfig.shopId
31+
) {
32+
this.log.debug("no pending transaction " + this.request.body.orderNumber);
33+
34+
this.body = respond('checkOrderResponse', {
35+
code: 100,
36+
invoiceId: this.request.body.invoiceId,
37+
message: 'pending transaction with given params not found'
38+
});
39+
return;
40+
}
41+
42+
this.body = respond('checkOrderResponse', {
43+
code: 0,
44+
invoiceId: this.request.body.invoiceId
45+
});
46+
47+
};
48+
49+
50+
exports.paymentAviso = function* (next) {
51+
52+
yield* this.loadTransaction('orderNumber', {skipOwnerCheck: true});
53+
54+
if (!checkSignature(this.request.body)) {
55+
this.log.debug("wrong signature");
56+
this.body = respond('paymentAvisoResponse', {
57+
code: 1,
58+
invoiceId: this.request.body.invoiceId
59+
});
60+
return;
61+
}
62+
63+
// if should not be needed, if responce is corect
64+
if (this.transaction.status != Transaction.STATUS_SUCCESS) {
65+
this.log.debug("will call order onPaid module=" + this.order.module);
66+
yield* this.order.onPaid(this.transaction);
67+
}
68+
69+
this.body = respond('paymentAvisoResponse', {
70+
code: 0,
71+
invoiceId: this.request.body.invoiceId
72+
});
73+
74+
};
75+
76+
function checkSignature(body) {
77+
78+
var signature = [
79+
body.action,
80+
body.orderSumAmount,
81+
body.orderSumCurrencyPaycash,
82+
body.orderSumBankPaycash,
83+
body.shopId,
84+
body.invoiceId,
85+
body.customerNumber,
86+
yakassaConfig.secret
87+
].join(';');
88+
89+
//console.log("MD5 pre", signature);
90+
91+
signature = md5(signature).toUpperCase();
92+
93+
//console.log("MD5", signature, body.md5);
94+
95+
return signature == body.md5;
96+
}
97+
98+
99+
function escapeHtml(str) {
100+
var HTML_ESCAPE_REPLACE_RE = /[&<>"]/g;
101+
var HTML_REPLACEMENTS = {
102+
'&': '&amp;',
103+
'<': '&lt;',
104+
'>': '&gt;',
105+
'"': '&quot;'
106+
};
107+
108+
return String(str).replace(HTML_ESCAPE_REPLACE_RE, function(ch) {
109+
return HTML_REPLACEMENTS[ch];
110+
});
111+
}
112+
113+
function renderAttrs(attrs) {
114+
var result;
115+
116+
result = '';
117+
118+
for (var key in attrs) {
119+
result += ' ' + escapeHtml(key) + '="' + escapeHtml(attrs[key]) + '"';
120+
}
121+
122+
return result;
123+
}
124+
125+
function respond(type, attrs) {
126+
attrs = Object.assign({}, attrs, {
127+
performedDatetime: new Date().toJSON(),
128+
shopId: yakassaConfig.shopId
129+
});
130+
131+
console.log("Yakassa RESPOND", type, attrs);
132+
return `<?xml version="1.0" encoding="UTF-8"?><${type} ` + renderAttrs(attrs) + '/>';
133+
}
134+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const mongoose = require('mongoose');
2+
const Transaction = require('../../models/transaction');
3+
4+
exports.all = function* (next) {
5+
6+
yield* this.loadTransaction('orderNumber');
7+
8+
yield this.transaction.persist({
9+
status: Transaction.STATUS_FAIL,
10+
statusMessage: 'оплата не прошла'
11+
});
12+
13+
this.redirectToOrder();
14+
15+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const config = require('config');
2+
const mongoose = require('mongoose');
3+
4+
exports.all = function* (next) {
5+
yield* this.loadTransaction('orderNumber');
6+
7+
this.redirectToOrder();
8+
};

0 commit comments

Comments
 (0)