Permalink
Browse files

Added a nodeunit test and fixed lib

  • Loading branch information...
1 parent f6c9771 commit 41e7932c21bd26957b677fd4d2c1fa4a6c8e5811 @andzdroid committed May 30, 2011
Showing with 23 additions and 7 deletions.
  1. +7 −7 lib/paypal-ipn.js
  2. +16 −0 tests/ipn.js
View
@@ -11,23 +11,23 @@ exports.verify = function verify(params, callback) {
//Set up the request to paypal
var req_options = {
host: (params.test_ipn) ? SANDBOX_URL : REGULAR_URL,
- method: 'POST',
+ method: 'GET',
path: '/cgi-bin/webscr?' + qs.stringify(params)
}
+
var req = https.request(req_options, function paypal_request(res) {
res.on('data', function paypal_response(d) {
+ var response = d.toString();
+
//Check if IPN is valid
- if (d == 'VERIFIED') {
- callback(false);
- } else {
- callback(true);
- }
+ callback(response != 'VERIFIED', response);
});
});
+ req.end();
//Request error
req.on('error', function request_error(e) {
- callback(e);
+ callback(true, e);
});
};
View
@@ -0,0 +1,16 @@
+var ipn = require('../index');
+var qs = require('querystring');
+
+module.exports['Fake IPN request'] = function(test) {
+ var message = 'mc_gross=19.95&protection_eligibility=Eligible&address_status=confirmed&payer_id=LPLWNMTBWMFAY&tax=0.00&address_street=1+Main+St&payment_date=20%3A12%3A59+Jan+13%2C+2009+PST&payment_status=Completed&charset=windows-1252&address_zip=95131&first_name=Test&mc_fee=0.88&address_country_code=US&address_name=Test+User&notify_version=2.6&custom=&payer_status=verified&address_country=United+States&address_city=San+Jose&quantity=1&verify_sign=AtkOfCXbDm2hu0ZELryHFjY-Vb7PAUvS6nMXgysbElEn9v-1XcmSoGtf&payer_email=gpmac_1231902590_per%40paypal.com&txn_id=61E67681CH3238416&payment_type=instant&last_name=User&address_state=CA&receiver_email=gpmac_1231902686_biz%40paypal.com&payment_fee=0.88&receiver_id=S8XGHLYDW9T3S&txn_type=express_checkout&item_name=&mc_currency=USD&item_number=&residence_country=US&test_ipn=1&handling_amount=0.00&transaction_subject=&payment_gross=19.95&shipping=0.00';
+
+ var params = qs.parse(message);
+
+ ipn.verify(params, function callback(err, msg) {
+ //Test for error
+ test.ok(err, msg);
+ console.log('Response from Paypal: %s', msg);
+
+ test.done();
+ });
+};

0 comments on commit 41e7932

Please sign in to comment.