-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPaypalIPNHandler.php
126 lines (103 loc) · 5.06 KB
/
PaypalIPNHandler.php
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
include_once( './PaypalIPNHandlerModel.php' );
include_once( './PaypalIPN.php' );
class PaypalIPNHandler{
function __construct() {
$this->PaypalIPNHanlderModel = new PaypalIPNHandlerModel();
}
public function updateSubscription( $paymentData ){
$paymentMail = $paymentData['payer_email'];
$transactionId = isset( $paymentData['txn_id'] ) ? $paymentData['txn_id'] : '';
$firstName = $paymentData[ 'first_name' ];
$lastName = $paymentData[ 'last_name' ];
$userCountry = $paymentData[ 'address_country_code' ];
$userCiy = $paymentData['address_city'];
$transactionType = isset( $paymentData['txn_type'] ) ? $paymentData['txn_type'] : '';
$transactionDate = isset( $paymentData['payment_date'] ) ? $paymentData['payment_date'] : '';
$subscriptionType = $paymentData['option_selection1'];
$ipnTrackId = $paymentData['ipn_track_id'];
$itemName = $paymentData['item_name'];
// check if there is an user with the same email, as the one, used for the payment
if( count( $this->PaypalIPNHanlderModel->getUserByEmail( $paymentMail ) ) > 0 ){
$this->PaypalIPNHanlderModel->addSubscriptionUpdate(
$paymentMail,
$transactionId,
$firstName,
$lastName,
$userCountry,
$userCiy,
$transactionType,
$transactionDate,
$subscriptionType,
$ipnTrackId,
$itemName
);
$this->PaypalIPNHanlderModel->removeCancelations( $paymentMail );
return true;
} else {
return false;
}
}
private function sendCancelationEmail($userEmail, $firstName, $lastName, $notificationData ){
}
private function cancelSubscription( $paymentData ){
$paymentMail = $paymentData['payer_email'];
$transactionId = isset( $paymentData['txn_id'] ) ? $paymentData['txn_id'] : '';
$this->PaypalIPNHanlderModel->addCancelation(
$paymentMail,
$transactionId
);
}
public function isSubscriptionActive( $userEmail ){
return $this->PaypalIPNHanlderModel->isSubscriptionActive($userEmail);
}
public function sendPaymentProblemEmail( $userEmail, $firstName, $lastName, $notificationData ){
}
private function sendThankYouEmail( $userEmail, $firstName, $lastName ){
}
private function sendSubscriptionProblemEmail( $userEmail, $firstName, $lastName ){
}
public function receiveIPN(){
$ipn = new PaypalIPN();
$verified = $ipn->verifyIPN();
$data_text = "";
foreach ($_POST as $key => $value) {
$data_text .= $key . " = " . $value . "\r\n";
}
$ipnType = $_POST['txn_type'];
$timeStamp = time();
$humanizedTimestamp = date( "Y-m-d:m-s", $timeStamp );
switch( $ipnType ){
case 'subscr_payment':
case 'recurring_payment':
$subscriptionOK = $this->updateSubscription( $_POST );
if( $subscriptionOK ){
$this->sendThankYouEmail( $_POST['payer_email'], $_POST['first_name'], $_POST['last_name'] );
} else {
$this->sendSubscriptionProblemEmail( $_POST['payer_email'], $_POST['first_name'], $_POST['last_name'] );
}
break;
case 'subscr_cancel':
case 'recurring_payment_profile_cancel':
$this->sendCancelationEmail( $_POST['payer_email'], $_POST['first_name'], $_POST['last_name'], $data_text );
$this->cancelSubscription( $_POST );
break;
case 'subscr_failed':
case 'recurring_payment_expired':
case 'recurring_payment_failed':
case 'recurring_payment_skipped':
case 'recurring_payment_suspended':
case 'recurring_payment_suspended_due_to_max_failed_payment':
$this->sendPaymentProblemEmail( $_POST['payer_email'], $_POST['first_name'], $_POST['last_name'], $data_text );
$this->cancelSubscription( $_POST );
break;
case 'subscr_signup':
break;
default:
// you can write in a log everything that hasn't been catched by the switch
}
// Reply with an empty 200 response to indicate to paypal the IPN was received correctly
header('HTTP/1.0 200 OK');
}
}
?>