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

251 lines 6.3 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 * Add the gateway for compatibility with the Payments submodule.
73 * This adds the Stripe checkbox option to the list of gateways.
74 *
75 * @param array $gateways
76 *
77 * @return array
78 */
79 public static function add_gateway( $gateways ) {
80 $gateways['stripe'] = array(
81 'label' => 'Stripe',
82 'user_label' => __( 'Payment', 'formidable' ),
83 'class' => 'StrpLite',
84 'recurring' => true,
85 'include' => array(
86 'billing_first_name',
87 'billing_last_name',
88 'credit_card',
89 'billing_address',
90 ),
91 );
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' );
249 }
250 }
251