PluginProbe ʕ •ᴥ•ʔ
LatePoint – Calendar Booking Plugin for Appointments and Events / trunk
LatePoint – Calendar Booking Plugin for Appointments and Events vtrunk
5.6.7 5.6.6 5.6.5 5.6.4 5.6.3 5.6.2 5.6.1 5.6.0 5.5.2 5.5.1 5.5.0 5.4.2 trunk 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.1.8 5.1.9 5.1.91 5.1.92 5.1.93 5.1.94 5.2.0 5.2.1 5.2.10 5.2.11 5.2.2 5.2.3 5.2.4 5.2.5 5.2.6 5.2.7 5.2.8 5.2.9 5.3.0 5.3.1 5.3.2 5.4.0 5.4.1
latepoint / lib / controllers / stripe_connect_controller.php
latepoint / lib / controllers Last commit date
activities_controller.php 2 months ago auth_controller.php 3 months ago booking_form_settings_controller.php 3 months ago bookings_controller.php 4 days ago calendars_controller.php 3 months ago carts_controller.php 4 days ago controller.php 3 months ago customer_cabinet_controller.php 2 months ago customers_controller.php 4 days ago dashboard_controller.php 2 months ago default_agent_controller.php 3 months ago events_controller.php 3 months ago form_fields_controller.php 1 week ago integrations_controller.php 3 months ago invoices_controller.php 4 days ago manage_booking_by_key_controller.php 3 months ago manage_order_by_key_controller.php 3 months ago notifications_controller.php 3 months ago orders_controller.php 4 days ago pro_controller.php 3 weeks ago process_jobs_controller.php 3 months ago processes_controller.php 1 month ago razorpay_connect_controller.php 1 week ago search_controller.php 3 months ago services_controller.php 3 months ago settings_controller.php 2 months ago steps_controller.php 3 weeks ago stripe_connect_controller.php 1 week ago support_topics_controller.php 3 months ago todos_controller.php 3 months ago transactions_controller.php 4 days ago wizard_controller.php 1 week ago
stripe_connect_controller.php
317 lines
1 <?php
2 /*
3 * Copyright (c) 2024 LatePoint LLC. All rights reserved.
4 */
5
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit; // Exit if accessed directly.
8 }
9
10
11 if ( ! class_exists( 'OsStripeConnectController' ) ) :
12
13
14 class OsStripeConnectController extends OsController {
15
16
17 function __construct() {
18 parent::__construct();
19
20 $this->action_access['public'] = array_merge( $this->action_access['public'], [ 'webhook', 'create_payment_intent_for_transaction', 'create_payment_intent' ] );
21 $this->action_access['customer'] = array_merge( $this->action_access['customer'], [] );
22 $this->views_folder = LATEPOINT_VIEWS_ABSPATH . 'stripe_connect/';
23 }
24
25 public function create_payment_intent_for_transaction() {
26 try {
27 $invoice_access_key = sanitize_text_field( $this->params['key'] ?? '' );
28 if ( empty( $invoice_access_key ) ) {
29 throw new Exception( __( 'Invoice not found', 'latepoint' ) );
30 }
31 $invoice = new OsInvoiceModel();
32 $invoice = OsInvoicesHelper::get_invoice_by_key( $invoice_access_key );
33 if ( ! ( $invoice instanceof OsInvoiceModel ) || $invoice->is_new_record() ) {
34 throw new Exception( __( 'Invoice not found', 'latepoint' ) );
35 }
36
37 $transaction_intent = OsTransactionIntentHelper::create_or_update_transaction_intent( $invoice, $this->params );
38
39 if ( OsSettingsHelper::get_settings_value( OsSettingsHelper::append_payment_env_key( 'stripe_connect_account_id' ) ) ) {
40 $payment_intent_data = OsStripeConnectHelper::generate_payment_intent_id_and_secret_for_transaction_intent( $transaction_intent );
41 $payment_intent_id = $payment_intent_data['id'];
42 $payment_intent_client_secret = $payment_intent_data['client_secret'];
43 } else {
44 throw new Exception( __( 'Stripe connect account ID not set', 'latepoint' ) );
45 }
46
47 $transaction_intent->set_payment_data_value( 'token', $payment_intent_id, false );
48 if ( ! $transaction_intent->save() ) {
49 throw new Exception( __( 'Unable to save transaction intent', 'latepoint' ) );
50 }
51
52
53 if ( $this->get_return_format() == 'json' ) {
54 $this->send_json(
55 [
56 'status' => LATEPOINT_STATUS_SUCCESS,
57 'continue_transaction_intent_url' => OsTransactionIntentHelper::generate_continue_intent_url( $transaction_intent->intent_key ),
58 'payment_intent_id' => $payment_intent_id,
59 'payment_intent_secret' => $payment_intent_client_secret,
60 'transaction_intent_key' => $transaction_intent->intent_key,
61 ]
62 );
63 }
64 } catch ( Exception $e ) {
65 if ( $this->get_return_format() == 'json' ) {
66 $this->send_json(
67 array(
68 'status' => LATEPOINT_STATUS_ERROR,
69 'message' => $e->getMessage(),
70 )
71 );
72 }
73 }
74 }
75
76 public function webhook() {
77 $payload = @file_get_contents( 'php://input' );
78 $data = json_decode( $payload, true );
79 if ( empty( $data['server_token'] ) || $data['server_token'] != OsStripeConnectHelper::get_server_token() || $data['stripe_account_id'] != OsStripeConnectHelper::get_connect_account_id() ) {
80 http_response_code( 400 );
81 echo 'Validation issue with webhook';
82 exit();
83 }
84 $event = $data['event'];
85 // Handle the event
86 switch ( $event['type'] ) {
87 case 'payment_intent.succeeded':
88 if ( ! empty( $event['data']['order_intent_key'] ) ) {
89 $order_intent = OsOrderIntentHelper::get_order_intent_by_intent_key( $event['data']['order_intent_key'] );
90 if ( $order_intent->is_new_record() ) {
91 OsDebugHelper::log( 'Error processing stripe connect webhook: Order intent not found for key' );
92 http_response_code( 400 );
93 exit();
94 }
95 if ( $order_intent->convert_to_order() ) {
96 http_response_code( 200 );
97 } else {
98 http_response_code( 400 );
99 OsDebugHelper::log( 'Error converting order intent', 'stripe_connect_webhook', $order_intent->get_error_messages() );
100 }
101 }
102 if ( ! empty( $event['data']['transaction_intent_key'] ) ) {
103 $transaction_intent = OsTransactionIntentHelper::get_transaction_intent_by_intent_key( $event['data']['transaction_intent_key'] );
104 if ( $transaction_intent->is_new_record() ) {
105 OsDebugHelper::log( 'Error processing stripe connect webhook: Transaction intent not found for key' );
106 http_response_code( 400 );
107 exit();
108 }
109 if ( $transaction_intent->convert_to_transaction() ) {
110 http_response_code( 200 );
111 } else {
112 http_response_code( 400 );
113 OsDebugHelper::log( 'Error converting transaction intent' );
114 }
115 }
116 break;
117 }
118 exit();
119 }
120
121 private function get_env_from_params(): string {
122 return ( ! empty(
123 $this->params['env'] && in_array(
124 $this->params['env'],
125 [
126 LATEPOINT_PAYMENTS_ENV_LIVE,
127 LATEPOINT_PAYMENTS_ENV_DEV,
128 ]
129 )
130 ) ) ? $this->params['env'] : OsSettingsHelper::get_payments_environment();
131 }
132
133 public function start_connect_process() {
134 $env = $this->get_env_from_params();
135 OsSettingsHelper::save_setting_by_name( OsSettingsHelper::append_payment_env_key( 'enable_payment_processor_stripe_connect', $env ), LATEPOINT_VALUE_ON );
136 $url = OsStripeConnectHelper::get_connect_url( $env );
137 $this->send_json(
138 array(
139 'status' => LATEPOINT_STATUS_SUCCESS,
140 'url' => $url,
141 'message' => __( 'Redirecting to Stripe', 'latepoint' ),
142 )
143 );
144 }
145
146 public function disconnect_connect_account() {
147 $this->check_nonce( 'stripe_disconnect_account' );
148 $env = $this->get_env_from_params();
149 try {
150 $path = 'server-tokens/' . OsStripeConnectHelper::get_server_token( $env ) . '/disconnect/';
151 $response = OsStripeConnectHelper::do_account_request( $path, $env, '', 'DELETE' );
152 if ( $response['status']['code'] == 200 ) {
153 OsSettingsHelper::remove_setting_by_name( OsSettingsHelper::append_payment_env_key( 'stripe_connect_charges_enabled' ) );
154 OsSettingsHelper::remove_setting_by_name( OsSettingsHelper::append_payment_env_key( 'stripe_connect_account_id' ) );
155 OsStripeConnectHelper::reset_server_token( $env );
156 } else {
157 OsDebugHelper::log( 'Stripe Connect Error', 'stripe_connect_disconnect_error', $response );
158 }
159 } catch ( Exception $e ) {
160 OsDebugHelper::log( 'Error getting status of a stripe connection', 'stripe', [ 'error_message' => $e->getMessage() ] );
161 $this->send_json(
162 array(
163 'status' => LATEPOINT_STATUS_ERROR,
164 'message' => $e->getMessage(),
165 )
166 );
167 }
168 $this->send_json(
169 array(
170 'status' => LATEPOINT_STATUS_SUCCESS,
171 'message' => OsStripeConnectHelper::get_connection_buttons_and_status( $env ),
172 )
173 );
174 }
175
176
177 public function check_connect_status() {
178 $env = $this->get_env_from_params();
179 try {
180 $response = OsStripeConnectHelper::do_request( 'server-tokens/' . OsStripeConnectHelper::get_server_token( $env ) . '/status', '', 'GET', [], [], $env );
181 if ( $env == 'live' ) {
182 if ( empty( $response['data']['transaction_fee_info'] ) ) {
183 OsSettingsHelper::save_setting_by_name( 'stripe_connect_transaction_fee_info', '0' );
184 } else {
185 OsSettingsHelper::save_setting_by_name( 'stripe_connect_transaction_fee_info', $response['data']['transaction_fee_info'] );
186 }
187 }
188 if ( ! empty( $response['data'] ) && ! empty( $response['data']['account_id'] ) ) {
189 OsSettingsHelper::save_setting_by_name( OsSettingsHelper::append_payment_env_key( 'stripe_connect_account_id', $env ), $response['data']['account_id'] );
190 if ( ! empty( $response['data']['charges_enabled'] ) ) {
191 OsSettingsHelper::save_setting_by_name( OsSettingsHelper::append_payment_env_key( 'stripe_connect_charges_enabled', $env ), LATEPOINT_VALUE_ON );
192 } else {
193 OsSettingsHelper::remove_setting_by_name( OsSettingsHelper::append_payment_env_key( 'stripe_connect_charges_enabled', $env ) );
194 }
195 } else {
196 OsSettingsHelper::remove_setting_by_name( OsSettingsHelper::append_payment_env_key( 'stripe_connect_charges_enabled', $env ) );
197 OsSettingsHelper::remove_setting_by_name( OsSettingsHelper::append_payment_env_key( 'stripe_connect_account_id', $env ) );
198 }
199 if ( ! empty( $response['data']['active_site_urls'] ) ) {
200 OsSettingsHelper::save_setting_by_name( OsSettingsHelper::append_payment_env_key( 'stripe_connect_duplicate_token_activations', $env ), $response['data']['active_site_urls'] );
201 } else {
202 OsSettingsHelper::remove_setting_by_name( OsSettingsHelper::append_payment_env_key( 'stripe_connect_duplicate_token_activations', $env ) );
203 }
204 if ( ! empty( $response['data']['error'] ) ) {
205 OsDebugHelper::log( 'Error checking status of server token', 'stripe_connect_error', [ 'error_message' => $response['data']['error'] ] );
206 }
207 } catch ( Exception $e ) {
208 OsDebugHelper::log( 'Error getting status of a stripe connection', 'stripe_connect_error', [ 'error_message' => $e->getMessage() ] );
209 $this->send_json(
210 array(
211 'status' => LATEPOINT_STATUS_ERROR,
212 'message' => $e->getMessage(),
213 )
214 );
215 }
216 $this->send_json(
217 array(
218 'status' => LATEPOINT_STATUS_SUCCESS,
219 'message' => OsStripeConnectHelper::get_connection_buttons_and_status( $env ),
220 )
221 );
222 }
223
224
225 public function heartbeat() {
226 $payload = @file_get_contents( 'php://input' );
227 $data = json_decode( $payload, true );
228
229 if ( empty( $data['wp_latepoint_server_token'] ) ) {
230 $this->send_json(
231 array(
232 'status' => LATEPOINT_STATUS_ERROR,
233 'message' => 'Token is missing',
234 ),
235 404
236 );
237 }
238 if ( $data['wp_latepoint_server_token'] != OsStripeConnectHelper::get_server_token() ) {
239 $this->send_json(
240 array(
241 'status' => LATEPOINT_STATUS_ERROR,
242 'message' => 'Invalid Token',
243 ),
244 404
245 );
246 }
247
248 $this->send_json(
249 array(
250 'status' => LATEPOINT_STATUS_SUCCESS,
251 'message' => 'Heartbeat detected',
252 ),
253 200
254 );
255 }
256
257 public function create_payment_intent() {
258 try {
259 OsStepsHelper::set_required_objects( $this->params );
260
261 $booking_form_page_url = $this->params['booking_form_page_url'] ?? OsUtilHelper::get_referrer();
262 $order_intent = OsOrderIntentHelper::create_or_update_order_intent( OsStepsHelper::$cart_object, OsStepsHelper::$restrictions, OsStepsHelper::$presets, $booking_form_page_url, OsStepsHelper::get_customer_object_id() );
263
264 if ( ! $order_intent->is_bookable() ) {
265 throw new Exception( empty( $order_intent->get_error_messages() ) ? __( 'Booking slot is not available anymore.', 'latepoint' ) : implode( ', ', $order_intent->get_error_messages() ) );
266 }
267
268 if ( OsSettingsHelper::get_settings_value( OsSettingsHelper::append_payment_env_key( 'stripe_connect_account_id' ) ) ) {
269 $payment_intent_data = OsStripeConnectHelper::generate_payment_intent_id_and_secret_for_order_intent( $order_intent );
270 $payment_intent_id = $payment_intent_data['id'];
271 $payment_intent_client_secret = $payment_intent_data['client_secret'];
272 } else {
273 throw new Exception( __( 'Stripe connect account ID not set', 'latepoint' ) );
274 }
275
276
277 // update cart with payment intent id
278 OsStepsHelper::$cart_object->payment_token = $payment_intent_id;
279
280 // cart_item_data might be changed after filters run, make sure to get the latest version
281 $cart_items_data = json_decode( $order_intent->cart_items_data, true );
282 $payment_data = json_decode( $order_intent->payment_data, true );
283
284 $payment_data['token'] = $payment_intent_id;
285 $order_intent->update_attributes(
286 [
287 'cart_items_data' => wp_json_encode( $cart_items_data ),
288 'payment_data' => wp_json_encode( $payment_data ),
289 ]
290 );
291 if ( $this->get_return_format() == 'json' ) {
292 $this->send_json(
293 [
294 'status' => LATEPOINT_STATUS_SUCCESS,
295 'continue_order_intent_url' => OsOrderIntentHelper::generate_continue_intent_url( $order_intent->intent_key ),
296 'payment_intent_id' => $payment_intent_id,
297 'payment_intent_secret' => $payment_intent_client_secret,
298 'order_intent_key' => $order_intent->intent_key,
299 ]
300 );
301 }
302 } catch ( Exception $e ) {
303 if ( $this->get_return_format() == 'json' ) {
304 $this->send_json(
305 array(
306 'status' => LATEPOINT_STATUS_ERROR,
307 'message' => $e->getMessage(),
308 )
309 );
310 }
311 }
312 }
313 }
314
315
316 endif;
317