| @@ 143-228 (lines=86) @@ | ||
| 140 | * @param bool $retry Should we retry the process? |
|
| 141 | * @param object $previous_error |
|
| 142 | */ |
|
| 143 | public function process_subscription_payment( $amount = 0.0, $renewal_order, $retry = true, $previous_error ) { |
|
| 144 | try { |
|
| 145 | if ( $amount * 100 < WC_Stripe_Helper::get_minimum_amount() ) { |
|
| 146 | /* translators: minimum amount */ |
|
| 147 | return new WP_Error( 'stripe_error', sprintf( __( 'Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe' ), wc_price( WC_Stripe_Helper::get_minimum_amount() / 100 ) ) ); |
|
| 148 | } |
|
| 149 | ||
| 150 | $order_id = WC_Stripe_Helper::is_pre_30() ? $renewal_order->id : $renewal_order->get_id(); |
|
| 151 | ||
| 152 | // Get source from order |
|
| 153 | $prepared_source = $this->prepare_order_source( $renewal_order ); |
|
| 154 | $source_object = $prepared_source->source_object; |
|
| 155 | ||
| 156 | if ( ! $prepared_source->customer ) { |
|
| 157 | return new WP_Error( 'stripe_error', __( 'Customer not found', 'woocommerce-gateway-stripe' ) ); |
|
| 158 | } |
|
| 159 | ||
| 160 | WC_Stripe_Logger::log( "Info: Begin processing subscription payment for order {$order_id} for the amount of {$amount}" ); |
|
| 161 | ||
| 162 | /* If we're doing a retry and source is chargeable, we need to pass |
|
| 163 | * a different idempotency key and retry for success. |
|
| 164 | */ |
|
| 165 | if ( is_object( $source_object ) && empty( $source_object->error ) && $this->need_update_idempotency_key( $source_object, $previous_error ) ) { |
|
| 166 | add_filter( 'wc_stripe_idempotency_key', array( $this, 'change_idempotency_key' ), 10, 2 ); |
|
| 167 | } |
|
| 168 | ||
| 169 | if ( ( $this->is_no_such_source_error( $previous_error ) || $this->is_no_linked_source_error( $previous_error ) ) && apply_filters( 'wc_stripe_use_default_customer_source', true ) ) { |
|
| 170 | // Passing empty source will charge customer default. |
|
| 171 | $prepared_source->source = ''; |
|
| 172 | } |
|
| 173 | ||
| 174 | $request = $this->generate_payment_request( $renewal_order, $prepared_source ); |
|
| 175 | $request['capture'] = 'true'; |
|
| 176 | $request['amount'] = WC_Stripe_Helper::get_stripe_amount( $amount, $request['currency'] ); |
|
| 177 | $response = WC_Stripe_API::request( $request ); |
|
| 178 | ||
| 179 | if ( ! empty( $response->error ) ) { |
|
| 180 | // We want to retry. |
|
| 181 | if ( $this->is_retryable_error( $response->error ) ) { |
|
| 182 | if ( $retry ) { |
|
| 183 | // Don't do anymore retries after this. |
|
| 184 | if ( 5 <= $this->retry_interval ) { |
|
| 185 | return $this->process_subscription_payment( $amount, $renewal_order, false, $response->error ); |
|
| 186 | } |
|
| 187 | ||
| 188 | sleep( $this->retry_interval ); |
|
| 189 | ||
| 190 | $this->retry_interval++; |
|
| 191 | ||
| 192 | return $this->process_subscription_payment( $amount, $renewal_order, true, $response->error ); |
|
| 193 | } else { |
|
| 194 | $localized_message = __( 'Sorry, we are unable to process your payment at this time. Please retry later.', 'woocommerce-gateway-stripe' ); |
|
| 195 | $renewal_order->add_order_note( $localized_message ); |
|
| 196 | throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message ); |
|
| 197 | } |
|
| 198 | } |
|
| 199 | ||
| 200 | $localized_messages = WC_Stripe_Helper::get_localized_messages(); |
|
| 201 | ||
| 202 | if ( 'card_error' === $response->error->type ) { |
|
| 203 | $localized_message = isset( $localized_messages[ $response->error->code ] ) ? $localized_messages[ $response->error->code ] : $response->error->message; |
|
| 204 | } else { |
|
| 205 | $localized_message = isset( $localized_messages[ $response->error->type ] ) ? $localized_messages[ $response->error->type ] : $response->error->message; |
|
| 206 | } |
|
| 207 | ||
| 208 | $renewal_order->add_order_note( $localized_message ); |
|
| 209 | ||
| 210 | throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message ); |
|
| 211 | } |
|
| 212 | ||
| 213 | do_action( 'wc_gateway_stripe_process_payment', $response, $renewal_order ); |
|
| 214 | ||
| 215 | $this->process_response( $response, $renewal_order ); |
|
| 216 | } catch ( WC_Stripe_Exception $e ) { |
|
| 217 | WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() ); |
|
| 218 | ||
| 219 | do_action( 'wc_gateway_stripe_process_payment_error', $e, $renewal_order ); |
|
| 220 | ||
| 221 | /* translators: error message */ |
|
| 222 | $renewal_order->update_status( 'failed' ); |
|
| 223 | ||
| 224 | if ( $renewal_order->has_status( array( 'pending', 'failed' ) ) ) { |
|
| 225 | $this->send_failed_order_email( $order_id ); |
|
| 226 | } |
|
| 227 | } |
|
| 228 | } |
|
| 229 | ||
| 230 | /** |
|
| 231 | * Don't transfer Stripe customer/token meta to resubscribe orders. |
|
| @@ 186-271 (lines=86) @@ | ||
| 183 | * @param bool $retry Should we retry the process? |
|
| 184 | * @param object $previous_error |
|
| 185 | */ |
|
| 186 | public function process_subscription_payment( $amount = 0.0, $renewal_order, $retry = true, $previous_error ) { |
|
| 187 | try { |
|
| 188 | if ( $amount * 100 < WC_Stripe_Helper::get_minimum_amount() ) { |
|
| 189 | /* translators: minimum amount */ |
|
| 190 | return new WP_Error( 'stripe_error', sprintf( __( 'Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe' ), wc_price( WC_Stripe_Helper::get_minimum_amount() / 100 ) ) ); |
|
| 191 | } |
|
| 192 | ||
| 193 | $order_id = WC_Stripe_Helper::is_pre_30() ? $renewal_order->id : $renewal_order->get_id(); |
|
| 194 | ||
| 195 | // Get source from order |
|
| 196 | $prepared_source = $this->prepare_order_source( $renewal_order ); |
|
| 197 | $source_object = $prepared_source->source_object; |
|
| 198 | ||
| 199 | if ( ! $prepared_source->customer ) { |
|
| 200 | return new WP_Error( 'stripe_error', __( 'Customer not found', 'woocommerce-gateway-stripe' ) ); |
|
| 201 | } |
|
| 202 | ||
| 203 | WC_Stripe_Logger::log( "Info: Begin processing subscription payment for order {$order_id} for the amount of {$amount}" ); |
|
| 204 | ||
| 205 | /* If we're doing a retry and source is chargeable, we need to pass |
|
| 206 | * a different idempotency key and retry for success. |
|
| 207 | */ |
|
| 208 | if ( is_object( $source_object ) && empty( $source_object->error ) && $this->need_update_idempotency_key( $source_object, $previous_error ) ) { |
|
| 209 | add_filter( 'wc_stripe_idempotency_key', array( $this, 'change_idempotency_key' ), 10, 2 ); |
|
| 210 | } |
|
| 211 | ||
| 212 | if ( ( $this->is_no_such_source_error( $previous_error ) || $this->is_no_linked_source_error( $previous_error ) ) && apply_filters( 'wc_stripe_use_default_customer_source', true ) ) { |
|
| 213 | // Passing empty source will charge customer default. |
|
| 214 | $prepared_source->source = ''; |
|
| 215 | } |
|
| 216 | ||
| 217 | $request = $this->generate_payment_request( $renewal_order, $prepared_source ); |
|
| 218 | $request['capture'] = 'true'; |
|
| 219 | $request['amount'] = WC_Stripe_Helper::get_stripe_amount( $amount, $request['currency'] ); |
|
| 220 | $response = WC_Stripe_API::request( $request ); |
|
| 221 | ||
| 222 | if ( ! empty( $response->error ) ) { |
|
| 223 | // We want to retry. |
|
| 224 | if ( $this->is_retryable_error( $response->error ) ) { |
|
| 225 | if ( $retry ) { |
|
| 226 | // Don't do anymore retries after this. |
|
| 227 | if ( 5 <= $this->retry_interval ) { |
|
| 228 | return $this->process_subscription_payment( $amount, $renewal_order, false, $response->error ); |
|
| 229 | } |
|
| 230 | ||
| 231 | sleep( $this->retry_interval ); |
|
| 232 | ||
| 233 | $this->retry_interval++; |
|
| 234 | ||
| 235 | return $this->process_subscription_payment( $amount, $renewal_order, true, $response->error ); |
|
| 236 | } else { |
|
| 237 | $localized_message = __( 'Sorry, we are unable to process your payment at this time. Please retry later.', 'woocommerce-gateway-stripe' ); |
|
| 238 | $renewal_order->add_order_note( $localized_message ); |
|
| 239 | throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message ); |
|
| 240 | } |
|
| 241 | } |
|
| 242 | ||
| 243 | $localized_messages = WC_Stripe_Helper::get_localized_messages(); |
|
| 244 | ||
| 245 | if ( 'card_error' === $response->error->type ) { |
|
| 246 | $localized_message = isset( $localized_messages[ $response->error->code ] ) ? $localized_messages[ $response->error->code ] : $response->error->message; |
|
| 247 | } else { |
|
| 248 | $localized_message = isset( $localized_messages[ $response->error->type ] ) ? $localized_messages[ $response->error->type ] : $response->error->message; |
|
| 249 | } |
|
| 250 | ||
| 251 | $renewal_order->add_order_note( $localized_message ); |
|
| 252 | ||
| 253 | throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message ); |
|
| 254 | } |
|
| 255 | ||
| 256 | do_action( 'wc_gateway_stripe_process_payment', $response, $renewal_order ); |
|
| 257 | ||
| 258 | $this->process_response( $response, $renewal_order ); |
|
| 259 | } catch ( WC_Stripe_Exception $e ) { |
|
| 260 | WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() ); |
|
| 261 | ||
| 262 | do_action( 'wc_gateway_stripe_process_payment_error', $e, $renewal_order ); |
|
| 263 | ||
| 264 | /* translators: error message */ |
|
| 265 | $renewal_order->update_status( 'failed' ); |
|
| 266 | ||
| 267 | if ( $renewal_order->has_status( array( 'pending', 'failed' ) ) ) { |
|
| 268 | $this->send_failed_order_email( $order_id ); |
|
| 269 | } |
|
| 270 | } |
|
| 271 | } |
|
| 272 | ||
| 273 | /** |
|
| 274 | * Updates other subscription sources. |
|