| @@ -5,11 +5,21 @@ | ||
| 5 | 5 | |
| 6 | 6 | class FrmStrpLiteAppController { |
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | + * Flag to delete the previous pay entry. | |
| 10 | + * | |
| 11 | + * @since 2.08 | |
| 12 | + * | |
| 13 | + * @var bool | |
| 14 | + */ | |
| 15 | + private static $delete_pay_entry = false; | |
| 16 | + | |
| 17 | + /** | |
| 9 | 18 | * Install required tables. |
| 10 | 19 | * |
| 11 | 20 | * @param mixed $old_db_version |
| 21 | + * | |
| 12 | 22 | * @return void |
| 13 | 23 | */ |
| 14 | 24 | public static function install( $old_db_version = false ) { |
| 15 | 25 | FrmTransLiteAppController::install( $old_db_version ); |
| @@ -34,8 +44,9 @@ | ||
| 34 | 44 | ); |
| 35 | 45 | |
| 36 | 46 | $modes = array( 'test', 'live' ); |
| 37 | 47 | $option_name_keys = array( 'account_id', 'client_password', 'server_password', 'details_submitted' ); |
| 48 | + | |
| 38 | 49 | foreach ( $modes as $mode ) { |
| 39 | 50 | foreach ( $option_name_keys as $key ) { |
| 40 | 51 | $options_to_delete[] = 'frm_strp_connect_' . $key . '_' . $mode; |
| 41 | 52 | } |
| @@ -46,36 +57,24 @@ | ||
| 46 | 57 | } |
| 47 | 58 | } |
| 48 | 59 | |
| 49 | 60 | /** |
| 50 | - * Redirect to Stripe settings when payments are not yet installed | |
| 51 | - * and the payments page is accessed by its URL. | |
| 61 | + * Gets payments settings URL. | |
| 52 | 62 | * |
| 53 | - * @return void | |
| 63 | + * @since 6.30 | |
| 64 | + * | |
| 65 | + * @return string | |
| 54 | 66 | */ |
| 55 | - public static function maybe_redirect_to_stripe_settings() { | |
| 56 | - if ( ! FrmAppHelper::is_admin_page( 'formidable-payments' ) || self::payments_are_installed() ) { | |
| 57 | - return; | |
| 58 | - } | |
| 59 | - | |
| 60 | - wp_safe_redirect( admin_url( 'admin.php?page=formidable-settings&t=stripe_settings' ) ); | |
| 61 | - die(); | |
| 67 | + public static function get_payments_settings_url() { | |
| 68 | + return admin_url( 'admin.php?page=formidable-settings&t=stripe_settings' ); | |
| 62 | 69 | } |
| 63 | 70 | |
| 64 | 71 | /** |
| 65 | - * @return bool | |
| 66 | - */ | |
| 67 | - private static function payments_are_installed() { | |
| 68 | - $db = new FrmTransLiteDb(); | |
| 69 | - $option = get_option( $db->db_opt_name ); | |
| 70 | - return false !== $option; | |
| 71 | - } | |
| 72 | - | |
| 73 | - /** | |
| 74 | 72 | * Add the gateway for compatibility with the Payments submodule. |
| 75 | 73 | * This adds the Stripe checkbox option to the list of gateways. |
| 76 | 74 | * |
| 77 | 75 | * @param array $gateways |
| 76 | + * | |
| 78 | 77 | * @return array |
| 79 | 78 | */ |
| 80 | 79 | public static function add_gateway( $gateways ) { |
| 81 | 80 | $gateways['stripe'] = array( |
| @@ -90,6 +89,162 @@ | ||
| 90 | 89 | 'billing_address', |
| 91 | 90 | ), |
| 92 | 91 | ); |
| 93 | 92 | return $gateways; |
| 93 | + } | |
| 94 | + | |
| 95 | + /** | |
| 96 | + * Maybe add payment error to the form errors data. | |
| 97 | + * | |
| 98 | + * @since 6.5.1 | |
| 99 | + * | |
| 100 | + * @param array $errors Errors data. Is empty array if no errors found. | |
| 101 | + * @param array $params Form params. See {@FrmForm::get_params()}. | |
| 102 | + * | |
| 103 | + * @return array | |
| 104 | + */ | |
| 105 | + public static function maybe_add_payment_error( $errors, $params ) { | |
| 106 | + if ( intval( $params['posted_form_id'] ) !== intval( $params['form_id'] ) ) { | |
| 107 | + return self::maybe_add_payment_error_on_redirect( $errors, (int) $params['form_id'] ); | |
| 108 | + } | |
| 109 | + return $errors; | |
| 110 | + } | |
| 111 | + | |
| 112 | + /** | |
| 113 | + * Handle Stripe Link redirect failures. | |
| 114 | + * When a payment fails, the entry is deleted, and the previous entry's values are loaded in the form. | |
| 115 | + * | |
| 116 | + * @since 6.5.1 | |
| 117 | + * | |
| 118 | + * @param array $errors | |
| 119 | + * @param int $form_id | |
| 120 | + * | |
| 121 | + * @return array | |
| 122 | + */ | |
| 123 | + private static function maybe_add_payment_error_on_redirect( $errors, $form_id ) { | |
| 124 | + $details = FrmStrpLiteUrlParamHelper::get_details_for_form( $form_id ); | |
| 125 | + | |
| 126 | + if ( ! is_array( $details ) ) { | |
| 127 | + return $errors; | |
| 128 | + } | |
| 129 | + | |
| 130 | + $entry = $details['entry']; | |
| 131 | + $intent = $details['intent']; | |
| 132 | + $payment = $details['payment']; | |
| 133 | + $payment_failed = FrmStrpLiteAuth::payment_failed( $payment, $intent ); | |
| 134 | + | |
| 135 | + // Only add the payment error if the intent is incomplete. | |
| 136 | + if ( ! $payment_failed ) { | |
| 137 | + return $errors; | |
| 138 | + } | |
| 139 | + | |
| 140 | + $cc_field_id = FrmDb::get_var( | |
| 141 | + 'frm_fields', | |
| 142 | + array( | |
| 143 | + 'type' => 'credit_card', | |
| 144 | + 'form_id' => $entry->form_id, | |
| 145 | + ) | |
| 146 | + ); | |
| 147 | + | |
| 148 | + if ( ! $cc_field_id ) { | |
| 149 | + return $errors; | |
| 150 | + } | |
| 151 | + | |
| 152 | + $is_setup_intent = str_starts_with( $intent->id, 'seti_' ); | |
| 153 | + | |
| 154 | + if ( $is_setup_intent ) { | |
| 155 | + $errors[ 'field' . $cc_field_id ] = is_object( $intent->last_setup_error ) ? $intent->last_setup_error->message : ''; | |
| 156 | + } else { | |
| 157 | + $errors[ 'field' . $cc_field_id ] = is_object( $intent->last_payment_error ) ? $intent->last_payment_error->message : ''; | |
| 158 | + } | |
| 159 | + | |
| 160 | + if ( ! $errors[ 'field' . $cc_field_id ] ) { | |
| 161 | + $errors[ 'field' . $cc_field_id ] = 'Payment was not successfully processed.'; | |
| 162 | + } | |
| 163 | + | |
| 164 | + global $frm_vars; | |
| 165 | + $frm_vars['frm_trans']['pay_entry'] = $entry; | |
| 166 | + | |
| 167 | + self::setup_form_after_payment_error( (int) $entry->form_id, (int) $entry->id, $errors ); | |
| 168 | + | |
| 169 | + add_filter( 'frm_setup_new_fields_vars', 'FrmTransLiteActionsController::fill_entry_from_previous', 20, 2 ); | |
| 170 | + | |
| 171 | + return $errors; | |
| 172 | + } | |
| 173 | + | |
| 174 | + /** | |
| 175 | + * Reset a form after a payment fails. | |
| 176 | + * | |
| 177 | + * @since 6.5.1 | |
| 178 | + * | |
| 179 | + * @param int $form_id | |
| 180 | + * @param int $entry_id | |
| 181 | + * @param array<string,string> $errors | |
| 182 | + * | |
| 183 | + * @return void | |
| 184 | + */ | |
| 185 | + private static function setup_form_after_payment_error( $form_id, $entry_id, $errors ) { | |
| 186 | + $form = FrmForm::getOne( $form_id ); | |
| 187 | + $save_draft = ! empty( $form->options['save_draft'] ); | |
| 188 | + | |
| 189 | + global $frm_vars; | |
| 190 | + $frm_vars['created_entries'][ $form_id ]['errors'] = $errors; | |
| 191 | + | |
| 192 | + // Set to true to get FrmProFieldsHelper::get_page_with_error() run. | |
| 193 | + $_POST[ 'frm_page_order_' . $form_id ] = true; | |
| 194 | + | |
| 195 | + if ( ! $save_draft ) { | |
| 196 | + // If draft saving is not on, delete the entry. | |
| 197 | + self::$delete_pay_entry = true; | |
| 198 | + return; | |
| 199 | + } | |
| 200 | + | |
| 201 | + // If draft saving is on, load the draft entry. | |
| 202 | + $frm_vars['created_entries'][ $form_id ]['entry_id'] = $entry_id; | |
| 203 | + add_action( | |
| 204 | + 'frm_filter_final_form', | |
| 205 | + /** | |
| 206 | + * Set the entry back to draft status after error. | |
| 207 | + * | |
| 208 | + * @param string $html | |
| 209 | + * @param int $entry_id | |
| 210 | + * | |
| 211 | + * @return string | |
| 212 | + */ | |
| 213 | + function ( $html ) use ( $entry_id ) { | |
| 214 | + global $wpdb; | |
| 215 | + $wpdb->update( $wpdb->prefix . 'frm_items', array( 'is_draft' => 1 ), array( 'id' => $entry_id ) ); | |
| 216 | + return $html; | |
| 217 | + } | |
| 218 | + ); | |
| 219 | + } | |
| 220 | + | |
| 221 | + /** | |
| 222 | + * Maybe delete the previous pay entry when error occurs. | |
| 223 | + * | |
| 224 | + * @since 2.08 | |
| 225 | + * | |
| 226 | + * @param array $values Entry edit values. | |
| 227 | + * @param object $field Field object. | |
| 228 | + * | |
| 229 | + * @return array | |
| 230 | + */ | |
| 231 | + public static function maybe_delete_pay_entry( $values, $field ) { | |
| 232 | + if ( self::$delete_pay_entry ) { | |
| 233 | + self::$delete_pay_entry = false; | |
| 234 | + return FrmTransLiteActionsController::fill_entry_from_previous( $values, $field ); | |
| 235 | + } | |
| 236 | + return $values; | |
| 237 | + } | |
| 238 | + | |
| 239 | + /** | |
| 240 | + * Redirect to Stripe settings when payments are not yet installed | |
| 241 | + * and the payments page is accessed by its URL. | |
| 242 | + * | |
| 243 | + * @deprecated 6.33 | |
| 244 | + * | |
| 245 | + * @return void | |
| 246 | + */ | |
| 247 | + public static function maybe_redirect_to_stripe_settings() { | |
| 248 | + _deprecated_function( __METHOD__, '6.33' ); | |
| 94 | 249 | } |
| 95 | 250 | } |