-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipn.ctrl.js
82 lines (72 loc) · 2.89 KB
/
ipn.ctrl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const Const_PaypalWebAccept=1;
const Const_PaypalSubscriptionPayment=2;
const Const_PaypalSubscriptionSignUp=3;
const Const_PaypalSubscriptionCancel=4;
const Const_PaypalSubscriptionEot=5;
const Const_PaypalErrorValidatingIPN=10;
const Const_PaypalProcessError=11;
const Const_PaypalRecurringPaymentSuspended=12;
const Const_PaypalrecurringPaymentSuspendedDueToMaxFailedPayment=13;
const Const_PaypalUnhandledTransactionType=14;
class IPNController {
static async runIt(req, res,callbackFunction) {
// Send 200 status back to PayPal
res.status(200).send('OK');
res.end();
const body = req.body || {};
// Validate IPN message with PayPal
try {
const isValidated = await PayPalService.validate(body);
if (!isValidated) {
console.error('Error validating IPN message.');
return callbackFunction(Const_PaypalErrorValidatingIPN);
}
// IPN Message is validated!
const transactionType = body.txn_type;
switch (transactionType) {
case 'web_accept':{
const status = body.payment_status;
const amount = body.mc_gross;
// Validate that the status is completed,
// and the amount match your expectaions.
return callbackFunction(Const_PaypalWebAccept);
break;
}
case 'subscr_payment':{
const status = body.payment_status;
const amount = body.mc_gross;
// Validate that the status is completed,
// and the amount match your expectaions.
return callbackFunction(Const_PaypalSubscriptionPayment);
break;
}
case 'subscr_signup':
return callbackFunction(Const_PaypalSubscriptionSignUp);
break;
case 'subscr_cancel':
return callbackFunction(Const_PaypalSubscriptionCancel);
break;
case 'subscr_eot':
// Update user profile
return callbackFunction(Const_PaypalSubscriptionEot);
break;
case 'recurring_payment_suspended':
return callbackFunction(Const_PaypalRecurringPaymentSuspended);
break;
case 'recurring_payment_suspended_due_to_max_failed_payment':
// Contact the user for more details
return callbackFunction(Const_PaypalrecurringPaymentSuspendedDueToMaxFailedPayment);
break;
default:
console.log('Unhandled transaction type: ', transactionType);
return callbackFunction(Const_PaypalUnhandledTransactionType);
break;
}
} catch(e) {
console.error(e);
return callbackFunction(Const_PaypalProcessError);
}
}
}
module.exports=IPNController;
//export default IPNController;