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

Payment method changes should not affect subscription status #197

Closed
rvdsteege opened this issue Nov 12, 2024 · 0 comments · Fixed by #198
Closed

Payment method changes should not affect subscription status #197

rvdsteege opened this issue Nov 12, 2024 · 0 comments · Fixed by #198
Assignees

Comments

@rvdsteege
Copy link
Member

In internal Help Scout ticket https://secure.helpscout.net/conversation/2729838268/27815 it came to our attention that an expired payment for a subscription payment method change, results in a subscription being put 'On Hold'.

I think we can safely ignore payments with the subscription_payment_method_change source in the payment status update handler of the subscriptions module.

/**
* Payment status update.
*
* @param Payment $payment The status updated payment.
* @return void
*/
public function payment_status_update( $payment ) {
foreach ( $payment->get_subscriptions() as $subscription ) {
// Status.
$status_before = $subscription->get_status();
$status_update = $status_before;
switch ( $payment->get_status() ) {
case PaymentStatus::OPEN:
// @todo
break;
case PaymentStatus::SUCCESS:
$status_update = SubscriptionStatus::ACTIVE;
break;
case PaymentStatus::FAILURE:
/**
* Subscription status for failed payment.
*
* @todo Determine update status based on reason of failed payment. Use `failure` for now as that is usually the desired status.
* @link https://www.europeanpaymentscouncil.eu/document-library/guidance-documents/guidance-reason-codes-sepa-direct-debit-r-transactions
* @link https://github.com/pronamic/wp-pronamic-ideal/commit/48449417eac49eb6a93480e3b523a396c7db9b3d#diff-6712c698c6b38adfa7190a4be983a093
*/
$status_update = SubscriptionStatus::ON_HOLD;
break;
case PaymentStatus::CANCELLED:
case PaymentStatus::EXPIRED:
// Set subscription status to 'On Hold' only if the subscription is not already active when processing the first payment.
if ( ! ( $subscription->is_first_payment( $payment ) && SubscriptionStatus::ACTIVE === $subscription->get_status() ) ) {
$status_update = SubscriptionStatus::ON_HOLD;
}
break;
}
/*
* The status of canceled or completed subscriptions will not be changed automatically,
* unless the cancelled subscription is manually being renewed.
*/
$is_renewal = false;
if ( true === $payment->get_meta( 'manual_subscription_renewal' ) && SubscriptionStatus::CANCELLED === $status_before && SubscriptionStatus::ACTIVE === $status_update ) {
$is_renewal = true;
}
if ( $is_renewal || ! in_array( $status_before, [ SubscriptionStatus::CANCELLED, SubscriptionStatus::COMPLETED, SubscriptionStatus::ON_HOLD ], true ) ) {
$subscription->set_status( $status_update );
// Update.
if ( $status_before !== $status_update ) {
$subscription->save();
}
}
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
1 participant