PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.18
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.18
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / stripe / controllers / FrmStrpLiteAppController.php

FrmStrpLiteAppController.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.18, at stripe/controllers/FrmStrpLiteAppController.php

232 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 die( 'You are not allowed to call this page directly.' );
4 }
5
6 class FrmStrpLiteAppController {
7
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 /**
18 * Install required tables.
19 *
20 * @param mixed $old_db_version
21 * @return void
22 */
23 public static function install( $old_db_version = false ) {
24 FrmTransLiteAppController::install( $old_db_version );
25 }
26
27 /**
28 * Remove Stripe database items after uninstall.
29 *
30 * @since 6.5, introduced in v2.07 of the Stripe add on.
31 *
32 * @return void
33 */
34 public static function uninstall() {
35 if ( ! current_user_can( 'administrator' ) ) {
36 $frm_settings = FrmAppHelper::get_settings();
37 wp_die( esc_html( $frm_settings->admin_permission ) );
38 }
39
40 $options_to_delete = array(
41 FrmStrpLiteEventsController::$events_to_skip_option_name,
42 'frm_strp_options',
43 );
44
45 $modes = array( 'test', 'live' );
46 $option_name_keys = array( 'account_id', 'client_password', 'server_password', 'details_submitted' );
47 foreach ( $modes as $mode ) {
48 foreach ( $option_name_keys as $key ) {
49 $options_to_delete[] = 'frm_strp_connect_' . $key . '_' . $mode;
50 }
51 }
52
53 foreach ( $options_to_delete as $option_name ) {
54 delete_option( $option_name );
55 }
56 }
57
58 /**
59 * Redirect to Stripe settings when payments are not yet installed
60 * and the payments page is accessed by its URL.
61 *
62 * @return void
63 */
64 public static function maybe_redirect_to_stripe_settings() {
65 if ( ! FrmAppHelper::is_admin_page( 'formidable-payments' ) || FrmTransLiteAppHelper::payments_table_exists() ) {
66 return;
67 }
68
69 wp_safe_redirect( admin_url( 'admin.php?page=formidable-settings&t=stripe_settings' ) );
70 die();
71 }
72
73 /**
74 * Add the gateway for compatibility with the Payments submodule.
75 * This adds the Stripe checkbox option to the list of gateways.
76 *
77 * @param array $gateways
78 * @return array
79 */
80 public static function add_gateway( $gateways ) {
81 $gateways['stripe'] = array(
82 'label' => 'Stripe',
83 'user_label' => __( 'Payment', 'formidable' ),
84 'class' => 'StrpLite',
85 'recurring' => true,
86 'include' => array(
87 'billing_first_name',
88 'billing_last_name',
89 'credit_card',
90 'billing_address',
91 ),
92 );
93 return $gateways;
94 }
95
96 /**
97 * Maybe add payment error to the form errors data.
98 *
99 * @since 6.5.1
100 *
101 * @param array $errors Errors data. Is empty array if no errors found.
102 * @param array $params Form params. See {@FrmForm::get_params()}.
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 * @return array
121 */
122 private static function maybe_add_payment_error_on_redirect( $errors, $form_id ) {
123 $details = FrmStrpLiteUrlParamHelper::get_details_for_form( $form_id );
124 if ( ! is_array( $details ) ) {
125 return $errors;
126 }
127
128 $entry = $details['entry'];
129 $intent = $details['intent'];
130 $payment = $details['payment'];
131 $payment_failed = FrmStrpLiteAuth::payment_failed( $payment, $intent );
132
133 // Only add the payment error if the intent is incomplete.
134 if ( ! $payment_failed ) {
135 return $errors;
136 }
137
138 $cc_field_id = FrmDb::get_var(
139 'frm_fields',
140 array(
141 'type' => 'credit_card',
142 'form_id' => $entry->form_id,
143 )
144 );
145 if ( ! $cc_field_id ) {
146 return $errors;
147 }
148
149 $is_setup_intent = 0 === strpos( $intent->id, 'seti_' );
150 if ( $is_setup_intent ) {
151 $errors[ 'field' . $cc_field_id ] = is_object( $intent->last_setup_error ) ? $intent->last_setup_error->message : '';
152 } else {
153 $errors[ 'field' . $cc_field_id ] = is_object( $intent->last_payment_error ) ? $intent->last_payment_error->message : '';
154 }
155
156 if ( ! $errors[ 'field' . $cc_field_id ] ) {
157 $errors[ 'field' . $cc_field_id ] = 'Payment was not successfully processed.';
158 }
159
160 global $frm_vars;
161 $frm_vars['frm_trans']['pay_entry'] = $entry;
162
163 self::setup_form_after_payment_error( (int) $entry->form_id, (int) $entry->id, $errors );
164
165 add_filter( 'frm_setup_new_fields_vars', 'FrmTransLiteActionsController::fill_entry_from_previous', 20, 2 );
166
167 return $errors;
168 }
169
170 /**
171 * Reset a form after a payment fails.
172 *
173 * @since 6.5.1
174 *
175 * @param int $form_id
176 * @param int $entry_id
177 * @param array<string,string> $errors
178 * @return void
179 */
180 private static function setup_form_after_payment_error( $form_id, $entry_id, $errors ) {
181 $form = FrmForm::getOne( $form_id );
182 $save_draft = ! empty( $form->options['save_draft'] );
183
184 global $frm_vars;
185 $frm_vars['created_entries'][ $form_id ]['errors'] = $errors;
186
187 // Set to true to get FrmProFieldsHelper::get_page_with_error() run.
188 $_POST[ 'frm_page_order_' . $form_id ] = true;
189
190 if ( ! $save_draft ) {
191 // If draft saving is not on, delete the entry.
192 self::$delete_pay_entry = true;
193 return;
194 }
195
196 // If draft saving is on, load the draft entry.
197 $frm_vars['created_entries'][ $form_id ]['entry_id'] = $entry_id;
198 add_action(
199 'frm_filter_final_form',
200 /**
201 * Set the entry back to draft status after error.
202 *
203 * @param string $html
204 * @param int $entry_id
205 * @return string
206 */
207 function ( $html ) use ( $entry_id ) {
208 global $wpdb;
209 $wpdb->update( $wpdb->prefix . 'frm_items', array( 'is_draft' => 1 ), array( 'id' => $entry_id ) );
210 return $html;
211 }
212 );
213 }
214
215 /**
216 * Maybe delete the previous pay entry when error occurs.
217 *
218 * @since 2.08
219 *
220 * @param array $values Entry edit values.
221 * @param object $field Field object.
222 * @return array
223 */
224 public static function maybe_delete_pay_entry( $values, $field ) {
225 if ( self::$delete_pay_entry ) {
226 self::$delete_pay_entry = false;
227 return FrmTransLiteActionsController::fill_entry_from_previous( $values, $field );
228 }
229 return $values;
230 }
231 }
232