|
| 1 | +const config = require('config') |
| 2 | +const paypalConfig = config.payments.modules.paypal; |
| 3 | +const Order = require('../../models/order'); |
| 4 | +const Transaction = require('../../models/transaction'); |
| 5 | +const TransactionLog = require('../../models/transactionLog'); |
| 6 | +const log = require('js-log')(); |
| 7 | +const request = require('koa-request'); |
| 8 | + |
| 9 | +// docs: |
| 10 | +// |
| 11 | +// https://developer.paypal.com/webapps/developer/docs/classic/ipn/integration-guide/IPNIntro/ |
| 12 | + |
| 13 | +log.debugOn(); |
| 14 | + |
| 15 | +/* jshint -W106 */ |
| 16 | +exports.post = function* (next) { |
| 17 | + |
| 18 | + yield* this.loadTransaction('invoice', {skipOwnerCheck: true}); |
| 19 | + |
| 20 | + yield this.transaction.logRequest('ipn-unverified', this.request); |
| 21 | + |
| 22 | + var qs = { |
| 23 | + 'cmd': '_notify-validate' |
| 24 | + }; |
| 25 | + |
| 26 | + for (var field in this.request.body) { |
| 27 | + qs[field] = this.request.body[field]; |
| 28 | + } |
| 29 | + |
| 30 | + // request oauth token |
| 31 | + var options = { |
| 32 | + method: 'GET', |
| 33 | + qs: qs, |
| 34 | + url: 'https://www.paypal.com/cgi-bin/webscr' |
| 35 | + }; |
| 36 | + |
| 37 | + |
| 38 | + yield this.transaction.log('request ipn verify', options); |
| 39 | + |
| 40 | + var response; |
| 41 | + try { |
| 42 | + response = yield request(options); |
| 43 | + } catch(e) { |
| 44 | + yield this.transaction.log('request ipn verify failed', e.message); |
| 45 | + this.throw(403, "Couldn't verify ipn"); |
| 46 | + } |
| 47 | + |
| 48 | + if (response.body != "VERIFIED") { |
| 49 | + this.throw(403, "Invalid IPN"); |
| 50 | + } |
| 51 | + |
| 52 | + // ipn is verified now! But we check if it's data matches the transaction (as recommended in docs) |
| 53 | + if (this.transaction.amount != parseFloat(this.request.body.mc_gross) || |
| 54 | + this.request.body.receiver_email != paypalConfig.email || |
| 55 | + this.request.body.mc_currency != config.payments.currency) { |
| 56 | + |
| 57 | + yield this.transaction.persist({ |
| 58 | + status: Transaction.STATUS_FAIL, |
| 59 | + statusMessage: "данные транзакции не совпадают с базой, свяжитесь с поддержкой" |
| 60 | + }); |
| 61 | + this.throw(404, "transaction data doesn't match the POST body"); |
| 62 | + } |
| 63 | + |
| 64 | + // match agains latest ipn in logs as recommended: |
| 65 | + // if there was an IPN about the same transaction, and it's state is the same |
| 66 | + // => then the current one is a duplicate |
| 67 | + |
| 68 | + var previousIpn = yield TransactionLog.findOne({ |
| 69 | + event: "ipn", |
| 70 | + transaction: this.transaction._id |
| 71 | + }).sort({created: -1}).exec(); |
| 72 | + |
| 73 | + if (previousIpn && previousIpn.data.payment_status == this.request.body.payment_status) { |
| 74 | + yield this.transaction.logRequest("ipn duplicate", this.request); |
| 75 | + // ignore duplicate |
| 76 | + this.body = ''; |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + // now we have a valid non-duplicate IPN, let's update the transaction |
| 81 | + |
| 82 | + |
| 83 | + this.body = ''; |
| 84 | +}; |
0 commit comments