@@ -13,72 +13,72 @@ discard block |
||
13 | 13 | class GetPaid_Paypal_Gateway extends GetPaid_Payment_Gateway { |
14 | 14 | |
15 | 15 | /** |
16 | - * Payment method id. |
|
17 | - * |
|
18 | - * @var string |
|
19 | - */ |
|
16 | + * Payment method id. |
|
17 | + * |
|
18 | + * @var string |
|
19 | + */ |
|
20 | 20 | public $id = 'paypal'; |
21 | 21 | |
22 | 22 | /** |
23 | - * An array of features that this gateway supports. |
|
24 | - * |
|
25 | - * @var array |
|
26 | - */ |
|
23 | + * An array of features that this gateway supports. |
|
24 | + * |
|
25 | + * @var array |
|
26 | + */ |
|
27 | 27 | protected $supports = array( 'subscription', 'sandbox' ); |
28 | 28 | |
29 | 29 | /** |
30 | - * Payment method order. |
|
31 | - * |
|
32 | - * @var int |
|
33 | - */ |
|
30 | + * Payment method order. |
|
31 | + * |
|
32 | + * @var int |
|
33 | + */ |
|
34 | 34 | public $order = 1; |
35 | 35 | |
36 | 36 | /** |
37 | - * Stores line items to send to PayPal. |
|
38 | - * |
|
39 | - * @var array |
|
40 | - */ |
|
37 | + * Stores line items to send to PayPal. |
|
38 | + * |
|
39 | + * @var array |
|
40 | + */ |
|
41 | 41 | protected $line_items = array(); |
42 | 42 | |
43 | 43 | /** |
44 | - * Endpoint for requests from PayPal. |
|
45 | - * |
|
46 | - * @var string |
|
47 | - */ |
|
48 | - protected $notify_url; |
|
44 | + * Endpoint for requests from PayPal. |
|
45 | + * |
|
46 | + * @var string |
|
47 | + */ |
|
48 | + protected $notify_url; |
|
49 | 49 | |
50 | - /** |
|
51 | - * Endpoint for requests to PayPal. |
|
52 | - * |
|
53 | - * @var string |
|
54 | - */ |
|
50 | + /** |
|
51 | + * Endpoint for requests to PayPal. |
|
52 | + * |
|
53 | + * @var string |
|
54 | + */ |
|
55 | 55 | protected $endpoint; |
56 | 56 | |
57 | 57 | /** |
58 | - * Currencies this gateway is allowed for. |
|
59 | - * |
|
60 | - * @var array |
|
61 | - */ |
|
62 | - public $currencies = array( 'AUD', 'BRL', 'CAD', 'MXN', 'NZD', 'HKD', 'SGD', 'USD', 'EUR', 'JPY', 'TRY', 'NOK', 'CZK', 'DKK', 'HUF', 'ILS', 'MYR', 'PHP', 'PLN', 'SEK', 'CHF', 'TWD', 'THB', 'GBP', 'RMB', 'RUB', 'INR' ); |
|
58 | + * Currencies this gateway is allowed for. |
|
59 | + * |
|
60 | + * @var array |
|
61 | + */ |
|
62 | + public $currencies = array( 'AUD', 'BRL', 'CAD', 'MXN', 'NZD', 'HKD', 'SGD', 'USD', 'EUR', 'JPY', 'TRY', 'NOK', 'CZK', 'DKK', 'HUF', 'ILS', 'MYR', 'PHP', 'PLN', 'SEK', 'CHF', 'TWD', 'THB', 'GBP', 'RMB', 'RUB', 'INR' ); |
|
63 | 63 | |
64 | 64 | /** |
65 | - * URL to view a transaction. |
|
66 | - * |
|
67 | - * @var string |
|
68 | - */ |
|
65 | + * URL to view a transaction. |
|
66 | + * |
|
67 | + * @var string |
|
68 | + */ |
|
69 | 69 | public $view_transaction_url = 'https://www.{sandbox}paypal.com/activity/payment/%s'; |
70 | 70 | |
71 | 71 | /** |
72 | - * URL to view a subscription. |
|
73 | - * |
|
74 | - * @var string |
|
75 | - */ |
|
76 | - public $view_subscription_url = 'https://www.{sandbox}paypal.com/cgi-bin/webscr?cmd=_profile-recurring-payments&encrypted_profile_id=%s'; |
|
72 | + * URL to view a subscription. |
|
73 | + * |
|
74 | + * @var string |
|
75 | + */ |
|
76 | + public $view_subscription_url = 'https://www.{sandbox}paypal.com/cgi-bin/webscr?cmd=_profile-recurring-payments&encrypted_profile_id=%s'; |
|
77 | 77 | |
78 | 78 | /** |
79 | - * Class constructor. |
|
80 | - */ |
|
81 | - public function __construct() { |
|
79 | + * Class constructor. |
|
80 | + */ |
|
81 | + public function __construct() { |
|
82 | 82 | |
83 | 83 | $this->title = __( 'PayPal Standard', 'invoicing' ); |
84 | 84 | $this->method_title = __( 'PayPal Standard', 'invoicing' ); |
@@ -92,15 +92,15 @@ discard block |
||
92 | 92 | } |
93 | 93 | |
94 | 94 | /** |
95 | - * Process Payment. |
|
96 | - * |
|
97 | - * |
|
98 | - * @param WPInv_Invoice $invoice Invoice. |
|
99 | - * @param array $submission_data Posted checkout fields. |
|
100 | - * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
|
101 | - * @return array |
|
102 | - */ |
|
103 | - public function process_payment( $invoice, $submission_data, $submission ) { |
|
95 | + * Process Payment. |
|
96 | + * |
|
97 | + * |
|
98 | + * @param WPInv_Invoice $invoice Invoice. |
|
99 | + * @param array $submission_data Posted checkout fields. |
|
100 | + * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
|
101 | + * @return array |
|
102 | + */ |
|
103 | + public function process_payment( $invoice, $submission_data, $submission ) { |
|
104 | 104 | |
105 | 105 | // Get redirect url. |
106 | 106 | $paypal_redirect = $this->get_request_url( $invoice ); |
@@ -123,15 +123,15 @@ discard block |
||
123 | 123 | } |
124 | 124 | |
125 | 125 | /** |
126 | - * Get the PayPal request URL for an invoice. |
|
127 | - * |
|
128 | - * @param WPInv_Invoice $invoice Invoice object. |
|
129 | - * @return string |
|
130 | - */ |
|
131 | - public function get_request_url( $invoice ) { |
|
126 | + * Get the PayPal request URL for an invoice. |
|
127 | + * |
|
128 | + * @param WPInv_Invoice $invoice Invoice object. |
|
129 | + * @return string |
|
130 | + */ |
|
131 | + public function get_request_url( $invoice ) { |
|
132 | 132 | |
133 | 133 | // Endpoint for this request |
134 | - $this->endpoint = $this->is_sandbox( $invoice ) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr?test_ipn=1&' : 'https://www.paypal.com/cgi-bin/webscr?'; |
|
134 | + $this->endpoint = $this->is_sandbox( $invoice ) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr?test_ipn=1&' : 'https://www.paypal.com/cgi-bin/webscr?'; |
|
135 | 135 | |
136 | 136 | // Retrieve paypal args. |
137 | 137 | $paypal_args = map_deep( $this->get_paypal_args( $invoice ), 'urlencode' ); |
@@ -144,44 +144,44 @@ discard block |
||
144 | 144 | |
145 | 145 | return add_query_arg( $paypal_args, $this->endpoint ); |
146 | 146 | |
147 | - } |
|
147 | + } |
|
148 | 148 | |
149 | 149 | /** |
150 | - * Get PayPal Args for passing to PP. |
|
151 | - * |
|
152 | - * @param WPInv_Invoice $invoice Invoice object. |
|
153 | - * @return array |
|
154 | - */ |
|
155 | - protected function get_paypal_args( $invoice ) { |
|
150 | + * Get PayPal Args for passing to PP. |
|
151 | + * |
|
152 | + * @param WPInv_Invoice $invoice Invoice object. |
|
153 | + * @return array |
|
154 | + */ |
|
155 | + protected function get_paypal_args( $invoice ) { |
|
156 | 156 | |
157 | 157 | // Whether or not to send the line items as one item. |
158 | - $force_one_line_item = apply_filters( 'getpaid_paypal_force_one_line_item', false, $invoice ); |
|
159 | - |
|
160 | - if ( $invoice->is_recurring() || ( wpinv_use_taxes() && wpinv_prices_include_tax() ) ) { |
|
161 | - $force_one_line_item = true; |
|
162 | - } |
|
163 | - |
|
164 | - $paypal_args = apply_filters( |
|
165 | - 'getpaid_paypal_args', |
|
166 | - array_merge( |
|
167 | - $this->get_transaction_args( $invoice ), |
|
168 | - $this->get_line_item_args( $invoice, $force_one_line_item ) |
|
169 | - ), |
|
170 | - $invoice |
|
171 | - ); |
|
172 | - |
|
173 | - return $this->fix_request_length( $invoice, $paypal_args ); |
|
158 | + $force_one_line_item = apply_filters( 'getpaid_paypal_force_one_line_item', false, $invoice ); |
|
159 | + |
|
160 | + if ( $invoice->is_recurring() || ( wpinv_use_taxes() && wpinv_prices_include_tax() ) ) { |
|
161 | + $force_one_line_item = true; |
|
162 | + } |
|
163 | + |
|
164 | + $paypal_args = apply_filters( |
|
165 | + 'getpaid_paypal_args', |
|
166 | + array_merge( |
|
167 | + $this->get_transaction_args( $invoice ), |
|
168 | + $this->get_line_item_args( $invoice, $force_one_line_item ) |
|
169 | + ), |
|
170 | + $invoice |
|
171 | + ); |
|
172 | + |
|
173 | + return $this->fix_request_length( $invoice, $paypal_args ); |
|
174 | 174 | } |
175 | 175 | |
176 | 176 | /** |
177 | - * Get transaction args for paypal request. |
|
178 | - * |
|
179 | - * @param WPInv_Invoice $invoice Invoice object. |
|
180 | - * @return array |
|
181 | - */ |
|
182 | - protected function get_transaction_args( $invoice ) { |
|
177 | + * Get transaction args for paypal request. |
|
178 | + * |
|
179 | + * @param WPInv_Invoice $invoice Invoice object. |
|
180 | + * @return array |
|
181 | + */ |
|
182 | + protected function get_transaction_args( $invoice ) { |
|
183 | 183 | |
184 | - return array( |
|
184 | + return array( |
|
185 | 185 | 'cmd' => '_cart', |
186 | 186 | 'business' => wpinv_get_option( 'paypal_email', false ), |
187 | 187 | 'no_shipping' => '1', |
@@ -206,16 +206,16 @@ discard block |
||
206 | 206 | } |
207 | 207 | |
208 | 208 | /** |
209 | - * Get line item args for paypal request. |
|
210 | - * |
|
211 | - * @param WPInv_Invoice $invoice Invoice object. |
|
212 | - * @param bool $force_one_line_item Create only one item for this invoice. |
|
213 | - * @return array |
|
214 | - */ |
|
215 | - protected function get_line_item_args( $invoice, $force_one_line_item = false ) { |
|
209 | + * Get line item args for paypal request. |
|
210 | + * |
|
211 | + * @param WPInv_Invoice $invoice Invoice object. |
|
212 | + * @param bool $force_one_line_item Create only one item for this invoice. |
|
213 | + * @return array |
|
214 | + */ |
|
215 | + protected function get_line_item_args( $invoice, $force_one_line_item = false ) { |
|
216 | 216 | |
217 | 217 | // Maybe send invoice as a single item. |
218 | - if ( $force_one_line_item ) { |
|
218 | + if ( $force_one_line_item ) { |
|
219 | 219 | return $this->get_line_item_args_single_item( $invoice ); |
220 | 220 | } |
221 | 221 | |
@@ -235,129 +235,129 @@ discard block |
||
235 | 235 | $line_item_args['discount_amount_cart'] = wpinv_sanitize_amount( (float) $invoice->get_total_discount(), 2 ); |
236 | 236 | } |
237 | 237 | |
238 | - return array_merge( $line_item_args, $this->get_line_items() ); |
|
238 | + return array_merge( $line_item_args, $this->get_line_items() ); |
|
239 | 239 | |
240 | 240 | } |
241 | 241 | |
242 | 242 | /** |
243 | - * Get line item args for paypal request as a single line item. |
|
244 | - * |
|
245 | - * @param WPInv_Invoice $invoice Invoice object. |
|
246 | - * @return array |
|
247 | - */ |
|
248 | - protected function get_line_item_args_single_item( $invoice ) { |
|
249 | - $this->delete_line_items(); |
|
243 | + * Get line item args for paypal request as a single line item. |
|
244 | + * |
|
245 | + * @param WPInv_Invoice $invoice Invoice object. |
|
246 | + * @return array |
|
247 | + */ |
|
248 | + protected function get_line_item_args_single_item( $invoice ) { |
|
249 | + $this->delete_line_items(); |
|
250 | 250 | |
251 | 251 | $item_name = sprintf( __( 'Invoice #%s', 'invoicing' ), $invoice->get_number() ); |
252 | - $this->add_line_item( $item_name, 1, wpinv_sanitize_amount( (float) $invoice->get_total(), 2 ), $invoice->get_id() ); |
|
252 | + $this->add_line_item( $item_name, 1, wpinv_sanitize_amount( (float) $invoice->get_total(), 2 ), $invoice->get_id() ); |
|
253 | 253 | |
254 | - return $this->get_line_items(); |
|
254 | + return $this->get_line_items(); |
|
255 | 255 | } |
256 | 256 | |
257 | 257 | /** |
258 | - * Return all line items. |
|
259 | - */ |
|
260 | - protected function get_line_items() { |
|
261 | - return $this->line_items; |
|
262 | - } |
|
258 | + * Return all line items. |
|
259 | + */ |
|
260 | + protected function get_line_items() { |
|
261 | + return $this->line_items; |
|
262 | + } |
|
263 | 263 | |
264 | 264 | /** |
265 | - * Remove all line items. |
|
266 | - */ |
|
267 | - protected function delete_line_items() { |
|
268 | - $this->line_items = array(); |
|
265 | + * Remove all line items. |
|
266 | + */ |
|
267 | + protected function delete_line_items() { |
|
268 | + $this->line_items = array(); |
|
269 | 269 | } |
270 | 270 | |
271 | 271 | /** |
272 | - * Prepare line items to send to paypal. |
|
273 | - * |
|
274 | - * @param WPInv_Invoice $invoice Invoice object. |
|
275 | - */ |
|
276 | - protected function prepare_line_items( $invoice ) { |
|
277 | - $this->delete_line_items(); |
|
278 | - |
|
279 | - // Items. |
|
280 | - foreach ( $invoice->get_items() as $item ) { |
|
281 | - $amount = $invoice->get_template() == 'amount' ? $item->get_price() : $item->get_sub_total(); |
|
282 | - $quantity = $invoice->get_template() == 'amount' ? 1 : $item->get_quantity(); |
|
283 | - $this->add_line_item( $item->get_raw_name(), $quantity, $amount, $item->get_id() ); |
|
272 | + * Prepare line items to send to paypal. |
|
273 | + * |
|
274 | + * @param WPInv_Invoice $invoice Invoice object. |
|
275 | + */ |
|
276 | + protected function prepare_line_items( $invoice ) { |
|
277 | + $this->delete_line_items(); |
|
278 | + |
|
279 | + // Items. |
|
280 | + foreach ( $invoice->get_items() as $item ) { |
|
281 | + $amount = $invoice->get_template() == 'amount' ? $item->get_price() : $item->get_sub_total(); |
|
282 | + $quantity = $invoice->get_template() == 'amount' ? 1 : $item->get_quantity(); |
|
283 | + $this->add_line_item( $item->get_raw_name(), $quantity, $amount, $item->get_id() ); |
|
284 | 284 | } |
285 | 285 | |
286 | 286 | // Fees. |
287 | - foreach ( $invoice->get_fees() as $fee => $data ) { |
|
287 | + foreach ( $invoice->get_fees() as $fee => $data ) { |
|
288 | 288 | $this->add_line_item( $fee, 1, wpinv_sanitize_amount( $data['initial_fee'] ) ); |
289 | 289 | } |
290 | 290 | |
291 | 291 | } |
292 | 292 | |
293 | 293 | /** |
294 | - * Add PayPal Line Item. |
|
295 | - * |
|
296 | - * @param string $item_name Item name. |
|
297 | - * @param int $quantity Item quantity. |
|
298 | - * @param float $amount Amount. |
|
299 | - * @param string $item_number Item number. |
|
300 | - */ |
|
301 | - protected function add_line_item( $item_name, $quantity = 1, $amount = 0.0, $item_number = '' ) { |
|
302 | - $index = ( count( $this->line_items ) / 4 ) + 1; |
|
303 | - |
|
304 | - $item = apply_filters( |
|
305 | - 'getpaid_paypal_line_item', |
|
306 | - array( |
|
307 | - 'item_name' => html_entity_decode( getpaid_limit_length( $item_name ? wp_strip_all_tags( $item_name ) : __( 'Item', 'invoicing' ), 127 ), ENT_NOQUOTES, 'UTF-8' ), |
|
308 | - 'quantity' => (int) $quantity, |
|
309 | - 'amount' => wpinv_sanitize_amount( (float) $amount, 2 ), |
|
310 | - 'item_number' => $item_number, |
|
311 | - ), |
|
312 | - $item_name, |
|
313 | - $quantity, |
|
314 | - $amount, |
|
315 | - $item_number |
|
316 | - ); |
|
317 | - |
|
318 | - $this->line_items[ 'item_name_' . $index ] = getpaid_limit_length( $item['item_name'], 127 ); |
|
294 | + * Add PayPal Line Item. |
|
295 | + * |
|
296 | + * @param string $item_name Item name. |
|
297 | + * @param int $quantity Item quantity. |
|
298 | + * @param float $amount Amount. |
|
299 | + * @param string $item_number Item number. |
|
300 | + */ |
|
301 | + protected function add_line_item( $item_name, $quantity = 1, $amount = 0.0, $item_number = '' ) { |
|
302 | + $index = ( count( $this->line_items ) / 4 ) + 1; |
|
303 | + |
|
304 | + $item = apply_filters( |
|
305 | + 'getpaid_paypal_line_item', |
|
306 | + array( |
|
307 | + 'item_name' => html_entity_decode( getpaid_limit_length( $item_name ? wp_strip_all_tags( $item_name ) : __( 'Item', 'invoicing' ), 127 ), ENT_NOQUOTES, 'UTF-8' ), |
|
308 | + 'quantity' => (int) $quantity, |
|
309 | + 'amount' => wpinv_sanitize_amount( (float) $amount, 2 ), |
|
310 | + 'item_number' => $item_number, |
|
311 | + ), |
|
312 | + $item_name, |
|
313 | + $quantity, |
|
314 | + $amount, |
|
315 | + $item_number |
|
316 | + ); |
|
317 | + |
|
318 | + $this->line_items[ 'item_name_' . $index ] = getpaid_limit_length( $item['item_name'], 127 ); |
|
319 | 319 | $this->line_items[ 'quantity_' . $index ] = $item['quantity']; |
320 | 320 | |
321 | 321 | // The price or amount of the product, service, or contribution, not including shipping, handling, or tax. |
322 | - $this->line_items[ 'amount_' . $index ] = $item['amount']; |
|
323 | - $this->line_items[ 'item_number_' . $index ] = getpaid_limit_length( $item['item_number'], 127 ); |
|
322 | + $this->line_items[ 'amount_' . $index ] = $item['amount']; |
|
323 | + $this->line_items[ 'item_number_' . $index ] = getpaid_limit_length( $item['item_number'], 127 ); |
|
324 | 324 | } |
325 | 325 | |
326 | 326 | /** |
327 | - * If the default request with line items is too long, generate a new one with only one line item. |
|
328 | - * |
|
329 | - * https://support.microsoft.com/en-us/help/208427/maximum-url-length-is-2-083-characters-in-internet-explorer. |
|
330 | - * |
|
331 | - * @param WPInv_Invoice $invoice Invoice to be sent to Paypal. |
|
332 | - * @param array $paypal_args Arguments sent to Paypal in the request. |
|
333 | - * @return array |
|
334 | - */ |
|
335 | - protected function fix_request_length( $invoice, $paypal_args ) { |
|
336 | - $max_paypal_length = 2083; |
|
337 | - $query_candidate = http_build_query( $paypal_args, '', '&' ); |
|
338 | - |
|
339 | - if ( strlen( $this->endpoint . $query_candidate ) <= $max_paypal_length ) { |
|
340 | - return $paypal_args; |
|
341 | - } |
|
342 | - |
|
343 | - return apply_filters( |
|
344 | - 'getpaid_paypal_args', |
|
345 | - array_merge( |
|
346 | - $this->get_transaction_args( $invoice ), |
|
347 | - $this->get_line_item_args( $invoice, true ) |
|
348 | - ), |
|
349 | - $invoice |
|
350 | - ); |
|
327 | + * If the default request with line items is too long, generate a new one with only one line item. |
|
328 | + * |
|
329 | + * https://support.microsoft.com/en-us/help/208427/maximum-url-length-is-2-083-characters-in-internet-explorer. |
|
330 | + * |
|
331 | + * @param WPInv_Invoice $invoice Invoice to be sent to Paypal. |
|
332 | + * @param array $paypal_args Arguments sent to Paypal in the request. |
|
333 | + * @return array |
|
334 | + */ |
|
335 | + protected function fix_request_length( $invoice, $paypal_args ) { |
|
336 | + $max_paypal_length = 2083; |
|
337 | + $query_candidate = http_build_query( $paypal_args, '', '&' ); |
|
338 | + |
|
339 | + if ( strlen( $this->endpoint . $query_candidate ) <= $max_paypal_length ) { |
|
340 | + return $paypal_args; |
|
341 | + } |
|
342 | + |
|
343 | + return apply_filters( |
|
344 | + 'getpaid_paypal_args', |
|
345 | + array_merge( |
|
346 | + $this->get_transaction_args( $invoice ), |
|
347 | + $this->get_line_item_args( $invoice, true ) |
|
348 | + ), |
|
349 | + $invoice |
|
350 | + ); |
|
351 | 351 | |
352 | 352 | } |
353 | 353 | |
354 | 354 | /** |
355 | - * Processes recurring invoices. |
|
356 | - * |
|
357 | - * @param array $paypal_args PayPal args. |
|
358 | - * @param WPInv_Invoice $invoice Invoice object. |
|
359 | - */ |
|
360 | - public function process_subscription( $paypal_args, $invoice ) { |
|
355 | + * Processes recurring invoices. |
|
356 | + * |
|
357 | + * @param array $paypal_args PayPal args. |
|
358 | + * @param WPInv_Invoice $invoice Invoice object. |
|
359 | + */ |
|
360 | + public function process_subscription( $paypal_args, $invoice ) { |
|
361 | 361 | |
362 | 362 | // Make sure this is a subscription. |
363 | 363 | if ( ! $invoice->is_recurring() || ! $subscription = wpinv_get_subscription( $invoice ) ) { |
@@ -382,11 +382,11 @@ discard block |
||
382 | 382 | |
383 | 383 | $paypal_args['a1'] = 0 == $initial_amount ? 0 : $initial_amount; |
384 | 384 | |
385 | - // Trial period length. |
|
386 | - $paypal_args['p1'] = $subscription_item->get_trial_interval(); |
|
385 | + // Trial period length. |
|
386 | + $paypal_args['p1'] = $subscription_item->get_trial_interval(); |
|
387 | 387 | |
388 | - // Trial period. |
|
389 | - $paypal_args['t1'] = $subscription_item->get_trial_period(); |
|
388 | + // Trial period. |
|
389 | + $paypal_args['t1'] = $subscription_item->get_trial_period(); |
|
390 | 390 | |
391 | 391 | } else if ( $initial_amount != $recurring_amount ) { |
392 | 392 | |
@@ -409,40 +409,40 @@ discard block |
||
409 | 409 | } |
410 | 410 | |
411 | 411 | // We have a recurring payment |
412 | - if ( ! isset( $param_number ) || 1 == $param_number ) { |
|
412 | + if ( ! isset( $param_number ) || 1 == $param_number ) { |
|
413 | 413 | |
414 | - // Subscription price |
|
415 | - $paypal_args['a3'] = $recurring_amount; |
|
414 | + // Subscription price |
|
415 | + $paypal_args['a3'] = $recurring_amount; |
|
416 | 416 | |
417 | - // Subscription duration |
|
418 | - $paypal_args['p3'] = $interval; |
|
417 | + // Subscription duration |
|
418 | + $paypal_args['p3'] = $interval; |
|
419 | 419 | |
420 | - // Subscription period |
|
421 | - $paypal_args['t3'] = $period; |
|
420 | + // Subscription period |
|
421 | + $paypal_args['t3'] = $period; |
|
422 | 422 | |
423 | 423 | } |
424 | 424 | |
425 | 425 | // Recurring payments |
426 | - if ( 1 == $bill_times || ( $initial_amount != $recurring_amount && ! $subscription_item->has_free_trial() && 2 == $bill_times ) ) { |
|
426 | + if ( 1 == $bill_times || ( $initial_amount != $recurring_amount && ! $subscription_item->has_free_trial() && 2 == $bill_times ) ) { |
|
427 | 427 | |
428 | - // Non-recurring payments |
|
429 | - $paypal_args['src'] = 0; |
|
428 | + // Non-recurring payments |
|
429 | + $paypal_args['src'] = 0; |
|
430 | 430 | |
431 | - } else { |
|
431 | + } else { |
|
432 | 432 | |
433 | - $paypal_args['src'] = 1; |
|
433 | + $paypal_args['src'] = 1; |
|
434 | 434 | |
435 | - if ( $bill_times > 0 ) { |
|
435 | + if ( $bill_times > 0 ) { |
|
436 | 436 | |
437 | - // An initial period is being used to charge a sign-up fee |
|
438 | - if ( $initial_amount != $recurring_amount && ! $subscription_item->has_free_trial() ) { |
|
439 | - $bill_times--; |
|
440 | - } |
|
437 | + // An initial period is being used to charge a sign-up fee |
|
438 | + if ( $initial_amount != $recurring_amount && ! $subscription_item->has_free_trial() ) { |
|
439 | + $bill_times--; |
|
440 | + } |
|
441 | 441 | |
442 | 442 | // Make sure it's not over the max of 52 |
443 | 443 | $paypal_args['srt'] = ( $bill_times <= 52 ? absint( $bill_times ) : 52 ); |
444 | 444 | |
445 | - } |
|
445 | + } |
|
446 | 446 | } |
447 | 447 | |
448 | 448 | // Force return URL so that order description & instructions display |
@@ -458,24 +458,24 @@ discard block |
||
458 | 458 | } |
459 | 459 | |
460 | 460 | return apply_filters( |
461 | - 'getpaid_paypal_subscription_args', |
|
462 | - $paypal_args, |
|
463 | - $invoice |
|
461 | + 'getpaid_paypal_subscription_args', |
|
462 | + $paypal_args, |
|
463 | + $invoice |
|
464 | 464 | ); |
465 | 465 | |
466 | 466 | } |
467 | 467 | |
468 | 468 | /** |
469 | - * Processes ipns and marks payments as complete. |
|
470 | - * |
|
471 | - * @return void |
|
472 | - */ |
|
473 | - public function verify_ipn() { |
|
469 | + * Processes ipns and marks payments as complete. |
|
470 | + * |
|
471 | + * @return void |
|
472 | + */ |
|
473 | + public function verify_ipn() { |
|
474 | 474 | |
475 | 475 | // Validate the IPN. |
476 | 476 | if ( empty( $_POST ) || ! $this->validate_ipn() ) { |
477 | - wp_die( 'PayPal IPN Request Failure', 'PayPal IPN', array( 'response' => 500 ) ); |
|
478 | - } |
|
477 | + wp_die( 'PayPal IPN Request Failure', 'PayPal IPN', array( 'response' => 500 ) ); |
|
478 | + } |
|
479 | 479 | |
480 | 480 | // Process the IPN. |
481 | 481 | $posted = wp_unslash( $_POST ); |
@@ -489,8 +489,8 @@ discard block |
||
489 | 489 | wpinv_error_log( 'Found invoice #' . $invoice->get_number() ); |
490 | 490 | wpinv_error_log( 'Payment status:' . $posted['payment_status'] ); |
491 | 491 | |
492 | - if ( method_exists( $this, 'ipn_txn_' . $posted['txn_type'] ) ) { |
|
493 | - call_user_func( array( $this, 'ipn_txn_' . $posted['txn_type'] ), $invoice, $posted ); |
|
492 | + if ( method_exists( $this, 'ipn_txn_' . $posted['txn_type'] ) ) { |
|
493 | + call_user_func( array( $this, 'ipn_txn_' . $posted['txn_type'] ), $invoice, $posted ); |
|
494 | 494 | } else { |
495 | 495 | wpinv_error_log( 'Aborting, Invalid type:' . $posted['txn_type'] ); |
496 | 496 | } |
@@ -502,33 +502,33 @@ discard block |
||
502 | 502 | } |
503 | 503 | |
504 | 504 | /** |
505 | - * Check PayPal IPN validity. |
|
506 | - */ |
|
507 | - public function validate_ipn() { |
|
508 | - |
|
509 | - wpinv_error_log( 'Validating PayPal IPN response' ); |
|
510 | - |
|
511 | - // Get received values from post data. |
|
512 | - $validate_ipn = wp_unslash( $_POST ); |
|
513 | - $validate_ipn['cmd'] = '_notify-validate'; |
|
514 | - |
|
515 | - // Send back post vars to paypal. |
|
516 | - $params = array( |
|
517 | - 'body' => $validate_ipn, |
|
518 | - 'timeout' => 60, |
|
519 | - 'httpversion' => '1.1', |
|
520 | - 'compress' => false, |
|
521 | - 'decompress' => false, |
|
522 | - 'user-agent' => 'GetPaid/' . WPINV_VERSION, |
|
523 | - ); |
|
505 | + * Check PayPal IPN validity. |
|
506 | + */ |
|
507 | + public function validate_ipn() { |
|
508 | + |
|
509 | + wpinv_error_log( 'Validating PayPal IPN response' ); |
|
510 | + |
|
511 | + // Get received values from post data. |
|
512 | + $validate_ipn = wp_unslash( $_POST ); |
|
513 | + $validate_ipn['cmd'] = '_notify-validate'; |
|
514 | + |
|
515 | + // Send back post vars to paypal. |
|
516 | + $params = array( |
|
517 | + 'body' => $validate_ipn, |
|
518 | + 'timeout' => 60, |
|
519 | + 'httpversion' => '1.1', |
|
520 | + 'compress' => false, |
|
521 | + 'decompress' => false, |
|
522 | + 'user-agent' => 'GetPaid/' . WPINV_VERSION, |
|
523 | + ); |
|
524 | 524 | |
525 | - // Post back to get a response. |
|
526 | - $response = wp_safe_remote_post( $this->is_sandbox() ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr', $params ); |
|
525 | + // Post back to get a response. |
|
526 | + $response = wp_safe_remote_post( $this->is_sandbox() ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr', $params ); |
|
527 | 527 | |
528 | 528 | // Check to see if the request was valid. |
529 | - if ( ! is_wp_error( $response ) && $response['response']['code'] >= 200 && $response['response']['code'] < 300 && strstr( $response['body'], 'VERIFIED' ) ) { |
|
529 | + if ( ! is_wp_error( $response ) && $response['response']['code'] >= 200 && $response['response']['code'] < 300 && strstr( $response['body'], 'VERIFIED' ) ) { |
|
530 | 530 | wpinv_error_log( $response['body'], 'Received valid response from PayPal IPN' ); |
531 | - return true; |
|
531 | + return true; |
|
532 | 532 | } |
533 | 533 | |
534 | 534 | if ( is_wp_error( $response ) ) { |
@@ -542,63 +542,63 @@ discard block |
||
542 | 542 | } |
543 | 543 | |
544 | 544 | /** |
545 | - * Check currency from IPN matches the invoice. |
|
546 | - * |
|
547 | - * @param WPInv_Invoice $invoice Invoice object. |
|
548 | - * @param string $currency currency to validate. |
|
549 | - */ |
|
550 | - protected function validate_ipn_currency( $invoice, $currency ) { |
|
551 | - if ( strtolower( $invoice->get_currency() ) !== strtolower( $currency ) ) { |
|
545 | + * Check currency from IPN matches the invoice. |
|
546 | + * |
|
547 | + * @param WPInv_Invoice $invoice Invoice object. |
|
548 | + * @param string $currency currency to validate. |
|
549 | + */ |
|
550 | + protected function validate_ipn_currency( $invoice, $currency ) { |
|
551 | + if ( strtolower( $invoice->get_currency() ) !== strtolower( $currency ) ) { |
|
552 | 552 | wpinv_record_gateway_error( 'IPN Error', "Currencies do not match: {$currency} instead of {$invoice->get_currency()}" ); |
553 | 553 | |
554 | - /* translators: %s: currency code. */ |
|
555 | - $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal currencies do not match (code %s).', 'invoicing' ), $currency ) ); |
|
556 | - exit; |
|
557 | - } |
|
558 | - } |
|
559 | - |
|
560 | - /** |
|
561 | - * Check payment amount from IPN matches the invoice. |
|
562 | - * |
|
563 | - * @param WPInv_Invoice $invoice Invoice object. |
|
564 | - * @param float $amount amount to validate. |
|
565 | - */ |
|
566 | - protected function validate_ipn_amount( $invoice, $amount ) { |
|
567 | - if ( number_format( $invoice->get_total(), 2, '.', '' ) !== number_format( $amount, 2, '.', '' ) ) { |
|
554 | + /* translators: %s: currency code. */ |
|
555 | + $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal currencies do not match (code %s).', 'invoicing' ), $currency ) ); |
|
556 | + exit; |
|
557 | + } |
|
558 | + } |
|
559 | + |
|
560 | + /** |
|
561 | + * Check payment amount from IPN matches the invoice. |
|
562 | + * |
|
563 | + * @param WPInv_Invoice $invoice Invoice object. |
|
564 | + * @param float $amount amount to validate. |
|
565 | + */ |
|
566 | + protected function validate_ipn_amount( $invoice, $amount ) { |
|
567 | + if ( number_format( $invoice->get_total(), 2, '.', '' ) !== number_format( $amount, 2, '.', '' ) ) { |
|
568 | 568 | wpinv_record_gateway_error( 'IPN Error', "Amounts do not match: {$amount} instead of {$invoice->get_total()}" ); |
569 | 569 | |
570 | - /* translators: %s: Amount. */ |
|
571 | - $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal amounts do not match (gross %s).', 'invoicing' ), $amount ) ); |
|
572 | - exit; |
|
573 | - } |
|
574 | - } |
|
575 | - |
|
576 | - /** |
|
577 | - * Verify receiver email from PayPal. |
|
578 | - * |
|
579 | - * @param WPInv_Invoice $invoice Invoice object. |
|
580 | - * @param string $receiver_email Email to validate. |
|
581 | - */ |
|
582 | - protected function validate_ipn_receiver_email( $invoice, $receiver_email ) { |
|
570 | + /* translators: %s: Amount. */ |
|
571 | + $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal amounts do not match (gross %s).', 'invoicing' ), $amount ) ); |
|
572 | + exit; |
|
573 | + } |
|
574 | + } |
|
575 | + |
|
576 | + /** |
|
577 | + * Verify receiver email from PayPal. |
|
578 | + * |
|
579 | + * @param WPInv_Invoice $invoice Invoice object. |
|
580 | + * @param string $receiver_email Email to validate. |
|
581 | + */ |
|
582 | + protected function validate_ipn_receiver_email( $invoice, $receiver_email ) { |
|
583 | 583 | $paypal_email = wpinv_get_option( 'paypal_email' ); |
584 | 584 | |
585 | - if ( strcasecmp( trim( $receiver_email ), trim( $paypal_email ) ) !== 0 ) { |
|
585 | + if ( strcasecmp( trim( $receiver_email ), trim( $paypal_email ) ) !== 0 ) { |
|
586 | 586 | wpinv_record_gateway_error( 'IPN Error', "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}" ); |
587 | 587 | |
588 | - /* translators: %s: email address . */ |
|
589 | - $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal IPN response from a different email address (%s).', 'invoicing' ), $receiver_email ) ); |
|
590 | - exit; |
|
588 | + /* translators: %s: email address . */ |
|
589 | + $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal IPN response from a different email address (%s).', 'invoicing' ), $receiver_email ) ); |
|
590 | + exit; |
|
591 | 591 | } |
592 | 592 | |
593 | - } |
|
593 | + } |
|
594 | 594 | |
595 | 595 | /** |
596 | - * Handles one time payments. |
|
597 | - * |
|
598 | - * @param WPInv_Invoice $invoice Invoice object. |
|
599 | - * @param array $posted Posted data. |
|
600 | - */ |
|
601 | - protected function ipn_txn_web_accept( $invoice, $posted ) { |
|
596 | + * Handles one time payments. |
|
597 | + * |
|
598 | + * @param WPInv_Invoice $invoice Invoice object. |
|
599 | + * @param array $posted Posted data. |
|
600 | + */ |
|
601 | + protected function ipn_txn_web_accept( $invoice, $posted ) { |
|
602 | 602 | |
603 | 603 | // Collect payment details |
604 | 604 | $payment_status = strtolower( $posted['payment_status'] ); |
@@ -659,27 +659,27 @@ discard block |
||
659 | 659 | // Process failures. |
660 | 660 | |
661 | 661 | /* translators: %s: payment status. */ |
662 | - $invoice->update_status( 'wpi-failed', sprintf( __( 'Payment %s via IPN.', 'invoicing' ), wpinv_clean( $posted['payment_status'] ) ) ); |
|
663 | - |
|
664 | - } |
|
665 | - |
|
666 | - /** |
|
667 | - * Handles one time payments. |
|
668 | - * |
|
669 | - * @param WPInv_Invoice $invoice Invoice object. |
|
670 | - * @param array $posted Posted data. |
|
671 | - */ |
|
672 | - protected function ipn_txn_cart( $invoice, $posted ) { |
|
673 | - $this->ipn_txn_web_accept( $invoice, $posted ); |
|
662 | + $invoice->update_status( 'wpi-failed', sprintf( __( 'Payment %s via IPN.', 'invoicing' ), wpinv_clean( $posted['payment_status'] ) ) ); |
|
663 | + |
|
664 | + } |
|
665 | + |
|
666 | + /** |
|
667 | + * Handles one time payments. |
|
668 | + * |
|
669 | + * @param WPInv_Invoice $invoice Invoice object. |
|
670 | + * @param array $posted Posted data. |
|
671 | + */ |
|
672 | + protected function ipn_txn_cart( $invoice, $posted ) { |
|
673 | + $this->ipn_txn_web_accept( $invoice, $posted ); |
|
674 | 674 | } |
675 | 675 | |
676 | 676 | /** |
677 | - * Handles subscription sign ups. |
|
678 | - * |
|
679 | - * @param WPInv_Invoice $invoice Invoice object. |
|
680 | - * @param array $posted Posted data. |
|
681 | - */ |
|
682 | - protected function ipn_txn_subscr_signup( $invoice, $posted ) { |
|
677 | + * Handles subscription sign ups. |
|
678 | + * |
|
679 | + * @param WPInv_Invoice $invoice Invoice object. |
|
680 | + * @param array $posted Posted data. |
|
681 | + */ |
|
682 | + protected function ipn_txn_subscr_signup( $invoice, $posted ) { |
|
683 | 683 | |
684 | 684 | // Make sure the invoice has a subscription. |
685 | 685 | $subscription = wpinv_get_subscription( $invoice ); |
@@ -718,12 +718,12 @@ discard block |
||
718 | 718 | } |
719 | 719 | |
720 | 720 | /** |
721 | - * Handles subscription renewals. |
|
722 | - * |
|
723 | - * @param WPInv_Invoice $invoice Invoice object. |
|
724 | - * @param array $posted Posted data. |
|
725 | - */ |
|
726 | - protected function ipn_txn_subscr_payment( $invoice, $posted ) { |
|
721 | + * Handles subscription renewals. |
|
722 | + * |
|
723 | + * @param WPInv_Invoice $invoice Invoice object. |
|
724 | + * @param array $posted Posted data. |
|
725 | + */ |
|
726 | + protected function ipn_txn_subscr_payment( $invoice, $posted ) { |
|
727 | 727 | |
728 | 728 | // Make sure the invoice has a subscription. |
729 | 729 | $subscription = wpinv_get_subscription( $invoice ); |
@@ -766,12 +766,12 @@ discard block |
||
766 | 766 | } |
767 | 767 | |
768 | 768 | /** |
769 | - * Handles subscription cancelations. |
|
770 | - * |
|
771 | - * @param WPInv_Invoice $invoice Invoice object. |
|
772 | - * @param array $posted Posted data. |
|
773 | - */ |
|
774 | - protected function ipn_txn_subscr_cancel( $invoice, $posted ) { |
|
769 | + * Handles subscription cancelations. |
|
770 | + * |
|
771 | + * @param WPInv_Invoice $invoice Invoice object. |
|
772 | + * @param array $posted Posted data. |
|
773 | + */ |
|
774 | + protected function ipn_txn_subscr_cancel( $invoice, $posted ) { |
|
775 | 775 | |
776 | 776 | if ( $subscription = wpinv_get_subscription( $invoice ) ) { |
777 | 777 | $subscription->cancel(); |
@@ -780,12 +780,12 @@ discard block |
||
780 | 780 | } |
781 | 781 | |
782 | 782 | /** |
783 | - * Handles subscription completions. |
|
784 | - * |
|
785 | - * @param WPInv_Invoice $invoice Invoice object. |
|
786 | - * @param array $posted Posted data. |
|
787 | - */ |
|
788 | - protected function ipn_txn_subscr_eot( $invoice, $posted ) { |
|
783 | + * Handles subscription completions. |
|
784 | + * |
|
785 | + * @param WPInv_Invoice $invoice Invoice object. |
|
786 | + * @param array $posted Posted data. |
|
787 | + */ |
|
788 | + protected function ipn_txn_subscr_eot( $invoice, $posted ) { |
|
789 | 789 | |
790 | 790 | if ( $subscription = wpinv_get_subscription( $invoice ) ) { |
791 | 791 | $subscription->complete(); |
@@ -794,12 +794,12 @@ discard block |
||
794 | 794 | } |
795 | 795 | |
796 | 796 | /** |
797 | - * Handles subscription fails. |
|
798 | - * |
|
799 | - * @param WPInv_Invoice $invoice Invoice object. |
|
800 | - * @param array $posted Posted data. |
|
801 | - */ |
|
802 | - protected function ipn_txn_subscr_failed( $invoice, $posted ) { |
|
797 | + * Handles subscription fails. |
|
798 | + * |
|
799 | + * @param WPInv_Invoice $invoice Invoice object. |
|
800 | + * @param array $posted Posted data. |
|
801 | + */ |
|
802 | + protected function ipn_txn_subscr_failed( $invoice, $posted ) { |
|
803 | 803 | |
804 | 804 | if ( $subscription = wpinv_get_subscription( $invoice ) ) { |
805 | 805 | $subscription->failing(); |