PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / 2.2.2
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More v2.2.2
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.2 2.2.1 2.2.0 2.1.2 2.1.1 trunk 0.0.1 0.0.2 0.0.3 0.0.4 0.0.5 0.0.6 0.0.7 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 All 66 releases
better-payment / includes / Classes / Actions.php

Actions.php in Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More 2.2.2, at includes/Classes/Actions.php

1,044 lines 51.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Better_Payment\Lite\Classes;
4 use Better_Payment\Lite\Traits\Helper as TraitsHelper;
5
6 /**
7 * Exit if accessed directly
8 */
9 if (!defined('ABSPATH')) {
10 exit;
11 }
12
13 /**
14 * The plugin action class
15 *
16 * @since 0.0.1
17 */
18 class Actions {
19 use TraitsHelper;
20 /**
21 * Constructor
22 *
23 * @since 0.0.1
24 */
25 public function __construct() {
26 add_action( 'admin_post_paypal_form_handle', [ $this, 'paypal_form_handle' ] );
27 add_action( 'admin_post_nopriv_paypal_form_handle', [ $this, 'paypal_form_handle' ] );
28
29 add_action( 'wp_ajax_better_payment_stripe_get_token', [ $this, 'better_payment_stripe_get_token' ] );
30 add_action( 'wp_ajax_nopriv_better_payment_stripe_get_token', [ $this, 'better_payment_stripe_get_token' ] );
31
32 add_action( 'wp_ajax_better_payment_paystack_get_token', [ $this, 'better_payment_paystack_get_token' ] );
33 add_action( 'wp_ajax_nopriv_better_payment_paystack_get_token', [ $this, 'better_payment_paystack_get_token' ] );
34
35 // NOTE: PayPal IPN + verification-poll hooks are registered unconditionally
36 // in better-payment.php (init_plugin), not here — they must run even when
37 // Elementor is inactive (block/campaign payment flow).
38 }
39
40
41 /**
42 * Handle the paypal form
43 *
44 * @since 0.0.1
45 */
46 public function paypal_form_handle() {
47
48 check_admin_referer( 'better-payment-paypal', 'security' );
49
50 if ( !empty( $_POST[ 'better_payment_page_id' ] ) ) {
51 $page_id = intval( $_POST[ 'better_payment_page_id' ], 10 );
52 } else {
53 $this->redirect_previous_page();
54 }
55
56 if ( !empty( $_POST[ 'better_payment_widget_id' ] ) ) {
57 $widget_id = sanitize_text_field( $_POST[ 'better_payment_widget_id' ] );
58 } else {
59 $this->redirect_previous_page();
60 }
61
62 $el_settings = $this->better_payment_widget_settings( $page_id, $widget_id );
63
64 if ( empty( $el_settings ) ) {
65 $this->redirect_previous_page();
66 }
67
68 if ( empty( $el_settings[ 'better_payment_paypal_business_email' ] ) ) {
69 $this->redirect_previous_page();
70 }
71
72 if ( $el_settings[ 'better_payment_paypal_live_mode' ] == 'yes' ) {
73 $path = "paypal";
74 } else {
75 $path = "sandbox.paypal";
76 }
77
78 $el_settings_currency = $el_settings[ 'better_payment_form_currency' ];
79 $woo_product_id = !empty($el_settings["better_payment_form_woocommerce_product_id"]) ? intval($el_settings["better_payment_form_woocommerce_product_id"]) : 0;
80 $woo_product_ids = !empty($el_settings["better_payment_form_woocommerce_product_ids"]) ? $el_settings["better_payment_form_woocommerce_product_ids"] : [0];
81 $fluentcart_product_id = !empty($el_settings["better_payment_form_fluentcart_product_id"]) ? intval($el_settings["better_payment_form_fluentcart_product_id"]) : 0;
82 $fluentcart_product_ids = !empty($el_settings["better_payment_form_fluentcart_product_ids"]) ? $el_settings["better_payment_form_fluentcart_product_ids"] : [0];
83 $is_layout_6 = ! empty( $el_settings[ 'better_payment_form_layout' ] ) && 'layout-6-pro' === $el_settings[ 'better_payment_form_layout' ];
84 $is_fluentcart_layout = $is_layout_6 && ! empty( $el_settings[ 'better_payment_form_layout_6_ecommerce_platform' ] ) && 'fluentcart' === $el_settings[ 'better_payment_form_layout_6_ecommerce_platform' ];
85 $is_woo_layout = $is_layout_6 && ( empty( $el_settings[ 'better_payment_form_layout_6_ecommerce_platform' ] ) || 'woocommerce' === $el_settings[ 'better_payment_form_layout_6_ecommerce_platform' ] );
86
87 if(!empty($el_settings['better_payment_form_currency_use_woocommerce']) && 'yes' === $el_settings['better_payment_form_currency_use_woocommerce'] &&
88 !empty($el_settings['better_payment_form_currency_woocommerce'])){
89 $el_settings_currency = $el_settings['better_payment_form_currency_woocommerce'];
90 }
91 if( !empty($_POST[ 'campaign_currency' ]) ) {
92 $el_settings_currency = sanitize_text_field( $_POST[ 'campaign_currency' ] );
93 }
94
95 $el_settings_currency_symbol = $this->get_currency_symbol( esc_html($el_settings_currency) );
96
97 $primary_payment_amount = isset( $_POST[ 'primary_payment_amount' ] ) ? floatval( $_POST[ 'primary_payment_amount' ] ) : 0;
98
99 if( empty( $_POST['primary_payment_amount'] ) && ! empty( $_POST['primary_payment_amount_radio'] ) ){
100 $primary_payment_amount = floatval( $_POST['primary_payment_amount_radio'] );
101 }
102
103 $primary_payment_amount_quantity = ! empty( $_POST['payment_amount_quantity'] ) ? intval( $_POST[ 'payment_amount_quantity' ] ) : '';
104 if ( $is_woo_layout ) {
105 $primary_payment_amount_quantity = 1;
106 }
107
108 $primary_payment_amount = ! empty( $primary_payment_amount_quantity ) ? $primary_payment_amount * $primary_payment_amount_quantity : $primary_payment_amount;
109
110 $order_id = 'paypal_' . uniqid();
111 $request_data = [
112 'business' => $el_settings[ 'better_payment_paypal_business_email' ],
113 'currency_code' => $el_settings_currency,
114 'rm' => '2',
115 'return' => esc_url_raw( $_POST[ 'return' ] ),
116 'cancel_return' => add_query_arg( 'better_payment_paypal_id', $order_id, esc_url_raw( $_POST[ 'cancel_return' ] ) ),
117 'item_number' => $order_id,
118 'item_name' => ! empty( $el_settings['better_payment_form_title'] ) ? esc_html__( $el_settings['better_payment_form_title'], 'better-payment' ) : esc_html__('Better Payment', 'better-payment'),
119 'amount' => $primary_payment_amount,
120 'cmd' => $el_settings[ 'better_payment_paypal_button_type' ],
121 'notify_url' => admin_url( 'admin-post.php?action=better_payment_paypal_ipn' ),
122 ];
123
124 $product_ids = [
125 'woo_product_ids' => $woo_product_ids,
126 'fluentcart_product_ids' => $fluentcart_product_ids,
127 ];
128
129 $detailed_product_info = $this->get_detailed_product_info( $product_ids );
130
131 //Form fields data to send via email
132 $better_form_fields = [
133 'amount' => sanitize_text_field($el_settings_currency_symbol) . $primary_payment_amount,
134 'referer_page_id' => $page_id,
135 'referer_widget_id' => $widget_id,
136 'woo_product_id' => $woo_product_id,
137 'woo_product_ids' => maybe_serialize( $woo_product_ids ),
138 'fluentcart_product_id' => $fluentcart_product_id,
139 'fluentcart_product_ids' => maybe_serialize( $fluentcart_product_ids ),
140 'source' => 'paypal',
141 'amount_quantity' => ! empty( $primary_payment_amount_quantity ) ? intval( $primary_payment_amount_quantity ) : '',
142 'is_woo_layout' => $is_woo_layout,
143 'is_fluentcart_layout' => $is_fluentcart_layout,
144 'detailed_product_info' => maybe_serialize( $detailed_product_info ),
145 'paypal_business_email' => sanitize_email( $el_settings['better_payment_paypal_business_email'] ),
146 // Persisted so the async PayPal IPN can rebuild the e-mail body + on/off toggle.
147 'email_settings' => Handler::extract_email_settings( $el_settings ),
148 ];
149
150 $better_form_fields = array_merge( $better_form_fields, $this->fetch_better_form_fields($el_settings, $_POST) );
151
152 if ( !empty( $better_form_fields[ 'primary_first_name' ] ) ) {
153 $request_data[ 'primary_first_name' ] = sanitize_text_field( $better_form_fields[ 'primary_first_name' ] );
154 }
155
156 if ( !empty( $better_form_fields[ 'primary_last_name' ] ) ) {
157 $request_data[ 'primary_last_name' ] = sanitize_text_field( $better_form_fields[ 'primary_last_name' ] );
158 }
159
160 if ( !empty( $better_form_fields[ 'primary_email' ] ) ) {
161 $request_data[ 'primary_email' ] = sanitize_email( $better_form_fields[ 'primary_email' ] );
162 }
163
164 if ( !empty( $better_form_fields[ 'primary_reference_number' ] ) ) {
165 $request_data[ 'invoice' ] = sanitize_text_field( $better_form_fields[ 'primary_reference_number' ] );
166 }
167
168 $campaign_id = ! empty( $_POST['campaign_id'] ) ? sanitize_text_field( $_POST['campaign_id'] ) : '';
169
170 Handler::payment_create(
171 [
172 'amount' => floatval( $primary_payment_amount ),
173 'order_id' => $order_id,
174 'payment_date' => date( 'Y-m-d H:i:s' ),
175 'source' => 'paypal',
176 'form_fields_info' => maybe_serialize( $better_form_fields ),
177 'currency' => sanitize_text_field($el_settings_currency),
178 'referer' => "widget",
179 'campaign_id' => $campaign_id,
180 ]
181 );
182 $paypal_url = "https://www.$path.com/cgi-bin/webscr?";
183 $paypal_url .= http_build_query( $request_data );
184 wp_redirect( esc_url_raw($paypal_url) );
185 }
186
187 /**
188 * Get the stripe token
189 *
190 * @since 0.0.1
191 */
192 public function better_payment_stripe_get_token() {
193 if ( !check_admin_referer( 'better-payment', 'security' ) ) {
194 wp_send_json_error();
195 }
196
197 $setting_data_page_id = isset($_POST[ 'setting_data' ]['page_id']) ? intval( $_POST[ 'setting_data' ]['page_id'] ) : 0;
198 $setting_data_widget_id = isset($_POST[ 'setting_data' ]['widget_id']) ? sanitize_text_field( $_POST[ 'setting_data' ]['widget_id'] ) : 0;
199
200 if ( !empty( $setting_data_page_id ) ) {
201 $page_id = $setting_data_page_id;
202 } else {
203 $err_msg = __( 'Page ID is missing', 'better-payment' );
204 wp_send_json_error( esc_html($err_msg) );
205 }
206
207 if ( !empty( $setting_data_widget_id ) ) {
208 $widget_id = sanitize_text_field( $setting_data_widget_id );
209 } else {
210 $err_msg = __( 'Widget ID is missing', 'better-payment' );
211 wp_send_json_error( esc_html($err_msg) );
212 }
213
214 $el_settings = $this->better_payment_widget_settings( $page_id, $widget_id );
215 $is_layout_6 = ! empty( $el_settings[ 'better_payment_form_layout' ] ) && 'layout-6-pro' === $el_settings[ 'better_payment_form_layout' ];
216 $is_fluentcart_layout = $is_layout_6 && ! empty( $el_settings[ 'better_payment_form_layout_6_ecommerce_platform' ] ) && 'fluentcart' === $el_settings[ 'better_payment_form_layout_6_ecommerce_platform' ];
217 $is_woo_layout = $is_layout_6 && ( empty( $el_settings[ 'better_payment_form_layout_6_ecommerce_platform' ] ) || 'woocommerce' === $el_settings[ 'better_payment_form_layout_6_ecommerce_platform' ] );
218
219 $better_payment_keys = [
220 'public_key' => 'yes' === sanitize_text_field( $el_settings[ 'better_payment_stripe_live_mode' ] ) ? sanitize_text_field( $el_settings[ 'better_payment_stripe_public_key_live' ] ) : sanitize_text_field( $el_settings[ 'better_payment_stripe_public_key' ] ),
221 'secret_key' => 'yes' === sanitize_text_field( $el_settings[ 'better_payment_stripe_live_mode' ] ) ? sanitize_text_field( $el_settings[ 'better_payment_stripe_secret_key_live' ] ) : sanitize_text_field( $el_settings[ 'better_payment_stripe_secret_key' ] ),
222 ];
223
224 if ( empty( $el_settings ) ) {
225 wp_send_json_error( esc_html(__( 'Setting Data is missing', 'better-payment' )) );
226 }
227
228 $is_payment_split_payment = ! empty( $el_settings["better_payment_form_payment_type"] ) && 'split-payment' === $el_settings["better_payment_form_payment_type"];
229
230 $amount = isset($_POST['fields']['primary_payment_amount']) ? floatval($_POST['fields']['primary_payment_amount']) : 0;
231
232 if ( empty( $_POST['fields']['primary_payment_amount'] ) && ! empty( $_POST['fields']['primary_payment_amount_radio'] ) ) {
233 $amount = floatval($_POST['fields']['primary_payment_amount_radio']);
234 }
235
236 $amount_quantity = ! empty( $_POST['fields']['payment_amount_quantity'] ) ? intval( $_POST['fields']['payment_amount_quantity'] ) : '';
237 $product_quantities = ! empty( $_POST['fields']['payment_amount_quantity'] ) && is_array( $_POST['fields']['payment_amount_quantity'] ) ? array_map( 'intval', $_POST['fields']['payment_amount_quantity'] ) : [];
238
239 if ( $is_woo_layout || $is_fluentcart_layout ) {
240 $amount_quantity = 1;
241 }
242
243 $amount = ! empty( $amount_quantity ) ? $amount * $amount_quantity : $amount;
244
245 if ( empty( $better_payment_keys['public_key'] ) || empty( $better_payment_keys['secret_key'] ) ) {
246 wp_send_json_error( esc_html(__( 'Stripe Key missing', 'better-payment' )) );
247 }
248
249 $header_info = array(
250 'Authorization' => 'Basic ' . base64_encode( sanitize_text_field( $better_payment_keys['secret_key'] ) . ':' ),
251 'Stripe-Version' => '2019-05-16'
252 );
253
254 $order_id = 'stripe_' . uniqid();
255
256 $el_settings_currency = $el_settings[ 'better_payment_form_currency' ];
257 $woo_product_id = !empty($el_settings["better_payment_form_woocommerce_product_id"]) ? intval($el_settings["better_payment_form_woocommerce_product_id"]) : 0;
258 $woo_product_ids = !empty($el_settings["better_payment_form_woocommerce_product_ids"]) ? $el_settings["better_payment_form_woocommerce_product_ids"] : [0];
259 $fluentcart_product_id = !empty($el_settings["better_payment_form_fluentcart_product_id"]) ? intval($el_settings["better_payment_form_fluentcart_product_id"]) : 0;
260 $fluentcart_product_ids = !empty($el_settings["better_payment_form_fluentcart_product_ids"]) ? $el_settings["better_payment_form_fluentcart_product_ids"] : [0];
261
262 if(!empty($el_settings['better_payment_form_currency_use_woocommerce']) && 'yes' === $el_settings['better_payment_form_currency_use_woocommerce'] &&
263 !empty($el_settings['better_payment_form_currency_woocommerce'])){
264 $el_settings_currency = $el_settings['better_payment_form_currency_woocommerce'];
265 }
266 if( !empty($_POST['fields'][ 'campaign_currency' ]) ) {
267 $el_settings_currency = sanitize_text_field( $_POST['fields'][ 'campaign_currency' ] );
268 }
269
270 $el_settings_currency_symbol = $this->get_currency_symbol( esc_html($el_settings_currency) );
271
272 $request = [
273 'success_url' => add_query_arg( [
274 'better_payment_stripe_status' => 'success',
275 'better_payment_stripe_id' => $order_id,
276 'better_payment_widget_id' => $widget_id
277 ], get_permalink( $setting_data_page_id ) ),
278 'cancel_url' => add_query_arg( [
279 'better_payment_error_status' => 'error',
280 'better_payment_stripe_id' => $order_id,
281 'better_payment_widget_id' => $widget_id
282 ], get_permalink( $setting_data_page_id ) ),
283 'locale' => 'auto',
284 'payment_method_types' => [ 'card' ],
285 'client_reference_id' => time(),
286 'billing_address_collection' => 'required',
287 'metadata' => [
288 'order_id' => $order_id
289 ],
290 'line_items' => [
291 [
292 'amount' => (int) round( $amount * 100 ),
293 'currency' => $el_settings_currency,
294 'name' => ! empty( $el_settings['better_payment_form_title'] ) ? esc_html__( $el_settings['better_payment_form_title'], 'better-payment' ) : esc_html__('Better Payment', 'better-payment'),
295 'quantity' => 1
296 ]
297 ],
298 'payment_intent_data' => [
299 'capture_method' => 'automatic',
300 'description' => ! empty( $el_settings['better_payment_form_title'] ) ? esc_html__( $el_settings['better_payment_form_title'], 'better-payment' ) : esc_html__('Better Payment', 'better-payment'),
301 'metadata' => [
302 'order_id' => $order_id
303 ]
304 ]
305 ];
306
307 $coupon_code = ! empty( $_POST['fields']['primary_coupon_code'] ) ? sanitize_text_field( $_POST['fields']['primary_coupon_code'] ) : '';
308 if ( !empty( $coupon_code ) ) {
309 $request['discounts'] = [
310 [
311 'coupon' => $coupon_code
312 ]
313 ];
314 }
315
316 $is_payment_recurring = ! empty( $_POST['fields']['better_payment_recurring_mode'] ) && 'subscription' === sanitize_text_field( $_POST['fields']['better_payment_recurring_mode'] );
317 $recurring_price_id = ! empty( $_POST['fields']['better_payment_recurring_price_id'] ) ? sanitize_text_field( $_POST['fields']['better_payment_recurring_price_id'] ) : '';
318 $mixed_payment_selection_data = $this->get_mixed_payment_selection_data( $el_settings, $_POST['fields'] );
319
320 if ( is_wp_error( $mixed_payment_selection_data ) ) {
321 wp_send_json_error( $mixed_payment_selection_data->get_error_message() );
322 }
323
324 $is_payment_mixed_recurring = ! empty( $mixed_payment_selection_data['is_mixed_recurring'] );
325 $mixed_dynamic_recurring_price = [];
326
327 if ( $is_payment_mixed_recurring ) {
328 $mixed_dynamic_recurring_price = $this->create_mixed_recurring_stripe_price(
329 $header_info,
330 $amount,
331 $el_settings_currency,
332 $order_id,
333 $widget_id,
334 $mixed_payment_selection_data,
335 $el_settings
336 );
337
338 if ( is_wp_error( $mixed_dynamic_recurring_price ) ) {
339 wp_send_json_error( $mixed_dynamic_recurring_price->get_error_message() );
340 }
341
342 // Reuse existing recurring checkout flow with a runtime-generated recurring Stripe Price.
343 $is_payment_recurring = true;
344 $recurring_price_id = ! empty( $mixed_dynamic_recurring_price['price_id'] ) ? sanitize_text_field( $mixed_dynamic_recurring_price['price_id'] ) : '';
345 }
346
347 $product_ids = [
348 'woo_product_ids' => $woo_product_ids,
349 'fluentcart_product_ids' => $fluentcart_product_ids,
350 ];
351
352 $detailed_product_info = $this->get_detailed_product_info( $product_ids, $product_quantities );
353
354 //Form fields data to send via email
355 $better_form_fields = [
356 'amount' => $el_settings_currency_symbol . $amount,
357 'referer_page_id' => $page_id,
358 'referer_widget_id' => $widget_id,
359 'woo_product_id' => $woo_product_id,
360 'woo_product_ids' => maybe_serialize( $woo_product_ids ),
361 'fluentcart_product_id' => $fluentcart_product_id,
362 'fluentcart_product_ids' => maybe_serialize( $fluentcart_product_ids ),
363 'source' => 'stripe',
364 'amount_quantity' => ! empty( $amount_quantity ) ? $amount_quantity : '',
365 'is_woo_layout' => $is_woo_layout,
366 'is_fluentcart_layout' => $is_fluentcart_layout,
367 'stripe_coupon_code' => $coupon_code,
368 'detailed_product_info' => maybe_serialize( $detailed_product_info ),
369 ];
370
371 $better_form_fields = array_merge( $better_form_fields, $this->fetch_better_form_fields($el_settings, $_POST['fields']) );
372
373 if ( ! empty( $mixed_payment_selection_data['is_payment_mixed'] ) ) {
374 $better_form_fields['is_payment_mixed'] = 1;
375 $better_form_fields['mixed_payment_selected_type'] = ! empty( $mixed_payment_selection_data['selected_type'] ) ? sanitize_text_field( $mixed_payment_selection_data['selected_type'] ) : 'one-time';
376 }
377
378 if ( $is_payment_mixed_recurring ) {
379 $better_form_fields['mixed_payment_selected_type'] = 'recurring';
380 $better_form_fields['mixed_payment_recurring_interval_count'] = ! empty( $mixed_payment_selection_data['interval_count'] ) ? absint( $mixed_payment_selection_data['interval_count'] ) : 1;
381 $better_form_fields['mixed_payment_recurring_interval'] = ! empty( $mixed_payment_selection_data['interval'] ) ? sanitize_text_field( $mixed_payment_selection_data['interval'] ) : 'month';
382 $better_form_fields['mixed_payment_dynamic_price_id'] = ! empty( $mixed_dynamic_recurring_price['price_id'] ) ? sanitize_text_field( $mixed_dynamic_recurring_price['price_id'] ) : '';
383 $better_form_fields['mixed_payment_dynamic_product_id'] = ! empty( $mixed_dynamic_recurring_price['product_id'] ) ? sanitize_text_field( $mixed_dynamic_recurring_price['product_id'] ) : '';
384 }
385
386 if ( !empty( $better_form_fields['primary_first_name'] ) ) {
387 $request[ 'customer_name' ] = $better_form_fields['primary_first_name'];
388 }
389
390 if ( !empty( $better_form_fields['primary_last_name'] ) ) {
391 $request[ 'customer_name' ] = !empty( $request[ 'customer_name' ] ) ? $request[ 'customer_name' ] . ' ' . $better_form_fields['primary_last_name'] : $better_form_fields['primary_last_name'];
392 }
393
394 if ( !empty( $request[ 'customer_name' ] ) ) {
395 $request[ 'metadata' ][ 'customer_name' ] = $request[ 'customer_name' ];
396 $request[ 'payment_intent_data' ][ 'metadata' ][ 'customer_name' ] = $request[ 'customer_name' ];
397 unset( $request[ 'customer_name' ] );
398 }
399
400 if ( !empty( $better_form_fields['primary_email'] ) ) {
401 $request[ 'customer_email' ] = $better_form_fields['primary_email'];
402 $request[ 'metadata' ][ 'customer_email' ] = $request[ 'customer_email' ];
403 $request[ 'payment_intent_data' ][ 'metadata' ][ 'customer_email' ] = $request[ 'customer_email' ];
404
405 }
406
407 if ( $is_payment_split_payment ) {
408 $installment_price_id = ! empty($_POST['fields']['split_payment_installment']) ? sanitize_text_field($_POST['fields']['split_payment_installment']) : '';
409 $split_payment_installments_data = ! empty( $el_settings['better_payment_split_installment_price_ids'] ) ? $el_settings['better_payment_split_installment_price_ids'] : [];
410
411 if ( is_array( $split_payment_installments_data ) && count( $split_payment_installments_data ) ){
412 foreach( $split_payment_installments_data as $split_payment_installment_data ){
413 $item_price_id = ! empty( $split_payment_installment_data[ 'better_payment_split_installment_price_id' ] ) ? sanitize_text_field( $split_payment_installment_data[ 'better_payment_split_installment_price_id' ] ) : '';
414
415 if ( $installment_price_id === $item_price_id ) {
416 $installment_price_iteration = ! empty( $split_payment_installment_data[ 'better_payment_split_installment_iteration' ] ) ? sanitize_text_field( $split_payment_installment_data[ 'better_payment_split_installment_iteration' ] ) : '';
417 break;
418 }
419 }
420 }
421
422 $better_form_fields['is_payment_split_payment'] = 1;
423 $better_form_fields['split_payment_installment_price_id'] = $installment_price_id;
424 $better_form_fields['split_payment_total_amount_price_id'] = $recurring_price_id;
425 $better_form_fields['split_payment_installment_iteration'] = ( !empty( $installment_price_id ) && !empty( $installment_price_iteration ) ) ? $installment_price_iteration : 1;
426 $better_form_fields['split_payment_total_amount'] = $amount * $better_form_fields['split_payment_installment_iteration'];
427 }
428
429 if ( $is_payment_recurring || $is_payment_split_payment ) {
430 $request['mode'] = 'subscription';
431 unset($request['line_items'][0]);
432 unset($request['payment_intent_data']);
433 $request['line_items'][0]['price'] = $is_payment_split_payment && ! empty( $installment_price_id ) ? $installment_price_id : $recurring_price_id;
434 $request['line_items'][0]['quantity'] = 1;
435
436 $better_form_fields['mode'] = $request['mode'];
437 $better_form_fields['recurring_price_id'] = $request['line_items'][0]['price'];
438 }
439
440 #ToDo: Need to prefill name field on stripe checkout
441 $response = wp_safe_remote_post(
442 'https://api.stripe.com/v1/checkout/sessions',
443 array(
444 'method' => 'POST',
445 'headers' => $header_info,
446 'body' => $request,
447 'timeout' => 70,
448 )
449 );
450
451 $response_ar = json_decode( $response[ 'body' ] );
452
453 if ( ! empty( $response_ar->payment_intent ) || ( ! empty( $response_ar->mode ) && 'subscription' === $response_ar->mode ) ) {
454 $campaign_id = ! empty( $_POST['fields']['campaign_id'] ) ? sanitize_text_field( $_POST['fields']['campaign_id'] ) : '';
455
456 Handler::payment_create(
457 [
458 'amount' => $amount,
459 'order_id' => $order_id,
460 'payment_date' => date( 'Y-m-d H:i:s' ),
461 'source' => 'stripe',
462 'transaction_id' => sanitize_text_field($response_ar->payment_intent),
463 'customer_info' => maybe_serialize( $response_ar ),
464 'form_fields_info' => maybe_serialize( $better_form_fields ),
465 'obj_id' => sanitize_text_field($response_ar->id),
466 'status' => sanitize_text_field($response_ar->payment_status),
467 'currency' => $el_settings_currency,
468 'referer' => "widget",
469 'campaign_id' => $campaign_id,
470 ]
471 );
472 wp_send_json_success(
473 [
474 'stripe_data' => sanitize_text_field($response_ar->id),
475 'stripe_public_key' => sanitize_text_field( $better_payment_keys['public_key'] )
476 ]
477 );
478 } else {
479 $error_message = 'Something went wrong!';
480
481 if (isset($response_ar->error)){
482 $error_message = sanitize_text_field($response_ar->error->message);
483 }
484
485 wp_send_json_error( $error_message );
486 }
487 }
488
489 /**
490 * Parse mixed payment selection payload.
491 *
492 * @param array $el_settings
493 * @param array $fields
494 *
495 * @return array|\WP_Error
496 */
497 private function get_mixed_payment_selection_data( $el_settings = [], $fields = [] ) {
498 $selection_data = [
499 'is_payment_mixed' => 0,
500 'selected_type' => 'one-time',
501 'is_mixed_recurring' => 0,
502 'interval_count' => 1,
503 'interval' => 'month',
504 ];
505
506 $is_payment_mixed = ! empty( $el_settings['better_payment_form_payment_type'] ) && 'mixed' === sanitize_text_field( $el_settings['better_payment_form_payment_type'] );
507 if ( ! $is_payment_mixed ) {
508 return $selection_data;
509 }
510
511 $selection_data['is_payment_mixed'] = 1;
512 $selected_type = ! empty( $fields['better_payment_mixed_selected_type'] ) ? sanitize_text_field( $fields['better_payment_mixed_selected_type'] ) : '';
513 if ( ! empty( $fields['payment_type'] ) && in_array( $fields['payment_type'], [ 'one-time', 'recurring' ], true ) ) {
514 $selected_type = sanitize_text_field( $fields['payment_type'] );
515 }
516
517 $selected_type = in_array( $selected_type, [ 'one-time', 'recurring' ], true ) ? $selected_type : 'one-time';
518 $selection_data['selected_type'] = $selected_type;
519 $selection_data['is_mixed_recurring'] = 'recurring' === $selected_type ? 1 : 0;
520
521 if ( ! $selection_data['is_mixed_recurring'] ) {
522 return $selection_data;
523 }
524
525 $allowed_intervals = [ 'day', 'week', 'month', 'year' ];
526 $mixed_interval_value = ! empty( $fields['better_payment_mixed_interval'] ) ? sanitize_text_field( $fields['better_payment_mixed_interval'] ) : '';
527
528 if ( empty( $mixed_interval_value ) ) {
529 $interval_type = ! empty( $fields['better_payment_mixed_interval_type'] ) ? sanitize_key( $fields['better_payment_mixed_interval_type'] ) : '';
530
531 if ( empty( $interval_type ) ) {
532 if ( ! empty( $fields['better_payment_mixed_interval_day'] ) ) {
533 $interval_type = 'day';
534 } elseif ( ! empty( $fields['better_payment_mixed_interval_week'] ) ) {
535 $interval_type = 'week';
536 } elseif ( ! empty( $fields['better_payment_mixed_interval_month'] ) ) {
537 $interval_type = 'month';
538 } elseif ( ! empty( $fields['better_payment_mixed_interval_year'] ) ) {
539 $interval_type = 'year';
540 }
541 }
542
543 $interval_type = in_array( $interval_type, $allowed_intervals, true ) ? $interval_type : '';
544 $interval_count = 0;
545
546 switch ( $interval_type ) {
547 case 'day':
548 $interval_count = ! empty( $fields['better_payment_mixed_interval_day'] ) ? absint( $fields['better_payment_mixed_interval_day'] ) : 0;
549 break;
550 case 'week':
551 $interval_count = ! empty( $fields['better_payment_mixed_interval_week'] ) ? absint( $fields['better_payment_mixed_interval_week'] ) : 0;
552 break;
553 case 'year':
554 $interval_count = ! empty( $fields['better_payment_mixed_interval_year'] ) ? absint( $fields['better_payment_mixed_interval_year'] ) : 0;
555 break;
556 case 'month':
557 default:
558 $interval_count = ! empty( $fields['better_payment_mixed_interval_month'] ) ? absint( $fields['better_payment_mixed_interval_month'] ) : 0;
559 break;
560 }
561
562 if ( ! empty( $interval_count ) && ! empty( $interval_type ) ) {
563 $mixed_interval_value = "{$interval_count}|{$interval_type}";
564 }
565 }
566
567 if ( empty( $mixed_interval_value ) || false === strpos( $mixed_interval_value, '|' ) ) {
568 return new \WP_Error( 'mixed_interval_missing', __( 'Recurring time period is required for mixed recurring payment.', 'better-payment' ) );
569 }
570
571 list( $interval_count_raw, $interval_raw ) = array_pad( explode( '|', $mixed_interval_value, 2 ), 2, '' );
572 $interval_count = absint( $interval_count_raw );
573 $interval = sanitize_key( $interval_raw );
574
575 if ( empty( $interval_count ) || ! in_array( $interval, $allowed_intervals, true ) ) {
576 return new \WP_Error( 'mixed_interval_invalid', __( 'Invalid recurring time period selected for mixed payment.', 'better-payment' ) );
577 }
578
579 $selection_data['interval_count'] = $interval_count;
580 $selection_data['interval'] = $interval;
581
582 return $selection_data;
583 }
584
585 /**
586 * Create a runtime Stripe recurring Price (and Product) for mixed recurring payments.
587 *
588 * @param array $header_info
589 * @param float $amount
590 * @param string $currency
591 * @param string $order_id
592 * @param string $widget_id
593 * @param array $selection_data
594 * @param array $el_settings
595 *
596 * @return array|\WP_Error
597 */
598 private function create_mixed_recurring_stripe_price( $header_info = [], $amount = 0, $currency = '', $order_id = '', $widget_id = '', $selection_data = [], $el_settings = [] ) {
599 $unit_amount = (int) round( floatval( $amount ) * 100 );
600 if ( $unit_amount < 1 ) {
601 return new \WP_Error( 'mixed_amount_invalid', __( 'Payment Amount field is required!', 'better-payment' ) );
602 }
603
604 $product_id = ! empty( $el_settings['better_payment_one_time_recurring_product_id'] )
605 ? sanitize_text_field( $el_settings['better_payment_one_time_recurring_product_id'] )
606 : '';
607
608 if ( empty( $product_id ) ) {
609 return new \WP_Error(
610 'mixed_product_id_missing',
611 __( 'Stripe Product ID is required for mixed recurring payments. Please provide it in the widget settings.', 'better-payment' )
612 );
613 }
614
615 $interval = ! empty( $selection_data['interval'] ) ? sanitize_key( $selection_data['interval'] ) : 'month';
616 $interval_count = ! empty( $selection_data['interval_count'] ) ? absint( $selection_data['interval_count'] ) : 1;
617 $currency = ! empty( $currency ) ? strtolower( sanitize_text_field( $currency ) ) : 'usd';
618
619 $form_name = ! empty( $el_settings['better_payment_form_title'] ) ? sanitize_text_field( $el_settings['better_payment_form_title'] ) : __( 'Better Payment', 'better-payment' );
620
621 $interval_label = $interval;
622 if ( $interval_count > 1 ) {
623 $interval_label = $interval_count . ' ' . $interval . 's';
624 }
625
626 $common_metadata = [
627 'order_id' => sanitize_text_field( $order_id ),
628 'widget_id' => sanitize_text_field( $widget_id ),
629 'is_mixed_payment' => '1',
630 'mixed_interval' => $interval,
631 'mixed_interval_count' => strval( $interval_count ),
632 ];
633
634 $price_request = [
635 'currency' => $currency,
636 'unit_amount' => $unit_amount,
637 'recurring' => [
638 'interval' => $interval,
639 'interval_count' => $interval_count,
640 ],
641 'product' => $product_id,
642 'nickname' => sprintf( '%s - %s', $form_name, $interval_label ),
643 'metadata' => $common_metadata,
644 ];
645
646 $response = wp_safe_remote_post(
647 'https://api.stripe.com/v1/prices',
648 [
649 'method' => 'POST',
650 'headers' => $header_info,
651 'body' => $price_request,
652 'timeout' => 70,
653 ]
654 );
655
656 if ( is_wp_error( $response ) ) {
657 return new \WP_Error( 'mixed_price_request_failed', $response->get_error_message() );
658 }
659
660 $response_body = wp_remote_retrieve_body( $response );
661 $response_ar = ! empty( $response_body ) ? json_decode( $response_body ) : null;
662 $error_message = ! empty( $response_ar->error->message ) ? sanitize_text_field( $response_ar->error->message ) : __( 'Unable to create recurring Stripe price for mixed payment.', 'better-payment' );
663
664 if ( empty( $response_ar ) || empty( $response_ar->id ) ) {
665 return new \WP_Error( 'mixed_price_create_failed', $error_message );
666 }
667
668 return [
669 'price_id' => sanitize_text_field( $response_ar->id ),
670 'product_id' => $product_id,
671 ];
672 }
673
674 /**
675 * Get the paystack token
676 *
677 * @since 0.0.1
678 */
679 public function better_payment_paystack_get_token() {
680 if ( !check_admin_referer( 'better-payment', 'security' ) ) {
681 wp_send_json_error();
682 }
683
684 $error_message = '';
685
686 if( empty( $_POST[ 'setting_data' ]['page_id'] ) ){
687 $error_message = __( 'Page ID is missing', 'better-payment' );
688 }
689
690 if( empty( $_POST[ 'setting_data' ]['widget_id'] ) ){
691 $error_message = __( 'Widget ID is missing', 'better-payment' );
692 }
693
694 if( ! empty( $error_message ) ){
695 wp_send_json_error( esc_html( $error_message ) );
696 }
697
698 $page_id = intval( $_POST[ 'setting_data' ]['page_id'] );
699 $widget_id = sanitize_text_field( $_POST[ 'setting_data' ]['widget_id'] );
700 $el_settings = $this->better_payment_widget_settings( $page_id, $widget_id );
701 $is_layout_6 = ! empty( $el_settings[ 'better_payment_form_layout' ] ) && 'layout-6-pro' === $el_settings[ 'better_payment_form_layout' ];
702 $is_fluentcart_layout = $is_layout_6 && ! empty( $el_settings[ 'better_payment_form_layout_6_ecommerce_platform' ] ) && 'fluentcart' === $el_settings[ 'better_payment_form_layout_6_ecommerce_platform' ];
703 $is_woo_layout = $is_layout_6 && ( empty( $el_settings[ 'better_payment_form_layout_6_ecommerce_platform' ] ) || 'woocommerce' === $el_settings[ 'better_payment_form_layout_6_ecommerce_platform' ] );
704
705 if ( empty( $el_settings ) ) {
706 wp_send_json_error( esc_html(__( 'Setting Data is missing', 'better-payment' )) );
707 }
708
709 if ( empty( $el_settings[ 'better_payment_paystack_public_key' ] ) || empty( $el_settings[ 'better_payment_paystack_secret_key' ] ) ) {
710 wp_send_json_error( esc_html(__( 'Paystack Key missing', 'better-payment' )) );
711 }
712
713 $amount = isset($_POST[ 'fields' ][ 'primary_payment_amount' ]) ? floatval($_POST[ 'fields' ][ 'primary_payment_amount' ]) : 0;
714
715 if ( empty( $_POST[ 'fields' ][ 'primary_payment_amount' ] ) && ! empty( $_POST[ 'fields' ][ 'primary_payment_amount_radio' ] ) ) {
716 $amount = floatval($_POST[ 'fields' ][ 'primary_payment_amount_radio' ]);
717 }
718
719 $amount_quantity = ! empty( $_POST['fields']['payment_amount_quantity'] ) ? intval( $_POST['fields']['payment_amount_quantity'] ) : '';
720 if ( $is_woo_layout ) {
721 $amount_quantity = 1;
722 }
723
724 $amount = ! empty( $amount_quantity ) ? $amount * $amount_quantity : $amount;
725
726 $header_info = array(
727 'Authorization' => 'Bearer ' . sanitize_text_field( $el_settings[ 'better_payment_paystack_secret_key' ] ),
728 "Cache-Control: no-cache",
729 );
730
731 $order_id = 'paystack_' . uniqid();
732
733 $el_settings_currency = $el_settings[ 'better_payment_form_currency' ];
734 $woo_product_id = !empty($el_settings["better_payment_form_woocommerce_product_id"]) ? intval($el_settings["better_payment_form_woocommerce_product_id"]) : 0;
735 $woo_product_ids = !empty($el_settings["better_payment_form_woocommerce_product_ids"]) ? $el_settings["better_payment_form_woocommerce_product_ids"] : [0];
736 $fluentcart_product_id = !empty($el_settings["better_payment_form_fluentcart_product_id"]) ? intval($el_settings["better_payment_form_fluentcart_product_id"]) : 0;
737 $fluentcart_product_ids = !empty($el_settings["better_payment_form_fluentcart_product_ids"]) ? $el_settings["better_payment_form_fluentcart_product_ids"] : [0];
738
739 if(!empty($settings['better_payment_form_currency_use_woocommerce']) && 'yes' === $el_settings['better_payment_form_currency_use_woocommerce'] &&
740 !empty($settings['better_payment_form_currency_woocommerce'])){
741 $el_settings_currency = $el_settings['better_payment_form_currency_woocommerce'];
742 }
743 if( !empty($_POST['fields'][ 'campaign_currency' ]) ) {
744 $el_settings_currency = sanitize_text_field( $_POST['fields'][ 'campaign_currency' ] );
745 }
746
747 $el_settings_currency_symbol = $this->get_currency_symbol( esc_html($el_settings_currency) );
748
749 $redirection_url_success = get_permalink( $page_id );
750 $redirection_url_error = get_permalink( $page_id );
751
752 $request = [
753 'amount' => ( $amount * 100 ),
754 'currency' => $el_settings_currency,
755 'cart_id' => $order_id,
756 'callback_url' => add_query_arg( [
757 'better_payment_paystack_status' => 'success',
758 'better_payment_paystack_id' => $order_id,
759 'better_payment_widget_id' => $widget_id
760 ], $redirection_url_success ),
761 'metadata' => [
762 'cancel_action' => add_query_arg( [
763 'better_payment_error_status' => 'error',
764 'better_payment_paystack_id' => $order_id,
765 'better_payment_widget_id' => $widget_id
766 ], $redirection_url_error )
767 ]
768 ];
769
770 $product_ids = [
771 'woo_product_ids' => $woo_product_ids,
772 'fluentcart_product_ids' => $fluentcart_product_ids,
773 ];
774
775 $detailed_product_info = $this->get_detailed_product_info( $product_ids );
776
777 //Form fields data to send via email
778 $better_form_fields = [
779 'amount' => $el_settings_currency_symbol . $amount,
780 'referer_page_id' => $page_id,
781 'referer_widget_id' => $widget_id,
782 'woo_product_id' => $woo_product_id,
783 'woo_product_ids' => maybe_serialize( $woo_product_ids ),
784 'fluentcart_product_id' => $fluentcart_product_id,
785 'fluentcart_product_ids' => maybe_serialize( $fluentcart_product_ids ),
786 'source' => 'paystack',
787 'amount_quantity' => ! empty( $amount_quantity ) ? $amount_quantity : '',
788 'is_woo_layout' => $is_woo_layout,
789 'is_fluentcart_layout' => $is_fluentcart_layout,
790 'detailed_product_info' => maybe_serialize( $detailed_product_info ),
791 ];
792
793 $better_form_fields = array_merge( $better_form_fields, $this->fetch_better_form_fields($el_settings, $_POST['fields']) );
794
795 if ( !empty( $better_form_fields['primary_email'] ) ) {
796 $request[ 'email' ] = $better_form_fields['primary_email'];
797 }
798
799 if ( !empty( $better_form_fields['primary_first_name'] ) ) {
800 $request[ 'customer_name' ] = $better_form_fields['primary_first_name'];
801 }
802
803 if ( !empty( $better_form_fields['primary_last_name'] ) ) {
804 $request[ 'customer_name' ] = !empty( $request[ 'customer_name' ] ) ? $request[ 'customer_name' ] . ' ' . $better_form_fields['primary_last_name'] : $better_form_fields['primary_last_name'];
805 }
806
807 if ( !empty( $request[ 'customer_name' ] ) ) {
808 $request[ 'metadata' ][ 'customer_name' ] = $request[ 'customer_name' ];
809 unset( $request[ 'customer_name' ] );
810 }
811
812 $response = wp_safe_remote_post(
813 'https://api.paystack.co/transaction/initialize',
814 array(
815 'method' => 'POST',
816 'headers' => $header_info,
817 'body' => $request,
818 'timeout' => 70,
819 )
820 );
821
822 $response_ar = json_decode( $response[ 'body' ] );
823
824 if( empty( $response_ar->status ) || empty( $response_ar->data ) ){
825 $error_message = $response_ar->message ? sanitize_text_field($response_ar->message) : 'Something went wrong!';
826
827 if (isset($response_ar->error)){
828 $error_message = sanitize_text_field($response_ar->error->message);
829 }
830
831 wp_send_json_error( $error_message );
832 }
833
834 $campaign_id = ! empty( $_POST['fields']['campaign_id'] ) ? sanitize_text_field( $_POST['fields']['campaign_id'] ) : '';
835
836 Handler::payment_create(
837 [
838 'amount' => $amount,
839 'order_id' => $order_id,
840 'payment_date' => date( 'Y-m-d H:i:s' ),
841 'source' => 'paystack',
842 'transaction_id' => '',
843 'customer_info' => maybe_serialize( $response_ar ),
844 'form_fields_info' => maybe_serialize( $better_form_fields ),
845 'status' => 'unpaid',
846 'currency' => $el_settings_currency,
847 'referer' => "widget",
848 'campaign_id' => $campaign_id,
849 ]
850 );
851
852 $authorization_url = ! empty( $response_ar->data->authorization_url ) ? esc_url_raw( $response_ar->data->authorization_url ) : '';
853
854 wp_send_json_success(
855 [
856 'authorization_url' => $authorization_url,
857 ]
858 );
859 }
860
861 /**
862 * Get detailed product information
863 * @param array $product_ids Array of woo_product_ids and fluentcart_product_ids
864 *
865 * @since 1.4.5
866 */
867 public function get_detailed_product_info( $product_ids, $product_quantities = [] ) {
868 $detailed_product_info = [];
869
870 if (function_exists('wc_get_product') && !empty($product_ids['woo_product_ids'])) {
871 foreach ($product_ids['woo_product_ids'] as $key => $product_id) {
872 if (empty($product_id)) continue;
873
874 $product = wc_get_product($product_id);
875 if ($product) {
876 $product_image_src_array = wp_get_attachment_image_src( get_post_thumbnail_id( $product->get_id() ), 'single-post-thumbnail' );
877 $product_image_src = is_array( $product_image_src_array ) && count( $product_image_src_array ) ? $product_image_src_array[0] : '';
878
879 $quantity = !empty($product_quantities[$key]) ? intval($product_quantities[$key]) : 1;
880 $price = floatval( $product->get_price() );
881 $total_price = floatval( $price * $quantity );
882
883 $detailed_product_info['woo_products'][$product_id] = [
884 'name' => $product->get_name(),
885 'product_id' => intval( $product_id ),
886 'permalink' => esc_url( get_permalink($product->get_id()) ),
887 'image_src' => esc_url( $product_image_src ),
888 'price' => $price,
889 'quantity' => $quantity,
890 'total_price' => $total_price,
891 ];
892 }
893 }
894 }
895
896 if (function_exists('fluentCart') && class_exists('\FluentCart\App\Models\Product') && !empty($product_ids['fluentcart_product_ids'])) {
897 foreach ($product_ids['fluentcart_product_ids'] as $key => $product_id) {
898 if (empty($product_id)) continue;
899
900 try {
901 $fluentcart_product = \FluentCart\App\Models\Product::query()
902 ->with(['detail', 'variants'])
903 ->find($product_id);
904
905 if ($fluentcart_product) {
906 $product_price = 0;
907 if (!empty($fluentcart_product->variants) && count($fluentcart_product->variants) > 0) {
908 $product_price = $fluentcart_product->variants[0]->item_price;
909 } elseif (!empty($fluentcart_product->detail)) {
910 $product_price = $fluentcart_product->detail->min_price;
911 }
912
913 if ( ! empty( $product_price ) ) {
914 $product_price = floatval( $product_price / 100 );
915 }
916
917 $product_image_src_array = wp_get_attachment_image_src( get_post_thumbnail_id( $fluentcart_product->ID ), 'single-post-thumbnail' );
918 $product_image_src = is_array( $product_image_src_array ) && count( $product_image_src_array ) ? $product_image_src_array[0] : '';
919
920 $quantity = !empty($product_quantities[$key]) ? intval($product_quantities[$key]) : 1;
921 $total_price = floatval( $product_price * $quantity );
922
923 $detailed_product_info['fluentcart_products'][$product_id] = [
924 'name' => sanitize_text_field( $fluentcart_product->post_title ),
925 'product_id' => intval( $product_id ),
926 'permalink' => esc_url( get_permalink($fluentcart_product->ID) ),
927 'image_src' => esc_url( $product_image_src ),
928 'price' => $product_price,
929 'quantity' => $quantity,
930 'total_price' => $total_price,
931 ];
932 }
933 } catch (\Exception $e) {
934 //
935 }
936 }
937 }
938
939 return $detailed_product_info;
940 }
941
942 /**
943 * Widget settings
944 *
945 * @since 1.0.0
946 */
947 public function better_payment_widget_settings( $page_id, $widget_id ) {
948 $settings = $this->get_elementor_widget_settings( $page_id, $widget_id );
949
950 return $settings;
951 }
952
953 /**
954 * Redirect to referer page
955 *
956 * @since 1.0.0
957 */
958 public function redirect_previous_page() {
959 $location = $_SERVER[ 'HTTP_REFERER' ];
960 wp_safe_redirect( $location );
961 exit();
962 }
963
964 /**
965 * Fetch form fields
966 *
967 * @since 0.0.4
968 */
969
970 public function fetch_better_form_fields($el_settings, $post_data_form_fields) {
971 $better_form_fields = array();
972
973 $better_payment_helper_obj = new Helper();
974 $post_data_primary_first_name = '';
975 $post_data_primary_last_name = '';
976 $post_data_primary_email = '';
977
978 $post_fields = $post_data_form_fields;
979
980 $layout = ! empty( $el_settings['better_payment_form_layout'] ) ? sanitize_text_field( $el_settings['better_payment_form_layout'] ) : 'layout-1';
981
982 switch( $layout ) {
983 case 'layout-4-pro':
984 $el_settings['better_payment_form_fields'] = $el_settings['better_payment_form_fields_layout_4_5_6'];
985 break;
986
987 case 'layout-5-pro':
988 $el_settings['better_payment_form_fields'] = $el_settings['better_payment_form_fields_layout_4_5_6_desc'];
989 break;
990
991 case 'layout-6-pro':
992 $el_settings['better_payment_form_fields'] = $el_settings['better_payment_form_fields_layout_4_5_6_woo'];
993 break;
994
995 default:
996 break;
997 }
998
999 //in case of duplicate post name using the first one
1000 if(isset($el_settings['better_payment_form_fields']) && count($el_settings['better_payment_form_fields'])){
1001 foreach ($el_settings['better_payment_form_fields'] as $item) {
1002
1003 $item_field_name = $better_payment_helper_obj->titleToSnake($item["better_payment_field_name_heading"]);
1004 if(!empty($item['better_payment_primary_field_type']) ){
1005 $item_primary_field_type = $item['better_payment_primary_field_type'];
1006
1007 if( isset($post_fields[$item_primary_field_type]) ) {
1008 $post_fields[$item_primary_field_type] = is_array($post_fields[$item_primary_field_type]) ? $post_fields[$item_primary_field_type][0] : $post_fields[$item_primary_field_type];
1009 }
1010
1011 if( isset($post_fields[$item_field_name]) ) {
1012 $post_fields[$item_field_name] = is_array($post_fields[$item_field_name]) ? $post_fields[$item_field_name][0] : $post_fields[$item_field_name];
1013 }
1014
1015 if ( 'primary_first_name' == $item_primary_field_type && isset($post_fields[$item_primary_field_type])){
1016 $better_form_fields[$item_primary_field_type] = sanitize_text_field($post_fields[$item_primary_field_type]);
1017 $post_data_primary_first_name = sanitize_text_field($post_fields[$item_primary_field_type]);
1018
1019 }else if ( 'primary_last_name' == $item_primary_field_type && isset($post_fields[$item_primary_field_type])){
1020 $better_form_fields[$item_primary_field_type] = sanitize_text_field($post_fields[$item_primary_field_type]);
1021 $post_data_primary_last_name = sanitize_text_field($post_fields[$item_primary_field_type]);
1022
1023 }else if( 'primary_email' == $item_primary_field_type && isset($post_fields[$item_primary_field_type])){
1024 $better_form_fields[$item_primary_field_type] = sanitize_email($post_fields[$item_primary_field_type]);
1025 $post_data_primary_email = sanitize_email($post_fields[$item_primary_field_type]);
1026
1027 }else if ( 'primary_payment_amount' == $item_primary_field_type && isset($post_fields[$item_primary_field_type]) ){
1028 $better_form_fields[$item_primary_field_type] = floatval($post_fields[$item_primary_field_type]);
1029
1030 }else if ( 'primary_reference_number' == $item_primary_field_type && isset($post_fields[$item_primary_field_type]) ){
1031 $better_form_fields[$item_primary_field_type] = sanitize_text_field($post_fields[$item_primary_field_type]);
1032
1033 } else {
1034 $better_form_fields[$item_field_name] = !empty($post_fields[$item_field_name]) ? sanitize_text_field($post_fields[$item_field_name]) : '';
1035 }
1036
1037 }
1038 }
1039 }
1040
1041 return $better_form_fields;
1042 }
1043 }
1044