@@ -50,7 +50,7 @@ discard block |
||
| 50 | 50 | */ |
| 51 | 51 | function getpaid_get_invoice_subscription_group( $invoice_id, $subscription_id ) { |
| 52 | 52 | $subscription_groups = getpaid_get_invoice_subscription_groups( $invoice_id ); |
| 53 | - $matching_group = wp_list_filter( $subscription_groups, compact( 'subscription_id' ) ); |
|
| 53 | + $matching_group = wp_list_filter( $subscription_groups, compact( 'subscription_id' ) ); |
|
| 54 | 54 | return reset( $matching_group ); |
| 55 | 55 | } |
| 56 | 56 | |
@@ -63,11 +63,11 @@ discard block |
||
| 63 | 63 | */ |
| 64 | 64 | function getpaid_get_subscription( $subscription ) { |
| 65 | 65 | |
| 66 | - if ( ! is_a( $subscription, 'WPInv_Subscription' ) ) { |
|
| 67 | - $subscription = new WPInv_Subscription( $subscription ); |
|
| 68 | - } |
|
| 66 | + if ( ! is_a( $subscription, 'WPInv_Subscription' ) ) { |
|
| 67 | + $subscription = new WPInv_Subscription( $subscription ); |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - return $subscription->exists() ? $subscription : false; |
|
| 70 | + return $subscription->exists() ? $subscription : false; |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | /** |
@@ -81,28 +81,28 @@ discard block |
||
| 81 | 81 | */ |
| 82 | 82 | function getpaid_get_subscriptions( $args = array(), $return = 'results' ) { |
| 83 | 83 | |
| 84 | - // Do not retrieve all fields if we just want the count. |
|
| 85 | - if ( 'count' == $return ) { |
|
| 86 | - $args['fields'] = 'id'; |
|
| 87 | - $args['number'] = 1; |
|
| 88 | - } |
|
| 84 | + // Do not retrieve all fields if we just want the count. |
|
| 85 | + if ( 'count' == $return ) { |
|
| 86 | + $args['fields'] = 'id'; |
|
| 87 | + $args['number'] = 1; |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - // Do not count all matches if we just want the results. |
|
| 91 | - if ( 'results' == $return ) { |
|
| 92 | - $args['count_total'] = false; |
|
| 93 | - } |
|
| 90 | + // Do not count all matches if we just want the results. |
|
| 91 | + if ( 'results' == $return ) { |
|
| 92 | + $args['count_total'] = false; |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - $query = new GetPaid_Subscriptions_Query( $args ); |
|
| 95 | + $query = new GetPaid_Subscriptions_Query( $args ); |
|
| 96 | 96 | |
| 97 | - if ( 'results' == $return ) { |
|
| 98 | - return $query->get_results(); |
|
| 99 | - } |
|
| 97 | + if ( 'results' == $return ) { |
|
| 98 | + return $query->get_results(); |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - if ( 'count' == $return ) { |
|
| 102 | - return $query->get_total(); |
|
| 103 | - } |
|
| 101 | + if ( 'count' == $return ) { |
|
| 102 | + return $query->get_total(); |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | - return $query; |
|
| 105 | + return $query; |
|
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | /** |
@@ -112,18 +112,18 @@ discard block |
||
| 112 | 112 | */ |
| 113 | 113 | function getpaid_get_subscription_statuses() { |
| 114 | 114 | |
| 115 | - return apply_filters( |
|
| 116 | - 'getpaid_get_subscription_statuses', |
|
| 117 | - array( |
|
| 118 | - 'pending' => __( 'Pending', 'invoicing' ), |
|
| 119 | - 'trialling' => __( 'Trialing', 'invoicing' ), |
|
| 120 | - 'active' => __( 'Active', 'invoicing' ), |
|
| 121 | - 'failing' => __( 'Failing', 'invoicing' ), |
|
| 122 | - 'expired' => __( 'Expired', 'invoicing' ), |
|
| 123 | - 'completed' => __( 'Complete', 'invoicing' ), |
|
| 124 | - 'cancelled' => __( 'Cancelled', 'invoicing' ), |
|
| 125 | - ) |
|
| 126 | - ); |
|
| 115 | + return apply_filters( |
|
| 116 | + 'getpaid_get_subscription_statuses', |
|
| 117 | + array( |
|
| 118 | + 'pending' => __( 'Pending', 'invoicing' ), |
|
| 119 | + 'trialling' => __( 'Trialing', 'invoicing' ), |
|
| 120 | + 'active' => __( 'Active', 'invoicing' ), |
|
| 121 | + 'failing' => __( 'Failing', 'invoicing' ), |
|
| 122 | + 'expired' => __( 'Expired', 'invoicing' ), |
|
| 123 | + 'completed' => __( 'Complete', 'invoicing' ), |
|
| 124 | + 'cancelled' => __( 'Cancelled', 'invoicing' ), |
|
| 125 | + ) |
|
| 126 | + ); |
|
| 127 | 127 | |
| 128 | 128 | } |
| 129 | 129 | |
@@ -133,8 +133,8 @@ discard block |
||
| 133 | 133 | * @return string |
| 134 | 134 | */ |
| 135 | 135 | function getpaid_get_subscription_status_label( $status ) { |
| 136 | - $statuses = getpaid_get_subscription_statuses(); |
|
| 137 | - return isset( $statuses[ $status ] ) ? $statuses[ $status ] : ucfirst( sanitize_text_field( $status ) ); |
|
| 136 | + $statuses = getpaid_get_subscription_statuses(); |
|
| 137 | + return isset( $statuses[ $status ] ) ? $statuses[ $status ] : ucfirst( sanitize_text_field( $status ) ); |
|
| 138 | 138 | } |
| 139 | 139 | |
| 140 | 140 | /** |
@@ -144,18 +144,18 @@ discard block |
||
| 144 | 144 | */ |
| 145 | 145 | function getpaid_get_subscription_status_classes() { |
| 146 | 146 | |
| 147 | - return apply_filters( |
|
| 148 | - 'getpaid_get_subscription_status_classes', |
|
| 149 | - array( |
|
| 150 | - 'pending' => 'badge-dark', |
|
| 151 | - 'trialling' => 'badge-info', |
|
| 152 | - 'active' => 'badge-success', |
|
| 153 | - 'failing' => 'badge-warning', |
|
| 154 | - 'expired' => 'badge-danger', |
|
| 155 | - 'completed' => 'badge-primary', |
|
| 156 | - 'cancelled' => 'badge-secondary', |
|
| 157 | - ) |
|
| 158 | - ); |
|
| 147 | + return apply_filters( |
|
| 148 | + 'getpaid_get_subscription_status_classes', |
|
| 149 | + array( |
|
| 150 | + 'pending' => 'badge-dark', |
|
| 151 | + 'trialling' => 'badge-info', |
|
| 152 | + 'active' => 'badge-success', |
|
| 153 | + 'failing' => 'badge-warning', |
|
| 154 | + 'expired' => 'badge-danger', |
|
| 155 | + 'completed' => 'badge-primary', |
|
| 156 | + 'cancelled' => 'badge-secondary', |
|
| 157 | + ) |
|
| 158 | + ); |
|
| 159 | 159 | |
| 160 | 160 | } |
| 161 | 161 | |
@@ -166,15 +166,15 @@ discard block |
||
| 166 | 166 | */ |
| 167 | 167 | function getpaid_get_subscription_status_counts( $args = array() ) { |
| 168 | 168 | |
| 169 | - $statuses = array_keys( getpaid_get_subscription_statuses() ); |
|
| 170 | - $counts = array(); |
|
| 169 | + $statuses = array_keys( getpaid_get_subscription_statuses() ); |
|
| 170 | + $counts = array(); |
|
| 171 | 171 | |
| 172 | - foreach ( $statuses as $status ) { |
|
| 173 | - $_args = wp_parse_args( "status=$status", $args ); |
|
| 174 | - $counts[ $status ] = getpaid_get_subscriptions( $_args, 'count' ); |
|
| 175 | - } |
|
| 172 | + foreach ( $statuses as $status ) { |
|
| 173 | + $_args = wp_parse_args( "status=$status", $args ); |
|
| 174 | + $counts[ $status ] = getpaid_get_subscriptions( $_args, 'count' ); |
|
| 175 | + } |
|
| 176 | 176 | |
| 177 | - return $counts; |
|
| 177 | + return $counts; |
|
| 178 | 178 | |
| 179 | 179 | } |
| 180 | 180 | |
@@ -185,32 +185,32 @@ discard block |
||
| 185 | 185 | */ |
| 186 | 186 | function getpaid_get_subscription_periods() { |
| 187 | 187 | |
| 188 | - return apply_filters( |
|
| 189 | - 'getpaid_get_subscription_periods', |
|
| 190 | - array( |
|
| 188 | + return apply_filters( |
|
| 189 | + 'getpaid_get_subscription_periods', |
|
| 190 | + array( |
|
| 191 | 191 | |
| 192 | - 'day' => array( |
|
| 193 | - 'singular' => __( '%s day', 'invoicing' ), |
|
| 194 | - 'plural' => __( '%d days', 'invoicing' ), |
|
| 195 | - ), |
|
| 192 | + 'day' => array( |
|
| 193 | + 'singular' => __( '%s day', 'invoicing' ), |
|
| 194 | + 'plural' => __( '%d days', 'invoicing' ), |
|
| 195 | + ), |
|
| 196 | 196 | |
| 197 | - 'week' => array( |
|
| 198 | - 'singular' => __( '%s week', 'invoicing' ), |
|
| 199 | - 'plural' => __( '%d weeks', 'invoicing' ), |
|
| 200 | - ), |
|
| 197 | + 'week' => array( |
|
| 198 | + 'singular' => __( '%s week', 'invoicing' ), |
|
| 199 | + 'plural' => __( '%d weeks', 'invoicing' ), |
|
| 200 | + ), |
|
| 201 | 201 | |
| 202 | - 'month' => array( |
|
| 203 | - 'singular' => __( '%s month', 'invoicing' ), |
|
| 204 | - 'plural' => __( '%d months', 'invoicing' ), |
|
| 205 | - ), |
|
| 202 | + 'month' => array( |
|
| 203 | + 'singular' => __( '%s month', 'invoicing' ), |
|
| 204 | + 'plural' => __( '%d months', 'invoicing' ), |
|
| 205 | + ), |
|
| 206 | 206 | |
| 207 | - 'year' => array( |
|
| 208 | - 'singular' => __( '%s year', 'invoicing' ), |
|
| 209 | - 'plural' => __( '%d years', 'invoicing' ), |
|
| 210 | - ), |
|
| 207 | + 'year' => array( |
|
| 208 | + 'singular' => __( '%s year', 'invoicing' ), |
|
| 209 | + 'plural' => __( '%d years', 'invoicing' ), |
|
| 210 | + ), |
|
| 211 | 211 | |
| 212 | - ) |
|
| 213 | - ); |
|
| 212 | + ) |
|
| 213 | + ); |
|
| 214 | 214 | |
| 215 | 215 | } |
| 216 | 216 | |
@@ -221,7 +221,7 @@ discard block |
||
| 221 | 221 | * @return int |
| 222 | 222 | */ |
| 223 | 223 | function getpaid_get_subscription_trial_period_interval( $trial_period ) { |
| 224 | - return (int) preg_replace( '/[^0-9]/', '', $trial_period ); |
|
| 224 | + return (int) preg_replace( '/[^0-9]/', '', $trial_period ); |
|
| 225 | 225 | } |
| 226 | 226 | |
| 227 | 227 | /** |
@@ -231,7 +231,7 @@ discard block |
||
| 231 | 231 | * @return string |
| 232 | 232 | */ |
| 233 | 233 | function getpaid_get_subscription_trial_period_period( $trial_period ) { |
| 234 | - return preg_replace( '/[^a-z]/', '', strtolower( $trial_period ) ); |
|
| 234 | + return preg_replace( '/[^a-z]/', '', strtolower( $trial_period ) ); |
|
| 235 | 235 | } |
| 236 | 236 | |
| 237 | 237 | /** |
@@ -242,8 +242,8 @@ discard block |
||
| 242 | 242 | * @return string |
| 243 | 243 | */ |
| 244 | 244 | function getpaid_get_subscription_period_label( $period, $interval = 1, $singular_prefix = '1' ) { |
| 245 | - $label = (int) $interval > 1 ? getpaid_get_plural_subscription_period_label( $period, $interval ) : getpaid_get_singular_subscription_period_label( $period, $singular_prefix ); |
|
| 246 | - return strtolower( sanitize_text_field( $label ) ); |
|
| 245 | + $label = (int) $interval > 1 ? getpaid_get_plural_subscription_period_label( $period, $interval ) : getpaid_get_singular_subscription_period_label( $period, $singular_prefix ); |
|
| 246 | + return strtolower( sanitize_text_field( $label ) ); |
|
| 247 | 247 | } |
| 248 | 248 | |
| 249 | 249 | /** |
@@ -254,22 +254,22 @@ discard block |
||
| 254 | 254 | */ |
| 255 | 255 | function getpaid_get_singular_subscription_period_label( $period, $singular_prefix = '1' ) { |
| 256 | 256 | |
| 257 | - $periods = getpaid_get_subscription_periods(); |
|
| 258 | - $period = strtolower( $period ); |
|
| 257 | + $periods = getpaid_get_subscription_periods(); |
|
| 258 | + $period = strtolower( $period ); |
|
| 259 | 259 | |
| 260 | - if ( isset( $periods[ $period ] ) ) { |
|
| 261 | - return sprintf( $periods[ $period ]['singular'], $singular_prefix ); |
|
| 262 | - } |
|
| 260 | + if ( isset( $periods[ $period ] ) ) { |
|
| 261 | + return sprintf( $periods[ $period ]['singular'], $singular_prefix ); |
|
| 262 | + } |
|
| 263 | 263 | |
| 264 | - // Backwards compatibility. |
|
| 265 | - foreach ( $periods as $key => $data ) { |
|
| 266 | - if ( strpos( $key, $period ) === 0 ) { |
|
| 267 | - return sprintf( $data['singular'], $singular_prefix ); |
|
| 268 | - } |
|
| 269 | - } |
|
| 264 | + // Backwards compatibility. |
|
| 265 | + foreach ( $periods as $key => $data ) { |
|
| 266 | + if ( strpos( $key, $period ) === 0 ) { |
|
| 267 | + return sprintf( $data['singular'], $singular_prefix ); |
|
| 268 | + } |
|
| 269 | + } |
|
| 270 | 270 | |
| 271 | - // Invalid string. |
|
| 272 | - return ''; |
|
| 271 | + // Invalid string. |
|
| 272 | + return ''; |
|
| 273 | 273 | } |
| 274 | 274 | |
| 275 | 275 | /** |
@@ -281,22 +281,22 @@ discard block |
||
| 281 | 281 | */ |
| 282 | 282 | function getpaid_get_plural_subscription_period_label( $period, $interval ) { |
| 283 | 283 | |
| 284 | - $periods = getpaid_get_subscription_periods(); |
|
| 285 | - $period = strtolower( $period ); |
|
| 284 | + $periods = getpaid_get_subscription_periods(); |
|
| 285 | + $period = strtolower( $period ); |
|
| 286 | 286 | |
| 287 | - if ( isset( $periods[ $period ] ) ) { |
|
| 288 | - return sprintf( $periods[ $period ]['plural'], $interval ); |
|
| 289 | - } |
|
| 287 | + if ( isset( $periods[ $period ] ) ) { |
|
| 288 | + return sprintf( $periods[ $period ]['plural'], $interval ); |
|
| 289 | + } |
|
| 290 | 290 | |
| 291 | - // Backwards compatibility. |
|
| 292 | - foreach ( $periods as $key => $data ) { |
|
| 293 | - if ( strpos( $key, $period ) === 0 ) { |
|
| 294 | - return sprintf( $data['plural'], $interval ); |
|
| 295 | - } |
|
| 296 | - } |
|
| 291 | + // Backwards compatibility. |
|
| 292 | + foreach ( $periods as $key => $data ) { |
|
| 293 | + if ( strpos( $key, $period ) === 0 ) { |
|
| 294 | + return sprintf( $data['plural'], $interval ); |
|
| 295 | + } |
|
| 296 | + } |
|
| 297 | 297 | |
| 298 | - // Invalid string. |
|
| 299 | - return ''; |
|
| 298 | + // Invalid string. |
|
| 299 | + return ''; |
|
| 300 | 300 | } |
| 301 | 301 | |
| 302 | 302 | /** |
@@ -307,101 +307,101 @@ discard block |
||
| 307 | 307 | */ |
| 308 | 308 | function getpaid_get_formatted_subscription_amount( $subscription ) { |
| 309 | 309 | |
| 310 | - $initial = wpinv_price( $subscription->get_initial_amount(), $subscription->get_parent_payment()->get_currency() ); |
|
| 311 | - $recurring = wpinv_price( $subscription->get_recurring_amount(), $subscription->get_parent_payment()->get_currency() ); |
|
| 312 | - $period = getpaid_get_subscription_period_label( $subscription->get_period(), $subscription->get_frequency(), '' ); |
|
| 313 | - $bill_times = $subscription->get_bill_times(); |
|
| 310 | + $initial = wpinv_price( $subscription->get_initial_amount(), $subscription->get_parent_payment()->get_currency() ); |
|
| 311 | + $recurring = wpinv_price( $subscription->get_recurring_amount(), $subscription->get_parent_payment()->get_currency() ); |
|
| 312 | + $period = getpaid_get_subscription_period_label( $subscription->get_period(), $subscription->get_frequency(), '' ); |
|
| 313 | + $bill_times = $subscription->get_bill_times(); |
|
| 314 | 314 | |
| 315 | - if ( ! empty( $bill_times ) ) { |
|
| 316 | - $bill_times = $subscription->get_frequency() * $bill_times; |
|
| 317 | - $bill_times = getpaid_get_subscription_period_label( $subscription->get_period(), $bill_times ); |
|
| 318 | - } |
|
| 315 | + if ( ! empty( $bill_times ) ) { |
|
| 316 | + $bill_times = $subscription->get_frequency() * $bill_times; |
|
| 317 | + $bill_times = getpaid_get_subscription_period_label( $subscription->get_period(), $bill_times ); |
|
| 318 | + } |
|
| 319 | 319 | |
| 320 | - // Trial periods. |
|
| 321 | - if ( $subscription->has_trial_period() ) { |
|
| 320 | + // Trial periods. |
|
| 321 | + if ( $subscription->has_trial_period() ) { |
|
| 322 | 322 | |
| 323 | - $trial_period = getpaid_get_subscription_trial_period_period( $subscription->get_trial_period() ); |
|
| 324 | - $trial_interval = getpaid_get_subscription_trial_period_interval( $subscription->get_trial_period() ); |
|
| 323 | + $trial_period = getpaid_get_subscription_trial_period_period( $subscription->get_trial_period() ); |
|
| 324 | + $trial_interval = getpaid_get_subscription_trial_period_interval( $subscription->get_trial_period() ); |
|
| 325 | 325 | |
| 326 | - if ( empty( $bill_times ) ) { |
|
| 326 | + if ( empty( $bill_times ) ) { |
|
| 327 | 327 | |
| 328 | - return sprintf( |
|
| 328 | + return sprintf( |
|
| 329 | 329 | |
| 330 | - // translators: $1: is the initial amount, $2: is the trial period, $3: is the recurring amount, $4: is the recurring period |
|
| 331 | - _x( '%1$s trial for %2$s then %3$s / %4$s', 'Subscription amount. (e.g.: $10 trial for 1 month then $120 / year)', 'invoicing' ), |
|
| 332 | - $initial, |
|
| 333 | - getpaid_get_subscription_period_label( $trial_period, $trial_interval ), |
|
| 334 | - $recurring, |
|
| 335 | - $period |
|
| 330 | + // translators: $1: is the initial amount, $2: is the trial period, $3: is the recurring amount, $4: is the recurring period |
|
| 331 | + _x( '%1$s trial for %2$s then %3$s / %4$s', 'Subscription amount. (e.g.: $10 trial for 1 month then $120 / year)', 'invoicing' ), |
|
| 332 | + $initial, |
|
| 333 | + getpaid_get_subscription_period_label( $trial_period, $trial_interval ), |
|
| 334 | + $recurring, |
|
| 335 | + $period |
|
| 336 | 336 | |
| 337 | - ); |
|
| 337 | + ); |
|
| 338 | 338 | |
| 339 | - } |
|
| 339 | + } |
|
| 340 | 340 | |
| 341 | - return sprintf( |
|
| 341 | + return sprintf( |
|
| 342 | 342 | |
| 343 | - // translators: $1: is the initial amount, $2: is the trial period, $3: is the recurring amount, $4: is the recurring period, $5: is the bill times |
|
| 344 | - _x( '%1$s trial for %2$s then %3$s / %4$s for %5$s', 'Subscription amount. (e.g.: $10 trial for 1 month then $120 / year for 4 years)', 'invoicing' ), |
|
| 345 | - $initial, |
|
| 346 | - getpaid_get_subscription_period_label( $trial_period, $trial_interval ), |
|
| 347 | - $recurring, |
|
| 348 | - $period, |
|
| 349 | - $bill_times |
|
| 350 | - ); |
|
| 343 | + // translators: $1: is the initial amount, $2: is the trial period, $3: is the recurring amount, $4: is the recurring period, $5: is the bill times |
|
| 344 | + _x( '%1$s trial for %2$s then %3$s / %4$s for %5$s', 'Subscription amount. (e.g.: $10 trial for 1 month then $120 / year for 4 years)', 'invoicing' ), |
|
| 345 | + $initial, |
|
| 346 | + getpaid_get_subscription_period_label( $trial_period, $trial_interval ), |
|
| 347 | + $recurring, |
|
| 348 | + $period, |
|
| 349 | + $bill_times |
|
| 350 | + ); |
|
| 351 | 351 | |
| 352 | - } |
|
| 352 | + } |
|
| 353 | 353 | |
| 354 | - if ( $initial != $recurring ) { |
|
| 354 | + if ( $initial != $recurring ) { |
|
| 355 | 355 | |
| 356 | - if ( empty( $bill_times ) ) { |
|
| 356 | + if ( empty( $bill_times ) ) { |
|
| 357 | 357 | |
| 358 | - return sprintf( |
|
| 358 | + return sprintf( |
|
| 359 | 359 | |
| 360 | - // translators: $1: is the initial amount, $2: is the recurring amount, $3: is the recurring period |
|
| 361 | - _x( 'Initial payment of %1$s which renews at %2$s / %3$s', 'Subscription amount. (e.g.:Initial payment of $100 which renews at $120 / year)', 'invoicing' ), |
|
| 362 | - $initial, |
|
| 363 | - $recurring, |
|
| 364 | - $period |
|
| 360 | + // translators: $1: is the initial amount, $2: is the recurring amount, $3: is the recurring period |
|
| 361 | + _x( 'Initial payment of %1$s which renews at %2$s / %3$s', 'Subscription amount. (e.g.:Initial payment of $100 which renews at $120 / year)', 'invoicing' ), |
|
| 362 | + $initial, |
|
| 363 | + $recurring, |
|
| 364 | + $period |
|
| 365 | 365 | |
| 366 | - ); |
|
| 366 | + ); |
|
| 367 | 367 | |
| 368 | - } |
|
| 368 | + } |
|
| 369 | 369 | |
| 370 | - return sprintf( |
|
| 370 | + return sprintf( |
|
| 371 | 371 | |
| 372 | - // translators: $1: is the initial amount, $2: is the recurring amount, $3: is the recurring period, $4: is the bill times |
|
| 373 | - _x( 'Initial payment of %1$s which renews at %2$s / %3$s for %4$s', 'Subscription amount. (e.g.:Initial payment of $100 which renews at $120 / year for 5 years)', 'invoicing' ), |
|
| 374 | - $initial, |
|
| 375 | - $recurring, |
|
| 376 | - $period, |
|
| 377 | - $bill_times |
|
| 372 | + // translators: $1: is the initial amount, $2: is the recurring amount, $3: is the recurring period, $4: is the bill times |
|
| 373 | + _x( 'Initial payment of %1$s which renews at %2$s / %3$s for %4$s', 'Subscription amount. (e.g.:Initial payment of $100 which renews at $120 / year for 5 years)', 'invoicing' ), |
|
| 374 | + $initial, |
|
| 375 | + $recurring, |
|
| 376 | + $period, |
|
| 377 | + $bill_times |
|
| 378 | 378 | |
| 379 | - ); |
|
| 379 | + ); |
|
| 380 | 380 | |
| 381 | - } |
|
| 381 | + } |
|
| 382 | 382 | |
| 383 | - if ( empty( $bill_times ) ) { |
|
| 383 | + if ( empty( $bill_times ) ) { |
|
| 384 | 384 | |
| 385 | - return sprintf( |
|
| 385 | + return sprintf( |
|
| 386 | 386 | |
| 387 | - // translators: $1: is the recurring amount, $2: is the recurring period |
|
| 388 | - _x( '%1$s / %2$s', 'Subscription amount. (e.g.: $120 / year)', 'invoicing' ), |
|
| 389 | - $initial, |
|
| 390 | - $period |
|
| 387 | + // translators: $1: is the recurring amount, $2: is the recurring period |
|
| 388 | + _x( '%1$s / %2$s', 'Subscription amount. (e.g.: $120 / year)', 'invoicing' ), |
|
| 389 | + $initial, |
|
| 390 | + $period |
|
| 391 | 391 | |
| 392 | - ); |
|
| 392 | + ); |
|
| 393 | 393 | |
| 394 | - } |
|
| 394 | + } |
|
| 395 | 395 | |
| 396 | - return sprintf( |
|
| 396 | + return sprintf( |
|
| 397 | 397 | |
| 398 | - // translators: $1: is the bill times, $2: is the recurring amount, $3: is the recurring period |
|
| 399 | - _x( '%2$s / %3$s for %1$s', 'Subscription amount. (e.g.: $120 / year for 5 years)', 'invoicing' ), |
|
| 400 | - $bill_times, |
|
| 401 | - $initial, |
|
| 402 | - $period |
|
| 398 | + // translators: $1: is the bill times, $2: is the recurring amount, $3: is the recurring period |
|
| 399 | + _x( '%2$s / %3$s for %1$s', 'Subscription amount. (e.g.: $120 / year for 5 years)', 'invoicing' ), |
|
| 400 | + $bill_times, |
|
| 401 | + $initial, |
|
| 402 | + $period |
|
| 403 | 403 | |
| 404 | - ); |
|
| 404 | + ); |
|
| 405 | 405 | |
| 406 | 406 | } |
| 407 | 407 | |
@@ -412,7 +412,7 @@ discard block |
||
| 412 | 412 | * @return WPInv_Subscription|false |
| 413 | 413 | */ |
| 414 | 414 | function getpaid_get_invoice_subscription( $invoice ) { |
| 415 | - return getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
| 415 | + return getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
| 416 | 416 | } |
| 417 | 417 | |
| 418 | 418 | /** |
@@ -421,10 +421,10 @@ discard block |
||
| 421 | 421 | * @param WPInv_Invoice $invoice |
| 422 | 422 | */ |
| 423 | 423 | function getpaid_activate_invoice_subscription( $invoice ) { |
| 424 | - $subscription = getpaid_get_invoice_subscription( $invoice ); |
|
| 425 | - if ( is_a( $subscription, 'WPInv_Subscription' ) ) { |
|
| 426 | - $subscription->activate(); |
|
| 427 | - } |
|
| 424 | + $subscription = getpaid_get_invoice_subscription( $invoice ); |
|
| 425 | + if ( is_a( $subscription, 'WPInv_Subscription' ) ) { |
|
| 426 | + $subscription->activate(); |
|
| 427 | + } |
|
| 428 | 428 | } |
| 429 | 429 | |
| 430 | 430 | /** |
@@ -433,7 +433,7 @@ discard block |
||
| 433 | 433 | * @return WPInv_Subscriptions |
| 434 | 434 | */ |
| 435 | 435 | function getpaid_subscriptions() { |
| 436 | - return getpaid()->get( 'subscriptions' ); |
|
| 436 | + return getpaid()->get( 'subscriptions' ); |
|
| 437 | 437 | } |
| 438 | 438 | |
| 439 | 439 | /** |
@@ -452,15 +452,15 @@ discard block |
||
| 452 | 452 | return false; |
| 453 | 453 | } |
| 454 | 454 | |
| 455 | - // Fetch the invoice subscription. |
|
| 456 | - $subscription = getpaid_get_subscriptions( |
|
| 457 | - array( |
|
| 458 | - 'invoice_in' => $invoice->is_renewal() ? $invoice->get_parent_id() : $invoice->get_id(), |
|
| 459 | - 'number' => 1, |
|
| 460 | - ) |
|
| 461 | - ); |
|
| 455 | + // Fetch the invoice subscription. |
|
| 456 | + $subscription = getpaid_get_subscriptions( |
|
| 457 | + array( |
|
| 458 | + 'invoice_in' => $invoice->is_renewal() ? $invoice->get_parent_id() : $invoice->get_id(), |
|
| 459 | + 'number' => 1, |
|
| 460 | + ) |
|
| 461 | + ); |
|
| 462 | 462 | |
| 463 | - return empty( $subscription ) ? false : $subscription[0]; |
|
| 463 | + return empty( $subscription ) ? false : $subscription[0]; |
|
| 464 | 464 | |
| 465 | 465 | } |
| 466 | 466 | |
@@ -477,48 +477,48 @@ discard block |
||
| 477 | 477 | */ |
| 478 | 478 | function getpaid_get_recurring_item_key( $cart_item ) { |
| 479 | 479 | |
| 480 | - $cart_key = 'renews_'; |
|
| 481 | - $interval = $cart_item->get_recurring_interval(); |
|
| 482 | - $period = $cart_item->get_recurring_period( true ); |
|
| 483 | - $length = $cart_item->get_recurring_limit() * $interval; |
|
| 484 | - $trial_period = $cart_item->get_trial_period( true ); |
|
| 485 | - $trial_length = $cart_item->get_trial_interval(); |
|
| 486 | - |
|
| 487 | - // First start with the billing interval and period |
|
| 488 | - switch ( $interval ) { |
|
| 489 | - case 1 : |
|
| 490 | - if ( 'day' == $period ) { |
|
| 491 | - $cart_key .= 'daily'; |
|
| 492 | - } else { |
|
| 493 | - $cart_key .= sprintf( '%sly', $period ); |
|
| 494 | - } |
|
| 495 | - break; |
|
| 496 | - case 2 : |
|
| 497 | - $cart_key .= sprintf( 'every_2nd_%s', $period ); |
|
| 498 | - break; |
|
| 499 | - case 3 : |
|
| 500 | - $cart_key .= sprintf( 'every_3rd_%s', $period ); |
|
| 501 | - break; |
|
| 502 | - default: |
|
| 503 | - $cart_key .= sprintf( 'every_%dth_%s', $interval, $period ); |
|
| 504 | - break; |
|
| 505 | - } |
|
| 506 | - |
|
| 507 | - // Maybe add the optional maximum billing periods... |
|
| 508 | - if ( $length > 0 ) { |
|
| 509 | - $cart_key .= '_for_'; |
|
| 510 | - $cart_key .= sprintf( '%d_%s', $length, $period ); |
|
| 511 | - if ( $length > 1 ) { |
|
| 512 | - $cart_key .= 's'; |
|
| 513 | - } |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - // And an optional free trial. |
|
| 517 | - if ( $cart_item->has_free_trial() ) { |
|
| 518 | - $cart_key .= sprintf( '_after_a_%d_%s_trial', $trial_length, $trial_period ); |
|
| 519 | - } |
|
| 520 | - |
|
| 521 | - return apply_filters( 'getpaid_get_recurring_item_key', $cart_key, $cart_item ); |
|
| 480 | + $cart_key = 'renews_'; |
|
| 481 | + $interval = $cart_item->get_recurring_interval(); |
|
| 482 | + $period = $cart_item->get_recurring_period( true ); |
|
| 483 | + $length = $cart_item->get_recurring_limit() * $interval; |
|
| 484 | + $trial_period = $cart_item->get_trial_period( true ); |
|
| 485 | + $trial_length = $cart_item->get_trial_interval(); |
|
| 486 | + |
|
| 487 | + // First start with the billing interval and period |
|
| 488 | + switch ( $interval ) { |
|
| 489 | + case 1 : |
|
| 490 | + if ( 'day' == $period ) { |
|
| 491 | + $cart_key .= 'daily'; |
|
| 492 | + } else { |
|
| 493 | + $cart_key .= sprintf( '%sly', $period ); |
|
| 494 | + } |
|
| 495 | + break; |
|
| 496 | + case 2 : |
|
| 497 | + $cart_key .= sprintf( 'every_2nd_%s', $period ); |
|
| 498 | + break; |
|
| 499 | + case 3 : |
|
| 500 | + $cart_key .= sprintf( 'every_3rd_%s', $period ); |
|
| 501 | + break; |
|
| 502 | + default: |
|
| 503 | + $cart_key .= sprintf( 'every_%dth_%s', $interval, $period ); |
|
| 504 | + break; |
|
| 505 | + } |
|
| 506 | + |
|
| 507 | + // Maybe add the optional maximum billing periods... |
|
| 508 | + if ( $length > 0 ) { |
|
| 509 | + $cart_key .= '_for_'; |
|
| 510 | + $cart_key .= sprintf( '%d_%s', $length, $period ); |
|
| 511 | + if ( $length > 1 ) { |
|
| 512 | + $cart_key .= 's'; |
|
| 513 | + } |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + // And an optional free trial. |
|
| 517 | + if ( $cart_item->has_free_trial() ) { |
|
| 518 | + $cart_key .= sprintf( '_after_a_%d_%s_trial', $trial_length, $trial_period ); |
|
| 519 | + } |
|
| 520 | + |
|
| 521 | + return apply_filters( 'getpaid_get_recurring_item_key', $cart_key, $cart_item ); |
|
| 522 | 522 | } |
| 523 | 523 | |
| 524 | 524 | /** |
@@ -529,17 +529,17 @@ discard block |
||
| 529 | 529 | */ |
| 530 | 530 | function getpaid_get_subscription_groups( $invoice ) { |
| 531 | 531 | |
| 532 | - // Generate subscription groups. |
|
| 533 | - $subscription_groups = array(); |
|
| 534 | - foreach ( $invoice->get_items() as $item ) { |
|
| 532 | + // Generate subscription groups. |
|
| 533 | + $subscription_groups = array(); |
|
| 534 | + foreach ( $invoice->get_items() as $item ) { |
|
| 535 | 535 | |
| 536 | - if ( $item->is_recurring() ) { |
|
| 537 | - $subscription_groups[ getpaid_get_recurring_item_key( $item ) ][] = $item; |
|
| 538 | - } |
|
| 536 | + if ( $item->is_recurring() ) { |
|
| 537 | + $subscription_groups[ getpaid_get_recurring_item_key( $item ) ][] = $item; |
|
| 538 | + } |
|
| 539 | 539 | |
| 540 | - } |
|
| 540 | + } |
|
| 541 | 541 | |
| 542 | - return $subscription_groups; |
|
| 542 | + return $subscription_groups; |
|
| 543 | 543 | } |
| 544 | 544 | |
| 545 | 545 | /** |
@@ -553,57 +553,57 @@ discard block |
||
| 553 | 553 | */ |
| 554 | 554 | function getpaid_calculate_subscription_totals( $invoice ) { |
| 555 | 555 | |
| 556 | - // Generate subscription groups. |
|
| 557 | - $subscription_groups = getpaid_get_subscription_groups( $invoice ); |
|
| 556 | + // Generate subscription groups. |
|
| 557 | + $subscription_groups = getpaid_get_subscription_groups( $invoice ); |
|
| 558 | 558 | |
| 559 | - // Now let's calculate the totals for each group of subscriptions |
|
| 560 | - $subscription_totals = array(); |
|
| 559 | + // Now let's calculate the totals for each group of subscriptions |
|
| 560 | + $subscription_totals = array(); |
|
| 561 | 561 | |
| 562 | - foreach ( $subscription_groups as $subscription_key => $items ) { |
|
| 562 | + foreach ( $subscription_groups as $subscription_key => $items ) { |
|
| 563 | 563 | |
| 564 | - if ( empty( $subscription_totals[ $subscription_key ] ) ) { |
|
| 564 | + if ( empty( $subscription_totals[ $subscription_key ] ) ) { |
|
| 565 | 565 | |
| 566 | - $subscription_totals[ $subscription_key ] = array( |
|
| 567 | - 'initial_total' => 0, |
|
| 568 | - 'recurring_total' => 0, |
|
| 569 | - 'items' => array(), |
|
| 570 | - 'trialling' => false, |
|
| 571 | - ); |
|
| 566 | + $subscription_totals[ $subscription_key ] = array( |
|
| 567 | + 'initial_total' => 0, |
|
| 568 | + 'recurring_total' => 0, |
|
| 569 | + 'items' => array(), |
|
| 570 | + 'trialling' => false, |
|
| 571 | + ); |
|
| 572 | 572 | |
| 573 | - } |
|
| 573 | + } |
|
| 574 | 574 | |
| 575 | - /** |
|
| 576 | - * Get the totals of the group. |
|
| 577 | - * @var GetPaid_Form_Item $item |
|
| 578 | - */ |
|
| 579 | - foreach ( $items as $item ) { |
|
| 575 | + /** |
|
| 576 | + * Get the totals of the group. |
|
| 577 | + * @var GetPaid_Form_Item $item |
|
| 578 | + */ |
|
| 579 | + foreach ( $items as $item ) { |
|
| 580 | 580 | |
| 581 | - $subscription_totals[ $subscription_key ]['items'][$item->get_id()] = $item->prepare_data_for_saving(); |
|
| 582 | - $subscription_totals[ $subscription_key ]['item_id'] = $item->get_id(); |
|
| 583 | - $subscription_totals[ $subscription_key ]['period'] = $item->get_recurring_period( true ); |
|
| 584 | - $subscription_totals[ $subscription_key ]['interval'] = $item->get_recurring_interval(); |
|
| 585 | - $subscription_totals[ $subscription_key ]['initial_total'] += $item->get_sub_total(); |
|
| 586 | - $subscription_totals[ $subscription_key ]['recurring_total'] += $item->get_recurring_sub_total(); |
|
| 587 | - $subscription_totals[ $subscription_key ]['recurring_limit'] = $item->get_recurring_limit(); |
|
| 581 | + $subscription_totals[ $subscription_key ]['items'][$item->get_id()] = $item->prepare_data_for_saving(); |
|
| 582 | + $subscription_totals[ $subscription_key ]['item_id'] = $item->get_id(); |
|
| 583 | + $subscription_totals[ $subscription_key ]['period'] = $item->get_recurring_period( true ); |
|
| 584 | + $subscription_totals[ $subscription_key ]['interval'] = $item->get_recurring_interval(); |
|
| 585 | + $subscription_totals[ $subscription_key ]['initial_total'] += $item->get_sub_total(); |
|
| 586 | + $subscription_totals[ $subscription_key ]['recurring_total'] += $item->get_recurring_sub_total(); |
|
| 587 | + $subscription_totals[ $subscription_key ]['recurring_limit'] = $item->get_recurring_limit(); |
|
| 588 | 588 | |
| 589 | - // Calculate the next renewal date. |
|
| 590 | - $period = $item->get_recurring_period( true ); |
|
| 591 | - $interval = $item->get_recurring_interval(); |
|
| 589 | + // Calculate the next renewal date. |
|
| 590 | + $period = $item->get_recurring_period( true ); |
|
| 591 | + $interval = $item->get_recurring_interval(); |
|
| 592 | 592 | |
| 593 | - // If the subscription item has a trial period... |
|
| 594 | - if ( $item->has_free_trial() ) { |
|
| 595 | - $period = $item->get_trial_period( true ); |
|
| 596 | - $interval = $item->get_trial_interval(); |
|
| 597 | - $subscription_totals[ $subscription_key ]['trialling'] = $interval . ' ' . $period; |
|
| 598 | - } |
|
| 593 | + // If the subscription item has a trial period... |
|
| 594 | + if ( $item->has_free_trial() ) { |
|
| 595 | + $period = $item->get_trial_period( true ); |
|
| 596 | + $interval = $item->get_trial_interval(); |
|
| 597 | + $subscription_totals[ $subscription_key ]['trialling'] = $interval . ' ' . $period; |
|
| 598 | + } |
|
| 599 | 599 | |
| 600 | - $subscription_totals[ $subscription_key ]['renews_on'] = date( 'Y-m-d H:i:s', strtotime( "+$interval $period", current_time( 'timestamp' ) ) ); |
|
| 600 | + $subscription_totals[ $subscription_key ]['renews_on'] = date( 'Y-m-d H:i:s', strtotime( "+$interval $period", current_time( 'timestamp' ) ) ); |
|
| 601 | 601 | |
| 602 | - } |
|
| 602 | + } |
|
| 603 | 603 | |
| 604 | - } |
|
| 604 | + } |
|
| 605 | 605 | |
| 606 | - return apply_filters( 'getpaid_calculate_subscription_totals', $subscription_totals, $invoice ); |
|
| 606 | + return apply_filters( 'getpaid_calculate_subscription_totals', $subscription_totals, $invoice ); |
|
| 607 | 607 | } |
| 608 | 608 | |
| 609 | 609 | /** |
@@ -614,17 +614,17 @@ discard block |
||
| 614 | 614 | */ |
| 615 | 615 | function getpaid_should_group_subscriptions( $invoice ) { |
| 616 | 616 | |
| 617 | - $recurring_items = 0; |
|
| 617 | + $recurring_items = 0; |
|
| 618 | 618 | |
| 619 | - foreach ( $invoice->get_items() as $item ) { |
|
| 619 | + foreach ( $invoice->get_items() as $item ) { |
|
| 620 | 620 | |
| 621 | - if ( $item->is_recurring() ) { |
|
| 622 | - $recurring_items ++; |
|
| 623 | - } |
|
| 621 | + if ( $item->is_recurring() ) { |
|
| 622 | + $recurring_items ++; |
|
| 623 | + } |
|
| 624 | 624 | |
| 625 | - } |
|
| 625 | + } |
|
| 626 | 626 | |
| 627 | - return apply_filters( 'getpaid_should_group_subscriptions', $recurring_items > 1, $invoice ); |
|
| 627 | + return apply_filters( 'getpaid_should_group_subscriptions', $recurring_items > 1, $invoice ); |
|
| 628 | 628 | } |
| 629 | 629 | |
| 630 | 630 | /** |
@@ -634,14 +634,14 @@ discard block |
||
| 634 | 634 | * @return int |
| 635 | 635 | */ |
| 636 | 636 | function getpaid_count_subscription_invoices( $parent_invoice_id ) { |
| 637 | - global $wpdb; |
|
| 638 | - |
|
| 639 | - return (int) $wpdb->get_var( |
|
| 640 | - $wpdb->prepare( |
|
| 641 | - "SELECT COUNT(ID) FROM $wpdb->posts WHERE ( post_parent=%d OR ID=%d ) AND post_status IN ( 'publish', 'wpi-processing', 'wpi-renewal' )", |
|
| 642 | - $parent_invoice_id, |
|
| 643 | - $parent_invoice_id |
|
| 644 | - ) |
|
| 645 | - ); |
|
| 637 | + global $wpdb; |
|
| 638 | + |
|
| 639 | + return (int) $wpdb->get_var( |
|
| 640 | + $wpdb->prepare( |
|
| 641 | + "SELECT COUNT(ID) FROM $wpdb->posts WHERE ( post_parent=%d OR ID=%d ) AND post_status IN ( 'publish', 'wpi-processing', 'wpi-renewal' )", |
|
| 642 | + $parent_invoice_id, |
|
| 643 | + $parent_invoice_id |
|
| 644 | + ) |
|
| 645 | + ); |
|
| 646 | 646 | |
| 647 | 647 | } |
@@ -12,472 +12,472 @@ |
||
| 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->add_note( sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $posted['txn_id'] ), false, false, true ); |
|
| 324 | - $invoice->set_transaction_id( $posted['txn_id'] ); |
|
| 325 | - } |
|
| 321 | + // Set the transaction id. |
|
| 322 | + if ( ! empty( $posted['txn_id'] ) ) { |
|
| 323 | + $invoice->add_note( sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $posted['txn_id'] ), false, false, true ); |
|
| 324 | + $invoice->set_transaction_id( $posted['txn_id'] ); |
|
| 325 | + } |
|
| 326 | 326 | |
| 327 | - // Update the payment status. |
|
| 328 | - $invoice->mark_paid(); |
|
| 327 | + // Update the payment status. |
|
| 328 | + $invoice->mark_paid(); |
|
| 329 | 329 | |
| 330 | - $invoice->add_note( sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true ); |
|
| 330 | + $invoice->add_note( sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true ); |
|
| 331 | 331 | |
| 332 | - wpinv_error_log( 'Subscription started.', false ); |
|
| 333 | - } |
|
| 332 | + wpinv_error_log( 'Subscription started.', false ); |
|
| 333 | + } |
|
| 334 | 334 | |
| 335 | - /** |
|
| 336 | - * Handles subscription renewals. |
|
| 337 | - * |
|
| 338 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 339 | - * @param array $posted Posted data. |
|
| 340 | - */ |
|
| 341 | - protected function ipn_txn_subscr_payment( $invoice, $posted ) { |
|
| 335 | + /** |
|
| 336 | + * Handles subscription renewals. |
|
| 337 | + * |
|
| 338 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 339 | + * @param array $posted Posted data. |
|
| 340 | + */ |
|
| 341 | + protected function ipn_txn_subscr_payment( $invoice, $posted ) { |
|
| 342 | 342 | |
| 343 | - // Make sure the invoice has a subscription. |
|
| 344 | - $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
| 343 | + // Make sure the invoice has a subscription. |
|
| 344 | + $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
| 345 | 345 | |
| 346 | - if ( empty( $subscription ) ) { |
|
| 347 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 348 | - } |
|
| 346 | + if ( empty( $subscription ) ) { |
|
| 347 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 348 | + } |
|
| 349 | 349 | |
| 350 | - wpinv_error_log( 'Found subscription #' . $subscription->get_id(), false ); |
|
| 350 | + wpinv_error_log( 'Found subscription #' . $subscription->get_id(), false ); |
|
| 351 | 351 | |
| 352 | - // PayPal sends a subscr_payment for the first payment too. |
|
| 353 | - $date_completed = getpaid_format_date( $invoice->get_date_completed() ); |
|
| 354 | - $date_created = getpaid_format_date( $invoice->get_date_created() ); |
|
| 355 | - $today_date = getpaid_format_date( current_time( 'mysql' ) ); |
|
| 356 | - $payment_date = getpaid_format_date( $posted['payment_date'] ); |
|
| 357 | - $subscribe_date = getpaid_format_date( $subscription->get_date_created() ); |
|
| 358 | - $dates = array_filter( compact( 'date_completed', 'date_created', 'subscribe_date' ) ); |
|
| 352 | + // PayPal sends a subscr_payment for the first payment too. |
|
| 353 | + $date_completed = getpaid_format_date( $invoice->get_date_completed() ); |
|
| 354 | + $date_created = getpaid_format_date( $invoice->get_date_created() ); |
|
| 355 | + $today_date = getpaid_format_date( current_time( 'mysql' ) ); |
|
| 356 | + $payment_date = getpaid_format_date( $posted['payment_date'] ); |
|
| 357 | + $subscribe_date = getpaid_format_date( $subscription->get_date_created() ); |
|
| 358 | + $dates = array_filter( compact( 'date_completed', 'date_created', 'subscribe_date' ) ); |
|
| 359 | 359 | |
| 360 | - foreach( $dates as $date ) { |
|
| 360 | + foreach( $dates as $date ) { |
|
| 361 | 361 | |
| 362 | - if ( $date !== $today_date && $date !== $payment_date ) { |
|
| 363 | - continue; |
|
| 364 | - } |
|
| 362 | + if ( $date !== $today_date && $date !== $payment_date ) { |
|
| 363 | + continue; |
|
| 364 | + } |
|
| 365 | 365 | |
| 366 | - if ( ! empty( $posted['txn_id'] ) ) { |
|
| 367 | - $invoice->set_transaction_id( sanitize_text_field( $posted['txn_id'] ) ); |
|
| 368 | - $invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , sanitize_text_field( $posted['txn_id'] ) ), false, false, true ); |
|
| 369 | - } |
|
| 366 | + if ( ! empty( $posted['txn_id'] ) ) { |
|
| 367 | + $invoice->set_transaction_id( sanitize_text_field( $posted['txn_id'] ) ); |
|
| 368 | + $invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , sanitize_text_field( $posted['txn_id'] ) ), false, false, true ); |
|
| 369 | + } |
|
| 370 | 370 | |
| 371 | - return $invoice->mark_paid(); |
|
| 372 | - |
|
| 373 | - } |
|
| 371 | + return $invoice->mark_paid(); |
|
| 372 | + |
|
| 373 | + } |
|
| 374 | 374 | |
| 375 | - wpinv_error_log( 'Processing subscription renewal payment for the invoice ' . $invoice->get_id(), false ); |
|
| 376 | - |
|
| 377 | - // Abort if the payment is already recorded. |
|
| 378 | - if ( wpinv_get_id_by_transaction_id( $posted['txn_id'] ) ) { |
|
| 379 | - return wpinv_error_log( 'Aborting, Transaction ' . $posted['txn_id'] .' has already been processed', false ); |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - $args = array( |
|
| 383 | - 'transaction_id' => $posted['txn_id'], |
|
| 384 | - 'gateway' => $this->id, |
|
| 385 | - ); |
|
| 386 | - |
|
| 387 | - $invoice = wpinv_get_invoice( $subscription->add_payment( $args ) ); |
|
| 375 | + wpinv_error_log( 'Processing subscription renewal payment for the invoice ' . $invoice->get_id(), false ); |
|
| 376 | + |
|
| 377 | + // Abort if the payment is already recorded. |
|
| 378 | + if ( wpinv_get_id_by_transaction_id( $posted['txn_id'] ) ) { |
|
| 379 | + return wpinv_error_log( 'Aborting, Transaction ' . $posted['txn_id'] .' has already been processed', false ); |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + $args = array( |
|
| 383 | + 'transaction_id' => $posted['txn_id'], |
|
| 384 | + 'gateway' => $this->id, |
|
| 385 | + ); |
|
| 386 | + |
|
| 387 | + $invoice = wpinv_get_invoice( $subscription->add_payment( $args ) ); |
|
| 388 | 388 | |
| 389 | - if ( empty( $invoice ) ) { |
|
| 390 | - return; |
|
| 391 | - } |
|
| 389 | + if ( empty( $invoice ) ) { |
|
| 390 | + return; |
|
| 391 | + } |
|
| 392 | 392 | |
| 393 | - $invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $posted['txn_id'] ), false, false, true ); |
|
| 394 | - $invoice->add_note( wp_sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true ); |
|
| 393 | + $invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $posted['txn_id'] ), false, false, true ); |
|
| 394 | + $invoice->add_note( wp_sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true ); |
|
| 395 | 395 | |
| 396 | - $subscription->renew(); |
|
| 397 | - wpinv_error_log( 'Subscription renewed.', false ); |
|
| 396 | + $subscription->renew(); |
|
| 397 | + wpinv_error_log( 'Subscription renewed.', false ); |
|
| 398 | 398 | |
| 399 | - } |
|
| 399 | + } |
|
| 400 | 400 | |
| 401 | - /** |
|
| 402 | - * Handles subscription cancelations. |
|
| 403 | - * |
|
| 404 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 405 | - */ |
|
| 406 | - protected function ipn_txn_subscr_cancel( $invoice ) { |
|
| 401 | + /** |
|
| 402 | + * Handles subscription cancelations. |
|
| 403 | + * |
|
| 404 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 405 | + */ |
|
| 406 | + protected function ipn_txn_subscr_cancel( $invoice ) { |
|
| 407 | 407 | |
| 408 | - // Make sure the invoice has a subscription. |
|
| 409 | - $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
| 410 | - |
|
| 411 | - if ( empty( $subscription ) ) { |
|
| 412 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false); |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - wpinv_error_log( 'Processing subscription cancellation for the invoice ' . $invoice->get_id(), false ); |
|
| 416 | - $subscription->cancel(); |
|
| 417 | - wpinv_error_log( 'Subscription cancelled.', false ); |
|
| 408 | + // Make sure the invoice has a subscription. |
|
| 409 | + $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
| 410 | + |
|
| 411 | + if ( empty( $subscription ) ) { |
|
| 412 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false); |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + wpinv_error_log( 'Processing subscription cancellation for the invoice ' . $invoice->get_id(), false ); |
|
| 416 | + $subscription->cancel(); |
|
| 417 | + wpinv_error_log( 'Subscription cancelled.', false ); |
|
| 418 | 418 | |
| 419 | - } |
|
| 419 | + } |
|
| 420 | 420 | |
| 421 | - /** |
|
| 422 | - * Handles subscription completions. |
|
| 423 | - * |
|
| 424 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 425 | - * @param array $posted Posted data. |
|
| 426 | - */ |
|
| 427 | - protected function ipn_txn_subscr_eot( $invoice ) { |
|
| 421 | + /** |
|
| 422 | + * Handles subscription completions. |
|
| 423 | + * |
|
| 424 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 425 | + * @param array $posted Posted data. |
|
| 426 | + */ |
|
| 427 | + protected function ipn_txn_subscr_eot( $invoice ) { |
|
| 428 | 428 | |
| 429 | - // Make sure the invoice has a subscription. |
|
| 430 | - $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
| 429 | + // Make sure the invoice has a subscription. |
|
| 430 | + $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
| 431 | 431 | |
| 432 | - if ( empty( $subscription ) ) { |
|
| 433 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 434 | - } |
|
| 432 | + if ( empty( $subscription ) ) { |
|
| 433 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 434 | + } |
|
| 435 | 435 | |
| 436 | - wpinv_error_log( 'Processing subscription end of life for the invoice ' . $invoice->get_id(), false ); |
|
| 437 | - $subscription->complete(); |
|
| 438 | - wpinv_error_log( 'Subscription completed.', false ); |
|
| 436 | + wpinv_error_log( 'Processing subscription end of life for the invoice ' . $invoice->get_id(), false ); |
|
| 437 | + $subscription->complete(); |
|
| 438 | + wpinv_error_log( 'Subscription completed.', false ); |
|
| 439 | 439 | |
| 440 | - } |
|
| 440 | + } |
|
| 441 | 441 | |
| 442 | - /** |
|
| 443 | - * Handles subscription fails. |
|
| 444 | - * |
|
| 445 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 446 | - * @param array $posted Posted data. |
|
| 447 | - */ |
|
| 448 | - protected function ipn_txn_subscr_failed( $invoice ) { |
|
| 442 | + /** |
|
| 443 | + * Handles subscription fails. |
|
| 444 | + * |
|
| 445 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 446 | + * @param array $posted Posted data. |
|
| 447 | + */ |
|
| 448 | + protected function ipn_txn_subscr_failed( $invoice ) { |
|
| 449 | 449 | |
| 450 | - // Make sure the invoice has a subscription. |
|
| 451 | - $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
| 450 | + // Make sure the invoice has a subscription. |
|
| 451 | + $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
| 452 | 452 | |
| 453 | - if ( empty( $subscription ) ) { |
|
| 454 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 455 | - } |
|
| 453 | + if ( empty( $subscription ) ) { |
|
| 454 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 455 | + } |
|
| 456 | 456 | |
| 457 | - wpinv_error_log( 'Processing subscription payment failure for the invoice ' . $invoice->get_id(), false ); |
|
| 458 | - $subscription->failing(); |
|
| 459 | - wpinv_error_log( 'Subscription marked as failing.', false ); |
|
| 457 | + wpinv_error_log( 'Processing subscription payment failure for the invoice ' . $invoice->get_id(), false ); |
|
| 458 | + $subscription->failing(); |
|
| 459 | + wpinv_error_log( 'Subscription marked as failing.', false ); |
|
| 460 | 460 | |
| 461 | - } |
|
| 461 | + } |
|
| 462 | 462 | |
| 463 | - /** |
|
| 464 | - * Handles subscription suspensions. |
|
| 465 | - * |
|
| 466 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 467 | - * @param array $posted Posted data. |
|
| 468 | - */ |
|
| 469 | - protected function ipn_txn_recurring_payment_suspended_due_to_max_failed_payment( $invoice ) { |
|
| 463 | + /** |
|
| 464 | + * Handles subscription suspensions. |
|
| 465 | + * |
|
| 466 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 467 | + * @param array $posted Posted data. |
|
| 468 | + */ |
|
| 469 | + protected function ipn_txn_recurring_payment_suspended_due_to_max_failed_payment( $invoice ) { |
|
| 470 | 470 | |
| 471 | - // Make sure the invoice has a subscription. |
|
| 472 | - $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
| 471 | + // Make sure the invoice has a subscription. |
|
| 472 | + $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
| 473 | 473 | |
| 474 | - if ( empty( $subscription ) ) { |
|
| 475 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 476 | - } |
|
| 477 | - |
|
| 478 | - wpinv_error_log( 'Processing subscription cancellation due to max failed payment for the invoice ' . $invoice->get_id(), false ); |
|
| 479 | - $subscription->cancel(); |
|
| 480 | - wpinv_error_log( 'Subscription cancelled.', false ); |
|
| 481 | - } |
|
| 474 | + if ( empty( $subscription ) ) { |
|
| 475 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 476 | + } |
|
| 477 | + |
|
| 478 | + wpinv_error_log( 'Processing subscription cancellation due to max failed payment for the invoice ' . $invoice->get_id(), false ); |
|
| 479 | + $subscription->cancel(); |
|
| 480 | + wpinv_error_log( 'Subscription cancelled.', false ); |
|
| 481 | + } |
|
| 482 | 482 | |
| 483 | 483 | } |