@@ -12,473 +12,473 @@ |
||
12 | 12 | */ |
13 | 13 | class GetPaid_Paypal_Gateway_IPN_Handler { |
14 | 14 | |
15 | - /** |
|
16 | - * Payment method id. |
|
17 | - * |
|
18 | - * @var string |
|
19 | - */ |
|
20 | - protected $id = 'paypal'; |
|
21 | - |
|
22 | - /** |
|
23 | - * Payment method object. |
|
24 | - * |
|
25 | - * @var GetPaid_Paypal_Gateway |
|
26 | - */ |
|
27 | - protected $gateway; |
|
28 | - |
|
29 | - /** |
|
30 | - * Class constructor. |
|
31 | - * |
|
32 | - * @param GetPaid_Paypal_Gateway $gateway |
|
33 | - */ |
|
34 | - public function __construct( $gateway ) { |
|
35 | - $this->gateway = $gateway; |
|
36 | - $this->verify_ipn(); |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * Processes ipns and marks payments as complete. |
|
41 | - * |
|
42 | - * @return void |
|
43 | - */ |
|
44 | - public function verify_ipn() { |
|
45 | - |
|
46 | - wpinv_error_log( 'GetPaid PayPal IPN Handler', false ); |
|
47 | - |
|
48 | - // Validate the IPN. |
|
49 | - if ( empty( $_POST ) || ! $this->validate_ipn() ) { |
|
50 | - wp_die( 'PayPal IPN Request Failure', 500 ); |
|
51 | - } |
|
52 | - |
|
53 | - // Process the IPN. |
|
54 | - $posted = wp_unslash( $_POST ); |
|
55 | - $invoice = $this->get_ipn_invoice( $posted ); |
|
56 | - |
|
57 | - // Abort if it was not paid by our gateway. |
|
58 | - if ( $this->id != $invoice->get_gateway() ) { |
|
59 | - wpinv_error_log( 'Aborting, Invoice was not paid via PayPal', false ); |
|
60 | - wp_die( 'Invoice not paid via PayPal', 200 ); |
|
61 | - } |
|
62 | - |
|
63 | - $posted['payment_status'] = isset( $posted['payment_status'] ) ? sanitize_key( strtolower( $posted['payment_status'] ) ) : ''; |
|
64 | - $posted['txn_type'] = sanitize_key( strtolower( $posted['txn_type'] ) ); |
|
65 | - |
|
66 | - wpinv_error_log( 'Payment status:' . $posted['payment_status'], false ); |
|
67 | - wpinv_error_log( 'IPN Type:' . $posted['txn_type'], false ); |
|
68 | - |
|
69 | - if ( method_exists( $this, 'ipn_txn_' . $posted['txn_type'] ) ) { |
|
70 | - call_user_func( array( $this, 'ipn_txn_' . $posted['txn_type'] ), $invoice, $posted ); |
|
71 | - wpinv_error_log( 'Done processing IPN', false ); |
|
72 | - wp_die( 'Processed', 200 ); |
|
73 | - } |
|
74 | - |
|
75 | - wpinv_error_log( 'Aborting, Unsupported IPN type:' . $posted['txn_type'], false ); |
|
76 | - wp_die( 'Unsupported IPN type', 200 ); |
|
77 | - |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * Retrieves IPN Invoice. |
|
82 | - * |
|
83 | - * @param array $posted |
|
84 | - * @return WPInv_Invoice |
|
85 | - */ |
|
86 | - protected function get_ipn_invoice( $posted ) { |
|
87 | - |
|
88 | - wpinv_error_log( 'Retrieving PayPal IPN Response Invoice', false ); |
|
89 | - |
|
90 | - if ( ! empty( $posted['custom'] ) ) { |
|
91 | - $invoice = new WPInv_Invoice( $posted['custom'] ); |
|
92 | - |
|
93 | - if ( $invoice->exists() ) { |
|
94 | - wpinv_error_log( 'Found invoice #' . $invoice->get_number(), false ); |
|
95 | - return $invoice; |
|
96 | - } |
|
97 | - } |
|
98 | - |
|
99 | - wpinv_error_log( 'Could not retrieve the associated invoice.', false ); |
|
100 | - wp_die( 'Could not retrieve the associated invoice.', 200 ); |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Check PayPal IPN validity. |
|
105 | - */ |
|
106 | - protected function validate_ipn() { |
|
107 | - |
|
108 | - wpinv_error_log( 'Validating PayPal IPN response', false ); |
|
109 | - |
|
110 | - // Retrieve the associated invoice. |
|
111 | - $posted = wp_unslash( $_POST ); |
|
112 | - $invoice = $this->get_ipn_invoice( $posted ); |
|
113 | - |
|
114 | - if ( $this->gateway->is_sandbox( $invoice ) ) { |
|
115 | - wpinv_error_log( $posted, 'Invoice was processed in sandbox hence logging the posted data', false ); |
|
116 | - } |
|
117 | - |
|
118 | - // Validate the IPN. |
|
119 | - $posted['cmd'] = '_notify-validate'; |
|
120 | - |
|
121 | - // Send back post vars to paypal. |
|
122 | - $params = array( |
|
123 | - 'body' => $posted, |
|
124 | - 'timeout' => 60, |
|
125 | - 'httpversion' => '1.1', |
|
126 | - 'compress' => false, |
|
127 | - 'decompress' => false, |
|
128 | - 'user-agent' => 'GetPaid/' . WPINV_VERSION, |
|
129 | - ); |
|
130 | - |
|
131 | - // Post back to get a response. |
|
132 | - $response = wp_safe_remote_post( $this->gateway->is_sandbox( $invoice ) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr', $params ); |
|
133 | - |
|
134 | - // Check to see if the request was valid. |
|
135 | - if ( ! is_wp_error( $response ) && $response['response']['code'] < 300 && strstr( $response['body'], 'VERIFIED' ) ) { |
|
136 | - wpinv_error_log( 'Received valid response from PayPal IPN: ' . $response['body'], false ); |
|
137 | - return true; |
|
138 | - } |
|
139 | - |
|
140 | - if ( is_wp_error( $response ) ) { |
|
141 | - wpinv_error_log( $response->get_error_message(), 'Received invalid response from PayPal IPN' ); |
|
142 | - return false; |
|
143 | - } |
|
144 | - |
|
145 | - wpinv_error_log( $response['body'], 'Received invalid response from PayPal IPN' ); |
|
146 | - return false; |
|
147 | - |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Check currency from IPN matches the invoice. |
|
152 | - * |
|
153 | - * @param WPInv_Invoice $invoice Invoice object. |
|
154 | - * @param string $currency currency to validate. |
|
155 | - */ |
|
156 | - protected function validate_ipn_currency( $invoice, $currency ) { |
|
15 | + /** |
|
16 | + * Payment method id. |
|
17 | + * |
|
18 | + * @var string |
|
19 | + */ |
|
20 | + protected $id = 'paypal'; |
|
21 | + |
|
22 | + /** |
|
23 | + * Payment method object. |
|
24 | + * |
|
25 | + * @var GetPaid_Paypal_Gateway |
|
26 | + */ |
|
27 | + protected $gateway; |
|
28 | + |
|
29 | + /** |
|
30 | + * Class constructor. |
|
31 | + * |
|
32 | + * @param GetPaid_Paypal_Gateway $gateway |
|
33 | + */ |
|
34 | + public function __construct( $gateway ) { |
|
35 | + $this->gateway = $gateway; |
|
36 | + $this->verify_ipn(); |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * Processes ipns and marks payments as complete. |
|
41 | + * |
|
42 | + * @return void |
|
43 | + */ |
|
44 | + public function verify_ipn() { |
|
45 | + |
|
46 | + wpinv_error_log( 'GetPaid PayPal IPN Handler', false ); |
|
47 | + |
|
48 | + // Validate the IPN. |
|
49 | + if ( empty( $_POST ) || ! $this->validate_ipn() ) { |
|
50 | + wp_die( 'PayPal IPN Request Failure', 500 ); |
|
51 | + } |
|
52 | + |
|
53 | + // Process the IPN. |
|
54 | + $posted = wp_unslash( $_POST ); |
|
55 | + $invoice = $this->get_ipn_invoice( $posted ); |
|
56 | + |
|
57 | + // Abort if it was not paid by our gateway. |
|
58 | + if ( $this->id != $invoice->get_gateway() ) { |
|
59 | + wpinv_error_log( 'Aborting, Invoice was not paid via PayPal', false ); |
|
60 | + wp_die( 'Invoice not paid via PayPal', 200 ); |
|
61 | + } |
|
62 | + |
|
63 | + $posted['payment_status'] = isset( $posted['payment_status'] ) ? sanitize_key( strtolower( $posted['payment_status'] ) ) : ''; |
|
64 | + $posted['txn_type'] = sanitize_key( strtolower( $posted['txn_type'] ) ); |
|
65 | + |
|
66 | + wpinv_error_log( 'Payment status:' . $posted['payment_status'], false ); |
|
67 | + wpinv_error_log( 'IPN Type:' . $posted['txn_type'], false ); |
|
68 | + |
|
69 | + if ( method_exists( $this, 'ipn_txn_' . $posted['txn_type'] ) ) { |
|
70 | + call_user_func( array( $this, 'ipn_txn_' . $posted['txn_type'] ), $invoice, $posted ); |
|
71 | + wpinv_error_log( 'Done processing IPN', false ); |
|
72 | + wp_die( 'Processed', 200 ); |
|
73 | + } |
|
74 | + |
|
75 | + wpinv_error_log( 'Aborting, Unsupported IPN type:' . $posted['txn_type'], false ); |
|
76 | + wp_die( 'Unsupported IPN type', 200 ); |
|
77 | + |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * Retrieves IPN Invoice. |
|
82 | + * |
|
83 | + * @param array $posted |
|
84 | + * @return WPInv_Invoice |
|
85 | + */ |
|
86 | + protected function get_ipn_invoice( $posted ) { |
|
87 | + |
|
88 | + wpinv_error_log( 'Retrieving PayPal IPN Response Invoice', false ); |
|
89 | + |
|
90 | + if ( ! empty( $posted['custom'] ) ) { |
|
91 | + $invoice = new WPInv_Invoice( $posted['custom'] ); |
|
92 | + |
|
93 | + if ( $invoice->exists() ) { |
|
94 | + wpinv_error_log( 'Found invoice #' . $invoice->get_number(), false ); |
|
95 | + return $invoice; |
|
96 | + } |
|
97 | + } |
|
98 | + |
|
99 | + wpinv_error_log( 'Could not retrieve the associated invoice.', false ); |
|
100 | + wp_die( 'Could not retrieve the associated invoice.', 200 ); |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Check PayPal IPN validity. |
|
105 | + */ |
|
106 | + protected function validate_ipn() { |
|
107 | + |
|
108 | + wpinv_error_log( 'Validating PayPal IPN response', false ); |
|
109 | + |
|
110 | + // Retrieve the associated invoice. |
|
111 | + $posted = wp_unslash( $_POST ); |
|
112 | + $invoice = $this->get_ipn_invoice( $posted ); |
|
113 | + |
|
114 | + if ( $this->gateway->is_sandbox( $invoice ) ) { |
|
115 | + wpinv_error_log( $posted, 'Invoice was processed in sandbox hence logging the posted data', false ); |
|
116 | + } |
|
117 | + |
|
118 | + // Validate the IPN. |
|
119 | + $posted['cmd'] = '_notify-validate'; |
|
120 | + |
|
121 | + // Send back post vars to paypal. |
|
122 | + $params = array( |
|
123 | + 'body' => $posted, |
|
124 | + 'timeout' => 60, |
|
125 | + 'httpversion' => '1.1', |
|
126 | + 'compress' => false, |
|
127 | + 'decompress' => false, |
|
128 | + 'user-agent' => 'GetPaid/' . WPINV_VERSION, |
|
129 | + ); |
|
130 | + |
|
131 | + // Post back to get a response. |
|
132 | + $response = wp_safe_remote_post( $this->gateway->is_sandbox( $invoice ) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr', $params ); |
|
133 | + |
|
134 | + // Check to see if the request was valid. |
|
135 | + if ( ! is_wp_error( $response ) && $response['response']['code'] < 300 && strstr( $response['body'], 'VERIFIED' ) ) { |
|
136 | + wpinv_error_log( 'Received valid response from PayPal IPN: ' . $response['body'], false ); |
|
137 | + return true; |
|
138 | + } |
|
139 | + |
|
140 | + if ( is_wp_error( $response ) ) { |
|
141 | + wpinv_error_log( $response->get_error_message(), 'Received invalid response from PayPal IPN' ); |
|
142 | + return false; |
|
143 | + } |
|
144 | + |
|
145 | + wpinv_error_log( $response['body'], 'Received invalid response from PayPal IPN' ); |
|
146 | + return false; |
|
147 | + |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Check currency from IPN matches the invoice. |
|
152 | + * |
|
153 | + * @param WPInv_Invoice $invoice Invoice object. |
|
154 | + * @param string $currency currency to validate. |
|
155 | + */ |
|
156 | + protected function validate_ipn_currency( $invoice, $currency ) { |
|
157 | 157 | |
158 | - if ( strtolower( $invoice->get_currency() ) !== strtolower( $currency ) ) { |
|
158 | + if ( strtolower( $invoice->get_currency() ) !== strtolower( $currency ) ) { |
|
159 | 159 | |
160 | - /* translators: %s: currency code. */ |
|
161 | - $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal currencies do not match (code %s).', 'invoicing' ), $currency ) ); |
|
160 | + /* translators: %s: currency code. */ |
|
161 | + $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal currencies do not match (code %s).', 'invoicing' ), $currency ) ); |
|
162 | 162 | |
163 | - wpinv_error_log( "Currencies do not match: {$currency} instead of {$invoice->get_currency()}", 'IPN Error', __FILE__, __LINE__, true ); |
|
164 | - } |
|
163 | + wpinv_error_log( "Currencies do not match: {$currency} instead of {$invoice->get_currency()}", 'IPN Error', __FILE__, __LINE__, true ); |
|
164 | + } |
|
165 | 165 | |
166 | - wpinv_error_log( $currency, 'Validated IPN Currency', false ); |
|
167 | - } |
|
166 | + wpinv_error_log( $currency, 'Validated IPN Currency', false ); |
|
167 | + } |
|
168 | 168 | |
169 | - /** |
|
170 | - * Check payment amount from IPN matches the invoice. |
|
171 | - * |
|
172 | - * @param WPInv_Invoice $invoice Invoice object. |
|
173 | - * @param float $amount amount to validate. |
|
174 | - */ |
|
175 | - protected function validate_ipn_amount( $invoice, $amount ) { |
|
176 | - if ( number_format( $invoice->get_total(), 2, '.', '' ) !== number_format( $amount, 2, '.', '' ) ) { |
|
169 | + /** |
|
170 | + * Check payment amount from IPN matches the invoice. |
|
171 | + * |
|
172 | + * @param WPInv_Invoice $invoice Invoice object. |
|
173 | + * @param float $amount amount to validate. |
|
174 | + */ |
|
175 | + protected function validate_ipn_amount( $invoice, $amount ) { |
|
176 | + if ( number_format( $invoice->get_total(), 2, '.', '' ) !== number_format( $amount, 2, '.', '' ) ) { |
|
177 | 177 | |
178 | - /* translators: %s: Amount. */ |
|
179 | - $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal amounts do not match (gross %s).', 'invoicing' ), $amount ) ); |
|
178 | + /* translators: %s: Amount. */ |
|
179 | + $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal amounts do not match (gross %s).', 'invoicing' ), $amount ) ); |
|
180 | 180 | |
181 | - wpinv_error_log( "Amounts do not match: {$amount} instead of {$invoice->get_total()}", 'IPN Error', __FILE__, __LINE__, true ); |
|
182 | - } |
|
181 | + wpinv_error_log( "Amounts do not match: {$amount} instead of {$invoice->get_total()}", 'IPN Error', __FILE__, __LINE__, true ); |
|
182 | + } |
|
183 | 183 | |
184 | - wpinv_error_log( $amount, 'Validated IPN Amount', false ); |
|
185 | - } |
|
184 | + wpinv_error_log( $amount, 'Validated IPN Amount', false ); |
|
185 | + } |
|
186 | 186 | |
187 | - /** |
|
188 | - * Verify receiver email from PayPal. |
|
189 | - * |
|
190 | - * @param WPInv_Invoice $invoice Invoice object. |
|
191 | - * @param string $receiver_email Email to validate. |
|
192 | - */ |
|
193 | - protected function validate_ipn_receiver_email( $invoice, $receiver_email ) { |
|
194 | - $paypal_email = wpinv_get_option( 'paypal_email' ); |
|
187 | + /** |
|
188 | + * Verify receiver email from PayPal. |
|
189 | + * |
|
190 | + * @param WPInv_Invoice $invoice Invoice object. |
|
191 | + * @param string $receiver_email Email to validate. |
|
192 | + */ |
|
193 | + protected function validate_ipn_receiver_email( $invoice, $receiver_email ) { |
|
194 | + $paypal_email = wpinv_get_option( 'paypal_email' ); |
|
195 | 195 | |
196 | - if ( strcasecmp( trim( $receiver_email ), trim( $paypal_email ) ) !== 0 ) { |
|
197 | - wpinv_record_gateway_error( 'IPN Error', "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}" ); |
|
196 | + if ( strcasecmp( trim( $receiver_email ), trim( $paypal_email ) ) !== 0 ) { |
|
197 | + wpinv_record_gateway_error( 'IPN Error', "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}" ); |
|
198 | 198 | |
199 | - /* translators: %s: email address . */ |
|
200 | - $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal IPN response from a different email address (%s).', 'invoicing' ), $receiver_email ) ); |
|
199 | + /* translators: %s: email address . */ |
|
200 | + $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal IPN response from a different email address (%s).', 'invoicing' ), $receiver_email ) ); |
|
201 | 201 | |
202 | - return wpinv_error_log( "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}", 'IPN Error', __FILE__, __LINE__, true ); |
|
203 | - } |
|
202 | + return wpinv_error_log( "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}", 'IPN Error', __FILE__, __LINE__, true ); |
|
203 | + } |
|
204 | 204 | |
205 | - wpinv_error_log( 'Validated PayPal Email', false ); |
|
206 | - } |
|
205 | + wpinv_error_log( 'Validated PayPal Email', false ); |
|
206 | + } |
|
207 | 207 | |
208 | - /** |
|
209 | - * Handles one time payments. |
|
210 | - * |
|
211 | - * @param WPInv_Invoice $invoice Invoice object. |
|
212 | - * @param array $posted Posted data. |
|
213 | - */ |
|
214 | - protected function ipn_txn_web_accept( $invoice, $posted ) { |
|
208 | + /** |
|
209 | + * Handles one time payments. |
|
210 | + * |
|
211 | + * @param WPInv_Invoice $invoice Invoice object. |
|
212 | + * @param array $posted Posted data. |
|
213 | + */ |
|
214 | + protected function ipn_txn_web_accept( $invoice, $posted ) { |
|
215 | 215 | |
216 | - // Collect payment details |
|
217 | - $payment_status = strtolower( $posted['payment_status'] ); |
|
218 | - $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
|
216 | + // Collect payment details |
|
217 | + $payment_status = strtolower( $posted['payment_status'] ); |
|
218 | + $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
|
219 | 219 | |
220 | - $this->validate_ipn_receiver_email( $invoice, $business_email ); |
|
221 | - $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
|
220 | + $this->validate_ipn_receiver_email( $invoice, $business_email ); |
|
221 | + $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
|
222 | 222 | |
223 | - // Update the transaction id. |
|
224 | - if ( ! empty( $posted['txn_id'] ) ) { |
|
225 | - $invoice->set_transaction_id( wpinv_clean( $posted['txn_id'] ) ); |
|
226 | - $invoice->save(); |
|
227 | - } |
|
223 | + // Update the transaction id. |
|
224 | + if ( ! empty( $posted['txn_id'] ) ) { |
|
225 | + $invoice->set_transaction_id( wpinv_clean( $posted['txn_id'] ) ); |
|
226 | + $invoice->save(); |
|
227 | + } |
|
228 | 228 | |
229 | - $invoice->add_system_note( __( 'Processing invoice IPN', 'invoicing' ) ); |
|
229 | + $invoice->add_system_note( __( 'Processing invoice IPN', 'invoicing' ) ); |
|
230 | 230 | |
231 | - // Process a refund. |
|
232 | - if ( $payment_status == 'refunded' || $payment_status == 'reversed' ) { |
|
231 | + // Process a refund. |
|
232 | + if ( $payment_status == 'refunded' || $payment_status == 'reversed' ) { |
|
233 | 233 | |
234 | - update_post_meta( $invoice->get_id(), 'refunded_remotely', 1 ); |
|
234 | + update_post_meta( $invoice->get_id(), 'refunded_remotely', 1 ); |
|
235 | 235 | |
236 | - if ( ! $invoice->is_refunded() ) { |
|
237 | - $invoice->update_status( 'wpi-refunded', $posted['reason_code'] ); |
|
238 | - } |
|
236 | + if ( ! $invoice->is_refunded() ) { |
|
237 | + $invoice->update_status( 'wpi-refunded', $posted['reason_code'] ); |
|
238 | + } |
|
239 | 239 | |
240 | - return wpinv_error_log( $posted['reason_code'], false ); |
|
241 | - } |
|
240 | + return wpinv_error_log( $posted['reason_code'], false ); |
|
241 | + } |
|
242 | 242 | |
243 | - // Process payments. |
|
244 | - if ( $payment_status == 'completed' ) { |
|
243 | + // Process payments. |
|
244 | + if ( $payment_status == 'completed' ) { |
|
245 | 245 | |
246 | - if ( $invoice->is_paid() && 'wpi_processing' != $invoice->get_status() ) { |
|
247 | - return wpinv_error_log( 'Aborting, Invoice #' . $invoice->get_number() . ' is already paid.', false ); |
|
248 | - } |
|
246 | + if ( $invoice->is_paid() && 'wpi_processing' != $invoice->get_status() ) { |
|
247 | + return wpinv_error_log( 'Aborting, Invoice #' . $invoice->get_number() . ' is already paid.', false ); |
|
248 | + } |
|
249 | 249 | |
250 | - $this->validate_ipn_amount( $invoice, $posted['mc_gross'] ); |
|
250 | + $this->validate_ipn_amount( $invoice, $posted['mc_gross'] ); |
|
251 | 251 | |
252 | - $note = ''; |
|
252 | + $note = ''; |
|
253 | 253 | |
254 | - if ( ! empty( $posted['mc_fee'] ) ) { |
|
255 | - $note = sprintf( __( 'PayPal Transaction Fee %s.', 'invoicing' ), sanitize_text_field( $posted['mc_fee'] ) ); |
|
256 | - } |
|
254 | + if ( ! empty( $posted['mc_fee'] ) ) { |
|
255 | + $note = sprintf( __( 'PayPal Transaction Fee %s.', 'invoicing' ), sanitize_text_field( $posted['mc_fee'] ) ); |
|
256 | + } |
|
257 | 257 | |
258 | - if ( ! empty( $posted['payer_status'] ) ) { |
|
259 | - $note = ' ' . sprintf( __( 'Buyer status %s.', 'invoicing' ), sanitize_text_field( $posted['payer_status'] ) ); |
|
260 | - } |
|
258 | + if ( ! empty( $posted['payer_status'] ) ) { |
|
259 | + $note = ' ' . sprintf( __( 'Buyer status %s.', 'invoicing' ), sanitize_text_field( $posted['payer_status'] ) ); |
|
260 | + } |
|
261 | 261 | |
262 | - $invoice->mark_paid( ( ! empty( $posted['txn_id'] ) ? sanitize_text_field( $posted['txn_id'] ) : '' ), trim( $note ) ); |
|
263 | - return wpinv_error_log( 'Invoice marked as paid.', false ); |
|
262 | + $invoice->mark_paid( ( ! empty( $posted['txn_id'] ) ? sanitize_text_field( $posted['txn_id'] ) : '' ), trim( $note ) ); |
|
263 | + return wpinv_error_log( 'Invoice marked as paid.', false ); |
|
264 | 264 | |
265 | - } |
|
265 | + } |
|
266 | 266 | |
267 | - // Pending payments. |
|
268 | - if ( $payment_status == 'pending' ) { |
|
267 | + // Pending payments. |
|
268 | + if ( $payment_status == 'pending' ) { |
|
269 | 269 | |
270 | - /* translators: %s: pending reason. */ |
|
271 | - $invoice->update_status( 'wpi-onhold', sprintf( __( 'Payment pending (%s).', 'invoicing' ), $posted['pending_reason'] ) ); |
|
270 | + /* translators: %s: pending reason. */ |
|
271 | + $invoice->update_status( 'wpi-onhold', sprintf( __( 'Payment pending (%s).', 'invoicing' ), $posted['pending_reason'] ) ); |
|
272 | 272 | |
273 | - return wpinv_error_log( 'Invoice marked as "payment held".', false ); |
|
274 | - } |
|
273 | + return wpinv_error_log( 'Invoice marked as "payment held".', false ); |
|
274 | + } |
|
275 | 275 | |
276 | - /* translators: %s: payment status. */ |
|
277 | - $invoice->update_status( 'wpi-failed', sprintf( __( 'Payment %s via IPN.', 'invoicing' ), sanitize_text_field( $posted['payment_status'] ) ) ); |
|
276 | + /* translators: %s: payment status. */ |
|
277 | + $invoice->update_status( 'wpi-failed', sprintf( __( 'Payment %s via IPN.', 'invoicing' ), sanitize_text_field( $posted['payment_status'] ) ) ); |
|
278 | 278 | |
279 | - } |
|
279 | + } |
|
280 | 280 | |
281 | - /** |
|
282 | - * Handles one time payments. |
|
283 | - * |
|
284 | - * @param WPInv_Invoice $invoice Invoice object. |
|
285 | - * @param array $posted Posted data. |
|
286 | - */ |
|
287 | - protected function ipn_txn_cart( $invoice, $posted ) { |
|
288 | - $this->ipn_txn_web_accept( $invoice, $posted ); |
|
289 | - } |
|
281 | + /** |
|
282 | + * Handles one time payments. |
|
283 | + * |
|
284 | + * @param WPInv_Invoice $invoice Invoice object. |
|
285 | + * @param array $posted Posted data. |
|
286 | + */ |
|
287 | + protected function ipn_txn_cart( $invoice, $posted ) { |
|
288 | + $this->ipn_txn_web_accept( $invoice, $posted ); |
|
289 | + } |
|
290 | 290 | |
291 | - /** |
|
292 | - * Handles subscription sign ups. |
|
293 | - * |
|
294 | - * @param WPInv_Invoice $invoice Invoice object. |
|
295 | - * @param array $posted Posted data. |
|
296 | - */ |
|
297 | - protected function ipn_txn_subscr_signup( $invoice, $posted ) { |
|
291 | + /** |
|
292 | + * Handles subscription sign ups. |
|
293 | + * |
|
294 | + * @param WPInv_Invoice $invoice Invoice object. |
|
295 | + * @param array $posted Posted data. |
|
296 | + */ |
|
297 | + protected function ipn_txn_subscr_signup( $invoice, $posted ) { |
|
298 | 298 | |
299 | - wpinv_error_log( 'Processing subscription signup', false ); |
|
299 | + wpinv_error_log( 'Processing subscription signup', false ); |
|
300 | 300 | |
301 | - // Make sure the invoice has a subscription. |
|
302 | - $subscription = getpaid_get_invoice_subscription( $invoice ); |
|
301 | + // Make sure the invoice has a subscription. |
|
302 | + $subscription = getpaid_get_invoice_subscription( $invoice ); |
|
303 | 303 | |
304 | - if ( empty( $subscription ) ) { |
|
305 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
306 | - } |
|
304 | + if ( empty( $subscription ) ) { |
|
305 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
306 | + } |
|
307 | 307 | |
308 | - wpinv_error_log( 'Found subscription #' . $subscription->get_id(), false ); |
|
308 | + wpinv_error_log( 'Found subscription #' . $subscription->get_id(), false ); |
|
309 | 309 | |
310 | - // Validate the IPN. |
|
311 | - $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
|
312 | - $this->validate_ipn_receiver_email( $invoice, $business_email ); |
|
313 | - $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
|
310 | + // Validate the IPN. |
|
311 | + $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
|
312 | + $this->validate_ipn_receiver_email( $invoice, $business_email ); |
|
313 | + $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
|
314 | 314 | |
315 | - // Activate the subscription. |
|
316 | - $duration = strtotime( $subscription->get_expiration() ) - strtotime( $subscription->get_date_created() ); |
|
317 | - $subscription->set_date_created( current_time( 'mysql' ) ); |
|
318 | - $subscription->set_expiration( date( 'Y-m-d H:i:s', ( current_time( 'timestamp' ) + $duration ) ) ); |
|
319 | - $subscription->set_profile_id( sanitize_text_field( $posted['subscr_id'] ) ); |
|
320 | - $subscription->activate(); |
|
315 | + // Activate the subscription. |
|
316 | + $duration = strtotime( $subscription->get_expiration() ) - strtotime( $subscription->get_date_created() ); |
|
317 | + $subscription->set_date_created( current_time( 'mysql' ) ); |
|
318 | + $subscription->set_expiration( date( 'Y-m-d H:i:s', ( current_time( 'timestamp' ) + $duration ) ) ); |
|
319 | + $subscription->set_profile_id( sanitize_text_field( $posted['subscr_id'] ) ); |
|
320 | + $subscription->activate(); |
|
321 | 321 | |
322 | - // Set the transaction id. |
|
323 | - if ( ! empty( $posted['txn_id'] ) ) { |
|
324 | - $invoice->add_note( sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ), $posted['txn_id'] ), false, false, true ); |
|
325 | - $invoice->set_transaction_id( $posted['txn_id'] ); |
|
326 | - } |
|
322 | + // Set the transaction id. |
|
323 | + if ( ! empty( $posted['txn_id'] ) ) { |
|
324 | + $invoice->add_note( sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ), $posted['txn_id'] ), false, false, true ); |
|
325 | + $invoice->set_transaction_id( $posted['txn_id'] ); |
|
326 | + } |
|
327 | 327 | |
328 | - // Update the payment status. |
|
329 | - $invoice->mark_paid(); |
|
328 | + // Update the payment status. |
|
329 | + $invoice->mark_paid(); |
|
330 | 330 | |
331 | - $invoice->add_note( sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ), $posted['subscr_id'] ), false, false, true ); |
|
331 | + $invoice->add_note( sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ), $posted['subscr_id'] ), false, false, true ); |
|
332 | 332 | |
333 | - wpinv_error_log( 'Subscription started.', false ); |
|
334 | - } |
|
333 | + wpinv_error_log( 'Subscription started.', false ); |
|
334 | + } |
|
335 | 335 | |
336 | - /** |
|
337 | - * Handles subscription renewals. |
|
338 | - * |
|
339 | - * @param WPInv_Invoice $invoice Invoice object. |
|
340 | - * @param array $posted Posted data. |
|
341 | - */ |
|
342 | - protected function ipn_txn_subscr_payment( $invoice, $posted ) { |
|
336 | + /** |
|
337 | + * Handles subscription renewals. |
|
338 | + * |
|
339 | + * @param WPInv_Invoice $invoice Invoice object. |
|
340 | + * @param array $posted Posted data. |
|
341 | + */ |
|
342 | + protected function ipn_txn_subscr_payment( $invoice, $posted ) { |
|
343 | 343 | |
344 | - // Make sure the invoice has a subscription. |
|
345 | - $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
344 | + // Make sure the invoice has a subscription. |
|
345 | + $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
346 | 346 | |
347 | - if ( empty( $subscription ) ) { |
|
348 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
349 | - } |
|
347 | + if ( empty( $subscription ) ) { |
|
348 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
349 | + } |
|
350 | 350 | |
351 | - wpinv_error_log( 'Found subscription #' . $subscription->get_id(), false ); |
|
351 | + wpinv_error_log( 'Found subscription #' . $subscription->get_id(), false ); |
|
352 | 352 | |
353 | - // PayPal sends a subscr_payment for the first payment too. |
|
354 | - $date_completed = getpaid_format_date( $invoice->get_date_completed() ); |
|
355 | - $date_created = getpaid_format_date( $invoice->get_date_created() ); |
|
356 | - $today_date = getpaid_format_date( current_time( 'mysql' ) ); |
|
357 | - $payment_date = getpaid_format_date( $posted['payment_date'] ); |
|
358 | - $subscribe_date = getpaid_format_date( $subscription->get_date_created() ); |
|
359 | - $dates = array_filter( compact( 'date_completed', 'date_created', 'subscribe_date' ) ); |
|
353 | + // PayPal sends a subscr_payment for the first payment too. |
|
354 | + $date_completed = getpaid_format_date( $invoice->get_date_completed() ); |
|
355 | + $date_created = getpaid_format_date( $invoice->get_date_created() ); |
|
356 | + $today_date = getpaid_format_date( current_time( 'mysql' ) ); |
|
357 | + $payment_date = getpaid_format_date( $posted['payment_date'] ); |
|
358 | + $subscribe_date = getpaid_format_date( $subscription->get_date_created() ); |
|
359 | + $dates = array_filter( compact( 'date_completed', 'date_created', 'subscribe_date' ) ); |
|
360 | 360 | |
361 | - foreach ( $dates as $date ) { |
|
361 | + foreach ( $dates as $date ) { |
|
362 | 362 | |
363 | - if ( $date !== $today_date && $date !== $payment_date ) { |
|
364 | - continue; |
|
365 | - } |
|
363 | + if ( $date !== $today_date && $date !== $payment_date ) { |
|
364 | + continue; |
|
365 | + } |
|
366 | 366 | |
367 | - if ( ! empty( $posted['txn_id'] ) ) { |
|
368 | - $invoice->set_transaction_id( sanitize_text_field( $posted['txn_id'] ) ); |
|
369 | - $invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ), sanitize_text_field( $posted['txn_id'] ) ), false, false, true ); |
|
370 | - } |
|
367 | + if ( ! empty( $posted['txn_id'] ) ) { |
|
368 | + $invoice->set_transaction_id( sanitize_text_field( $posted['txn_id'] ) ); |
|
369 | + $invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ), sanitize_text_field( $posted['txn_id'] ) ), false, false, true ); |
|
370 | + } |
|
371 | 371 | |
372 | - return $invoice->mark_paid(); |
|
373 | - |
|
374 | - } |
|
372 | + return $invoice->mark_paid(); |
|
373 | + |
|
374 | + } |
|
375 | 375 | |
376 | - wpinv_error_log( 'Processing subscription renewal payment for the invoice ' . $invoice->get_id(), false ); |
|
377 | - |
|
378 | - // Abort if the payment is already recorded. |
|
379 | - if ( wpinv_get_id_by_transaction_id( $posted['txn_id'] ) ) { |
|
380 | - return wpinv_error_log( 'Aborting, Transaction ' . $posted['txn_id'] . ' has already been processed', false ); |
|
381 | - } |
|
382 | - |
|
383 | - $args = array( |
|
384 | - 'transaction_id' => $posted['txn_id'], |
|
385 | - 'gateway' => $this->id, |
|
386 | - ); |
|
387 | - |
|
388 | - $invoice = wpinv_get_invoice( $subscription->add_payment( $args ) ); |
|
376 | + wpinv_error_log( 'Processing subscription renewal payment for the invoice ' . $invoice->get_id(), false ); |
|
377 | + |
|
378 | + // Abort if the payment is already recorded. |
|
379 | + if ( wpinv_get_id_by_transaction_id( $posted['txn_id'] ) ) { |
|
380 | + return wpinv_error_log( 'Aborting, Transaction ' . $posted['txn_id'] . ' has already been processed', false ); |
|
381 | + } |
|
382 | + |
|
383 | + $args = array( |
|
384 | + 'transaction_id' => $posted['txn_id'], |
|
385 | + 'gateway' => $this->id, |
|
386 | + ); |
|
387 | + |
|
388 | + $invoice = wpinv_get_invoice( $subscription->add_payment( $args ) ); |
|
389 | 389 | |
390 | - if ( empty( $invoice ) ) { |
|
391 | - return; |
|
392 | - } |
|
390 | + if ( empty( $invoice ) ) { |
|
391 | + return; |
|
392 | + } |
|
393 | 393 | |
394 | - $invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ), $posted['txn_id'] ), false, false, true ); |
|
395 | - $invoice->add_note( wp_sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ), $posted['subscr_id'] ), false, false, true ); |
|
394 | + $invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ), $posted['txn_id'] ), false, false, true ); |
|
395 | + $invoice->add_note( wp_sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ), $posted['subscr_id'] ), false, false, true ); |
|
396 | 396 | |
397 | - $subscription->renew(); |
|
398 | - wpinv_error_log( 'Subscription renewed.', false ); |
|
397 | + $subscription->renew(); |
|
398 | + wpinv_error_log( 'Subscription renewed.', false ); |
|
399 | 399 | |
400 | - } |
|
400 | + } |
|
401 | 401 | |
402 | - /** |
|
403 | - * Handles subscription cancelations. |
|
404 | - * |
|
405 | - * @param WPInv_Invoice $invoice Invoice object. |
|
406 | - */ |
|
407 | - protected function ipn_txn_subscr_cancel( $invoice ) { |
|
402 | + /** |
|
403 | + * Handles subscription cancelations. |
|
404 | + * |
|
405 | + * @param WPInv_Invoice $invoice Invoice object. |
|
406 | + */ |
|
407 | + protected function ipn_txn_subscr_cancel( $invoice ) { |
|
408 | 408 | |
409 | - // Make sure the invoice has a subscription. |
|
410 | - $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
411 | - |
|
412 | - if ( empty( $subscription ) ) { |
|
413 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
414 | - } |
|
415 | - |
|
416 | - wpinv_error_log( 'Processing subscription cancellation for the invoice ' . $invoice->get_id(), false ); |
|
417 | - $subscription->cancel(); |
|
418 | - wpinv_error_log( 'Subscription cancelled.', false ); |
|
409 | + // Make sure the invoice has a subscription. |
|
410 | + $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
411 | + |
|
412 | + if ( empty( $subscription ) ) { |
|
413 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
414 | + } |
|
415 | + |
|
416 | + wpinv_error_log( 'Processing subscription cancellation for the invoice ' . $invoice->get_id(), false ); |
|
417 | + $subscription->cancel(); |
|
418 | + wpinv_error_log( 'Subscription cancelled.', false ); |
|
419 | 419 | |
420 | - } |
|
420 | + } |
|
421 | 421 | |
422 | - /** |
|
423 | - * Handles subscription completions. |
|
424 | - * |
|
425 | - * @param WPInv_Invoice $invoice Invoice object. |
|
426 | - * @param array $posted Posted data. |
|
427 | - */ |
|
428 | - protected function ipn_txn_subscr_eot( $invoice ) { |
|
422 | + /** |
|
423 | + * Handles subscription completions. |
|
424 | + * |
|
425 | + * @param WPInv_Invoice $invoice Invoice object. |
|
426 | + * @param array $posted Posted data. |
|
427 | + */ |
|
428 | + protected function ipn_txn_subscr_eot( $invoice ) { |
|
429 | 429 | |
430 | - // Make sure the invoice has a subscription. |
|
431 | - $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
430 | + // Make sure the invoice has a subscription. |
|
431 | + $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
432 | 432 | |
433 | - if ( empty( $subscription ) ) { |
|
434 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
435 | - } |
|
433 | + if ( empty( $subscription ) ) { |
|
434 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
435 | + } |
|
436 | 436 | |
437 | - wpinv_error_log( 'Processing subscription end of life for the invoice ' . $invoice->get_id(), false ); |
|
438 | - $subscription->complete(); |
|
439 | - wpinv_error_log( 'Subscription completed.', false ); |
|
437 | + wpinv_error_log( 'Processing subscription end of life for the invoice ' . $invoice->get_id(), false ); |
|
438 | + $subscription->complete(); |
|
439 | + wpinv_error_log( 'Subscription completed.', false ); |
|
440 | 440 | |
441 | - } |
|
441 | + } |
|
442 | 442 | |
443 | - /** |
|
444 | - * Handles subscription fails. |
|
445 | - * |
|
446 | - * @param WPInv_Invoice $invoice Invoice object. |
|
447 | - * @param array $posted Posted data. |
|
448 | - */ |
|
449 | - protected function ipn_txn_subscr_failed( $invoice ) { |
|
443 | + /** |
|
444 | + * Handles subscription fails. |
|
445 | + * |
|
446 | + * @param WPInv_Invoice $invoice Invoice object. |
|
447 | + * @param array $posted Posted data. |
|
448 | + */ |
|
449 | + protected function ipn_txn_subscr_failed( $invoice ) { |
|
450 | 450 | |
451 | - // Make sure the invoice has a subscription. |
|
452 | - $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
451 | + // Make sure the invoice has a subscription. |
|
452 | + $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
453 | 453 | |
454 | - if ( empty( $subscription ) ) { |
|
455 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
456 | - } |
|
454 | + if ( empty( $subscription ) ) { |
|
455 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
456 | + } |
|
457 | 457 | |
458 | - wpinv_error_log( 'Processing subscription payment failure for the invoice ' . $invoice->get_id(), false ); |
|
459 | - $subscription->failing(); |
|
460 | - wpinv_error_log( 'Subscription marked as failing.', false ); |
|
458 | + wpinv_error_log( 'Processing subscription payment failure for the invoice ' . $invoice->get_id(), false ); |
|
459 | + $subscription->failing(); |
|
460 | + wpinv_error_log( 'Subscription marked as failing.', false ); |
|
461 | 461 | |
462 | - } |
|
462 | + } |
|
463 | 463 | |
464 | - /** |
|
465 | - * Handles subscription suspensions. |
|
466 | - * |
|
467 | - * @param WPInv_Invoice $invoice Invoice object. |
|
468 | - * @param array $posted Posted data. |
|
469 | - */ |
|
470 | - protected function ipn_txn_recurring_payment_suspended_due_to_max_failed_payment( $invoice ) { |
|
464 | + /** |
|
465 | + * Handles subscription suspensions. |
|
466 | + * |
|
467 | + * @param WPInv_Invoice $invoice Invoice object. |
|
468 | + * @param array $posted Posted data. |
|
469 | + */ |
|
470 | + protected function ipn_txn_recurring_payment_suspended_due_to_max_failed_payment( $invoice ) { |
|
471 | 471 | |
472 | - // Make sure the invoice has a subscription. |
|
473 | - $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
472 | + // Make sure the invoice has a subscription. |
|
473 | + $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
474 | 474 | |
475 | - if ( empty( $subscription ) ) { |
|
476 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
477 | - } |
|
478 | - |
|
479 | - wpinv_error_log( 'Processing subscription cancellation due to max failed payment for the invoice ' . $invoice->get_id(), false ); |
|
480 | - $subscription->cancel(); |
|
481 | - wpinv_error_log( 'Subscription cancelled.', false ); |
|
482 | - } |
|
475 | + if ( empty( $subscription ) ) { |
|
476 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
477 | + } |
|
478 | + |
|
479 | + wpinv_error_log( 'Processing subscription cancellation due to max failed payment for the invoice ' . $invoice->get_id(), false ); |
|
480 | + $subscription->cancel(); |
|
481 | + wpinv_error_log( 'Subscription cancelled.', false ); |
|
482 | + } |
|
483 | 483 | |
484 | 484 | } |