vippsas / vipps-recurring-woocommerce

Vipps Recurring payments for WooCommerce
GNU Affero General Public License v3.0
6 stars 4 forks source link

Fatal Error in `get_agreement_id_from_order()` When Handling Renewal Orders #88

Closed magnuskl closed 5 months ago

magnuskl commented 5 months ago

Description

A fatal error occurs when get_agreement_id_from_order() is called. The issue seems to stem from the function not specifying ['order_type' => 'any'] as an argument or performing appropriate checks when calling wcs_get_subscriptions_for_order(), leading to a situation where $subscriptions typically becomes null, and get_meta() is subsequently called on a non-existent subscription object.

Steps to Reproduce

  1. Save the following script as test.php:
<?php
$order_id = 123456; // Replace 123456 with an order ID that is not a parent or a switch order.

$subscriptions = wcs_get_subscriptions_for_order($order_id);
print_r($subscriptions);

$subscriptions_with_args = wcs_get_subscriptions_for_order($order_id, ['order_type' => 'any']);
print_r($subscriptions_with_args);
?>
  1. Run the script using wp-cli:
wp eval-file path/to/test.php
  1. Observe the output: When wcs_get_subscriptions_for_order() is called without the $args argument, it should return an empty array. When called with ['order_type' => 'any'] as the $args argument, it should return an array of the associated subscription objects.

Expected Behavior

get_agreement_id_from_order() should handle orders of any type, including renewals, by specifying the order type as any when calling wcs_get_subscriptions_for_order().

Actual Behavior

A fatal error occurs due to a null reference when attempting to call get_meta() on a non-existent subscription object.

Proposed Solution

Modify get_agreement_id_from_order() to specify the order type as any when calling wcs_get_subscriptions_for_order():

$subscriptions = wcs_get_subscriptions_for_order( $order, ['order_type' => 'any'] );

Environment

Additional Context

[22-Mar-2024 19:27:35 UTC] PHP Fatal error:  Uncaught Error: Call to a member function get_meta() on null in /var/www/wp-content/plugins/vipps-recurring-payments-gateway-for-woocommerce/includes/wc-vipps-recurring-helper.php:188
Stack trace:
#0 /var/www/wp-content/plugins/vipps-recurring-payments-gateway-for-woocommerce/includes/wc-vipps-recurring-helper.php(231): WC_Vipps_Recurring_Helper::get_meta()
#1 /var/www/wp-content/plugins/vipps-recurring-payments-gateway-for-woocommerce/includes/wc-gateway-vipps-recurring.php(1964): WC_Vipps_Recurring_Helper::get_agreement_id_from_order()
#2 /var/www/wp-includes/class-wp-hook.php(326): WC_Gateway_Vipps_Recurring->maybe_cancel_due_charge()
#3 /var/www/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters()
#4 /var/www/wp-includes/plugin.php(517): WP_Hook->do_action()
#5 /var/www/wp-content/plugins/woocommerce/includes/class-wc-order.php(411): do_action()
#6 /var/www/wp-content/plugins/woocommerce/includes/class-wc-order.php(253): WC_Order->status_transition()
#7 /var/www/wp-content/plugins/woocommerce/includes/admin/meta-boxes/class-wc-meta-box-order-data.php(768): WC_Order->save()
#8 /var/www/wp-includes/class-wp-hook.php(326): WC_Meta_Box_Order_Data::save()
#9 /var/www/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters()
#10 /var/www/wp-includes/plugin.php(517): WP_Hook->do_action()
#11 /var/www/wp-content/plugins/woocommerce/includes/admin/class-wc-admin-meta-boxes.php(263): do_action()
#12 /var/www/wp-includes/class-wp-hook.php(326): WC_Admin_Meta_Boxes->save_meta_boxes()
#13 /var/www/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters()
#14 /var/www/wp-includes/plugin.php(517): WP_Hook->do_action()
#15 /var/www/wp-includes/post.php(4760): do_action()
#16 /var/www/wp-includes/post.php(4862): wp_insert_post()
#17 /var/www/wp-admin/includes/post.php(453): wp_update_post()
#18 /var/www/wp-admin/post.php(227): edit_post()
#19 {main}
  thrown in /var/www/wp-content/plugins/vipps-recurring-payments-gateway-for-woocommerce/includes/wc-vipps-recurring-helper.php on line 188
Marcuzz commented 5 months ago

Hey Magnus,

Thank you for the incredibly detailed bug report!

I love the amount of effort you put into it 🤩

I've now pushed a fix which'll make it's way into the plugin repository shortly 😄

magnuskl commented 5 months ago

No problem. I am glad that this issue could be resolved.