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