This repository was archived by the owner on Jun 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathIpn.php
More file actions
139 lines (115 loc) · 5.7 KB
/
Copy pathIpn.php
File metadata and controls
139 lines (115 loc) · 5.7 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
* Provides bugfixes and enhancements to Paypal IPN handling.
*
* @author Luke Mills <luke@aligent.com.au>
* @author Jim O'Halloran <jim@aligent.com.au>
*/
class Aligent_Paypal_Model_Ipn extends Mage_Paypal_Model_Ipn
{
const CONFIG_IPN_REFUND_METHOD = 'payment/modpaypal/ipn_refund_method';
/**
* Get ipn data, send verification to PayPal, run corresponding handler. Override
* to allow handling of mc_cancel transactions which don't have an order increment
* id attached.
*
* @param array $request
* @param Zend_Http_Client_Adapter_Interface $httpAdapter
* @throws Exception
*/
public function processIpnRequest(array $request, Zend_Http_Client_Adapter_Interface $httpAdapter = null)
{
$this->_request = $request;
$this->_debugData = array('ipn' => $request);
ksort($this->_debugData['ipn']);
if (isset($this->_request['reason_code']) && 'refund' == $this->_request['reason_code']) {
$this->_registerMpCancel();
} else {
parent::processIpnRequest($request, $httpAdapter);
}
}
/**
* Process a cancellation. Standard Magento fails at this because there's
* no order number in the IPN.
*/
protected function _registerMpCancel()
{
$reason = $this->getRequestData('reason_code');
Mage::log('IPN Cancellation received. Reason Code: '.$reason.' Payer Email: ' . $this->getRequestData('payer_email') . ' First Name: ' . $this->getRequestData('payer_first_name') . ' Last Name: ' . $this->getRequestData('last_name'));
$vCommentText = 'Payer Email: ' . $this->getRequestData('payer_email') . ' First Name: ' . $this->getRequestData('payer_first_name');
$vCommentText .= ' Last Name: ' . $this->getRequestData('last_name');
$vCommentText .= ' Reason Code: ' . $reason;
$oNotification = Mage::getModel('adminnotification/inbox');
$oNotification->setSeverity(Mage_AdminNotification_Model_Inbox::SEVERITY_MINOR);
$oNotification->setDateAdded(date("c", time()));
$oNotification->setTitle('Paypal Cancellation IPN Received');
$oNotification->setDescription($vCommentText);
$oNotification->save();
}
/**
* Process a refund or a chargeback
*/
protected function _registerPaymentRefund()
{
if (Mage::getStoreConfig(self::CONFIG_IPN_REFUND_METHOD) == Aligent_Paypal_Model_System_Config_Source_Refundmethod::METHOD_DEFAULT) {
return parent::_registerPaymentRefund();
} else {
$this->_importPaymentInformation();
$reason = $this->getRequestData('reason_code');
$isRefundFinal = !$this->_info->isReversalDisputable($reason);
$amount = -1 * $this->getRequestData('mc_gross');
Mage::log('IPN Refund received. Reason Code: ' . $reason . ' isRefundFinal: ' . $isRefundFinal . ' Amount: ' . $amount);
$vCommentText = Mage::helper('paypal')->__('Refunded amount of %s. Transaction ID: "%s".', $this->_order->getBaseCurrency()->formatTxt($amount), $this->getRequestData('txn_id'));
$vCommentText .= ' Reason: ' . $this->_info->explainReasonCode($reason);
$this->_createIpnComment($vCommentText, true);
$this->_order->save();
$vCommentText .= " for order #" . $this->_order->getIncrementId();
$oNotification = Mage::getModel('adminnotification/inbox');
$oNotification->setSeverity(Mage_AdminNotification_Model_Inbox::SEVERITY_MINOR);
$oNotification->setDateAdded(date("c", time()));
$oNotification->setTitle('Paypal Refund IPN Received');
$oNotification->setDescription($vCommentText);
$oNotification->save();
}
}
/**
* Post back to PayPal to check whether this request is a valid one
*
* @param Zend_Http_Client_Adapter_Interface $httpAdapter
* @throws Exception
*/
protected function _postBack(Zend_Http_Client_Adapter_Interface $httpAdapter)
{
$sReq = '';
foreach ($this->_request as $k => $v) {
$sReq .= '&' . $k . '=' . urlencode(stripslashes($v));
}
$sReq .= "&cmd=_notify-validate";
$sReq = substr($sReq, 1);
$this->_debugData['postback'] = $sReq;
$this->_debugData['postback_to'] = $this->_config->getPaypalUrl();
$httpAdapter->write(Zend_Http_Client::POST, $this->_config->getPaypalUrl(), '1.1', array(), $sReq);
try {
$response = $httpAdapter->read();
} catch (Exception $e) {
$this->_debugData['http_error'] = array('error' => $e->getMessage(), 'code' => $e->getCode());
throw $e;
}
$this->_debugData['postback_result'] = $response;
// =====================================================================
// Changed from default code. Paypal now regularly returns a 100
// response with an empty body followed by a 200 response with the
// VERIFIED/INVALID message. The code below will check the last
// response for the VERIFIED/INVALID code rather than the first.
//
// ref: http://www.dhmedia.com.au/blog/debugging-paypal-ipn-postback-failure-magent
// Magento 2 Pull Request: https://github.com/magento/magento2/pull/136
$response = preg_split('/^\r?$/m', $response);
$response = trim(end($response));
// =====================================================================
if ($response != 'VERIFIED') {
throw new Exception('PayPal IPN postback failure. See ' . self::DEFAULT_LOG_FILE . ' for details.');
}
unset($this->_debugData['postback'], $this->_debugData['postback_result']);
}
}