/**
  * Customise which actions are shown against a subscriptions order on the My Account page.
  *
  * @since 1.3
  */
 public static function filter_woocommerce_my_account_my_orders_actions($actions, $order)
 {
     if (WC_Subscriptions_Order::order_contains_subscription($order) || WC_Subscriptions_Renewal_Order::is_renewal($order)) {
         unset($actions['cancel']);
         if (is_numeric(get_post_meta($order->id, '_failed_order_replaced_by', true))) {
             unset($actions['pay']);
         }
         $original_order = WC_Subscriptions_Renewal_Order::get_parent_order($order);
         $order_items = WC_Subscriptions_Order::get_recurring_items($original_order);
         $first_order_item = reset($order_items);
         $product_id = WC_Subscriptions_Order::get_items_product_id($first_order_item);
         $subscription_key = WC_Subscriptions_Manager::get_subscription_key($original_order->id, $product_id);
         $subscription = WC_Subscriptions_Manager::get_users_subscription($original_order->customer_user, $subscription_key);
         if (empty($subscription) || !in_array($subscription['status'], array('on-hold', 'pending'))) {
             unset($actions['pay']);
         }
     }
     return $actions;
 }
 /**
  * Check if a payment is being made on a failed renewal order from 'My Account'. If so,
  * redirect the order into a cart/checkout payment flow.
  *
  * @since 1.3
  */
 public static function before_woocommerce_pay()
 {
     global $woocommerce;
     if (isset($_GET['pay_for_order']) && isset($_GET['order']) && isset($_GET['order_id'])) {
         // Pay for existing order
         $order_key = urldecode($_GET['order']);
         $order_id = absint($_GET['order_id']);
         $order = new WC_Order($order_id);
         $failed_order_replaced_by = get_post_meta($order_id, '_failed_order_replaced_by', true);
         if (is_numeric($failed_order_replaced_by)) {
             $woocommerce->add_error(sprintf(__('Sorry, this failed order has already been paid. See order %s.', WC_Subscriptions::$text_domain), $failed_order_replaced_by));
             wp_safe_redirect(get_permalink(woocommerce_get_page_id('myaccount')));
             exit;
         }
         if ($order->id == $order_id && $order->order_key == $order_key && in_array($order->status, array('pending', 'failed')) && WC_Subscriptions_Renewal_Order::is_renewal($order)) {
             // If order being paid is a parent order, get the original order, else query parent_order
             if (WC_Subscriptions_Renewal_Order::is_renewal($order_id, array('order_role' => 'parent'))) {
                 $role = 'parent';
                 $original_order = new WC_Order($order->order_custom_fields['_original_order'][0]);
             } elseif (WC_Subscriptions_Renewal_Order::is_renewal($order_id, array('order_role' => 'child'))) {
                 $role = 'child';
                 $original_order = WC_Subscriptions_Renewal_Order::get_parent_order($order_id);
             }
             $order_items = WC_Subscriptions_Order::get_recurring_items($original_order);
             $first_order_item = reset($order_items);
             $product_id = WC_Subscriptions_Order::get_items_product_id($first_order_item);
             $product = get_product($product_id);
             // Make sure we don't actually need the variation ID
             if ($product->is_type(array('variable-subscription'))) {
                 $item = WC_Subscriptions_Order::get_item_by_product_id($original_order, $product_id);
                 $variation_id = $item['variation_id'];
                 $variation = get_product($variation_id);
                 $variation_data = $variation->get_variation_attributes();
             } elseif ($product->is_type(array('subscription_variation'))) {
                 // Handle existing renewal orders incorrectly using variation_id as the product_id
                 $product_id = $product->id;
                 $variation_id = $product->get_variation_id();
                 $variation_data = $product->get_variation_attributes();
             } else {
                 $variation_id = '';
                 $variation_data = array();
             }
             $woocommerce->cart->empty_cart(true);
             $woocommerce->cart->add_to_cart($product_id, 1, $variation_id, $variation_data, array('subscription_renewal' => array('original_order' => $original_order->id, 'failed_order' => $order_id, 'role' => $role)));
             wp_safe_redirect($woocommerce->cart->get_checkout_url());
             exit;
         }
     }
 }
 /**
  * Check if a payment is being made on a failed renewal order from 'My Account'. If so,
  * redirect the order into a cart/checkout payment flow.
  *
  * @since 1.3
  */
 public static function before_woocommerce_pay()
 {
     global $woocommerce, $wp;
     if (isset($_GET['pay_for_order']) && (isset($_GET['order']) && isset($_GET['order_id']) || isset($_GET['key']) && isset($wp->query_vars['order-pay']))) {
         // Pay for existing order
         $order_key = isset($_GET['key']) ? $_GET['key'] : $_GET['order'];
         // WC 2.1 compatibility
         $order_id = isset($wp->query_vars['order-pay']) ? $wp->query_vars['order-pay'] : absint($_GET['order_id']);
         $order = new WC_Order($order_id);
         $failed_order_replaced_by = get_post_meta($order_id, '_failed_order_replaced_by', true);
         if (is_numeric($failed_order_replaced_by)) {
             WC_Subscriptions::add_notice(sprintf(__('Sorry, this failed order has already been paid. See order %s.', 'woocommerce-subscriptions'), $failed_order_replaced_by), 'error');
             wp_safe_redirect(get_permalink(woocommerce_get_page_id('myaccount')));
             exit;
         }
         if ($order->id == $order_id && $order->order_key == $order_key && in_array($order->status, array('pending', 'failed')) && WC_Subscriptions_Renewal_Order::is_renewal($order)) {
             // If order being paid is a parent order, get the original order, else query parent_order
             if (WC_Subscriptions_Renewal_Order::is_renewal($order_id, array('order_role' => 'parent'))) {
                 $role = 'parent';
                 $original_order = new WC_Order(WC_Subscriptions_Order::get_meta($order, 'original_order', false));
             } elseif (WC_Subscriptions_Renewal_Order::is_renewal($order_id, array('order_role' => 'child'))) {
                 $role = 'child';
                 $original_order = WC_Subscriptions_Renewal_Order::get_parent_order($order_id);
             }
             $order_items = WC_Subscriptions_Order::get_recurring_items($original_order);
             $first_order_item = reset($order_items);
             $product_id = WC_Subscriptions_Order::get_items_product_id($first_order_item);
             $product = get_product($product_id);
             $variation_id = '';
             $variation_data = array();
             // Display error message for deleted products
             if (false === $product) {
                 WC_Subscriptions::add_notice(self::$product_deleted_error_message, 'error');
                 // Make sure we don't actually need the variation ID
             } elseif ($product->is_type(array('variable-subscription'))) {
                 $item = WC_Subscriptions_Order::get_item_by_product_id($original_order, $product_id);
                 $variation_id = $item['variation_id'];
                 $variation = get_product($variation_id);
                 // Display error message for deleted product variations
                 if (false === $variation) {
                     WC_Subscriptions::add_notice(self::$product_deleted_error_message, 'error');
                     $variation_data = array();
                 } else {
                     $variation_data = $variation->get_variation_attributes();
                 }
             } elseif ($product->is_type(array('subscription_variation'))) {
                 // Handle existing renewal orders incorrectly using variation_id as the product_id
                 $product_id = $product->id;
                 $variation_id = $product->get_variation_id();
                 $variation_data = $product->get_variation_attributes();
             }
             $woocommerce->cart->empty_cart(true);
             $woocommerce->cart->add_to_cart($product_id, 1, $variation_id, $variation_data, array('subscription_renewal' => array('original_order' => $original_order->id, 'failed_order' => $order_id, 'role' => $role)));
             wp_safe_redirect($woocommerce->cart->get_checkout_url());
             exit;
         }
     }
 }
 /**
  * Override the default PayPal standard args in WooCommerce for subscription purchases when
  * automatic payments are enabled and when the recurring order totals is over $0.00 (because
  * PayPal doesn't support subscriptions with a $0 recurring total, we need to circumvent it and
  * manage it entirely ourselves.)
  *
  * Based on the HTML Variables documented here: https://developer.paypal.com/webapps/developer/docs/classic/paypal-payments-standard/integration-guide/Appx_websitestandard_htmlvariables/#id08A6HI00JQU
  *
  * @since 1.0
  */
 public static function paypal_standard_subscription_args($paypal_args)
 {
     extract(self::get_order_id_and_key($paypal_args));
     $order = new WC_Order($order_id);
     if ($cart_item = WC_Subscriptions_Cart::cart_contains_failed_renewal_order_payment() || false !== WC_Subscriptions_Renewal_Order::get_failed_order_replaced_by($order_id)) {
         $renewal_order = $order;
         $order = WC_Subscriptions_Renewal_Order::get_parent_order($renewal_order);
         $order_contains_failed_renewal = true;
     } else {
         $order_contains_failed_renewal = false;
     }
     if ($order_contains_failed_renewal || WC_Subscriptions_Order::order_contains_subscription($order) && WC_Subscriptions_Order::get_recurring_total($order) > 0 && 'yes' !== get_option(WC_Subscriptions_Admin::$option_prefix . '_turn_off_automatic_payments', 'no')) {
         // Only one subscription allowed in the cart when PayPal Standard is active
         $product = $order->get_product_from_item(array_pop(WC_Subscriptions_Order::get_recurring_items($order)));
         // It's a subscription
         $paypal_args['cmd'] = '_xclick-subscriptions';
         if (count($order->get_items()) > 1) {
             foreach ($order->get_items() as $item) {
                 if ($item['qty'] > 1) {
                     $item_names[] = $item['qty'] . ' x ' . self::paypal_item_name($item['name']);
                 } elseif ($item['qty'] > 0) {
                     $item_names[] = self::paypal_item_name($item['name']);
                 }
             }
             $paypal_args['item_name'] = self::paypal_item_name(sprintf(__('Order %s', 'woocommerce-subscriptions'), $order->get_order_number() . " - " . implode(', ', $item_names)));
         } else {
             $paypal_args['item_name'] = self::paypal_item_name($product->get_title());
         }
         $unconverted_periods = array('billing_period' => WC_Subscriptions_Order::get_subscription_period($order), 'trial_period' => WC_Subscriptions_Order::get_subscription_trial_period($order));
         $converted_periods = array();
         // Convert period strings into PayPay's format
         foreach ($unconverted_periods as $key => $period) {
             switch (strtolower($period)) {
                 case 'day':
                     $converted_periods[$key] = 'D';
                     break;
                 case 'week':
                     $converted_periods[$key] = 'W';
                     break;
                 case 'year':
                     $converted_periods[$key] = 'Y';
                     break;
                 case 'month':
                 default:
                     $converted_periods[$key] = 'M';
                     break;
             }
         }
         $price_per_period = WC_Subscriptions_Order::get_recurring_total($order);
         $subscription_interval = WC_Subscriptions_Order::get_subscription_interval($order);
         $subscription_length = WC_Subscriptions_Order::get_subscription_length($order);
         $subscription_installments = $subscription_length / $subscription_interval;
         $is_payment_change = WC_Subscriptions_Change_Payment_Gateway::$is_request_to_change_payment;
         $is_switch_order = WC_Subscriptions_Switcher::order_contains_subscription_switch($order->id);
         $is_synced_subscription = WC_Subscriptions_Synchroniser::order_contains_synced_subscription($order->id);
         $sign_up_fee = $is_payment_change ? 0 : WC_Subscriptions_Order::get_sign_up_fee($order);
         $initial_payment = $is_payment_change ? 0 : WC_Subscriptions_Order::get_total_initial_payment($order);
         if ($is_payment_change) {
             // Add a nonce to the order ID to avoid "This invoice has already been paid" error when changing payment method to PayPal when it was previously PayPal
             $paypal_args['invoice'] = $paypal_args['invoice'] . '-wcscpm-' . wp_create_nonce();
         } elseif ($order_contains_failed_renewal) {
             // Set the invoice details to the original order's invoice but also append a special string and this renewal orders ID so that we can match it up as a failed renewal order payment later
             $paypal_args['invoice'] = self::$invoice_prefix . ltrim($order->get_order_number(), '#') . '-wcsfrp-' . $renewal_order->id;
             $paypal_args['custom'] = serialize(array($order->id, $order->order_key));
         }
         if ($order_contains_failed_renewal) {
             $sign_up_fee = 0;
             $initial_payment = $renewal_order->get_total();
             // Initial payment can be left in case the customer is purchased other products with the payment
             $subscription_trial_length = 0;
             $subscription_installments = max($subscription_installments - WC_Subscriptions_Manager::get_subscriptions_completed_payment_count(WC_Subscriptions_Manager::get_subscription_key($order_id, $product->id)), 0);
             // If we're changing the payment date or switching subs, we need to set the trial period to the next payment date & installments to be the number of installments left
         } elseif ($is_payment_change || $is_switch_order || $is_synced_subscription) {
             $subscription_key = WC_Subscriptions_Manager::get_subscription_key($order_id, $product->id);
             // Give a free trial until the next payment date
             if ($is_switch_order) {
                 $next_payment_timestamp = get_post_meta($order->id, '_switched_subscription_first_payment_timestamp', true);
             } elseif ($is_synced_subscription) {
                 $next_payment_timestamp = WC_Subscriptions_Synchroniser::calculate_first_payment_date($product, 'timestamp');
             } else {
                 $next_payment_timestamp = WC_Subscriptions_Manager::get_next_payment_date($subscription_key, $order->user_id, 'timestamp');
             }
             // When the subscription is on hold
             if ($next_payment_timestamp != false && !empty($next_payment_timestamp)) {
                 $trial_until = self::calculate_trial_periods_until($next_payment_timestamp);
                 $subscription_trial_length = $trial_until['first_trial_length'];
                 $converted_periods['trial_period'] = $trial_until['first_trial_period'];
                 $second_trial_length = $trial_until['second_trial_length'];
                 $second_trial_period = $trial_until['second_trial_period'];
             } else {
                 $subscription_trial_length = 0;
             }
             // If is a payment change, we need to account for completed payments on the number of installments owing
             if ($is_payment_change && $subscription_length > 0) {
                 $subscription_installments -= WC_Subscriptions_Manager::get_subscriptions_completed_payment_count($subscription_key);
             }
         } else {
             $subscription_trial_length = WC_Subscriptions_Order::get_subscription_trial_length($order);
         }
         if ($subscription_trial_length > 0) {
             // Specify a free trial period
             if ($is_switch_order || $is_synced_subscription || $initial_payment != $sign_up_fee) {
                 $paypal_args['a1'] = $initial_payment > 0 ? $initial_payment : 0;
             } else {
                 $paypal_args['a1'] = $sign_up_fee > 0 ? $sign_up_fee : 0;
                 // Maybe add the sign up fee to the free trial period
             }
             // Trial period length
             $paypal_args['p1'] = $subscription_trial_length;
             // Trial period
             $paypal_args['t1'] = $converted_periods['trial_period'];
             // We need to use a second trial period before we have more than 90 days until the next payment
             if (isset($second_trial_length) && $second_trial_length > 0) {
                 $paypal_args['a2'] = 0.01;
                 // Alas, although it's undocumented, PayPal appears to require a non-zero value in order to allow a second trial period
                 $paypal_args['p2'] = $second_trial_length;
                 $paypal_args['t2'] = $second_trial_period;
             }
         } elseif ($sign_up_fee > 0 || $initial_payment != $price_per_period) {
             // No trial period, so charge sign up fee and per period price for the first period
             if ($subscription_installments == 1) {
                 $param_number = 3;
             } else {
                 $param_number = 1;
             }
             $paypal_args['a' . $param_number] = $initial_payment;
             // Sign Up interval
             $paypal_args['p' . $param_number] = $subscription_interval;
             // Sign Up unit of duration
             $paypal_args['t' . $param_number] = $converted_periods['billing_period'];
         }
         // We have a recurring payment
         if (!isset($param_number) || $param_number == 1) {
             // Subscription price
             $paypal_args['a3'] = $price_per_period;
             // Subscription duration
             $paypal_args['p3'] = $subscription_interval;
             // Subscription period
             $paypal_args['t3'] = $converted_periods['billing_period'];
         }
         // Recurring payments
         if ($subscription_installments == 1 || $sign_up_fee > 0 && $subscription_trial_length == 0 && $subscription_installments == 2) {
             // Non-recurring payments
             $paypal_args['src'] = 0;
         } else {
             $paypal_args['src'] = 1;
             if ($subscription_installments > 0) {
                 if ($sign_up_fee > 0 && $subscription_trial_length == 0) {
                     // An initial period is being used to charge a sign-up fee
                     $subscription_installments--;
                 }
                 $paypal_args['srt'] = $subscription_installments;
             }
         }
         // Don't reattempt failed payments, instead let Subscriptions handle the failed payment
         $paypal_args['sra'] = 0;
         // Force return URL so that order description & instructions display
         $paypal_args['rm'] = 2;
     }
     return $paypal_args;
 }