Skip to content

Commit

Permalink
Fix handling subscription payment method changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
rvdsteege committed Feb 4, 2022
1 parent 3862e81 commit 86b5b15
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 30 deletions.
34 changes: 7 additions & 27 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,32 +81,8 @@ public function setup() {

\add_action( 'pronamic_pay_update_payment', array( $this, 'maybe_update_refunded_payment' ), 15, 1 );

\add_action(
'save_post_shop_subscription',
function( $post_id ) {
$woocommerce_subscription = \wcs_get_subscription( $post_id );

if ( false === $woocommerce_subscription ) {
return;
}

$subscription_helper = new SubscriptionHelper( $woocommerce_subscription );

$pronamic_subscription = $subscription_helper->get_pronamic_subscription();

if ( null === $pronamic_subscription ) {
return;
}

$subscription_updater = new SubscriptionUpdater( $woocommerce_subscription, $pronamic_subscription );

$subscription_updater->update_pronamic_subscription();

$pronamic_subscription->save();
},
100,
1
);
\add_action( 'save_post_shop_subscription', array( __NAMESPACE__ . '\SubscriptionUpdater', 'maybe_update_pronamic_subscription' ), 10, 1 );
\add_action( 'woocommerce_subscription_payment_method_updated', array( __NAMESPACE__ . '\SubscriptionUpdater', 'maybe_update_pronamic_subscription' ), 100, 1 );
}

/**
Expand Down Expand Up @@ -649,7 +625,11 @@ public static function status_update( Payment $payment ) {
$order_payment_id = (int) $order->get_meta( '_pronamic_payment_id' );

if ( empty( $order_payment_id ) || $payment->get_id() === $order_payment_id ) {
$order->update_status( $new_status );
try {
$order->update_status( $new_status );
} catch ( \Exception $exception ) {
// Nothing to do.
}
}
}

Expand Down
21 changes: 19 additions & 2 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,15 @@ public function get_pronamic_option( $key ) {
return $value;
}

/**
* Get WordPress Pay payment method.
*
* @return string|null
*/
public function get_wp_payment_method() {
return $this->payment_method;
}

/**
* Initialise form fields
*
Expand Down Expand Up @@ -412,9 +421,13 @@ public function process_payment( $order_id ) {
}
}

// Set Mollie sequence type on payment method change.
if ( \did_action( 'woocommerce_subscription_change_payment_method_via_pay_shortcode' ) ) {
$payment->set_meta( 'mollie_sequence_type', 'first' );
}

// Start payment.
try {
// Start payment.
$this->payment = Plugin::start_payment( $payment );
} catch ( \Exception $exception ) {
WooCommerce::add_notice( Plugin::get_default_error_message(), 'error' );
Expand Down Expand Up @@ -464,7 +477,11 @@ public function process_payment( $order_id ) {
$order->add_order_note( $note );
} elseif ( PaymentStatus::SUCCESS !== $payment->get_status() ) {
// Mark as pending (we're awaiting the payment).
$order->update_status( $new_status_slug, $note );
try {
$order->update_status( $new_status_slug, $note );
} catch ( \Exception $exception ) {
// Nothing to do.
}
}

// Return results array.
Expand Down
51 changes: 50 additions & 1 deletion src/SubscriptionUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,25 @@ public function update_pronamic_subscription() {
$pronamic_subscription->set_source_id( $woocommerce_subscription->get_id() );

// Method.
$pronamic_subscription->set_payment_method( $this->payment_method );
$available_gateways = WC()->payment_gateways()->get_available_payment_gateways();
$payment_method = $woocommerce_subscription->get_payment_method( 'raw' );

if (
\array_key_exists( $payment_method, $available_gateways )
&&
\is_callable( array( $available_gateways[ $payment_method ], 'get_wp_payment_method' ) )
) {
$payment_method = $available_gateways[ $payment_method ]->get_wp_payment_method();

if ( null !== $payment_method ) {
$pronamic_subscription->set_payment_method( $payment_method );
}
}

// Do not update subscription phases when payment method is updated only.
if ( \did_action( 'woocommerce_subscription_change_payment_method_via_pay_shortcode' ) ) {
return;
}

// Description.
$pronamic_subscription->set_description(
Expand Down Expand Up @@ -149,4 +167,35 @@ public function update_pronamic_subscription() {
// Add phase.
$pronamic_subscription->add_phase( $regular_phase );
}

/**
* Maybe update Pronamic subscription for WooCommerce subscription.
*
* @param int $post_id WooCommerce Subscription post ID.
* @return void
*/
public static function maybe_update_pronamic_subscription( $post_id ) {
// Get WooCommerce subscription.
$woocommerce_subscription = \wcs_get_subscription( $post_id );

if ( false === $woocommerce_subscription ) {
return;
}

// Get Pronamic subscription.
$subscription_helper = new SubscriptionHelper( $woocommerce_subscription );

$pronamic_subscription = $subscription_helper->get_pronamic_subscription();

if ( null === $pronamic_subscription ) {
return;
}

// Update Pronamic subscription.
$subscription_updater = new SubscriptionUpdater( $woocommerce_subscription, $pronamic_subscription );

$subscription_updater->update_pronamic_subscription();

$pronamic_subscription->save();
}
}

0 comments on commit 86b5b15

Please sign in to comment.