/**
  * Uses the details of an order to create a pending subscription on the customers account
  * for a subscription product, as specified with $product_id.
  *
  * @param $order mixed int | WC_Order The order ID or WC_Order object to create the subscription from.
  * @param $product_id int The ID of the subscription product on the order.
  * @param $args array An array of name => value pairs to customise the details of the subscription, including:
  * 			'start_date' A MySQL formatted date/time string on which the subscription should start, in UTC timezone
  * 			'expiry_date' A MySQL formatted date/time string on which the subscription should expire, in UTC timezone
  * @since 1.1
  */
 public static function create_pending_subscription_for_order($order, $product_id, $args = array())
 {
     if (!is_object($order)) {
         $order = new WC_Order($order);
     }
     if (!WC_Subscriptions_Product::is_subscription($product_id)) {
         return;
     }
     $args = wp_parse_args($args, array('start_date' => '', 'expiry_date' => ''));
     $subscription_key = self::get_subscription_key($order->id, $product_id);
     // In case the subscription exists already
     $subscription = self::get_users_subscription($order->customer_user, $subscription_key);
     // Adding a new subscription so set the start date/time to now
     if (!empty($args['start_date'])) {
         if (is_numeric($args['start_date'])) {
             $args['start_date'] = date('Y-m-d H:i:s', $args['start_date']);
         }
         $start_date = $args['start_date'];
     } else {
         $start_date = isset($subscription['start_date']) ? $subscription['start_date'] : gmdate('Y-m-d H:i:s');
     }
     // Adding a new subscription so set the expiry date/time from the order date
     if (!empty($args['expiry_date'])) {
         if (is_numeric($args['expiry_date'])) {
             $args['expiry_date'] = date('Y-m-d H:i:s', $args['expiry_date']);
         }
         $expiration = $args['expiry_date'];
     } else {
         $expiration = isset($subscription['expiry_date']) ? $subscription['expiry_date'] : WC_Subscriptions_Product::get_expiration_date($product_id, $start_date);
     }
     // Adding a new subscription so set the expiry date/time from the order date
     $trial_expiration = isset($subscription['trial_expiry_date']) ? $subscription['trial_expiry_date'] : WC_Subscriptions_Product::get_trial_expiration_date($product_id, $start_date);
     $failed_payments = isset($subscription['failed_payments']) ? $subscription['failed_payments'] : 0;
     $completed_payments = isset($subscription['completed_payments']) ? $subscription['completed_payments'] : array();
     $subscriptions[$subscription_key] = array('product_id' => $product_id, 'order_key' => $order->order_key, 'order_id' => $order->id, 'start_date' => $start_date, 'expiry_date' => $expiration, 'end_date' => 0, 'status' => 'pending', 'trial_expiry_date' => $trial_expiration, 'failed_payments' => $failed_payments, 'completed_payments' => $completed_payments);
     self::update_users_subscriptions($order->customer_user, $subscriptions);
     $product = WC_Subscriptions::get_product($product_id);
     // Set subscription status to active and log activation
     $order->add_order_note(sprintf(__('Pending subscription created for "%s".', WC_Subscriptions::$text_domain), $product->get_title()));
     do_action('pending_subscription_created_for_order', $order, $product_id);
 }
 /**
  * Uses the details of an order to create a pending subscription on the customers account
  * for a subscription product, as specified with $product_id.
  *
  * @param int|WC_Order $order The order ID or WC_Order object to create the subscription from.
  * @param int $product_id The ID of the subscription product on the order, if a variation, it must be the variation's ID.
  * @param array $args An array of name => value pairs to customise the details of the subscription, including:
  * 			'start_date' A MySQL formatted date/time string on which the subscription should start, in UTC timezone
  * 			'expiry_date' A MySQL formatted date/time string on which the subscription should expire, in UTC timezone
  * @since 1.1
  */
 public static function create_pending_subscription_for_order($order, $product_id, $args = array())
 {
     _deprecated_function(__METHOD__, '2.0', 'wcs_create_subscription()');
     if (!is_object($order)) {
         $order = new WC_Order($order);
     }
     if (!WC_Subscriptions_Product::is_subscription($product_id)) {
         return;
     }
     $args = wp_parse_args($args, array('start_date' => get_gmt_from_date($order->order_date), 'expiry_date' => ''));
     $billing_period = WC_Subscriptions_Product::get_period($product_id);
     $billing_interval = WC_Subscriptions_Product::get_interval($product_id);
     // Support passing timestamps
     $args['start_date'] = is_numeric($args['start_date']) ? date('Y-m-d H:i:s', $args['start_date']) : $args['start_date'];
     $product = wc_get_product($product_id);
     // Check if there is already a subscription for this product and order
     $subscriptions = wcs_get_subscriptions(array('order_id' => $order->id, 'product_id' => $product_id));
     if (!empty($subscriptions)) {
         $subscription = array_pop($subscriptions);
         // Make sure the subscription is pending and start date is set correctly
         wp_update_post(array('ID' => $subscription->id, 'post_status' => 'wc-' . apply_filters('woocommerce_default_subscription_status', 'pending'), 'post_date' => get_date_from_gmt($args['start_date'])));
     } else {
         $subscription = wcs_create_subscription(array('start_date' => get_date_from_gmt($args['start_date']), 'order_id' => $order->id, 'customer_id' => $order->get_user_id(), 'billing_period' => $billing_period, 'billing_interval' => $billing_interval, 'customer_note' => $order->customer_note));
         if (is_wp_error($subscription)) {
             throw new Exception(__('Error: Unable to create subscription. Please try again.', 'woocommerce-subscriptions'));
         }
         $item_id = $subscription->add_product($product, 1, array('variation' => method_exists($product, 'get_variation_attributes') ? $product->get_variation_attributes() : array(), 'totals' => array('subtotal' => $product->get_price(), 'subtotal_tax' => 0, 'total' => $product->get_price(), 'tax' => 0, 'tax_data' => array('subtotal' => array(), 'total' => array()))));
         if (!$item_id) {
             throw new Exception(__('Error: Unable to add product to created subscription. Please try again.', 'woocommerce-subscriptions'));
         }
     }
     // Make sure some of the meta is copied form the order rather than the store's defaults
     update_post_meta($subscription->id, '_order_currency', $order->order_currency);
     update_post_meta($subscription->id, '_prices_include_tax', $order->prices_include_tax);
     // Adding a new subscription so set the expiry date/time from the order date
     if (!empty($args['expiry_date'])) {
         if (is_numeric($args['expiry_date'])) {
             $args['expiry_date'] = date('Y-m-d H:i:s', $args['expiry_date']);
         }
         $expiration = $args['expiry_date'];
     } else {
         $expiration = WC_Subscriptions_Product::get_expiration_date($product_id, $args['start_date']);
     }
     // Adding a new subscription so set the expiry date/time from the order date
     $trial_expiration = WC_Subscriptions_Product::get_trial_expiration_date($product_id, $args['start_date']);
     $dates_to_update = array();
     if ($trial_expiration > 0) {
         $dates_to_update['trial_end'] = $trial_expiration;
     }
     if ($expiration > 0) {
         $dates_to_update['end'] = $expiration;
     }
     if (!empty($dates_to_update)) {
         $subscription->update_dates($dates_to_update);
     }
     // Set the recurring totals on the subscription
     $subscription->set_total(0, 'tax');
     $subscription->set_total($product->get_price(), 'total');
     $subscription->add_order_note(__('Pending subscription created.', 'woocommerce-subscriptions'));
     do_action('pending_subscription_created_for_order', $order, $product_id);
 }
 /**
  * Make sure the expiration date is calculated from the synced start date for products where the start date
  * will be synced.
  *
  * @param string $expiration_date MySQL formatted date on which the subscription is set to expire
  * @param mixed $product_id The product/post ID of the subscription
  * @param mixed $from_date A MySQL formatted date/time string from which to calculate the expiration date, or empty (default), which will use today's date/time.
  * @since 1.5
  */
 public static function recalculate_product_expiration_date($expiration_date, $product_id, $from_date)
 {
     if (self::is_product_synced($product_id)) {
         remove_filter('woocommerce_subscriptions_product_expiration_date', __CLASS__ . '::' . __FUNCTION__);
         // avoid infinite loop
         $expiration_date = WC_Subscriptions_Product::get_expiration_date($product_id, self::calculate_first_payment_date($product_id, 'mysql'));
         add_filter('woocommerce_subscriptions_product_expiration_date', __CLASS__ . '::' . __FUNCTION__);
     }
     return $expiration_date;
 }
 /**
  * Adjust the product ID that grants access to a membership plan on purchase
  *
  * Subscription products take priority over all other products
  *
  * @since 1.0.0
  * @param int $product_id Product ID
  * @param array $access_granting_product_ids Array of product IDs
  * @param WC_Memberships_Membership_Plan $plan
  * @return int Product ID, adjusted if necessary
  */
 public function adjust_access_granting_product_id($product_id, $access_granting_product_ids, WC_Memberships_Membership_Plan $plan)
 {
     // Check if more than one products may grant access, and if the plan even
     // allows access while subscription is active
     if (count($access_granting_product_ids) > 1 && $this->plan_grants_access_while_subscription_active($plan->get_id())) {
         // First, find all subscription products that grant access
         $access_granting_subscription_product_ids = array();
         foreach ($access_granting_product_ids as $_product_id) {
             $product = wc_get_product($_product_id);
             if ($product->is_type(array('subscription', 'subscription_variation', 'variable-subscription'))) {
                 $access_granting_subscription_product_ids[] = $product->id;
             }
         }
         // If there are any, decide which one actually gets to grant access
         if (!empty($access_granting_subscription_product_ids)) {
             // Only one subscription grants access, short-circuit it as the winner
             if (count($access_granting_subscription_product_ids) == 1) {
                 $product_id = $access_granting_subscription_product_ids[0];
             } else {
                 $longest_expiration_date = 0;
                 foreach ($access_granting_subscription_product_ids as $_subscription_product_id) {
                     $expiration_date = WC_Subscriptions_Product::get_expiration_date($_subscription_product_id);
                     // No expiration date? Ladies and gentlemen - we've got a winner!
                     if (!$expiration_date) {
                         $product_id = $_subscription_product_id;
                         break;
                     }
                     // This one beats the previous sub! Out of the way, you sub-optimal sucker, you!
                     if (strtotime($expiration_date) > $longest_expiration_date) {
                         $product_id = $_subscription_product_id;
                         $longest_expiration_date = strtotime($expiration_date);
                     }
                 }
             }
         }
     }
     return $product_id;
 }
 /**
  * Uses the details of an order to create a pending subscription on the customers account
  * for a subscription product, as specified with $product_id.
  *
  * @param int|WC_Order $order The order ID or WC_Order object to create the subscription from.
  * @param int $product_id The ID of the subscription product on the order.
  * @param array $args An array of name => value pairs to customise the details of the subscription, including:
  * 			'start_date' A MySQL formatted date/time string on which the subscription should start, in UTC timezone
  * 			'expiry_date' A MySQL formatted date/time string on which the subscription should expire, in UTC timezone
  * @since 1.1
  */
 public static function create_pending_subscription_for_order($order, $product_id, $args = array())
 {
     if (!is_object($order)) {
         $order = new WC_Order($order);
     }
     if (!WC_Subscriptions_Product::is_subscription($product_id)) {
         return;
     }
     $args = wp_parse_args($args, array('start_date' => '', 'expiry_date' => ''));
     $subscription_key = self::get_subscription_key($order->id, $product_id);
     // In case the subscription exists already
     $subscription = self::get_subscription($subscription_key);
     if (!empty($subscription['variation_id'])) {
         $product_id = $subscription['variation_id'];
     } elseif (!empty($subscription['product_id'])) {
         $product_id = $subscription['product_id'];
     }
     // Adding a new subscription so set the start date/time to now
     if (!empty($args['start_date'])) {
         if (is_numeric($args['start_date'])) {
             $args['start_date'] = date('Y-m-d H:i:s', $args['start_date']);
         }
         $start_date = $args['start_date'];
     } else {
         $start_date = !empty($subscription['start_date']) ? $subscription['start_date'] : gmdate('Y-m-d H:i:s');
     }
     // Adding a new subscription so set the expiry date/time from the order date
     if (!empty($args['expiry_date'])) {
         if (is_numeric($args['expiry_date'])) {
             $args['expiry_date'] = date('Y-m-d H:i:s', $args['expiry_date']);
         }
         $expiration = $args['expiry_date'];
     } else {
         $expiration = !empty($subscription['expiry_date']) ? $subscription['expiry_date'] : WC_Subscriptions_Product::get_expiration_date($product_id, $start_date);
     }
     // Adding a new subscription so set the expiry date/time from the order date
     $trial_expiration = !empty($subscription['trial_expiry_date']) ? $subscription['trial_expiry_date'] : WC_Subscriptions_Product::get_trial_expiration_date($product_id, $start_date);
     $failed_payments = !empty($subscription['failed_payments']) ? $subscription['failed_payments'] : 0;
     $completed_payments = !empty($subscription['completed_payments']) ? $subscription['completed_payments'] : array();
     $order_item_id = WC_Subscriptions_Order::get_item_id_by_subscription_key($subscription_key);
     // Store the subscription details in item meta
     woocommerce_add_order_item_meta($order_item_id, '_subscription_start_date', $start_date, true);
     woocommerce_add_order_item_meta($order_item_id, '_subscription_expiry_date', $expiration, true);
     woocommerce_add_order_item_meta($order_item_id, '_subscription_trial_expiry_date', $trial_expiration, true);
     woocommerce_add_order_item_meta($order_item_id, '_subscription_failed_payments', $failed_payments, true);
     woocommerce_add_order_item_meta($order_item_id, '_subscription_completed_payments', $completed_payments, true);
     woocommerce_add_order_item_meta($order_item_id, '_subscription_status', 'pending', true);
     woocommerce_add_order_item_meta($order_item_id, '_subscription_end_date', 0, true);
     woocommerce_add_order_item_meta($order_item_id, '_subscription_suspension_count', 0, true);
     $product = WC_Subscriptions::get_product($product_id);
     // Set subscription status to active and log activation
     $order->add_order_note(sprintf(__('Pending subscription created for "%s".', 'woocommerce-subscriptions'), $product->get_title()));
     do_action('pending_subscription_created_for_order', $order, $product_id);
 }
 /**
  * Make sure the end date of the switched subscription starts after already paid term
  *
  * @since 2.0
  */
 public static function recurring_cart_end_date($end_date, $cart, $product)
 {
     if (0 !== $end_date) {
         foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
             if (isset($cart_item['subscription_switch']['subscription_id']) && isset($cart_item['data']) && $product == $cart_item['data']) {
                 $next_payment_time = isset($cart_item['subscription_switch']['first_payment_timestamp']) ? $cart_item['subscription_switch']['first_payment_timestamp'] : 0;
                 $end_timestamp = WC()->cart->cart_contents[$cart_item_key]['subscription_switch']['end_timestamp'];
                 // if the subscription is length 1 and prorated, we want to use the prorated the next payment date as the end date
                 if (1 == $cart_item['data']->subscription_length && 0 !== $next_payment_time && isset($cart_item['subscription_switch']['recurring_payment_prorated'])) {
                     $end_date = date('Y-m-d H:i:s', $next_payment_time);
                     // if the subscription is more than 1 (and not 0) and we have a next payment date (prorated or not) we want to calculate the new end date from that
                 } elseif (0 !== $next_payment_time && $cart_item['data']->subscription_length > 1) {
                     // remove trial period on the switched subscription when calculating the new end date
                     $trial_length = $cart_item['data']->subscription_trial_length;
                     $cart_item['data']->subscription_trial_length = 0;
                     $end_date = WC_Subscriptions_Product::get_expiration_date($cart_item['data'], date('Y-m-d H:i:s', $next_payment_time));
                     // add back the trial length if it has been spoofed
                     $cart_item['data']->subscription_trial_length = $trial_length;
                     // elseif fallback to using the end date set on the cart item
                 } elseif (!empty($end_timestamp)) {
                     $end_date = date('Y-m-d H:i:s', $end_timestamp);
                 }
                 break;
             }
         }
     }
     return $end_date;
 }
 /**
  * Maybe recalculate the end date for synced subscription products that contain the unnecessary
  * "one day trial" period.
  *
  * @since 2.0.9
  * @deprecated 2.0.14
  */
 public static function recalculate_end_date($end_date, $recurring_cart, $product)
 {
     _deprecated_function(__METHOD__, '2.0.14');
     if (self::is_product_synced($product)) {
         $product_id = isset($product->variation_id) ? $product->variation_id : $product->id;
         $end_date = WC_Subscriptions_Product::get_expiration_date($product_id);
     }
     return $end_date;
 }
 /**
  * Calculate the initial and recurring totals for all subscription products in the cart.
  *
  * We need to group subscriptions by billing schedule to make the display and creation of recurring totals sane,
  * when there are multiple subscriptions in the cart. To do that, we use an array with keys of the form:
  * '{billing_interval}_{billing_period}_{trial_interval}_{trial_period}_{length}_{billing_period}'. This key
  * is used to reference WC_Cart objects for each recurring billing schedule and these are stored in the master
  * cart with the billing schedule key.
  *
  * After we have calculated and grouped all recurring totals, we need to checks the structure of the subscription
  * product prices to see whether they include sign-up fees and/or free trial periods and then recalculates the
  * appropriate totals by using the @see self::$calculation_type flag and cloning the cart to run @see WC_Cart::calculate_totals()
  *
  * @since 1.3.5
  * @version 2.0
  */
 public static function calculate_subscription_totals($total, $cart)
 {
     if (!self::cart_contains_subscription() && !wcs_cart_contains_resubscribe()) {
         // cart doesn't contain subscription
         return $total;
     } elseif ('none' != self::$calculation_type) {
         // We're in the middle of a recalculation, let it run
         return $total;
     }
     // Save the original cart values/totals, as we'll use this when there is no sign-up fee
     WC()->cart->total = $total < 0 ? 0 : $total;
     do_action('woocommerce_subscription_cart_before_grouping');
     $subscription_groups = array();
     // Group the subscription items by their cart item key based on billing schedule
     foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
         if (WC_Subscriptions_Product::is_subscription($cart_item['data'])) {
             $subscription_groups[self::get_recurring_cart_key($cart_item)][] = $cart_item_key;
         }
     }
     do_action('woocommerce_subscription_cart_after_grouping');
     $recurring_carts = array();
     // Now let's calculate the totals for each group of subscriptions
     self::$calculation_type = 'recurring_total';
     foreach ($subscription_groups as $recurring_cart_key => $subscription_group) {
         // Create a clone cart to calculate and store totals for this group of subscriptions
         $recurring_cart = clone WC()->cart;
         $product = null;
         // Remove any items not in this subscription group
         foreach ($recurring_cart->get_cart() as $cart_item_key => $cart_item) {
             if (!in_array($cart_item_key, $subscription_group)) {
                 unset($recurring_cart->cart_contents[$cart_item_key]);
                 continue;
             }
             if (null === $product) {
                 $product = $cart_item['data'];
             }
         }
         $recurring_cart->start_date = apply_filters('wcs_recurring_cart_start_date', gmdate('Y-m-d H:i:s'), $recurring_cart);
         $recurring_cart->trial_end_date = apply_filters('wcs_recurring_cart_trial_end_date', WC_Subscriptions_Product::get_trial_expiration_date($product, $recurring_cart->start_date), $recurring_cart, $product);
         $recurring_cart->next_payment_date = apply_filters('wcs_recurring_cart_next_payment_date', WC_Subscriptions_Product::get_first_renewal_payment_date($product, $recurring_cart->start_date), $recurring_cart, $product);
         $recurring_cart->end_date = apply_filters('wcs_recurring_cart_end_date', WC_Subscriptions_Product::get_expiration_date($product, $recurring_cart->start_date), $recurring_cart, $product);
         // No fees recur (yet)
         $recurring_cart->fees = array();
         $recurring_cart->fee_total = 0;
         WC()->shipping->reset_shipping();
         self::maybe_recalculate_shipping();
         $recurring_cart->calculate_totals();
         // Store this groups cart details
         $recurring_carts[$recurring_cart_key] = clone $recurring_cart;
         // And remove some other floatsam
         $recurring_carts[$recurring_cart_key]->removed_cart_contents = array();
         $recurring_carts[$recurring_cart_key]->cart_session_data = array();
     }
     self::$calculation_type = 'none';
     // We need to reset the packages and totals stored in WC()->shipping too
     self::maybe_recalculate_shipping();
     WC()->cart->calculate_shipping();
     // If there is no sign-up fee and a free trial, and no products being purchased with the subscription, we need to zero the fees for the first billing period
     if (0 == self::get_cart_subscription_sign_up_fee() && self::all_cart_items_have_free_trial()) {
         foreach (WC()->cart->get_fees() as $fee_index => $fee) {
             WC()->cart->fees[$fee_index]->amount = 0;
             WC()->cart->fees[$fee_index]->tax = 0;
         }
         WC()->cart->fee_total = 0;
     }
     WC()->cart->recurring_carts = $recurring_carts;
     $total = max(0, round(WC()->cart->cart_contents_total + WC()->cart->tax_total + WC()->cart->shipping_tax_total + WC()->cart->shipping_total + WC()->cart->fee_total, WC()->cart->dp));
     if (isset(WC()->cart->discount_total) && 0 !== WC()->cart->discount_total) {
         // WC < 2.3, deduct deprecated after tax discount total
         $total = max(0, round($total - WC()->cart->discount_total, WC()->cart->dp));
     }
     if (!self::charge_shipping_up_front()) {
         $total = max(0, $total - WC()->cart->shipping_tax_total - WC()->cart->shipping_total);
         WC()->cart->shipping_taxes = array();
         WC()->cart->shipping_tax_total = 0;
         WC()->cart->shipping_total = 0;
     }
     return apply_filters('woocommerce_subscriptions_calculated_total', $total);
 }
 /**
  * Records the initial payment against a subscription.
  *
  * This function is called when an orders status is changed to completed or processing
  * for those gateways which never call @see WC_Order::payment_complete(), like the core
  * WooCommerce Cheque and Bank Transfer gateways.
  *
  * It will also set the start date on the subscription to the time the payment is completed.
  *
  * @param $order_id int|WC_Order
  * @param $old_order_status
  * @param $new_order_status
  * @since 2.0
  */
 public static function maybe_record_subscription_payment($order_id, $old_order_status, $new_order_status)
 {
     if (wcs_order_contains_subscription($order_id)) {
         $subscriptions = wcs_get_subscriptions_for_order($order_id);
         $was_activated = false;
         $order_completed = in_array($new_order_status, array(apply_filters('woocommerce_payment_complete_order_status', 'processing', $order_id), 'processing', 'completed')) && in_array($old_order_status, apply_filters('woocommerce_valid_order_statuses_for_payment', array('pending', 'on-hold', 'failed')));
         foreach ($subscriptions as $subscription) {
             // Do we need to activate a subscription?
             if ($order_completed && !$subscription->has_status(wcs_get_subscription_ended_statuses()) && !$subscription->has_status('active')) {
                 $new_start_date_offset = current_time('timestamp', true) - $subscription->get_time('start');
                 // if the payment has been processed more than an hour after the order was first created, let's update the dates on the subscription to account for that, because it may have even been processed days after it was first placed
                 if ($new_start_date_offset > HOUR_IN_SECONDS) {
                     $dates = array('start' => current_time('mysql', true));
                     if (WC_Subscriptions_Synchroniser::subscription_contains_synced_product($subscription)) {
                         $trial_end = $subscription->get_time('trial_end');
                         $next_payment = $subscription->get_time('next_payment');
                         // if either there is a free trial date or a next payment date that falls before now, we need to recalculate all the sync'd dates
                         if ($trial_end > 0 && $trial_end < strtotime($dates['start']) || $next_payment > 0 && $next_payment < strtotime($dates['start'])) {
                             foreach ($subscription->get_items() as $item) {
                                 $product_id = wcs_get_canonical_product_id($item);
                                 if (WC_Subscriptions_Synchroniser::is_product_synced($product_id)) {
                                     $dates['trial_end'] = WC_Subscriptions_Product::get_trial_expiration_date($product_id, $dates['start']);
                                     $dates['next_payment'] = WC_Subscriptions_Synchroniser::calculate_first_payment_date($product_id, 'mysql', $dates['start']);
                                     $dates['end'] = WC_Subscriptions_Product::get_expiration_date($product_id, $dates['start']);
                                     break;
                                 }
                             }
                         }
                     } else {
                         // No sync'ing to mess about with, just add the offset to the existing dates
                         foreach (array('trial_end', 'next_payment', 'end') as $date_type) {
                             if (0 != $subscription->get_time($date_type)) {
                                 $dates[$date_type] = gmdate('Y-m-d H:i:s', $subscription->get_time($date_type) + $new_start_date_offset);
                             }
                         }
                     }
                     $subscription->update_dates($dates);
                 }
                 $subscription->payment_complete();
                 $was_activated = true;
             } elseif ('failed' == $new_order_status) {
                 $subscription->payment_failed();
             }
         }
         if ($was_activated) {
             do_action('subscriptions_activated_for_order', $order_id);
         }
     }
 }
