| @@ -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 string|int $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 | |
| @@ -46,18 +46,30 @@ | ||
| 46 | 46 | 'amount' => FrmTransLiteAppHelper::get_formatted_amount_for_currency( $atts['charge']->amount, $atts['action'] ), |
| 47 | 47 | 'paysys' => 'stripe', |
| 48 | 48 | 'item_id' => $atts['entry']->id, |
| 49 | 49 | 'action_id' => $atts['action']->ID, |
| 50 | - 'sub_id' => isset( $atts['charge']->sub_id ) ? $atts['charge']->sub_id : '', | |
| 50 | + 'sub_id' => $atts['charge']->sub_id ?? '', | |
| 51 | 51 | 'interval_count' => $atts['action']->post_content['interval_count'], |
| 52 | 52 | 'time_interval' => $atts['action']->post_content['interval'], |
| 53 | 53 | 'status' => 'active', |
| 54 | 54 | 'next_bill_date' => gmdate( 'Y-m-d' ), |
| 55 | + 'test' => 'test' === FrmStrpLiteAppHelper::active_mode() ? 1 : 0, | |
| 55 | 56 | ); |
| 56 | 57 | |
| 58 | + if ( ! empty( $atts['action']->post_content['payment_limit'] ) ) { | |
| 59 | + $end_count = self::prepare_payment_limit( | |
| 60 | + $atts['action']->post_content['payment_limit'], | |
| 61 | + (int) $atts['entry']->form_id, | |
| 62 | + (int) $atts['entry']->id | |
| 63 | + ); | |
| 64 | + | |
| 65 | + if ( is_int( $end_count ) ) { | |
| 66 | + $new_values['end_count'] = $end_count; | |
| 67 | + } | |
| 68 | + } | |
| 69 | + | |
| 57 | 70 | $frm_sub = new FrmTransLiteSubscription(); |
| 58 | - $sub_id = $frm_sub->create( $new_values ); | |
| 59 | - return $sub_id; | |
| 71 | + return $frm_sub->create( $new_values ); | |
| 60 | 72 | } |
| 61 | 73 | |
| 62 | 74 | /** |
| 63 | 75 | * Get a plan for Stripe subscription. |
| @@ -64,12 +76,15 @@ | ||
| 64 | 76 | * |
| 65 | 77 | * @since 6.5, introduced in v3.0 of the Stripe add on. |
| 66 | 78 | * |
| 67 | 79 | * @param array $atts { |
| 80 | + * The plan details. | |
| 81 | + * | |
| 68 | 82 | * @type WP_Post $action |
| 69 | 83 | * @type string $amount |
| 70 | 84 | * } |
| 71 | - * @return string Plan id. | |
| 85 | + * | |
| 86 | + * @return false|string Plan id. | |
| 72 | 87 | */ |
| 73 | 88 | public static function get_plan_from_atts( $atts ) { |
| 74 | 89 | $action = $atts['action']; |
| 75 | 90 | $action->post_content['amount'] = $atts['amount']; |
| @@ -79,20 +94,23 @@ | ||
| 79 | 94 | /** |
| 80 | 95 | * @since 6.5 |
| 81 | 96 | * |
| 82 | 97 | * @param WP_Post $action |
| 83 | - * @return string|false | |
| 98 | + * | |
| 99 | + * @return false|string | |
| 84 | 100 | */ |
| 85 | 101 | private static function get_plan_for_action( $action ) { |
| 86 | 102 | $plan_id = $action->post_content['plan_id']; |
| 87 | - if ( ! $plan_id ) { | |
| 88 | - // the amount has already been formatted, so add the decimal back in | |
| 89 | - $amount = $action->post_content['amount']; | |
| 90 | - $action->post_content['amount'] = number_format( ( $amount / 100 ), 2, '.', '' ); | |
| 91 | - $plan_opts = self::prepare_plan_options( $action->post_content ); | |
| 92 | - $plan_id = self::maybe_create_plan( $plan_opts ); | |
| 103 | + | |
| 104 | + if ( $plan_id ) { | |
| 105 | + return $plan_id; | |
| 93 | 106 | } |
| 94 | - 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 ); | |
| 95 | 113 | } |
| 96 | 114 | |
| 97 | 115 | /** |
| 98 | 116 | * @since 6.5 |
| @@ -97,19 +115,20 @@ | ||
| 97 | 115 | /** |
| 98 | 116 | * @since 6.5 |
| 99 | 117 | * |
| 100 | 118 | * @param array $settings |
| 119 | + * | |
| 101 | 120 | * @return array |
| 102 | 121 | */ |
| 103 | 122 | public static function prepare_plan_options( $settings ) { |
| 104 | 123 | $amount = FrmStrpLiteActionsController::prepare_amount( $settings['amount'], $settings ); |
| 105 | - $default_description = number_format( ( $amount / 100 ), 2 ) . '/' . $settings['interval']; | |
| 124 | + $default_description = number_format( $amount / 100, 2 ) . '/' . $settings['interval']; | |
| 106 | 125 | $plan_opts = array( |
| 107 | 126 | 'amount' => $amount, |
| 108 | 127 | 'interval' => $settings['interval'], |
| 109 | 128 | 'interval_count' => $settings['interval_count'], |
| 110 | 129 | 'currency' => $settings['currency'], |
| 111 | - 'name' => empty( $settings['description'] ) ? $default_description : $settings['description'], | |
| 130 | + 'name' => ! empty( $settings['description'] ) ? $settings['description'] : $default_description, | |
| 112 | 131 | ); |
| 113 | 132 | |
| 114 | 133 | if ( ! empty( $settings['trial_interval_count'] ) ) { |
| 115 | 134 | $plan_opts['trial_period_days'] = self::get_trial_with_default( $settings['trial_interval_count'] ); |
| @@ -123,12 +142,12 @@ | ||
| 123 | 142 | /** |
| 124 | 143 | * @since 3.0 This was moved from FrmStrpLiteActionsController. |
| 125 | 144 | * |
| 126 | 145 | * @param array $plan |
| 146 | + * | |
| 127 | 147 | * @return mixed |
| 128 | 148 | */ |
| 129 | 149 | public static function maybe_create_plan( $plan ) { |
| 130 | - FrmStrpLiteAppHelper::call_stripe_helper_class( 'initialize_api' ); | |
| 131 | 150 | return FrmStrpLiteAppHelper::call_stripe_helper_class( 'maybe_create_plan', $plan ); |
| 132 | 151 | } |
| 133 | 152 | |
| 134 | 153 | /** |
| @@ -138,13 +157,15 @@ | ||
| 138 | 157 | * |
| 139 | 158 | * @since 6.5 |
| 140 | 159 | * |
| 141 | 160 | * @param mixed $trial |
| 161 | + * | |
| 142 | 162 | * @return int |
| 143 | 163 | */ |
| 144 | 164 | private static function get_trial_with_default( $trial ) { |
| 145 | 165 | if ( ! is_numeric( $trial ) ) { |
| 146 | - $trial = 1; | |
| 166 | + // Use 0 as this is only ever overwritten when it is non-zero. | |
| 167 | + $trial = 0; | |
| 147 | 168 | } |
| 148 | 169 | return absint( $trial ); |
| 149 | 170 | } |
| 150 | 171 | |
| @@ -152,16 +173,17 @@ | ||
| 152 | 173 | * If a subscription fails because the plan does not exist, create the plan and try again. |
| 153 | 174 | * |
| 154 | 175 | * @since 6.5.1 |
| 155 | 176 | * |
| 156 | - * @param object|string|false $subscription | |
| 177 | + * @param false|object|string $subscription | |
| 157 | 178 | * @param array $charge_data |
| 158 | 179 | * @param WP_Post $action |
| 159 | 180 | * @param int $amount |
| 160 | - * @return object|string|false | |
| 181 | + * | |
| 182 | + * @return false|object|string | |
| 161 | 183 | */ |
| 162 | 184 | public static function maybe_create_missing_plan_and_create_subscription( $subscription, $charge_data, $action, $amount ) { |
| 163 | - if ( ! is_string( $subscription ) || 0 !== strpos( $subscription, 'No such plan: ' ) ) { | |
| 185 | + if ( ! is_string( $subscription ) || ! str_starts_with( $subscription, 'No such plan: ' ) ) { | |
| 164 | 186 | // Only retry when there is a No such plan string error. |
| 165 | 187 | return $subscription; |
| 166 | 188 | } |
| 167 | 189 | |
| @@ -167,8 +189,71 @@ | ||
| 167 | 189 | |
| 168 | 190 | // The full error message looks like "No such plan: '_399_1month_usd". |
| 169 | 191 | $action->post_content['plan_id'] = ''; |
| 170 | 192 | $charge_data['plan'] = self::get_plan_from_atts( compact( 'action', 'amount' ) ); |
| 171 | - $subscription = FrmStrpLiteAppHelper::call_stripe_helper_class( 'create_subscription', $charge_data ); | |
| 172 | - return $subscription; | |
| 193 | + return FrmStrpLiteAppHelper::call_stripe_helper_class( 'create_subscription', $charge_data ); | |
| 194 | + } | |
| 195 | + | |
| 196 | + /** | |
| 197 | + * When this is filtered and returns false, the subscription will be canceled immediately instead. | |
| 198 | + * | |
| 199 | + * @since 6.8 | |
| 200 | + * | |
| 201 | + * @return bool | |
| 202 | + */ | |
| 203 | + public static function should_cancel_at_period_end() { | |
| 204 | + /** | |
| 205 | + * @param bool $cancel_at_period_end | |
| 206 | + */ | |
| 207 | + return (bool) apply_filters( 'frm_stripe_cancel_subscription_at_period_end', true ); | |
| 208 | + } | |
| 209 | + | |
| 210 | + /** | |
| 211 | + * Get an end_count value to use for our subscription. | |
| 212 | + * | |
| 213 | + * @since 6.11 | |
| 214 | + * | |
| 215 | + * @param string $payment_limit The raw payment value string. It is not empty. | |
| 216 | + * @param int $form_id Required for processing shortcodes. | |
| 217 | + * @param int $entry_id Required for processing shortcodes. | |
| 218 | + * | |
| 219 | + * @return int|WP_Error | |
| 220 | + */ | |
| 221 | + public static function prepare_payment_limit( $payment_limit, $form_id, $entry_id ) { | |
| 222 | + if ( is_numeric( $payment_limit ) ) { | |
| 223 | + return (int) $payment_limit; | |
| 224 | + } | |
| 225 | + | |
| 226 | + if ( ! str_contains( $payment_limit, '[' ) ) { | |
| 227 | + return self::get_invalid_payment_limit_error( $payment_limit ); | |
| 228 | + } | |
| 229 | + | |
| 230 | + $payment_limit = FrmTransLiteAppHelper::process_shortcodes( | |
| 231 | + array( | |
| 232 | + 'value' => $payment_limit, | |
| 233 | + 'form' => $form_id, | |
| 234 | + 'entry' => $entry_id, | |
| 235 | + ) | |
| 236 | + ); | |
| 237 | + | |
| 238 | + if ( ! is_numeric( $payment_limit ) ) { | |
| 239 | + return self::get_invalid_payment_limit_error( $payment_limit ); | |
| 240 | + } | |
| 241 | + | |
| 242 | + return (int) $payment_limit; | |
| 243 | + } | |
| 244 | + | |
| 245 | + /** | |
| 246 | + * @since 6.11 | |
| 247 | + * | |
| 248 | + * @param string $payment_limit | |
| 249 | + * | |
| 250 | + * @return WP_Error | |
| 251 | + */ | |
| 252 | + private static function get_invalid_payment_limit_error( $payment_limit ) { | |
| 253 | + return new WP_Error( | |
| 254 | + 'invalid_payment_limit', | |
| 255 | + /* translators: %s: Invalid payment limit value title */ | |
| 256 | + sprintf( __( 'Invalid payment limit value %s', 'formidable' ), $payment_limit ) | |
| 257 | + ); | |
| 173 | 258 | } |
| 174 | 259 | } |