PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / 0.0.3
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More v0.0.3
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 / Classes / Better_Payment_Actions.php

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

343 lines 13.0 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
5 /**
6 * Exit if accessed directly
7 */
8 if (!defined('ABSPATH')) {
9 exit;
10 }
11
12 /**
13 * The plugin action class
14 *
15 * @since 0.0.1
16 */
17 class Better_Payment_Actions {
18 /**
19 * Constructor
20 *
21 * @since 0.0.1
22 */
23 public function __construct() {
24 add_action( 'admin_post_paypal_form_handle', [ $this, 'paypal_form_handle' ] );
25 add_action( 'admin_post_nopriv_paypal_form_handle', [ $this, 'paypal_form_handle' ] );
26
27 add_action( 'wp_ajax_better_payment_stripe_get_token', [ $this, 'better_payment_stripe_get_token' ] );
28 add_action( 'wp_ajax_nopriv_better_payment_stripe_get_token', [ $this, 'better_payment_stripe_get_token' ] );
29 }
30
31
32 /**
33 * Handle the paypal form
34 *
35 * @since 0.0.1
36 */
37 public function paypal_form_handle() {
38 check_admin_referer( 'better-payment-paypal', 'security' );
39
40 if ( !empty( $_POST[ 'better_payment_page_id' ] ) ) {
41 $page_id = intval( $_POST[ 'better_payment_page_id' ], 10 );
42 } else {
43 $this->redirect_previous_page();
44 }
45
46 if ( !empty( $_POST[ 'better_payment_widget_id' ] ) ) {
47 $widget_id = sanitize_text_field( $_POST[ 'better_payment_widget_id' ] );
48 } else {
49 $this->redirect_previous_page();
50 }
51
52 $el_settings = $this->better_payment_widget_settings( $page_id, $widget_id );
53
54 if ( empty( $el_settings ) ) {
55 $this->redirect_previous_page();
56 }
57
58 if ( empty( $el_settings[ 'better_payment_paypal_business_email' ] ) ) {
59 $this->redirect_previous_page();
60 }
61
62 if ( $el_settings[ 'better_payment_paypal_live_mode' ] == 'yes' ) {
63 $path = "paypal";
64 } else {
65 $path = "sandbox.paypal";
66 }
67
68 $order_id = 'paypal_' . uniqid();
69 $request_data = [
70 'business' => $el_settings[ 'better_payment_paypal_business_email' ],
71 'currency_code' => $el_settings[ 'better_payment_form_currency' ],
72 'rm' => '2',
73 'return' => esc_url_raw( $_POST[ 'return' ] ),
74 'cancel_return' => esc_url_raw( $_POST[ 'cancel_return' ] ),
75 'item_number' => $order_id,
76 'item_name' => sanitize_text_field( $el_settings[ 'better_payment_form_title' ] ),
77 'amount' => floatval( $_POST[ 'payment_amount' ] ),
78 'cmd' => $el_settings[ 'better_payment_paypal_button_type' ],
79 ];
80
81 if ( !empty( $_POST[ 'first_name' ] ) ) {
82 $request_data[ 'first_name' ] = sanitize_text_field( $_POST[ 'first_name' ] );
83 }
84
85 if ( !empty( $_POST[ 'last_name' ] ) ) {
86 $request_data[ 'last_name' ] = sanitize_text_field( $_POST[ 'last_name' ] );
87 }
88
89 if ( !empty( $_POST[ 'email' ] ) ) {
90 $request_data[ 'email' ] = sanitize_email( $_POST[ 'email' ] );
91 }
92
93 //Form fields data to send via email
94 $better_form_fields = [
95 'first_name' => !empty( $_POST[ 'first_name' ] ) ? sanitize_text_field( $_POST[ 'first_name' ] ) : '' ,
96 'last_name' => !empty( $_POST[ 'last_name' ] ) ? sanitize_text_field( $_POST[ 'last_name' ] ) : '',
97 'email' => !empty( $_POST[ 'email' ] ) ? sanitize_email( $_POST[ 'email' ] ) : '',
98 'amount' => $el_settings[ 'better_payment_form_currency' ] . floatval( $_POST[ 'payment_amount' ] ),
99 'referer_page_id' => $page_id,
100 'referer_widget_id' => $widget_id,
101 'source' => 'paypal'
102 ];
103
104 Better_Payment_Handler::payment_create(
105 [
106 'amount' => floatval( $_POST[ 'payment_amount' ] ),
107 'order_id' => $order_id,
108 'payment_date' => date( 'Y-m-d H:i:s' ),
109 'source' => 'paypal',
110 'form_fields_info' => maybe_serialize( $better_form_fields ),
111 'currency' => $el_settings[ 'better_payment_form_currency' ],
112 'referer' => "widget",
113 ]
114 );
115 $paypal_url = "https://www.$path.com/cgi-bin/webscr?";
116 $paypal_url .= http_build_query( $request_data );
117 wp_redirect( esc_url_raw($paypal_url) );
118 }
119
120 /**
121 * Get the stripe token
122 *
123 * @since 0.0.1
124 */
125 public function better_payment_stripe_get_token() {
126 if ( !check_admin_referer( 'better-payment', 'security' ) ) {
127 wp_send_json_error();
128 }
129
130 $setting_data_page_id = isset($_POST[ 'setting_data' ]['page_id']) ? intval( $_POST[ 'setting_data' ]['page_id'] ) : 0;
131 $setting_data_widget_id = isset($_POST[ 'setting_data' ]['widget_id']) ? sanitize_text_field( $_POST[ 'setting_data' ]['widget_id'] ) : 0;
132
133 if ( !empty( $setting_data_page_id ) ) {
134 $page_id = $setting_data_page_id;
135 } else {
136 $err_msg = __( 'Page ID is missing', 'better-payment' );
137 wp_send_json_error( esc_html($err_msg) );
138 }
139
140 if ( !empty( $setting_data_widget_id ) ) {
141 $widget_id = sanitize_text_field( $setting_data_widget_id );
142 } else {
143 $err_msg = __( 'Widget ID is missing', 'better-payment' );
144 wp_send_json_error( esc_html($err_msg) );
145 }
146
147 $el_settings = $this->better_payment_widget_settings( $page_id, $widget_id );
148 if ( empty( $el_settings ) ) {
149 wp_send_json_error( esc_html(__( 'Setting Data is missing', 'better-payment' )) );
150 }
151
152 $amount = isset($_POST[ 'fields' ][ 'payment_amount' ]) ? floatval($_POST[ 'fields' ][ 'payment_amount' ]) : 0;
153
154 if ( empty( $el_settings[ 'better_payment_stripe_public_key' ] ) || empty( $el_settings[ 'better_payment_stripe_secret_key' ] ) ) {
155 wp_send_json_error( esc_html(__( 'Stripe Key missing', 'better-payment' )) );
156 }
157
158 $header_info = array(
159 'Authorization' => 'Basic ' . base64_encode( sanitize_text_field( $el_settings[ 'better_payment_stripe_secret_key' ] ) . ':' ),
160 'Stripe-Version' => '2019-05-16'
161 );
162
163 $order_id = 'stripe_' . uniqid();
164 $request = [
165 'success_url' => add_query_arg( [
166 'better_payment_stripe_status' => 'success',
167 'better_payment_stripe_id' => $order_id,
168 'better_payment_widget_id' => $widget_id
169 ], get_permalink( $setting_data_page_id ) ),
170 'cancel_url' => add_query_arg( [
171 'better_payment_error_status' => 'error',
172 'better_payment_stripe_id' => $order_id,
173 'better_payment_widget_id' => $widget_id
174 ], get_permalink( $setting_data_page_id ) ),
175 'locale' => 'auto',
176 'payment_method_types' => [ 'card' ],
177 'client_reference_id' => time(),
178 'billing_address_collection' => 'required',
179 'metadata' => [
180 'order_id' => $order_id
181 ],
182 'line_items' => [
183 [
184 'amount' => ( $amount * 100 ),
185 'currency' => $el_settings[ 'better_payment_form_currency' ],
186 'name' => $el_settings[ 'better_payment_form_title' ],
187 'quantity' => 1
188 ]
189 ],
190 'payment_intent_data' => [
191 'capture_method' => 'automatic',
192 'description' => $el_settings[ 'better_payment_form_title' ],
193 'metadata' => [
194 'order_id' => $order_id
195 ]
196 ]
197 ];
198
199 $post_data_email = '';
200
201 if( isset($_POST[ 'fields' ][ 'email' ]) ){
202 $post_data_email = sanitize_email($_POST[ 'fields' ][ 'email' ]);
203 } else if( isset($_POST[ 'fields' ][ 'email_address' ]) ){
204 $post_data_email = sanitize_email($_POST[ 'fields' ][ 'email_address' ]);
205 }
206
207 $post_data_first_name = isset($_POST[ 'fields' ][ 'first_name' ]) ? sanitize_text_field($_POST[ 'fields' ][ 'first_name' ]) : '';
208 $post_data_last_name = isset($_POST[ 'fields' ][ 'last_name' ]) ? sanitize_text_field($_POST[ 'fields' ][ 'last_name' ]) : '';
209
210 if ( !empty( $post_data_email ) ) {
211 $request[ 'customer_email' ] = $post_data_email;
212 $request[ 'metadata' ][ 'customer_email' ] = $request[ 'customer_email' ];
213 $request[ 'payment_intent_data' ][ 'metadata' ][ 'customer_email' ] = $request[ 'customer_email' ];
214
215 }
216
217 if ( !empty( $post_data_first_name ) ) {
218 $request[ 'customer_name' ] = $post_data_first_name;
219 }
220
221 if ( !empty( $post_data_last_name ) ) {
222 $request[ 'customer_name' ] = !empty( $request[ 'customer_name' ] ) ? $request[ 'customer_name' ] . ' ' . $post_data_last_name : $post_data_last_name;
223 }
224
225 if ( !empty( $request[ 'customer_name' ] ) ) {
226 $request[ 'metadata' ][ 'customer_name' ] = $request[ 'customer_name' ];
227 $request[ 'payment_intent_data' ][ 'metadata' ][ 'customer_name' ] = $request[ 'customer_name' ];
228 unset( $request[ 'customer_name' ] );
229 }
230
231 $response = wp_safe_remote_post(
232 'https://api.stripe.com/v1/checkout/sessions',
233 array(
234 'method' => 'POST',
235 'headers' => $header_info,
236 'body' => $request,
237 'timeout' => 70,
238 )
239 );
240
241 //Form fields data to send via email
242 $better_form_fields = [
243 'first_name' => $post_data_first_name ,
244 'last_name' => $post_data_last_name,
245 'email' => $post_data_email,
246 'amount' => $el_settings[ 'better_payment_form_currency' ]. $amount,
247 'referer_page_id' => $page_id,
248 'referer_widget_id' => $widget_id,
249 'source' => 'stripe'
250 ];
251
252 $response_ar = json_decode( $response[ 'body' ] );
253
254 if ( !empty( $response_ar->payment_intent ) ) {
255 Better_Payment_Handler::payment_create(
256 [
257 'amount' => $amount,
258 'order_id' => $order_id,
259 'payment_date' => date( 'Y-m-d H:i:s' ),
260 'source' => 'stripe',
261 'transaction_id' => sanitize_text_field($response_ar->payment_intent),
262 'customer_info' => maybe_serialize( $response_ar ),
263 'form_fields_info' => maybe_serialize( $better_form_fields ),
264 'obj_id' => sanitize_text_field($response_ar->id),
265 'status' => sanitize_text_field($response_ar->payment_status),
266 'currency' => $el_settings[ 'better_payment_form_currency' ],
267 'referer' => "widget",
268 ]
269 );
270 wp_send_json_success(
271 [
272 'stripe_data' => sanitize_text_field($response_ar->id),
273 'stripe_public_key' => sanitize_text_field( $el_settings[ 'better_payment_stripe_public_key' ] )
274 ]
275 );
276 } else {
277 $error_message = 'Something went wrong!';
278
279 if (isset($response_ar->error)){
280 $error_message = sanitize_text_field($response_ar->error->message);
281 }
282
283 wp_send_json_error( $error_message );
284 }
285 }
286
287 /**
288 * Widget settings
289 *
290 * @since 1.0.0
291 */
292 public function better_payment_widget_settings( $page_id, $widget_id ) {
293 $document = \Elementor\Plugin::$instance->documents->get( $page_id );
294 $settings = [];
295 if ( $document ) {
296 $elements = \Elementor\Plugin::instance()->documents->get( $page_id )->get_elements_data();
297 $widget_data = $this->find_element_recursive( $elements, $widget_id );
298 $widget = \Elementor\Plugin::instance()->elements_manager->create_element_instance( $widget_data );
299 if ( $widget ) {
300 $settings = $widget->get_settings_for_display();
301 }
302 }
303 return $settings;
304 }
305
306 /**
307 * Find element recursive
308 *
309 * @since 1.0.0
310 */
311 public function find_element_recursive( $elements, $form_id ) {
312
313 foreach ( $elements as $element ) {
314 if ( $form_id === $element[ 'id' ] ) {
315 return $element;
316 }
317
318 if ( !empty( $element[ 'elements' ] ) ) {
319 $element = $this->find_element_recursive( $element[ 'elements' ], $form_id );
320
321 if ( $element ) {
322 return $element;
323 }
324 }
325 }
326
327 return false;
328 }
329
330 /**
331 * Redirect to referer page
332 *
333 * @since 1.0.0
334 */
335 public function redirect_previous_page() {
336 $location = $_SERVER[ 'HTTP_REFERER' ];
337 wp_safe_redirect( $location );
338 exit();
339 }
340
341 }
342
343