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