PluginProbe
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More / 0.0.2
Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More v0.0.2
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 / class-better-payment-actions.php

class-better-payment-actions.php in Better Payment – Instant Payments, Donations, Fundraising with Subscriptions & More 0.0.2, at includes/class-better-payment-actions.php

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