PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / 2.4.1
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More v2.4.1
2.4.1 2.4.0 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 All 68 releases
better-payment / includes / Admin / Elementor / Form_Actions / Paypal_Integration.php

Paypal_Integration.php in Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More 2.4.1, at includes/Admin/Elementor/Form_Actions/Paypal_Integration.php

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