Example #10
0
if ('' != $this->color_checkbox_checkmark) {
    $create['options']['color_checkbox_checkmark'] = $this->color_checkbox_checkmark;
}
if ('' != $this->color_header) {
    $create['options']['color_header'] = $this->color_header;
}
if ('' != $this->color_link) {
    $create['options']['color_link'] = $this->color_link;
}
// Check if there's a subscription product in cart
if (class_exists('WC_Subscriptions_Cart') && WC_Subscriptions_Cart::cart_contains_subscription()) {
    $create['recurring'] = true;
    // Extra merchant data
    $fetched_subscription_product_id = $this->get_subscription_product_id();
    if ($fetched_subscription_product_id) {
        $subscription_expiration_time = WC_Subscriptions_Product::get_expiration_date($fetched_subscription_product_id);
        if (0 !== $subscription_expiration_time) {
            $end_time = date('Y-m-d\\TH:i', strtotime($subscription_expiration_time));
        } else {
            $end_time = date('Y-m-d\\TH:i', strtotime('+50 year'));
        }
        $klarna_subscription_info = array('subscription_name' => 'Subscription: ' . get_the_title($fetched_subscription_product_id), 'start_time' => date('Y-m-d\\TH:i'), 'end_time' => $end_time, 'auto_renewal_of_subscription' => true);
        if (get_current_user_id()) {
            $klarna_subscription_info['customer_account_info'] = array('unique_account_identifier' => (string) get_current_user_id());
        }
        $klarna_subscription = array($klarna_subscription_info);
        $body_attachment = json_encode(array('subscription' => $klarna_subscription));
        if ($body_attachment) {
            $create['attachment']['content_type'] = 'application/vnd.klarna.internal.emd-v2+json';
            $create['attachment']['body'] = $body_attachment;
        }