| @@ -13,13 +13,12 @@ | ||
| 13 | 13 | /** |
| 14 | 14 | * Prepare a charge object for a Stripe subscription. |
| 15 | 15 | * |
| 16 | 16 | * @since 6.5, introduced in v3.0 of the Stripe add on. |
| 17 | - * @todo I removed the $charge_object->paid = false; line from here is it isn't required for Stripe link. | |
| 18 | - * Make sure that if/when we re-use this in Stripe that we still include that. | |
| 19 | 17 | * |
| 20 | 18 | * @param object $subscription A Stripe Subscription object. |
| 21 | 19 | * @param string $amount |
| 20 | + * | |
| 22 | 21 | * @return stdClass |
| 23 | 22 | */ |
| 24 | 23 | public static function prepare_charge_object_for_subscription( $subscription, $amount ) { |
| 25 | 24 | $charge_object = new stdClass(); |
| @@ -36,9 +35,10 @@ | ||
| 36 | 35 | * |
| 37 | 36 | * @since 6.5 |
| 38 | 37 | * |
| 39 | 38 | * @param array $atts |
| 40 | - * @return int|string $sub_id | |
| 39 | + * | |
| 40 | + * @return int|string Sub ID. | |
| 41 | 41 | */ |
| 42 | 42 | public static function create_new_subscription( $atts ) { |
| 43 | 43 | $atts['charge'] = (object) $atts['charge']; |
| 44 | 44 | |
| @@ -60,8 +60,9 @@ | ||
| 60 | 60 | $atts['action']->post_content['payment_limit'], |
| 61 | 61 | (int) $atts['entry']->form_id, |
| 62 | 62 | (int) $atts['entry']->id |
| 63 | 63 | ); |
| 64 | + | |
| 64 | 65 | if ( is_int( $end_count ) ) { |
| 65 | 66 | $new_values['end_count'] = $end_count; |
| 66 | 67 | } |
| 67 | 68 | } |
| @@ -66,10 +67,9 @@ | ||
| 66 | 67 | } |
| 67 | 68 | } |
| 68 | 69 | |
| 69 | 70 | $frm_sub = new FrmTransLiteSubscription(); |
| 70 | - $sub_id = $frm_sub->create( $new_values ); | |
| 71 | - return $sub_id; | |
| 71 | + return $frm_sub->create( $new_values ); | |
| 72 | 72 | } |
| 73 | 73 | |
| 74 | 74 | /** |
| 75 | 75 | * Get a plan for Stripe subscription. |
| @@ -81,9 +81,10 @@ | ||
| 81 | 81 | * |
| 82 | 82 | * @type WP_Post $action |
| 83 | 83 | * @type string $amount |
| 84 | 84 | * } |
| 85 | - * @return string Plan id. | |
| 85 | + * | |
| 86 | + * @return false|string Plan id. | |
| 86 | 87 | */ |
| 87 | 88 | public static function get_plan_from_atts( $atts ) { |
| 88 | 89 | $action = $atts['action']; |
| 89 | 90 | $action->post_content['amount'] = $atts['amount']; |
| @@ -93,20 +94,23 @@ | ||
| 93 | 94 | /** |
| 94 | 95 | * @since 6.5 |
| 95 | 96 | * |
| 96 | 97 | * @param WP_Post $action |
| 98 | + * | |
| 97 | 99 | * @return false|string |
| 98 | 100 | */ |
| 99 | 101 | private static function get_plan_for_action( $action ) { |
| 100 | 102 | $plan_id = $action->post_content['plan_id']; |
| 101 | - if ( ! $plan_id ) { | |
| 102 | - // The amount has already been formatted, so add the decimal back in. | |
| 103 | - $amount = $action->post_content['amount']; | |
| 104 | - $action->post_content['amount'] = number_format( $amount / 100, 2, '.', '' ); | |
| 105 | - $plan_opts = self::prepare_plan_options( $action->post_content ); | |
| 106 | - $plan_id = self::maybe_create_plan( $plan_opts ); | |
| 103 | + | |
| 104 | + if ( $plan_id ) { | |
| 105 | + return $plan_id; | |
| 107 | 106 | } |
| 108 | - return $plan_id; | |
| 107 | + | |
| 108 | + // The amount has already been formatted, so add the decimal back in. | |
| 109 | + $amount = $action->post_content['amount']; | |
| 110 | + $action->post_content['amount'] = number_format( $amount / 100, 2, '.', '' ); | |
| 111 | + $plan_opts = self::prepare_plan_options( $action->post_content ); | |
| 112 | + return self::maybe_create_plan( $plan_opts ); | |
| 109 | 113 | } |
| 110 | 114 | |
| 111 | 115 | /** |
| 112 | 116 | * @since 6.5 |
| @@ -111,8 +115,9 @@ | ||
| 111 | 115 | /** |
| 112 | 116 | * @since 6.5 |
| 113 | 117 | * |
| 114 | 118 | * @param array $settings |
| 119 | + * | |
| 115 | 120 | * @return array |
| 116 | 121 | */ |
| 117 | 122 | public static function prepare_plan_options( $settings ) { |
| 118 | 123 | $amount = FrmStrpLiteActionsController::prepare_amount( $settings['amount'], $settings ); |
| @@ -121,9 +126,9 @@ | ||
| 121 | 126 | 'amount' => $amount, |
| 122 | 127 | 'interval' => $settings['interval'], |
| 123 | 128 | 'interval_count' => $settings['interval_count'], |
| 124 | 129 | 'currency' => $settings['currency'], |
| 125 | - 'name' => empty( $settings['description'] ) ? $default_description : $settings['description'], | |
| 130 | + 'name' => ! empty( $settings['description'] ) ? $settings['description'] : $default_description, | |
| 126 | 131 | ); |
| 127 | 132 | |
| 128 | 133 | if ( ! empty( $settings['trial_interval_count'] ) ) { |
| 129 | 134 | $plan_opts['trial_period_days'] = self::get_trial_with_default( $settings['trial_interval_count'] ); |
| @@ -137,12 +142,12 @@ | ||
| 137 | 142 | /** |
| 138 | 143 | * @since 3.0 This was moved from FrmStrpLiteActionsController. |
| 139 | 144 | * |
| 140 | 145 | * @param array $plan |
| 146 | + * | |
| 141 | 147 | * @return mixed |
| 142 | 148 | */ |
| 143 | 149 | public static function maybe_create_plan( $plan ) { |
| 144 | - FrmStrpLiteAppHelper::call_stripe_helper_class( 'initialize_api' ); | |
| 145 | 150 | return FrmStrpLiteAppHelper::call_stripe_helper_class( 'maybe_create_plan', $plan ); |
| 146 | 151 | } |
| 147 | 152 | |
| 148 | 153 | /** |
| @@ -152,8 +157,9 @@ | ||
| 152 | 157 | * |
| 153 | 158 | * @since 6.5 |
| 154 | 159 | * |
| 155 | 160 | * @param mixed $trial |
| 161 | + * | |
| 156 | 162 | * @return int |
| 157 | 163 | */ |
| 158 | 164 | private static function get_trial_with_default( $trial ) { |
| 159 | 165 | if ( ! is_numeric( $trial ) ) { |
| @@ -171,12 +177,13 @@ | ||
| 171 | 177 | * @param false|object|string $subscription |
| 172 | 178 | * @param array $charge_data |
| 173 | 179 | * @param WP_Post $action |
| 174 | 180 | * @param int $amount |
| 181 | + * | |
| 175 | 182 | * @return false|object|string |
| 176 | 183 | */ |
| 177 | 184 | public static function maybe_create_missing_plan_and_create_subscription( $subscription, $charge_data, $action, $amount ) { |
| 178 | - if ( ! is_string( $subscription ) || 0 !== strpos( $subscription, 'No such plan: ' ) ) { | |
| 185 | + if ( ! is_string( $subscription ) || ! str_starts_with( $subscription, 'No such plan: ' ) ) { | |
| 179 | 186 | // Only retry when there is a No such plan string error. |
| 180 | 187 | return $subscription; |
| 181 | 188 | } |
| 182 | 189 | |
| @@ -182,10 +189,9 @@ | ||
| 182 | 189 | |
| 183 | 190 | // The full error message looks like "No such plan: '_399_1month_usd". |
| 184 | 191 | $action->post_content['plan_id'] = ''; |
| 185 | 192 | $charge_data['plan'] = self::get_plan_from_atts( compact( 'action', 'amount' ) ); |
| 186 | - $subscription = FrmStrpLiteAppHelper::call_stripe_helper_class( 'create_subscription', $charge_data ); | |
| 187 | - return $subscription; | |
| 193 | + return FrmStrpLiteAppHelper::call_stripe_helper_class( 'create_subscription', $charge_data ); | |
| 188 | 194 | } |
| 189 | 195 | |
| 190 | 196 | /** |
| 191 | 197 | * When this is filtered and returns false, the subscription will be canceled immediately instead. |
| @@ -208,8 +214,9 @@ | ||
| 208 | 214 | * |
| 209 | 215 | * @param string $payment_limit The raw payment value string. It is not empty. |
| 210 | 216 | * @param int $form_id Required for processing shortcodes. |
| 211 | 217 | * @param int $entry_id Required for processing shortcodes. |
| 218 | + * | |
| 212 | 219 | * @return int|WP_Error |
| 213 | 220 | */ |
| 214 | 221 | public static function prepare_payment_limit( $payment_limit, $form_id, $entry_id ) { |
| 215 | 222 | if ( is_numeric( $payment_limit ) ) { |
| @@ -215,9 +222,9 @@ | ||
| 215 | 222 | if ( is_numeric( $payment_limit ) ) { |
| 216 | 223 | return (int) $payment_limit; |
| 217 | 224 | } |
| 218 | 225 | |
| 219 | - if ( false === strpos( $payment_limit, '[' ) ) { | |
| 226 | + if ( ! str_contains( $payment_limit, '[' ) ) { | |
| 220 | 227 | return self::get_invalid_payment_limit_error( $payment_limit ); |
| 221 | 228 | } |
| 222 | 229 | |
| 223 | 230 | $payment_limit = FrmTransLiteAppHelper::process_shortcodes( |
| @@ -226,8 +233,9 @@ | ||
| 226 | 233 | 'form' => $form_id, |
| 227 | 234 | 'entry' => $entry_id, |
| 228 | 235 | ) |
| 229 | 236 | ); |
| 237 | + | |
| 230 | 238 | if ( ! is_numeric( $payment_limit ) ) { |
| 231 | 239 | return self::get_invalid_payment_limit_error( $payment_limit ); |
| 232 | 240 | } |
| 233 | 241 | |
| @@ -237,8 +245,9 @@ | ||
| 237 | 245 | /** |
| 238 | 246 | * @since 6.11 |
| 239 | 247 | * |
| 240 | 248 | * @param string $payment_limit |
| 249 | + * | |
| 241 | 250 | * @return WP_Error |
| 242 | 251 | */ |
| 243 | 252 | private static function get_invalid_payment_limit_error( $payment_limit ) { |
| 244 | 253 | return new WP_Error( |