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