activities_controller.php
1 year ago
auth_controller.php
9 months ago
booking_form_settings_controller.php
1 year ago
bookings_controller.php
1 year ago
calendars_controller.php
9 months ago
carts_controller.php
1 year ago
controller.php
9 months ago
customer_cabinet_controller.php
9 months ago
customers_controller.php
9 months ago
dashboard_controller.php
9 months ago
default_agent_controller.php
1 year ago
events_controller.php
1 year ago
form_fields_controller.php
9 months ago
integrations_controller.php
9 months ago
invoices_controller.php
1 year ago
manage_booking_by_key_controller.php
1 year ago
manage_order_by_key_controller.php
1 year ago
notifications_controller.php
1 year ago
orders_controller.php
1 year ago
pro_controller.php
1 year ago
process_jobs_controller.php
9 months ago
processes_controller.php
1 year ago
search_controller.php
1 year ago
services_controller.php
9 months ago
settings_controller.php
1 year ago
steps_controller.php
9 months ago
stripe_connect_controller.php
9 months ago
support_topics_controller.php
1 year ago
todos_controller.php
1 year ago
transactions_controller.php
1 year ago
wizard_controller.php
1 year ago
stripe_connect_controller.php
242 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 | 'status' => LATEPOINT_STATUS_SUCCESS, |
| 52 | 'continue_transaction_intent_url' => OsTransactionIntentHelper::generate_continue_intent_url( $transaction_intent->intent_key ), |
| 53 | 'payment_intent_id' => $payment_intent_id, |
| 54 | 'payment_intent_secret' => $payment_intent_client_secret, |
| 55 | 'transaction_intent_key' => $transaction_intent->intent_key |
| 56 | ] ); |
| 57 | } |
| 58 | } catch ( Exception $e ) { |
| 59 | if ( $this->get_return_format() == 'json' ) { |
| 60 | $this->send_json( array( 'status' => LATEPOINT_STATUS_ERROR, 'message' => $e->getMessage() ) ); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | public function webhook() { |
| 66 | $payload = @file_get_contents( 'php://input' ); |
| 67 | $data = json_decode( $payload, true ); |
| 68 | if ( empty( $data['server_token'] ) || $data['server_token'] != OsStripeConnectHelper::get_server_token() || $data['stripe_account_id'] != OsStripeConnectHelper::get_connect_account_id() ) { |
| 69 | http_response_code( 400 ); |
| 70 | echo 'Validation issue with webhook'; |
| 71 | exit(); |
| 72 | } |
| 73 | $event = $data['event']; |
| 74 | // Handle the event |
| 75 | switch ( $event['type'] ) { |
| 76 | case 'payment_intent.succeeded': |
| 77 | if ( ! empty( $event['data']['order_intent_key'] ) ) { |
| 78 | $order_intent = OsOrderIntentHelper::get_order_intent_by_intent_key( $event['data']['order_intent_key'] ); |
| 79 | if ( $order_intent->is_new_record() ) { |
| 80 | OsDebugHelper::log( 'Error processing stripe connect webhook: Order intent not found for key' ); |
| 81 | http_response_code( 400 ); |
| 82 | exit(); |
| 83 | } |
| 84 | if ( $order_intent->convert_to_order() ) { |
| 85 | http_response_code( 200 ); |
| 86 | } else { |
| 87 | http_response_code( 400 ); |
| 88 | OsDebugHelper::log( 'Error converting order intent', 'stripe_connect_webhook', $order_intent->get_error_messages() ); |
| 89 | } |
| 90 | } |
| 91 | if ( ! empty( $event['data']['transaction_intent_key'] ) ) { |
| 92 | $transaction_intent = OsTransactionIntentHelper::get_transaction_intent_by_intent_key( $event['data']['transaction_intent_key'] ); |
| 93 | if ( $transaction_intent->is_new_record() ) { |
| 94 | OsDebugHelper::log( 'Error processing stripe connect webhook: Transaction intent not found for key' ); |
| 95 | http_response_code( 400 ); |
| 96 | exit(); |
| 97 | } |
| 98 | if ( $transaction_intent->convert_to_transaction() ) { |
| 99 | http_response_code( 200 ); |
| 100 | } else { |
| 101 | http_response_code( 400 ); |
| 102 | OsDebugHelper::log( 'Error converting transaction intent' ); |
| 103 | } |
| 104 | } |
| 105 | break; |
| 106 | } |
| 107 | exit(); |
| 108 | } |
| 109 | |
| 110 | private function get_env_from_params(): string { |
| 111 | return ( ! empty( $this->params['env'] && in_array( $this->params['env'], [ |
| 112 | LATEPOINT_PAYMENTS_ENV_LIVE, |
| 113 | LATEPOINT_PAYMENTS_ENV_DEV |
| 114 | ] ) ) ) ? $this->params['env'] : OsSettingsHelper::get_payments_environment(); |
| 115 | } |
| 116 | |
| 117 | public function start_connect_process() { |
| 118 | $env = $this->get_env_from_params(); |
| 119 | OsSettingsHelper::save_setting_by_name( OsSettingsHelper::append_payment_env_key( 'enable_payment_processor_stripe_connect', $env ), LATEPOINT_VALUE_ON ); |
| 120 | $url = OsStripeConnectHelper::get_connect_url( $env ); |
| 121 | $this->send_json( array( 'status' => LATEPOINT_STATUS_SUCCESS, 'url' => $url, 'message' => __( 'Redirecting to Stripe', 'latepoint' ) ) ); |
| 122 | } |
| 123 | |
| 124 | public function disconnect_connect_account() { |
| 125 | $env = $this->get_env_from_params(); |
| 126 | try { |
| 127 | $path = 'server-tokens/' . OsStripeConnectHelper::get_server_token( $env ) . '/disconnect/'; |
| 128 | $response = OsStripeConnectHelper::do_account_request( $path, $env, '', 'DELETE' ); |
| 129 | if ( $response['status']['code'] == 200 ) { |
| 130 | OsSettingsHelper::remove_setting_by_name( OsSettingsHelper::append_payment_env_key( 'stripe_connect_charges_enabled' ) ); |
| 131 | OsSettingsHelper::remove_setting_by_name( OsSettingsHelper::append_payment_env_key( 'stripe_connect_account_id' ) ); |
| 132 | } else { |
| 133 | OsDebugHelper::log( 'Stripe Connect Error', 'stripe_connect_disconnect_error', $response ); |
| 134 | } |
| 135 | } catch ( Exception $e ) { |
| 136 | OsDebugHelper::log( 'Error getting status of a stripe connection', 'stripe', [ 'error_message' => $e->getMessage() ] ); |
| 137 | $this->send_json( array( 'status' => LATEPOINT_STATUS_ERROR, 'message' => $e->getMessage() ) ); |
| 138 | } |
| 139 | $this->send_json( array( 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => OsStripeConnectHelper::get_connection_buttons_and_status( $env ) ) ); |
| 140 | } |
| 141 | |
| 142 | |
| 143 | public function check_connect_status() { |
| 144 | $env = $this->get_env_from_params(); |
| 145 | try { |
| 146 | $response = OsStripeConnectHelper::do_request( 'server-tokens/' . OsStripeConnectHelper::get_server_token( $env ) . '/status', '', 'GET', [], [], $env ); |
| 147 | if($env == 'live'){ |
| 148 | if(empty($response['data']['transaction_fee_info'])){ |
| 149 | OsSettingsHelper::save_setting_by_name( 'stripe_connect_transaction_fee_info', '0' ); |
| 150 | }else{ |
| 151 | OsSettingsHelper::save_setting_by_name( 'stripe_connect_transaction_fee_info', $response['data']['transaction_fee_info'] ); |
| 152 | } |
| 153 | } |
| 154 | if ( ! empty( $response['data'] ) && ! empty( $response['data']['account_id'] ) ) { |
| 155 | OsSettingsHelper::save_setting_by_name( OsSettingsHelper::append_payment_env_key( 'stripe_connect_account_id', $env ), $response['data']['account_id'] ); |
| 156 | if ( ! empty( $response['data']['charges_enabled'] ) ) { |
| 157 | OsSettingsHelper::save_setting_by_name( OsSettingsHelper::append_payment_env_key( 'stripe_connect_charges_enabled', $env ), LATEPOINT_VALUE_ON ); |
| 158 | } else { |
| 159 | OsSettingsHelper::remove_setting_by_name( OsSettingsHelper::append_payment_env_key( 'stripe_connect_charges_enabled', $env ) ); |
| 160 | } |
| 161 | } else { |
| 162 | OsSettingsHelper::remove_setting_by_name( OsSettingsHelper::append_payment_env_key( 'stripe_connect_charges_enabled', $env ) ); |
| 163 | OsSettingsHelper::remove_setting_by_name( OsSettingsHelper::append_payment_env_key( 'stripe_connect_account_id', $env ) ); |
| 164 | } |
| 165 | if ( ! empty( $response['data']['error'] ) ) { |
| 166 | OsDebugHelper::log( 'Error checking status of server token', 'stripe_connect_error', [ 'error_message' => $response['data']['error'] ] ); |
| 167 | } |
| 168 | } catch ( Exception $e ) { |
| 169 | OsDebugHelper::log( 'Error getting status of a stripe connection', 'stripe_connect_error', [ 'error_message' => $e->getMessage() ] ); |
| 170 | $this->send_json( array( 'status' => LATEPOINT_STATUS_ERROR, 'message' => $e->getMessage() ) ); |
| 171 | } |
| 172 | $this->send_json( array( 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => OsStripeConnectHelper::get_connection_buttons_and_status( $env ) ) ); |
| 173 | } |
| 174 | |
| 175 | |
| 176 | public function heartbeat() { |
| 177 | $payload = @file_get_contents( 'php://input' ); |
| 178 | $data = json_decode( $payload, true ); |
| 179 | |
| 180 | if ( empty( $data['wp_latepoint_server_token'] ) ) { |
| 181 | $this->send_json( array( 'status' => LATEPOINT_STATUS_ERROR, 'message' => 'Token is missing' ), 404 ); |
| 182 | } |
| 183 | if ( $data['wp_latepoint_server_token'] != OsStripeConnectHelper::get_server_token() ) { |
| 184 | $this->send_json( array( 'status' => LATEPOINT_STATUS_ERROR, 'message' => 'Invalid Token' ), 404 ); |
| 185 | } |
| 186 | |
| 187 | $this->send_json( array( 'status' => LATEPOINT_STATUS_SUCCESS, 'message' => 'Heartbeat detected' ), 200 ); |
| 188 | } |
| 189 | |
| 190 | public function create_payment_intent() { |
| 191 | try { |
| 192 | OsStepsHelper::set_required_objects( $this->params ); |
| 193 | |
| 194 | $booking_form_page_url = $this->params['booking_form_page_url'] ?? OsUtilHelper::get_referrer(); |
| 195 | $order_intent = OsOrderIntentHelper::create_or_update_order_intent( OsStepsHelper::$cart_object, OsStepsHelper::$restrictions, OsStepsHelper::$presets, $booking_form_page_url, OsStepsHelper::get_customer_object_id() ); |
| 196 | |
| 197 | if ( ! $order_intent->is_bookable() ) { |
| 198 | throw new Exception( empty( $order_intent->get_error_messages() ) ? __( 'Booking slot is not available anymore.', 'latepoint' ) : implode( ', ', $order_intent->get_error_messages() ) ); |
| 199 | } |
| 200 | |
| 201 | if ( OsSettingsHelper::get_settings_value( OsSettingsHelper::append_payment_env_key( 'stripe_connect_account_id' ) ) ) { |
| 202 | $payment_intent_data = OsStripeConnectHelper::generate_payment_intent_id_and_secret_for_order_intent( $order_intent ); |
| 203 | $payment_intent_id = $payment_intent_data['id']; |
| 204 | $payment_intent_client_secret = $payment_intent_data['client_secret']; |
| 205 | } else { |
| 206 | throw new Exception( __( 'Stripe connect account ID not set', 'latepoint' ) ); |
| 207 | } |
| 208 | |
| 209 | |
| 210 | // update cart with payment intent id |
| 211 | OsStepsHelper::$cart_object->payment_token = $payment_intent_id; |
| 212 | |
| 213 | // cart_item_data might be changed after filters run, make sure to get the latest version |
| 214 | $cart_items_data = json_decode( $order_intent->cart_items_data, true ); |
| 215 | $payment_data = json_decode( $order_intent->payment_data, true ); |
| 216 | |
| 217 | $payment_data['token'] = $payment_intent_id; |
| 218 | $order_intent->update_attributes( [ |
| 219 | 'cart_items_data' => wp_json_encode( $cart_items_data ), |
| 220 | 'payment_data' => wp_json_encode( $payment_data ) |
| 221 | ] ); |
| 222 | if ( $this->get_return_format() == 'json' ) { |
| 223 | $this->send_json( [ |
| 224 | 'status' => LATEPOINT_STATUS_SUCCESS, |
| 225 | 'continue_order_intent_url' => OsOrderIntentHelper::generate_continue_intent_url( $order_intent->intent_key ), |
| 226 | 'payment_intent_id' => $payment_intent_id, |
| 227 | 'payment_intent_secret' => $payment_intent_client_secret, |
| 228 | 'order_intent_key' => $order_intent->intent_key |
| 229 | ] ); |
| 230 | } |
| 231 | } catch ( Exception $e ) { |
| 232 | if ( $this->get_return_format() == 'json' ) { |
| 233 | $this->send_json( array( 'status' => LATEPOINT_STATUS_ERROR, 'message' => $e->getMessage() ) ); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | |
| 241 | endif; |
| 242 |