PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.26.1
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.26.1
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 / square / controllers / FrmSquareLiteAppController.php

FrmSquareLiteAppController.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.26.1, at square/controllers/FrmSquareLiteAppController.php

245 lines 6.4 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 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 ( empty( $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 ( strpos( $amount, '[' ) === false ) {
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 $amount = FrmSquareLiteActionsController::prepare_amount( $amount, compact( 'form', 'entry', 'action' ) );
126
127 return $amount;
128 }
129
130 /**
131 * @param WP_Post $action
132 *
133 * @return array
134 */
135 public static function get_billing_contact( $action ) {
136 $email_setting = $action->post_content['email'];
137 $first_name_setting = $action->post_content['billing_first_name'];
138 $last_name_setting = $action->post_content['billing_last_name'];
139 $address_setting = $action->post_content['billing_address'];
140
141 $entry = self::generate_false_entry();
142 $first_name = $first_name_setting && isset( $entry->metas[ $first_name_setting ] ) ? $entry->metas[ $first_name_setting ] : '';
143 $last_name = $last_name_setting && isset( $entry->metas[ $last_name_setting ] ) ? $entry->metas[ $last_name_setting ] : '';
144 $address = $address_setting && isset( $entry->metas[ $address_setting ] ) ? $entry->metas[ $address_setting ] : '';
145
146 if ( is_array( $first_name ) && isset( $first_name['first'] ) ) {
147 $first_name = $first_name['first'];
148 }
149
150 if ( is_array( $last_name ) && isset( $last_name['last'] ) ) {
151 $last_name = $last_name['last'];
152 }
153
154 $details = array(
155 'givenName' => $first_name,
156 'familyName' => $last_name,
157 );
158
159 if ( $email_setting ) {
160 $shortcode_atts = array(
161 'entry' => $entry,
162 'form' => $action->menu_order,
163 'value' => $email_setting,
164 );
165 $details['email'] = FrmTransLiteAppHelper::process_shortcodes( $shortcode_atts );
166 }
167
168 self::maybe_add_address_data( $details, $address, (int) $address_setting );
169
170 return $details;
171 }
172
173 /**
174 * @since 6.25
175 *
176 * @param array $details
177 * @param array $address
178 * @param int $address_field_id
179 *
180 * @return void
181 */
182 private static function maybe_add_address_data( &$details, $address, $address_field_id ) {
183 if ( ! is_array( $address ) || ! isset( $address['line1'] ) || ! isset( $address['line2'] ) || ! is_callable( 'FrmProAddressesController::get_country_code' ) ) {
184 return;
185 }
186
187 $address_field = FrmField::getOne( $address_field_id );
188
189 if ( ! $address_field ) {
190 return;
191 }
192
193 if ( 'us' === $address_field->field_options['address_type'] ) {
194 $country_code = 'US';
195 } else {
196 $country_code = FrmProAddressesController::get_country_code( $address['country'] );
197 }
198
199 if ( ! $address['line1'] && ! $address['line2'] && ! $address['city'] && ! $address['state'] && ! $address['zip'] && ! $country_code ) {
200 return;
201 }
202
203 $details['addressLines'] = array( $address['line1'], $address['line2'] );
204 $details['city'] = $address['city'];
205 $details['state'] = $address['state'];
206 $details['postalCode'] = $address['zip'];
207 $details['countryCode'] = $country_code;
208 }
209
210 /**
211 * Create an entry object with posted values.
212 *
213 * @since 6.22
214 *
215 * @return stdClass
216 */
217 private static function generate_false_entry() {
218 $entry = new stdClass();
219 $entry->post_id = 0;
220 $entry->id = 0;
221 $entry->item_key = '';
222 $entry->metas = array();
223
224 // phpcs:ignore WordPress.Security.NonceVerification.Missing
225 foreach ( $_POST as $k => $v ) {
226 $k = sanitize_text_field( stripslashes( $k ) );
227 $v = wp_unslash( $v );
228
229 if ( $k === 'item_meta' ) {
230 if ( is_array( $v ) ) {
231 foreach ( $v as $f => $value ) {
232 FrmAppHelper::sanitize_value( 'wp_kses_post', $value );
233 $entry->metas[ absint( $f ) ] = $value;
234 }
235 }
236 } else {
237 FrmAppHelper::sanitize_value( 'wp_kses_post', $v );
238 $entry->{$k} = $v;
239 }
240 }
241
242 return $entry;
243 }
244 }
245