Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subscription phase alignment issue for subscriptions with end date #183

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions src/Admin/AdminModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@

$payment->order_id = $order_id;

$payment->set_config_id( \filter_input( \INPUT_POST, 'post_ID', \FILTER_SANITIZE_NUMBER_INT ) );

Check failure on line 422 in src/Admin/AdminModule.php

View workflow job for this annotation

GitHub Actions / phpstan / phpstan

Parameter #1 $config_id of method Pronamic\WordPress\Pay\Payments\PaymentInfo::set_config_id() expects int|null, string|false|null given.

if ( \array_key_exists( 'pronamic_pay_test_payment_method', $_POST ) ) {
$payment_method = \sanitize_text_field( \wp_unslash( $_POST['pronamic_pay_test_payment_method'] ) );
Expand Down Expand Up @@ -533,28 +533,32 @@
$subscription->set_description( $description );
$subscription->set_lines( $payment->get_lines() );

// Phase.
$phase = new SubscriptionPhase(
$subscription,
new DateTimeImmutable(),
new SubscriptionInterval( 'P' . $interval . Util::to_period( $interval_period ) ),
$price
);

// Ends on.
$total_periods = null;

if ( \array_key_exists( 'pronamic_pay_ends_on', $_POST ) ) {
$total_periods = null;

switch ( $_POST['pronamic_pay_ends_on'] ) {
case 'count':
$count = \filter_input( \INPUT_POST, 'pronamic_pay_ends_on_count', \FILTER_VALIDATE_INT );

if ( ! empty( $count ) ) {
$total_periods = $count;
}
$total_periods = (int) \filter_input( \INPUT_POST, 'pronamic_pay_ends_on_count', \FILTER_VALIDATE_INT );

break;
case 'date':
$end_date = \array_key_exists( 'pronamic_pay_ends_on_date', $_POST ) ? \sanitize_text_field( \wp_unslash( $_POST['pronamic_pay_ends_on_date'] ) ) : '';

if ( ! empty( $end_date ) ) {
$interval_spec = 'P' . $interval . Util::to_period( $interval_period );

$period = new \DatePeriod(
new \DateTime(),
new \DateInterval( $interval_spec ),
$phase->get_start_date(),
$phase->get_interval(),
new \DateTime( $end_date )
);

Expand All @@ -563,17 +567,13 @@

break;
}
}

// Phase.
$phase = new SubscriptionPhase(
$subscription,
new DateTimeImmutable(),
new SubscriptionInterval( 'P' . $interval . Util::to_period( $interval_period ) ),
$price
);
if ( null !== $total_periods ) {
$end_date = $phase->get_start_date()->add( $phase->get_interval()->multiply( $total_periods ) );

$phase->set_total_periods( $total_periods );
$phase->set_end_date( $end_date );
}
}

$subscription->add_phase( $phase );

Expand Down
26 changes: 13 additions & 13 deletions src/Subscriptions/SubscriptionPhase.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ private function add_interval( $date, $times = 1 ) {
*
* @param DateTimeImmutable $start_date Start date.
* @return SubscriptionPeriod|null
* @throws \Exception Throws exception on invalid date period.
*/
public function get_period( DateTimeImmutable $start_date = null ) {
if ( null === $start_date ) {
Expand All @@ -487,7 +488,11 @@ public function get_period( DateTimeImmutable $start_date = null ) {
$end_date = $this->add_interval( $start_date );

if ( null !== $this->end_date && $end_date > $this->end_date ) {
return null;
$end_date = $this->end_date;

if ( $start_date > $end_date ) {
throw new \Exception( 'The start date of a subscription period cannot be later than the end date.' );
}
}

$period = new SubscriptionPeriod( $this, $start_date, $end_date, $this->get_amount() );
Expand Down Expand Up @@ -675,24 +680,19 @@ public static function align( self $phase, \DateTimeInterface $align_date ) {

$alignment_phase = new self( $phase->get_subscription(), $start_date, $alignment_interval, $phase->get_amount() );

$alignment_phase->set_total_periods( 1 );
$alignment_phase->set_alignment_rate( $alignment_difference->days / $regular_difference->days );
$alignment_end_date = $start_date->add( $alignment_interval );

// Remove one period from regular phase.
$total_periods = $phase->get_total_periods();
$alignment_phase->set_end_date( $alignment_end_date );
$alignment_phase->set_alignment_rate( $alignment_difference->days / $regular_difference->days );

if ( null !== $total_periods ) {
$phase->set_total_periods( $total_periods - 1 );
}
$phase->set_start_date( $alignment_end_date );

$alignment_end_date = $alignment_phase->get_end_date();
if ( null !== $phase->end_date ) {
$end_date = $phase->end_date->add( $alignment_interval );

if ( null === $alignment_end_date ) {
throw new \Exception( 'The align phase should always end because this phase exists for one period.' );
$phase->set_end_date( $end_date );
}

$phase->set_start_date( $alignment_end_date );

return $alignment_phase;
}
}
Loading