| @@ -24,10 +24,9 @@ | ||
| 24 | 24 | public static function cancel_subscription( $sub_id ) { |
| 25 | 25 | if ( current_user_can( 'administrator' ) ) { |
| 26 | 26 | $customer_id = false; |
| 27 | 27 | } else { |
| 28 | - $user_id = get_current_user_id(); | |
| 29 | - $customer = self::get_customer_by_id( $user_id ); | |
| 28 | + $customer = self::get_customer_by_id( get_current_user_id() ); | |
| 30 | 29 | |
| 31 | 30 | if ( ! is_object( $customer ) ) { |
| 32 | 31 | return false; |
| 33 | 32 | } |
| @@ -33,12 +32,28 @@ | ||
| 33 | 32 | } |
| 34 | 33 | |
| 35 | 34 | $customer_id = $customer->id; |
| 36 | 35 | } |
| 36 | + | |
| 37 | 37 | return FrmStrpLiteConnectHelper::cancel_subscription( $sub_id, $customer_id ); |
| 38 | 38 | } |
| 39 | 39 | |
| 40 | 40 | /** |
| 41 | + * Cancel a subscription without checking that it belongs to the current user. | |
| 42 | + * Use this when there is no logged in user, like when a webhook event is processed. | |
| 43 | + * The customer check in self::cancel_subscription would always fail there because the current user ID is 0. | |
| 44 | + * | |
| 45 | + * @since 6.35 | |
| 46 | + * | |
| 47 | + * @param string $sub_id | |
| 48 | + * | |
| 49 | + * @return bool | |
| 50 | + */ | |
| 51 | + public static function cancel_subscription_without_customer_check( $sub_id ) { | |
| 52 | + return FrmStrpLiteConnectHelper::cancel_subscription( $sub_id ); | |
| 53 | + } | |
| 54 | + | |
| 55 | + /** | |
| 41 | 56 | * @param string $payment_id |
| 42 | 57 | * |
| 43 | 58 | * @return bool |
| 44 | 59 | */ |
| @@ -69,11 +84,10 @@ | ||
| 69 | 84 | * |
| 70 | 85 | * @return object|string |
| 71 | 86 | */ |
| 72 | 87 | public static function get_customer( $options = array() ) { |
| 73 | - $user_id = ! empty( $options['user_id'] ) ? $options['user_id'] : get_current_user_id(); | |
| 74 | - $meta_name = FrmStrpLiteAppHelper::get_customer_id_meta_name(); | |
| 75 | - | |
| 88 | + $user_id = ! empty( $options['user_id'] ) ? $options['user_id'] : get_current_user_id(); | |
| 89 | + $meta_name = FrmStrpLiteAppHelper::get_customer_id_meta_name(); | |
| 76 | 90 | $customer_id_error_message = ''; |
| 77 | 91 | |
| 78 | 92 | if ( $user_id ) { |
| 79 | 93 | $customer_id = get_user_meta( $user_id, $meta_name, true ); |
| @@ -98,9 +112,9 @@ | ||
| 98 | 112 | $a_customer_id_value_was_previously_set = ! empty( $customer_id ); |
| 99 | 113 | $customer_id = FrmStrpLiteConnectHelper::get_customer_id( $options ); |
| 100 | 114 | |
| 101 | 115 | if ( $customer_id ) { |
| 102 | - $customer_id_is_actually_an_error_message = false === strpos( $customer_id, 'cus_' ); | |
| 116 | + $customer_id_is_actually_an_error_message = ! str_contains( $customer_id, 'cus_' ); | |
| 103 | 117 | |
| 104 | 118 | if ( $customer_id_is_actually_an_error_message ) { |
| 105 | 119 | $customer_id_error_message = $customer_id; |
| 106 | 120 | $customer_id = false; |
| @@ -111,9 +125,9 @@ | ||
| 111 | 125 | if ( $a_customer_id_value_was_previously_set ) { |
| 112 | 126 | delete_user_meta( $user_id, $meta_name ); |
| 113 | 127 | } |
| 114 | 128 | |
| 115 | - if ( ! empty( $customer_id_error_message ) ) { | |
| 129 | + if ( $customer_id_error_message ) { | |
| 116 | 130 | return $customer_id_error_message; |
| 117 | 131 | } |
| 118 | 132 | |
| 119 | 133 | return __( 'Unable to retrieve customer through Stripe Connect.', 'formidable' ); |