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