|
| 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 | + '&': '&', |
| 103 | + '<': '<', |
| 104 | + '>': '>', |
| 105 | + '"': '"' |
| 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 | + |
0 commit comments