/** * Calculate expire date. * * Based in the membership definition * * @since 1.0.0 * @internal * * @param string $start_date Optional. The start date to calculate date from. * @param bool $paid If the user made a payment to extend the expire date. * @return string The calculated expire date. */ public function calc_expire_date($start_date = null, $paid = false) { $membership = $this->get_membership(); $gateway = $this->get_gateway(); $start_date = $this->calc_trial_expire_date($start_date); $expire_date = null; /* * When in trial period and gateway does not send automatic recurring * payment notifications, the expire date is equal to trial expire date. */ if ($this->is_trial_eligible()) { $expire_date = $start_date; } else { if ($paid) { /* * Always extend the membership from current date or later, even if * the specified start-date is in the past. * * Example: User does not pay for 3 days (subscription set "pending") * Then he pays: The 3 days without access are for free; * his subscriptions is extended from current date! */ $today = MS_Helper_Period::current_date(); if (MS_Helper_Period::is_after($today, $start_date)) { $start_date = $today; } } /* * The gatway calls the payment handler URL automatically: * This means that the user does not need to re-authorize each * payment. */ switch ($membership->payment_type) { case MS_Model_Membership::PAYMENT_TYPE_PERMANENT: $expire_date = false; break; case MS_Model_Membership::PAYMENT_TYPE_FINITE: $period_unit = MS_Helper_Period::get_period_value($membership->period, 'period_unit'); $period_type = MS_Helper_Period::get_period_value($membership->period, 'period_type'); $expire_date = MS_Helper_Period::add_interval($period_unit, $period_type, $start_date); break; case MS_Model_Membership::PAYMENT_TYPE_DATE_RANGE: $expire_date = $membership->period_date_end; break; case MS_Model_Membership::PAYMENT_TYPE_RECURRING: $period_unit = MS_Helper_Period::get_period_value($membership->pay_cycle_period, 'period_unit'); $period_type = MS_Helper_Period::get_period_value($membership->pay_cycle_period, 'period_type'); $expire_date = MS_Helper_Period::add_interval($period_unit, $period_type, $start_date); break; } } return apply_filters('ms_model_relationship_calc_expire_date', $expire_date, $this); }
/** * Returns the effective date on which the specified item becomes available. * * @since 1.0.0 * * @param string $item_id The content id to verify dripped access. * @param string $start_date The start date of the member membership. * @return string Date on which the item is revealed (e.g. '2015-02-16') */ public function get_dripped_avail_date($item_id, $start_date = null) { $avail_date = MS_Helper_Period::current_date(); $drip_data = false; if (!is_array($this->dripped)) { $this->dripped = array(); } if (isset($this->dripped[$item_id])) { $drip_data = $this->dripped[$item_id]; } if (is_array($drip_data)) { lib3()->array->equip($drip_data, 'type', 'date', 'delay_unit', 'delay_type'); switch ($drip_data['type']) { case MS_Model_Rule::DRIPPED_TYPE_SPEC_DATE: $avail_date = $drip_data['date']; break; case MS_Model_Rule::DRIPPED_TYPE_FROM_REGISTRATION: if (empty($start_date)) { $start_date = MS_Helper_Period::current_date(null, false); } $period_unit = $drip_data['delay_unit']; $period_type = $drip_data['delay_type']; $avail_date = MS_Helper_Period::add_interval($period_unit, $period_type, $start_date); break; case MS_Model_Rule::DRIPPED_TYPE_INSTANTLY: default: $avail_date = MS_Helper_Period::current_date(); break; } } return apply_filters('ms_rule_get_dripped_avail_date', $avail_date, $item_id, $start_date, $this); }
/** * Test changing the membership payment plan from permanent to recurring. * @test */ function permanent_to_recurring() { $user_id = TData::id('user', 'editor'); $membership_id = TData::id('membership', 'simple'); $subscription = TData::subscribe($user_id, $membership_id); $invoice = $subscription->get_current_invoice(); $invoice->pay_it('stripe', 'external_123'); // Now we have a user that is subscribed to a permanent membership. $start_date = MS_Helper_Period::current_date(); $this->assertEquals($start_date, $subscription->start_date); $this->assertEquals('', $subscription->expire_date); $this->assertTrue($invoice->is_paid()); $this->assertEquals('active', $subscription->status); // This check should not modify the subscription. $subscription->check_membership_status(); $this->assertEquals($start_date, $subscription->start_date); $this->assertEquals('', $subscription->expire_date); $this->assertTrue($invoice->is_paid()); $this->assertEquals('active', $subscription->status); // Now the user changes the membership to recurring. $membership = $subscription->get_membership(); $membership->payment_type = MS_Model_Membership::PAYMENT_TYPE_RECURRING; $membership->pay_cycle_period_unit = 7; $membership->pay_cycle_period_type = 'days'; $membership->save(); $this->assertEquals(MS_Model_Membership::PAYMENT_TYPE_RECURRING, $membership->payment_type); // The membership status check is automaticaly done every six hours. // It will update the subscription details to match the new payment type. $subscription->check_membership_status(); // Confirm that the existing subscription has a correct expire date. $expire_date = MS_Helper_Period::add_interval(7, 'days', $start_date); $this->assertEquals($expire_date, $subscription->expire_date); }