| 1 |
<?php |
| 2 |
|
| 3 |
namespace Better_Payment\Lite\Admin\Elementor\Form_Actions; |
| 4 |
|
| 5 |
use Better_Payment\Lite\Admin\DB; |
| 6 |
use Better_Payment\Lite\Campaign\CampaignStats; |
| 7 |
use Better_Payment\Lite\Classes\Handler; |
| 8 |
use Better_Payment\Lite\Traits\Helper; |
| 9 |
use Elementor\Controls_Manager; |
| 10 |
use ElementorPro\Modules\Forms\Classes\Action_Base; |
| 11 |
use ElementorPro\Modules\Forms\Classes\Ajax_Handler; |
| 12 |
use ElementorPro\Modules\Forms\Classes\Form_Record; |
| 13 |
|
| 14 |
if ( ! defined( 'ABSPATH' ) ) { |
| 15 |
exit; // Exit if accessed directly |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* PayPal integration class |
| 20 |
* |
| 21 |
* @since 0.0.1 |
| 22 |
*/ |
| 23 |
class Paypal_Integration extends Action_Base { |
| 24 |
use Helper; |
| 25 |
|
| 26 |
private $better_payment_global_settings = []; |
| 27 |
|
| 28 |
public function get_name() { |
| 29 |
return 'paypal'; |
| 30 |
} |
| 31 |
|
| 32 |
public function get_label() { |
| 33 |
return __( 'PayPal', 'better-payment' ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* @param \Elementor\Widget_Base $widget |
| 38 |
*/ |
| 39 |
public function register_settings_section( $widget ) { |
| 40 |
$this->better_payment_global_settings = DB::get_settings(); |
| 41 |
|
| 42 |
$widget->start_controls_section( |
| 43 |
'section_paypal_payment', |
| 44 |
[ |
| 45 |
'label' => __( 'PayPal', 'better-payment' ), |
| 46 |
'condition' => [ |
| 47 |
'submit_actions' => $this->get_name(), |
| 48 |
'better_payment_payment_amount_enable' => 'yes' |
| 49 |
], |
| 50 |
] |
| 51 |
); |
| 52 |
|
| 53 |
$better_payment_helper = new \Better_Payment\Lite\Classes\Helper(); |
| 54 |
|
| 55 |
$widget->add_control( |
| 56 |
'better_payment_form_paypal_currency', |
| 57 |
[ |
| 58 |
'label' => esc_html__( 'Currency', 'better-payment' ), |
| 59 |
'type' => Controls_Manager::SELECT, |
| 60 |
'default' => esc_html($this->better_payment_global_settings['better_payment_settings_general_general_currency']), |
| 61 |
'options' => $better_payment_helper->get_currency_list(), |
| 62 |
] |
| 63 |
); |
| 64 |
|
| 65 |
$widget->add_control( |
| 66 |
'better_payment_paypal_currecny_support_notice', |
| 67 |
[ |
| 68 |
'type' => Controls_Manager::RAW_HTML, |
| 69 |
'raw' => __( 'Currency is not supported by PayPal!', 'better-payment' ), |
| 70 |
'content_classes' => 'elementor-panel-alert elementor-panel-alert-info', |
| 71 |
'condition' => [ |
| 72 |
'better_payment_form_paypal_currency' => $this->bp_unsupported_currencies( 'paypal' ), |
| 73 |
], |
| 74 |
] |
| 75 |
); |
| 76 |
|
| 77 |
$widget->add_control( |
| 78 |
'better_payment_form_currency_alignment_paypal', |
| 79 |
[ |
| 80 |
'label' => esc_html__( 'Currency Alignment', 'better-payment' ), |
| 81 |
'type' => Controls_Manager::CHOOSE, |
| 82 |
'options' => [ |
| 83 |
'left' => [ |
| 84 |
'title' => esc_html__( 'Left', 'better-payment' ), |
| 85 |
'icon' => 'eicon-text-align-left', |
| 86 |
], |
| 87 |
'right' => [ |
| 88 |
'title' => esc_html__( 'Right', 'better-payment' ), |
| 89 |
'icon' => 'eicon-text-align-right', |
| 90 |
], |
| 91 |
], |
| 92 |
] |
| 93 |
); |
| 94 |
|
| 95 |
$widget->add_control( |
| 96 |
'better_payment_paypal_business_email', |
| 97 |
[ |
| 98 |
'label' => __( 'Business Email', 'better-payment' ), |
| 99 |
'type' => current_user_can('manage_options') ? Controls_Manager::TEXT : Controls_Manager::HIDDEN, |
| 100 |
'dynamic' => [ |
| 101 |
'active' => true, |
| 102 |
], |
| 103 |
'label_block' => true, |
| 104 |
'default' => esc_html($this->better_payment_global_settings['better_payment_settings_payment_paypal_email']), |
| 105 |
'ai' => [ |
| 106 |
'active' => false, |
| 107 |
], |
| 108 |
] |
| 109 |
); |
| 110 |
|
| 111 |
$widget->add_control( |
| 112 |
'better_payment_paypal_button_type', |
| 113 |
[ |
| 114 |
'label' => esc_html__( 'Button Type', 'better-payment' ), |
| 115 |
'type' => Controls_Manager::HIDDEN, |
| 116 |
'default' => '_xclick', |
| 117 |
'options' => [ |
| 118 |
'_xclick' => 'XCLICK', |
| 119 |
'_cart' => 'CART', |
| 120 |
'_donations' => 'DONATIONS' |
| 121 |
] |
| 122 |
] |
| 123 |
); |
| 124 |
|
| 125 |
$widget->add_control( |
| 126 |
'better_payment_paypal_live_mode', |
| 127 |
[ |
| 128 |
'label' => __( 'Live Mode', 'better-payment' ), |
| 129 |
'type' => Controls_Manager::SWITCHER, |
| 130 |
'label_on' => __( 'Yes', 'better-payment' ), |
| 131 |
'label_off' => __( 'No', 'better-payment' ), |
| 132 |
'return_value' => 'yes', |
| 133 |
'default' => esc_html($this->better_payment_global_settings['better_payment_settings_payment_paypal_live_mode']), //yes or no |
| 134 |
] |
| 135 |
); |
| 136 |
$widget->end_controls_section(); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* @param array $element |
| 141 |
* @return array |
| 142 |
*/ |
| 143 |
public function on_export( $element ) { |
| 144 |
unset( |
| 145 |
$element[ 'settings' ][ 'better_payment_paypal_business_email' ], |
| 146 |
$element[ 'settings' ][ 'better_payment_paypal_button_type' ], |
| 147 |
$element[ 'settings' ][ 'better_payment_paypal_live_mode' ], |
| 148 |
$element[ 'settings' ][ 'better_payment_form_paypal_currency' ] |
| 149 |
); |
| 150 |
|
| 151 |
return $element; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* @param Form_Record $record |
| 156 |
* @param Ajax_Handler $ajax_handler |
| 157 |
*/ |
| 158 |
public function run( $record, $ajax_handler ) { |
| 159 |
|
| 160 |
$email = $record->get_form_settings( 'better_payment_paypal_business_email' ); |
| 161 |
$button_type = $record->get_form_settings( 'better_payment_paypal_button_type' ); |
| 162 |
$live_mode = $record->get_form_settings( 'better_payment_paypal_live_mode' ); |
| 163 |
|
| 164 |
if ( $live_mode == 'yes' ) { |
| 165 |
$path = "paypal"; |
| 166 |
} else { |
| 167 |
$path = "sandbox.paypal"; |
| 168 |
} |
| 169 |
|
| 170 |
if ( empty( $email ) ) { |
| 171 |
$ajax_handler->add_error_message( 'Business Email is required!' ); |
| 172 |
return false; |
| 173 |
} |
| 174 |
|
| 175 |
// Get submitted Form data |
| 176 |
$sent_data = $record->get( 'sent_data' ); |
| 177 |
|
| 178 |
$el_form_form_settings = $record->get('form_settings'); |
| 179 |
$page_id = $el_form_form_settings['form_post_id'] ? $el_form_form_settings['form_post_id'] : ''; |
| 180 |
$widget_id = $el_form_form_settings['id'] ? $el_form_form_settings['id'] : ''; |
| 181 |
|
| 182 |
if ( empty( $sent_data['payment_amount'] ) && empty( $sent_data['primary_payment_amount_radio'] ) ) { |
| 183 |
$ajax_handler->add_error_message( 'Amount field is required!' ); |
| 184 |
return false; |
| 185 |
} |
| 186 |
|
| 187 |
$amount = isset( $sent_data['payment_amount'] ) ? floatval($sent_data['payment_amount']) : 0; |
| 188 |
|
| 189 |
if ( empty( $amount ) && ! empty( $sent_data['primary_payment_amount_radio'] ) ) { |
| 190 |
$amount = floatval( $sent_data['primary_payment_amount_radio'] ); |
| 191 |
} |
| 192 |
|
| 193 |
$quantity = 1; |
| 194 |
if ( !empty( $sent_data[ 'pay_quantity' ] ) ) { |
| 195 |
$quantity = intval( $sent_data[ 'pay_quantity' ] ); |
| 196 |
} |
| 197 |
|
| 198 |
$return_url = get_the_permalink() . '?better_payment_paypal_status=success&better_payment_widget_id=' . sanitize_text_field( $record->get( 'form_settings' )[ 'id' ] ); |
| 199 |
$cancel_url = get_the_permalink() . '?better_payment_error_status=error&better_payment_widget_id=' . sanitize_text_field( $record->get( 'form_settings' )[ 'id' ] ); |
| 200 |
$order_id = 'paypal_' . uniqid(); |
| 201 |
|
| 202 |
$paypal_unsupported_currencies = $this->bp_unsupported_currencies( 'paypal' ); |
| 203 |
$currency_code = sanitize_text_field( $record->get_form_settings( 'better_payment_form_paypal_currency' ) ); |
| 204 |
if ( is_array( $paypal_unsupported_currencies ) && in_array( $currency_code, $paypal_unsupported_currencies ) ) { |
| 205 |
$ajax_handler->add_error_message( 'Currency is not supported by PayPal!' ); |
| 206 |
return false; |
| 207 |
} |
| 208 |
|
| 209 |
$request_data = [ |
| 210 |
'business' => $email, |
| 211 |
'currency_code' => $currency_code, |
| 212 |
'rm' => '2', |
| 213 |
'return' => esc_url_raw( $return_url ), |
| 214 |
'cancel_return' => esc_url_raw( $cancel_url ), |
| 215 |
'item_number' => $order_id, |
| 216 |
'quantity' => $quantity, |
| 217 |
'item_name' => 'Test Product', |
| 218 |
'amount' => $amount , |
| 219 |
'cmd' => $button_type, |
| 220 |
]; |
| 221 |
|
| 222 |
$customer_name = ! empty( $sent_data['name'] ) ? $sent_data['name'] : ''; |
| 223 |
$customer_name = ! empty( $sent_data['first_name'] ) ? $customer_name . ' ' .$sent_data['first_name'] : $customer_name; |
| 224 |
$customer_name = ! empty( $sent_data['last_name'] ) ? $customer_name . ' ' .$sent_data['last_name'] : $customer_name; |
| 225 |
|
| 226 |
$customer_email = ! empty( $sent_data['email'] ) ? sanitize_text_field( $sent_data['email'] ) : ''; |
| 227 |
$customer_email = ! empty( $sent_data['email_address'] ) ? sanitize_text_field( $sent_data['email_address'] ) : $customer_email; |
| 228 |
|
| 229 |
$better_form_fields = [ |
| 230 |
'primary_first_name' => $customer_name, |
| 231 |
'email' => $customer_email, |
| 232 |
'el_form_fields' => maybe_serialize( $sent_data ), |
| 233 |
'amount' => $currency_code . $amount, |
| 234 |
'referer_page_id' => $page_id, |
| 235 |
'referer_widget_id' => $widget_id, |
| 236 |
'source' => 'paypal' |
| 237 |
]; |
| 238 |
|
| 239 |
// Get campaign_id from form data if available |
| 240 |
$campaign_id = ! empty( $sent_data['campaign_id'] ) ? sanitize_text_field( $sent_data['campaign_id'] ) : ''; |
| 241 |
|
| 242 |
Handler::payment_create( |
| 243 |
[ |
| 244 |
'amount' => $amount, |
| 245 |
'order_id' => $order_id, |
| 246 |
'payment_date' => date( 'Y-m-d H:i:s' ), |
| 247 |
'source' => 'paypal', |
| 248 |
'form_fields_info' => maybe_serialize( $better_form_fields ), |
| 249 |
'currency' => $currency_code, |
| 250 |
'referer' => "elementor-form", |
| 251 |
'campaign_id' => $campaign_id, |
| 252 |
] |
| 253 |
); |
| 254 |
if ( $campaign_id ) { |
| 255 |
CampaignStats::bust_cache( (int) $campaign_id ); |
| 256 |
} |
| 257 |
$paypal_url = "https://www.$path.com/cgi-bin/webscr?"; |
| 258 |
$paypal_url .= http_build_query( $request_data ); |
| 259 |
|
| 260 |
if ( !empty( $paypal_url ) && filter_var( $paypal_url, FILTER_VALIDATE_URL ) ) { |
| 261 |
$ajax_handler->add_response_data( 'redirect_url', esc_url_raw($paypal_url) ); |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|