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

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