| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
die( 'You are not allowed to call this page directly.' ); |
| 4 |
} |
| 5 |
|
| 6 |
class FrmSquareLiteAppController { |
| 7 |
|
| 8 |
/** |
| 9 |
* Add the gateway for compatibility with the Payments submodule. |
| 10 |
* This adds the Stripe checkbox option to the list of gateways. |
| 11 |
* |
| 12 |
* @param array $gateways |
| 13 |
* |
| 14 |
* @return array |
| 15 |
*/ |
| 16 |
public static function add_gateway( $gateways ) { |
| 17 |
$gateways['square'] = array( |
| 18 |
'label' => 'Square', |
| 19 |
'user_label' => __( 'Payment', 'formidable' ), |
| 20 |
'class' => 'SquareLite', |
| 21 |
'recurring' => true, |
| 22 |
'include' => array( |
| 23 |
'billing_first_name', |
| 24 |
'billing_last_name', |
| 25 |
'credit_card', |
| 26 |
'billing_address', |
| 27 |
), |
| 28 |
); |
| 29 |
return $gateways; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Handle the request to initialize with Square Api |
| 34 |
* |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
public static function handle_oauth() { |
| 38 |
FrmAppHelper::permission_check( 'frm_change_settings' ); |
| 39 |
|
| 40 |
if ( ! check_admin_referer( 'frm_ajax', 'nonce' ) ) { |
| 41 |
wp_send_json_error(); |
| 42 |
} |
| 43 |
|
| 44 |
$redirect_url = FrmSquareLiteConnectHelper::get_oauth_redirect_url(); |
| 45 |
|
| 46 |
if ( false === $redirect_url ) { |
| 47 |
wp_send_json_error( 'Unable to connect to Square successfully' ); |
| 48 |
} |
| 49 |
|
| 50 |
$response_data = array( |
| 51 |
'redirect_url' => $redirect_url, |
| 52 |
); |
| 53 |
wp_send_json_success( $response_data ); |
| 54 |
} |
| 55 |
|
| 56 |
public static function handle_disconnect() { |
| 57 |
FrmAppHelper::permission_check( 'frm_change_settings' ); |
| 58 |
|
| 59 |
if ( ! check_admin_referer( 'frm_ajax', 'nonce' ) ) { |
| 60 |
wp_send_json_error(); |
| 61 |
} |
| 62 |
|
| 63 |
FrmSquareLiteConnectHelper::handle_disconnect(); |
| 64 |
wp_send_json_success(); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Handle the verify buyer action. |
| 69 |
* |
| 70 |
* @return void |
| 71 |
*/ |
| 72 |
public static function verify_buyer() { |
| 73 |
check_ajax_referer( 'frm_square_ajax', 'nonce' ); |
| 74 |
|
| 75 |
$form_id = FrmAppHelper::get_post_param( 'form_id', 0, 'absint' ); |
| 76 |
|
| 77 |
if ( ! $form_id ) { |
| 78 |
wp_send_json_error( __( 'Invalid form ID', 'formidable' ) ); |
| 79 |
} |
| 80 |
|
| 81 |
$actions = FrmSquareLiteActionsController::get_actions_before_submit( $form_id ); |
| 82 |
|
| 83 |
if ( ! $actions ) { |
| 84 |
wp_send_json_error( __( 'No Square actions found for this form', 'formidable' ) ); |
| 85 |
} |
| 86 |
|
| 87 |
$action = reset( $actions ); |
| 88 |
$verification_details = array( |
| 89 |
'amount' => self::get_amount_value_for_verification( $action ), |
| 90 |
'billingContact' => self::get_billing_contact( $action ), |
| 91 |
'currencyCode' => strtoupper( $action->post_content['currency'] ), |
| 92 |
'intent' => 'CHARGE', |
| 93 |
); |
| 94 |
|
| 95 |
wp_send_json_success( |
| 96 |
array( |
| 97 |
'verificationDetails' => $verification_details, |
| 98 |
'hash' => md5( serialize( $verification_details ) ), |
| 99 |
) |
| 100 |
); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Get the amount value for verification. |
| 105 |
* |
| 106 |
* @param WP_Post $action |
| 107 |
* |
| 108 |
* @return string |
| 109 |
*/ |
| 110 |
private static function get_amount_value_for_verification( $action ) { |
| 111 |
$amount = $action->post_content['amount']; |
| 112 |
|
| 113 |
if ( ! str_contains( $amount, '[' ) ) { |
| 114 |
return $amount; |
| 115 |
} |
| 116 |
|
| 117 |
$form = FrmForm::getOne( $action->menu_order ); |
| 118 |
|
| 119 |
if ( ! $form ) { |
| 120 |
return $amount; |
| 121 |
} |
| 122 |
|
| 123 |
// Update amount based on field shortcodes. |
| 124 |
$entry = self::generate_false_entry(); |
| 125 |
|
| 126 |
return FrmSquareLiteActionsController::prepare_amount( $amount, compact( 'form', 'entry', 'action' ) ); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* @param WP_Post $action |
| 131 |
* |
| 132 |
* @return array |
| 133 |
*/ |
| 134 |
public static function get_billing_contact( $action ) { |
| 135 |
$email_setting = $action->post_content['email']; |
| 136 |
$first_name_setting = $action->post_content['billing_first_name']; |
| 137 |
$last_name_setting = $action->post_content['billing_last_name']; |
| 138 |
$address_setting = $action->post_content['billing_address']; |
| 139 |
|
| 140 |
$entry = self::generate_false_entry(); |
| 141 |
$first_name = $first_name_setting && isset( $entry->metas[ $first_name_setting ] ) ? $entry->metas[ $first_name_setting ] : ''; |
| 142 |
$last_name = $last_name_setting && isset( $entry->metas[ $last_name_setting ] ) ? $entry->metas[ $last_name_setting ] : ''; |
| 143 |
$address = $address_setting && isset( $entry->metas[ $address_setting ] ) ? $entry->metas[ $address_setting ] : ''; |
| 144 |
|
| 145 |
if ( is_array( $first_name ) && isset( $first_name['first'] ) ) { |
| 146 |
$first_name = $first_name['first']; |
| 147 |
} |
| 148 |
|
| 149 |
if ( is_array( $last_name ) && isset( $last_name['last'] ) ) { |
| 150 |
$last_name = $last_name['last']; |
| 151 |
} |
| 152 |
|
| 153 |
$details = array( |
| 154 |
'givenName' => $first_name, |
| 155 |
'familyName' => $last_name, |
| 156 |
); |
| 157 |
|
| 158 |
if ( $email_setting ) { |
| 159 |
$shortcode_atts = array( |
| 160 |
'entry' => $entry, |
| 161 |
'form' => $action->menu_order, |
| 162 |
'value' => $email_setting, |
| 163 |
); |
| 164 |
$details['email'] = FrmTransLiteAppHelper::process_shortcodes( $shortcode_atts ); |
| 165 |
} |
| 166 |
|
| 167 |
self::maybe_add_address_data( $details, $address, (int) $address_setting ); |
| 168 |
|
| 169 |
return $details; |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* @since 6.25 |
| 174 |
* |
| 175 |
* @param array $details |
| 176 |
* @param array $address |
| 177 |
* @param int $address_field_id |
| 178 |
* |
| 179 |
* @return void |
| 180 |
*/ |
| 181 |
private static function maybe_add_address_data( &$details, $address, $address_field_id ) { |
| 182 |
if ( ! is_array( $address ) || ! isset( $address['line1'] ) || ! isset( $address['line2'] ) || ! is_callable( 'FrmProAddressesController::get_country_code' ) ) { |
| 183 |
return; |
| 184 |
} |
| 185 |
|
| 186 |
$address_field = FrmField::getOne( $address_field_id ); |
| 187 |
|
| 188 |
if ( ! $address_field ) { |
| 189 |
return; |
| 190 |
} |
| 191 |
|
| 192 |
if ( 'us' === $address_field->field_options['address_type'] ) { |
| 193 |
$country_code = 'US'; |
| 194 |
} else { |
| 195 |
$country_code = FrmProAddressesController::get_country_code( $address['country'] ); |
| 196 |
} |
| 197 |
|
| 198 |
if ( ! $address['line1'] && ! $address['line2'] && ! $address['city'] && ! $address['state'] && ! $address['zip'] && ! $country_code ) { |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
$details['addressLines'] = array( $address['line1'], $address['line2'] ); |
| 203 |
$details['city'] = $address['city']; |
| 204 |
$details['state'] = $address['state']; |
| 205 |
$details['postalCode'] = $address['zip']; |
| 206 |
$details['countryCode'] = $country_code; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Create an entry object with posted values. |
| 211 |
* |
| 212 |
* @since 6.22 |
| 213 |
* |
| 214 |
* @return stdClass |
| 215 |
*/ |
| 216 |
private static function generate_false_entry() { |
| 217 |
$entry = new stdClass(); |
| 218 |
$entry->post_id = 0; |
| 219 |
$entry->id = 0; |
| 220 |
$entry->item_key = ''; |
| 221 |
$entry->metas = array(); |
| 222 |
|
| 223 |
// phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 224 |
foreach ( $_POST as $k => $v ) { |
| 225 |
$k = sanitize_text_field( stripslashes( $k ) ); |
| 226 |
$v = wp_unslash( $v ); |
| 227 |
|
| 228 |
if ( $k !== 'item_meta' ) { |
| 229 |
FrmAppHelper::sanitize_value( 'wp_kses_post', $v ); |
| 230 |
$entry->{$k} = $v; |
| 231 |
continue; |
| 232 |
} |
| 233 |
|
| 234 |
if ( ! is_array( $v ) ) { |
| 235 |
continue; |
| 236 |
} |
| 237 |
|
| 238 |
foreach ( $v as $f => $value ) { |
| 239 |
FrmAppHelper::sanitize_value( 'wp_kses_post', $value ); |
| 240 |
$entry->metas[ absint( $f ) ] = $value; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
return $entry; |
| 245 |
} |
| 246 |
} |
| 247 |
|