PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.32.1
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.32.1
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.32.1, at stripe/controllers/FrmStrpLiteAppController.php

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