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

202 lines 5.7 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 '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 if ( is_array( $address ) && isset( $address['line1'] ) && isset( $address['line2'] ) && is_callable( 'FrmProAddressesController::get_country_code' ) ) {
159 $details['addressLines'] = array( $address['line1'], $address['line2'] );
160 $details['city'] = $address['city'];
161 $details['state'] = $address['state'];
162 $details['postalCode'] = $address['zip'];
163 $details['countryCode'] = FrmProAddressesController::get_country_code( $address['country'] );
164 }
165
166 return $details;
167 }
168
169 /**
170 * Create an entry object with posted values.
171 *
172 * @since 6.22
173 * @return stdClass
174 */
175 private static function generate_false_entry() {
176 $entry = new stdClass();
177 $entry->post_id = 0;
178 $entry->id = 0;
179 $entry->metas = array();
180
181 // phpcs:ignore WordPress.Security.NonceVerification.Missing
182 foreach ( $_POST as $k => $v ) {
183 $k = sanitize_text_field( stripslashes( $k ) );
184 $v = wp_unslash( $v );
185
186 if ( $k === 'item_meta' ) {
187 if ( is_array( $v ) ) {
188 foreach ( $v as $f => $value ) {
189 FrmAppHelper::sanitize_value( 'wp_kses_post', $value );
190 $entry->metas[ absint( $f ) ] = $value;
191 }
192 }
193 } else {
194 FrmAppHelper::sanitize_value( 'wp_kses_post', $v );
195 $entry->{$k} = $v;
196 }
197 }
198
199 return $entry;
200 }
201 }
202