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