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

201 lines 5.6 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 * @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 )
93 );
94 }
95
96 /**
97 * Get the amount value for verification.
98 *
99 * @param WP_Post $action
100 * @return string
101 */
102 private static function get_amount_value_for_verification( $action ) {
103 $amount = $action->post_content['amount'];
104 if ( strpos( $amount, '[' ) === false ) {
105 return $amount;
106 }
107
108 $form = FrmForm::getOne( $action->menu_order );
109 if ( ! $form ) {
110 return $amount;
111 }
112
113 // Update amount based on field shortcodes.
114 $entry = self::generate_false_entry();
115 $amount = FrmSquareLiteActionsController::prepare_amount( $amount, compact( 'form', 'entry', 'action' ) );
116
117 return $amount;
118 }
119
120 /**
121 * @param WP_Post $action
122 * @return array
123 */
124 public static function get_billing_contact( $action ) {
125 $email_setting = $action->post_content['email'];
126 $first_name_setting = $action->post_content['billing_first_name'];
127 $last_name_setting = $action->post_content['billing_last_name'];
128 $address_setting = $action->post_content['billing_address'];
129
130 $entry = self::generate_false_entry();
131 $first_name = $first_name_setting && isset( $entry->metas[ $first_name_setting ] ) ? $entry->metas[ $first_name_setting ] : '';
132 $last_name = $last_name_setting && isset( $entry->metas[ $last_name_setting ] ) ? $entry->metas[ $last_name_setting ] : '';
133 $address = $address_setting && isset( $entry->metas[ $address_setting ] ) ? $entry->metas[ $address_setting ] : '';
134
135 if ( is_array( $first_name ) && isset( $first_name['first'] ) ) {
136 $first_name = $first_name['first'];
137 }
138
139 if ( is_array( $last_name ) && isset( $last_name['last'] ) ) {
140 $last_name = $last_name['last'];
141 }
142
143 $details = array(
144 'givenName' => $first_name,
145 'familyName' => $last_name,
146 );
147
148 if ( $email_setting ) {
149 $shortcode_atts = array(
150 'entry' => $entry,
151 'form' => $action->menu_order,
152 'value' => $email_setting,
153 );
154 $details['email'] = FrmTransLiteAppHelper::process_shortcodes( $shortcode_atts );
155 }
156
157 if ( is_array( $address ) && isset( $address['line1'] ) && isset( $address['line2'] ) && is_callable( 'FrmProAddressesController::get_country_code' ) ) {
158 $details['addressLines'] = array( $address['line1'], $address['line2'] );
159 $details['city'] = $address['city'];
160 $details['state'] = $address['state'];
161 $details['postalCode'] = $address['zip'];
162 $details['countryCode'] = FrmProAddressesController::get_country_code( $address['country'] );
163 }
164
165 return $details;
166 }
167
168 /**
169 * Create an entry object with posted values.
170 *
171 * @since 6.22
172 * @return stdClass
173 */
174 private static function generate_false_entry() {
175 $entry = new stdClass();
176 $entry->post_id = 0;
177 $entry->id = 0;
178 $entry->metas = array();
179
180 // phpcs:ignore WordPress.Security.NonceVerification.Missing
181 foreach ( $_POST as $k => $v ) {
182 $k = sanitize_text_field( stripslashes( $k ) );
183 $v = wp_unslash( $v );
184
185 if ( $k === 'item_meta' ) {
186 if ( is_array( $v ) ) {
187 foreach ( $v as $f => $value ) {
188 FrmAppHelper::sanitize_value( 'wp_kses_post', $value );
189 $entry->metas[ absint( $f ) ] = $value;
190 }
191 }
192 } else {
193 FrmAppHelper::sanitize_value( 'wp_kses_post', $v );
194 $entry->{$k} = $v;
195 }
196 }
197
198 return $entry;
199 }
200 }
201