activities_helper.php
3 months ago
agent_helper.php
3 months ago
analytics_helper.php
1 week ago
auth_helper.php
6 days ago
blocks_helper.php
3 weeks ago
booking_helper.php
4 days ago
bricks_helper.php
3 months ago
bundles_helper.php
4 days ago
calendar_helper.php
3 days ago
carts_helper.php
3 months ago
connector_helper.php
3 months ago
csv_helper.php
3 months ago
customer_helper.php
1 month ago
customer_import_helper.php
1 month ago
database_helper.php
1 week ago
debug_helper.php
3 months ago
defaults_helper.php
3 months ago
elementor_helper.php
3 months ago
email_helper.php
3 months ago
encrypt_helper.php
3 months ago
events_helper.php
3 months ago
form_helper.php
3 months ago
icalendar_helper.php
3 months ago
image_helper.php
3 months ago
invoices_helper.php
3 weeks ago
license_helper.php
3 months ago
location_helper.php
3 months ago
marketing_systems_helper.php
3 months ago
meeting_systems_helper.php
3 months ago
menu_helper.php
3 weeks ago
meta_helper.php
3 months ago
migrations_helper.php
3 months ago
money_helper.php
3 months ago
notifications_helper.php
3 months ago
nps_survey_helper.php
3 months ago
order_intent_helper.php
2 months ago
orders_helper.php
3 months ago
otp_helper.php
3 months ago
pages_helper.php
3 months ago
params_helper.php
3 months ago
payments_helper.php
2 months ago
plugin_version_update_helper.php
2 months ago
price_breakdown_helper.php
3 months ago
process_jobs_helper.php
3 months ago
processes_helper.php
3 months ago
razorpay_connect_helper.php
1 week ago
replacer_helper.php
3 months ago
resource_helper.php
4 days ago
roles_helper.php
3 weeks ago
router_helper.php
3 months ago
service_helper.php
3 months ago
sessions_helper.php
3 months ago
settings_helper.php
1 week ago
short_links_systems_helper.php
3 months ago
shortcodes_helper.php
3 weeks ago
sms_helper.php
3 months ago
steps_helper.php
1 week ago
stripe_connect_helper.php
1 week ago
styles_helper.php
3 months ago
support_topics_helper.php
3 months ago
time_helper.php
2 months ago
timeline_helper.php
2 months ago
transaction_helper.php
1 month ago
transaction_intent_helper.php
3 months ago
util_helper.php
1 month ago
version_specific_updates_helper.php
3 months ago
whatsapp_helper.php
1 month ago
work_periods_helper.php
3 months ago
wp_datetime.php
3 months ago
wp_user_helper.php
3 months ago
razorpay_connect_helper.php
606 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 | class OsRazorpayConnectHelper { |
| 11 | public static $default_currency_iso_code = 'INR'; |
| 12 | public static $processor_code = 'razorpay_connect'; |
| 13 | |
| 14 | // ------------------------------------------------------------------------- |
| 15 | // Payment processor registration |
| 16 | // ------------------------------------------------------------------------- |
| 17 | |
| 18 | public static function register_payment_processor( array $payment_processors ): array { |
| 19 | $payment_processors[ self::$processor_code ] = [ |
| 20 | 'code' => self::$processor_code, |
| 21 | 'name' => __( 'Razorpay Checkout', 'latepoint' ), |
| 22 | 'front_name' => __( 'Razorpay Checkout', 'latepoint' ), |
| 23 | 'image_url' => LATEPOINT_IMAGES_URL . 'processor-razorpay-logo.png', |
| 24 | ]; |
| 25 | return $payment_processors; |
| 26 | } |
| 27 | |
| 28 | public static function add_settings_fields( $processor_code ) { |
| 29 | if ( $processor_code !== self::$processor_code ) { |
| 30 | return false; |
| 31 | } |
| 32 | ?> |
| 33 | <div class="sub-section-row"> |
| 34 | <div class="sub-section-label"> |
| 35 | <h3><?php esc_html_e( 'Connect (Live)', 'latepoint' ); ?></h3> |
| 36 | </div> |
| 37 | <div class="sub-section-content"> |
| 38 | <div data-env="<?php echo esc_attr( LATEPOINT_PAYMENTS_ENV_LIVE ); ?>" |
| 39 | class="payment-processor-connect-status-wrapper razorpay-connect-status-wrapper" |
| 40 | data-route-name="<?php echo esc_attr( OsRouterHelper::build_route_name( 'razorpay_connect', 'check_connect_status' ) ); ?>"> |
| 41 | <div class="os-loading-spinner"></div> |
| 42 | </div> |
| 43 | </div> |
| 44 | </div> |
| 45 | <div class="sub-section-row"> |
| 46 | <div class="sub-section-label"> |
| 47 | <h3><?php esc_html_e( 'Connect (Dev)', 'latepoint' ); ?></h3> |
| 48 | </div> |
| 49 | <div class="sub-section-content"> |
| 50 | <div data-env="<?php echo esc_attr( LATEPOINT_PAYMENTS_ENV_DEV ); ?>" |
| 51 | class="payment-processor-connect-status-wrapper razorpay-connect-status-wrapper" |
| 52 | data-route-name="<?php echo esc_attr( OsRouterHelper::build_route_name( 'razorpay_connect', 'check_connect_status' ) ); ?>"> |
| 53 | <div class="os-loading-spinner"></div> |
| 54 | </div> |
| 55 | </div> |
| 56 | </div> |
| 57 | <div class="sub-section-row"> |
| 58 | <div class="sub-section-label"> |
| 59 | <h3><?php esc_html_e( 'Currency Settings', 'latepoint' ); ?></h3> |
| 60 | </div> |
| 61 | <div class="sub-section-content"> |
| 62 | <?php |
| 63 | $selected_country_code = OsSettingsHelper::get_settings_value( 'razorpay_connect_country_code', 'IN' ); |
| 64 | $selected_currency_code = OsSettingsHelper::get_settings_value( 'razorpay_connect_currency_iso_code', self::$default_currency_iso_code ); |
| 65 | ?> |
| 66 | <div class="os-row"> |
| 67 | <div class="os-col-6"> |
| 68 | <?php echo OsFormHelper::select_field( 'settings[razorpay_connect_country_code]', __( 'Country', 'latepoint' ), self::load_countries_list(), $selected_country_code ); ?> |
| 69 | </div> |
| 70 | <div class="os-col-6"> |
| 71 | <?php echo OsFormHelper::select_field( 'settings[razorpay_connect_currency_iso_code]', __( 'Currency Code', 'latepoint' ), self::load_currencies_list(), $selected_currency_code ); ?> |
| 72 | </div> |
| 73 | </div> |
| 74 | </div> |
| 75 | </div> |
| 76 | <div class="sub-section-row"> |
| 77 | <div class="sub-section-label"> |
| 78 | <h3><?php esc_html_e( 'Other Settings', 'latepoint' ); ?></h3> |
| 79 | </div> |
| 80 | <div class="sub-section-content"> |
| 81 | <div class="os-row os-mb-1"> |
| 82 | <div class="os-col-6"> |
| 83 | <?php echo OsFormHelper::text_field( 'settings[razorpay_connect_company_name]', __( 'Company Name (Appears on Payment Modal)', 'latepoint' ), OsSettingsHelper::get_settings_value( 'razorpay_connect_company_name' ), [ 'theme' => 'simple' ] ); ?> |
| 84 | </div> |
| 85 | <div class="os-col-6"> |
| 86 | <?php echo OsFormHelper::color_picker( 'settings[razorpay_connect_theme_color]', __( 'Color for Payment Modal', 'latepoint' ), OsSettingsHelper::get_settings_value( 'razorpay_connect_theme_color', '#4366ff' ) ); ?> |
| 87 | </div> |
| 88 | </div> |
| 89 | <div class="os-row os-mb-2"> |
| 90 | <div class="os-col-12"> |
| 91 | <?php echo OsFormHelper::media_uploader_field( 'settings[razorpay_connect_logo_image_id]', 0, __( 'Logo for Payment Modal', 'latepoint' ), __( 'Remove Logo', 'latepoint' ), OsSettingsHelper::get_settings_value( 'razorpay_connect_logo_image_id' ) ); ?> |
| 92 | </div> |
| 93 | </div> |
| 94 | </div> |
| 95 | </div> |
| 96 | <?php |
| 97 | } |
| 98 | |
| 99 | // ------------------------------------------------------------------------- |
| 100 | // Payment methods |
| 101 | // ------------------------------------------------------------------------- |
| 102 | |
| 103 | public static function get_supported_payment_methods(): array { |
| 104 | return [ |
| 105 | 'razorpay_checkout' => [ |
| 106 | 'name' => __( 'Razorpay Checkout', 'latepoint' ), |
| 107 | 'label' => __( 'Pay with Razorpay', 'latepoint' ), |
| 108 | 'image_url' => LATEPOINT_IMAGES_URL . 'processor-razorpay-logo.png', |
| 109 | ], |
| 110 | ]; |
| 111 | } |
| 112 | |
| 113 | public static function add_all_payment_methods_to_payment_times( array $payment_times ): array { |
| 114 | $payment_methods = self::get_supported_payment_methods(); |
| 115 | foreach ( $payment_methods as $payment_method_code => $payment_method_info ) { |
| 116 | $payment_times[ LATEPOINT_PAYMENT_TIME_NOW ][ $payment_method_code ][ self::$processor_code ] = $payment_method_info; |
| 117 | } |
| 118 | return $payment_times; |
| 119 | } |
| 120 | |
| 121 | public static function add_enabled_payment_methods_to_payment_times( array $payment_times ): array { |
| 122 | if ( OsPaymentsHelper::is_payment_processor_enabled( self::$processor_code ) ) { |
| 123 | $payment_times = self::add_all_payment_methods_to_payment_times( $payment_times ); |
| 124 | } |
| 125 | return $payment_times; |
| 126 | } |
| 127 | |
| 128 | // ------------------------------------------------------------------------- |
| 129 | // Payment step UI |
| 130 | // ------------------------------------------------------------------------- |
| 131 | |
| 132 | public static function output_payment_step_contents( OsCartModel $cart ) { |
| 133 | if ( ! OsPaymentsHelper::should_processor_handle_payment_for_cart( self::$processor_code, $cart ) ) { |
| 134 | return; |
| 135 | } |
| 136 | echo '<div class="lp-payment-method-content" data-payment-method="razorpay_checkout">'; |
| 137 | echo '<div class="lp-payment-method-content-i">'; |
| 138 | echo '<div class="razorpay-payment-trigger"></div>'; |
| 139 | echo '</div>'; |
| 140 | echo '</div>'; |
| 141 | } |
| 142 | |
| 143 | public static function output_order_payment_pay_contents( OsTransactionIntentModel $transaction_intent ) { |
| 144 | if ( ! OsPaymentsHelper::should_processor_handle_payment_for_transaction_intent( self::$processor_code, $transaction_intent ) ) { |
| 145 | return; |
| 146 | } |
| 147 | echo '<div class="lp-payment-method-content" data-payment-method="razorpay_checkout">'; |
| 148 | echo '<div class="lp-payment-method-content-i">'; |
| 149 | echo '<div class="razorpay-payment-trigger"></div>'; |
| 150 | echo '</div>'; |
| 151 | echo '</div>'; |
| 152 | } |
| 153 | |
| 154 | // ------------------------------------------------------------------------- |
| 155 | // Amount conversion |
| 156 | // ------------------------------------------------------------------------- |
| 157 | |
| 158 | public static function convert_charge_amount_to_requirements( $charge_amount, OsCartModel $cart ) { |
| 159 | if ( OsPaymentsHelper::should_processor_handle_payment_for_cart( self::$processor_code, $cart ) ) { |
| 160 | $charge_amount = self::convert_amount_to_specs( $charge_amount ); |
| 161 | } |
| 162 | return $charge_amount; |
| 163 | } |
| 164 | |
| 165 | public static function convert_transaction_intent_charge_amount_to_specs( $amount, OsTransactionIntentModel $transaction_intent ) { |
| 166 | if ( OsPaymentsHelper::should_processor_handle_payment_for_transaction_intent( self::$processor_code, $transaction_intent ) ) { |
| 167 | $amount = self::convert_amount_to_specs( $amount ); |
| 168 | } |
| 169 | return $amount; |
| 170 | } |
| 171 | |
| 172 | public static function convert_amount_to_specs( $charge_amount ) { |
| 173 | $iso_code = self::get_currency_iso_code(); |
| 174 | if ( in_array( strtolower( $iso_code ), self::zero_decimal_currencies_list() ) ) { |
| 175 | $charge_amount = round( $charge_amount ); |
| 176 | } else { |
| 177 | $number_of_decimals = OsSettingsHelper::get_settings_value( 'number_of_decimals', '2' ); |
| 178 | $charge_amount = number_format( (float) $charge_amount, $number_of_decimals, '.', '' ) * pow( 10, $number_of_decimals ); |
| 179 | } |
| 180 | return (int) $charge_amount; |
| 181 | } |
| 182 | |
| 183 | public static function convert_amount_back_from_specs_to_db_format( $charge_amount ) { |
| 184 | $iso_code = self::get_currency_iso_code(); |
| 185 | $number_of_decimals = OsSettingsHelper::get_settings_value( 'number_of_decimals', '2' ); |
| 186 | if ( ! in_array( strtolower( $iso_code ), self::zero_decimal_currencies_list() ) && ! empty( $number_of_decimals ) ) { |
| 187 | $charge_amount = $charge_amount / pow( 10, $number_of_decimals ); |
| 188 | $charge_amount = number_format( (float) $charge_amount, 4, '.', '' ); |
| 189 | } else { |
| 190 | $charge_amount = OsMoneyHelper::pad_to_db_format( $charge_amount ); |
| 191 | } |
| 192 | return $charge_amount; |
| 193 | } |
| 194 | |
| 195 | public static function zero_decimal_currencies_list(): array { |
| 196 | return array( 'bif', 'clp', 'djf', 'gnf', 'jpy', 'kmf', 'krw', 'mga', 'pyg', 'rwf', 'ugx', 'vnd', 'vuv', 'xaf', 'xof', 'xpf' ); |
| 197 | } |
| 198 | |
| 199 | // ------------------------------------------------------------------------- |
| 200 | // Payment processing |
| 201 | // ------------------------------------------------------------------------- |
| 202 | |
| 203 | public static function process_payment( $result, OsOrderIntentModel $order_intent ) { |
| 204 | if ( ! OsPaymentsHelper::should_processor_handle_payment_for_order_intent( self::$processor_code, $order_intent ) ) { |
| 205 | return $result; |
| 206 | } |
| 207 | switch ( $order_intent->get_payment_data_value( 'method' ) ) { |
| 208 | case 'razorpay_checkout': |
| 209 | if ( $order_intent->get_payment_data_value( 'token' ) ) { |
| 210 | $payment_data = self::retrieve_payment( $order_intent->get_payment_data_value( 'token' ) ); |
| 211 | if ( ! empty( $payment_data['data'] ) && in_array( $payment_data['data']['status'], [ 'authorized', 'captured' ] ) ) { |
| 212 | $result['status'] = LATEPOINT_STATUS_SUCCESS; |
| 213 | $result['processor'] = self::$processor_code; |
| 214 | $result['charge_id'] = $payment_data['data']['id']; |
| 215 | $result['amount'] = $payment_data['data']['amount']; |
| 216 | $result['kind'] = $payment_data['data']['status'] === 'authorized' ? LATEPOINT_TRANSACTION_KIND_AUTHORIZATION : LATEPOINT_TRANSACTION_KIND_CAPTURE; |
| 217 | } else { |
| 218 | $result['status'] = LATEPOINT_STATUS_ERROR; |
| 219 | $result['message'] = __( 'Payment Error', 'latepoint' ); |
| 220 | $order_intent->add_error( 'payment_error', $result['message'] ); |
| 221 | $order_intent->add_error( 'send_to_step', $result['message'], 'payment' ); |
| 222 | } |
| 223 | } else { |
| 224 | $result['status'] = LATEPOINT_STATUS_ERROR; |
| 225 | $result['message'] = __( 'Payment Error — token missing', 'latepoint' ); |
| 226 | $order_intent->add_error( 'payment_error', $result['message'] ); |
| 227 | } |
| 228 | break; |
| 229 | } |
| 230 | return $result; |
| 231 | } |
| 232 | |
| 233 | public static function process_payment_for_transaction_intent( $result, OsTransactionIntentModel $transaction_intent ) { |
| 234 | if ( ! OsPaymentsHelper::should_processor_handle_payment_for_transaction_intent( self::$processor_code, $transaction_intent ) ) { |
| 235 | return $result; |
| 236 | } |
| 237 | switch ( $transaction_intent->get_payment_data_value( 'method' ) ) { |
| 238 | case 'razorpay_checkout': |
| 239 | if ( $transaction_intent->get_payment_data_value( 'token' ) ) { |
| 240 | $payment_data = self::retrieve_payment( $transaction_intent->get_payment_data_value( 'token' ) ); |
| 241 | if ( ! empty( $payment_data['data'] ) && in_array( $payment_data['data']['status'], [ 'authorized', 'captured' ] ) ) { |
| 242 | $result['status'] = LATEPOINT_STATUS_SUCCESS; |
| 243 | $result['processor'] = self::$processor_code; |
| 244 | $result['charge_id'] = $payment_data['data']['id']; |
| 245 | $result['amount'] = $payment_data['data']['amount']; |
| 246 | $result['kind'] = $payment_data['data']['status'] === 'authorized' ? LATEPOINT_TRANSACTION_KIND_AUTHORIZATION : LATEPOINT_TRANSACTION_KIND_CAPTURE; |
| 247 | } else { |
| 248 | $result['status'] = LATEPOINT_STATUS_ERROR; |
| 249 | $result['message'] = __( 'Payment Error', 'latepoint' ); |
| 250 | $transaction_intent->add_error( 'send_to_step', $result['message'], 'payment' ); |
| 251 | } |
| 252 | } else { |
| 253 | $result['status'] = LATEPOINT_STATUS_ERROR; |
| 254 | $result['message'] = __( 'Payment Error — token missing', 'latepoint' ); |
| 255 | $transaction_intent->add_error( 'payment_error', $result['message'] ); |
| 256 | } |
| 257 | break; |
| 258 | } |
| 259 | return $result; |
| 260 | } |
| 261 | |
| 262 | public static function transaction_is_refund_available( $result, OsTransactionModel $transaction_model ): bool { |
| 263 | if ( OsPaymentsHelper::is_payment_processor_enabled( self::$processor_code ) && $transaction_model->processor === self::$processor_code ) { |
| 264 | $result = true; |
| 265 | } |
| 266 | return $result; |
| 267 | } |
| 268 | |
| 269 | public static function process_refund( $transaction_refund, OsTransactionModel $transaction, $custom_amount = null ) { |
| 270 | if ( $transaction->processor !== self::$processor_code ) { |
| 271 | return $transaction_refund; |
| 272 | } |
| 273 | if ( ! $transaction->can_refund() ) { |
| 274 | throw new Exception( __( 'Invalid Transaction', 'latepoint' ) ); |
| 275 | } |
| 276 | |
| 277 | $refund_data = [ |
| 278 | 'payment_id' => $transaction->token, |
| 279 | ]; |
| 280 | if ( $custom_amount ) { |
| 281 | $refund_data['custom_amount'] = self::convert_amount_to_specs( $custom_amount ); |
| 282 | } |
| 283 | |
| 284 | $response = self::do_account_request( 'refunds', OsSettingsHelper::get_payments_environment(), '', 'POST', $refund_data ); |
| 285 | |
| 286 | if ( empty( $response['data'] ) ) { |
| 287 | throw new Exception( __( 'Error Refunding', 'latepoint' ) ); |
| 288 | } |
| 289 | |
| 290 | $transaction_refund = new OsTransactionRefundModel(); |
| 291 | $transaction_refund->transaction_id = $transaction->id; |
| 292 | $transaction_refund->amount = self::convert_amount_back_from_specs_to_db_format( $response['data']['amount'] ); |
| 293 | $transaction_refund->token = $response['data']['id']; |
| 294 | if ( $transaction_refund->save() ) { |
| 295 | do_action( 'latepoint_transaction_refund_created', $transaction_refund ); |
| 296 | return $transaction_refund; |
| 297 | } else { |
| 298 | throw new Exception( implode( ', ', $transaction_refund->get_error_messages() ) ); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | // ------------------------------------------------------------------------- |
| 303 | // Middleware requests |
| 304 | // ------------------------------------------------------------------------- |
| 305 | |
| 306 | public static function generate_razorpay_order_for_order_intent( OsOrderIntentModel $order_intent ): array { |
| 307 | $options = [ |
| 308 | 'amount' => $order_intent->specs_charge_amount, |
| 309 | 'currency' => strtoupper( self::get_currency_iso_code() ), |
| 310 | 'notes' => [ |
| 311 | 'order_intent_key' => $order_intent->intent_key, |
| 312 | ], |
| 313 | ]; |
| 314 | $result = self::do_account_request( |
| 315 | 'orders', |
| 316 | OsSettingsHelper::get_payments_environment(), |
| 317 | '', |
| 318 | 'POST', |
| 319 | [ 'order_options' => $options ] |
| 320 | ); |
| 321 | if ( empty( $result['data'] ) ) { |
| 322 | $error_message = ! empty( $result['error'] ) ? sprintf( __( 'Payment Error: %s', 'latepoint' ), esc_html( $result['error'] ) ) : __( 'Error generating Razorpay order', 'latepoint' ); |
| 323 | OsDebugHelper::log( $error_message ); |
| 324 | throw new Exception( $error_message ); |
| 325 | } |
| 326 | return $result['data']; |
| 327 | } |
| 328 | |
| 329 | public static function generate_razorpay_order_for_transaction_intent( OsTransactionIntentModel $transaction_intent ): array { |
| 330 | $options = [ |
| 331 | 'amount' => $transaction_intent->specs_charge_amount, |
| 332 | 'currency' => strtoupper( self::get_currency_iso_code() ), |
| 333 | 'notes' => [ |
| 334 | 'transaction_intent_key' => $transaction_intent->intent_key, |
| 335 | ], |
| 336 | ]; |
| 337 | $result = self::do_account_request( |
| 338 | 'orders', |
| 339 | OsSettingsHelper::get_payments_environment(), |
| 340 | '', |
| 341 | 'POST', |
| 342 | [ 'order_options' => $options ] |
| 343 | ); |
| 344 | if ( empty( $result['data'] ) ) { |
| 345 | $error_message = ! empty( $result['error'] ) ? sprintf( __( 'Payment Error: %s', 'latepoint' ), esc_html( $result['error'] ) ) : __( 'Error generating Razorpay order for transaction', 'latepoint' ); |
| 346 | OsDebugHelper::log( $error_message ); |
| 347 | throw new Exception( $error_message ); |
| 348 | } |
| 349 | return $result['data']; |
| 350 | } |
| 351 | |
| 352 | public static function retrieve_payment( string $payment_id ): array { |
| 353 | $env = OsSettingsHelper::get_payments_environment(); |
| 354 | return self::do_account_request( 'payments/' . $payment_id, $env ); |
| 355 | } |
| 356 | |
| 357 | public static function do_account_request( string $path, string $env = '', string $connection_data = '', string $method = 'GET', array $vars = [], array $headers = [] ) { |
| 358 | if ( empty( $env ) ) { |
| 359 | $env = OsSettingsHelper::get_payments_environment(); |
| 360 | } |
| 361 | $path = self::get_connect_account_id( $env ) . '/' . $path; |
| 362 | try { |
| 363 | return self::do_request( $path, $connection_data, $method, $vars, $headers, $env ); |
| 364 | } catch ( Exception $e ) { |
| 365 | OsDebugHelper::log( 'Error processing request to Razorpay middleware: ' . $e->getMessage(), 'razorpay_connect_error' ); |
| 366 | return []; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | public static function do_request( string $path, string $connection_data = '', string $method = 'GET', array $vars = [], array $headers = [], string $force_env = '' ) { |
| 371 | $default_headers = [ |
| 372 | 'latepoint-version' => LATEPOINT_VERSION, |
| 373 | 'latepoint-domain' => OsUtilHelper::get_site_url(), |
| 374 | 'latepoint-license-key' => OsLicenseHelper::get_license_key(), |
| 375 | ]; |
| 376 | |
| 377 | if ( ! empty( $connection_data ) ) { |
| 378 | $default_headers['connection-data'] = $connection_data; |
| 379 | } |
| 380 | |
| 381 | $args = array( |
| 382 | 'timeout' => 15, |
| 383 | 'headers' => array_merge( $default_headers, $headers ), |
| 384 | 'body' => $vars, |
| 385 | 'sslverify' => false, |
| 386 | 'method' => $method, |
| 387 | ); |
| 388 | |
| 389 | if ( ! empty( $force_env ) && in_array( $force_env, [ LATEPOINT_PAYMENTS_ENV_DEV, LATEPOINT_PAYMENTS_ENV_LIVE ] ) ) { |
| 390 | $env = ( $force_env === LATEPOINT_PAYMENTS_ENV_DEV ) ? 'test' : 'live'; |
| 391 | } else { |
| 392 | $env = OsSettingsHelper::is_env_payments_dev() ? 'test' : 'live'; |
| 393 | } |
| 394 | |
| 395 | $url = LATEPOINT_RAZORPAY_CONNECT_URL . "/api/wp/v1/razorpay-connect/{$env}/{$path}"; |
| 396 | $response = wp_remote_request( $url, $args ); |
| 397 | |
| 398 | if ( ! is_wp_error( $response ) ) { |
| 399 | $data = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 400 | $data['status'] = $response['response']; |
| 401 | return $data; |
| 402 | } else { |
| 403 | $error_message = $response->get_error_message(); |
| 404 | throw new Exception( $error_message ); |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | // ------------------------------------------------------------------------- |
| 409 | // Connection management |
| 410 | // ------------------------------------------------------------------------- |
| 411 | |
| 412 | public static function get_server_token( string $force_env = '' ): string { |
| 413 | $key = OsSettingsHelper::append_payment_env_key( 'server_token_for_razorpay_connect', $force_env ); |
| 414 | $server_token = OsSettingsHelper::get_settings_value( $key, '' ); |
| 415 | if ( empty( $server_token ) ) { |
| 416 | $server_token = OsUtilHelper::generate_uuid(); |
| 417 | OsSettingsHelper::save_setting_by_name( $key, $server_token ); |
| 418 | } |
| 419 | return $server_token; |
| 420 | } |
| 421 | |
| 422 | public static function reset_server_token( string $force_env = '' ): string { |
| 423 | $key = OsSettingsHelper::append_payment_env_key( 'server_token_for_razorpay_connect', $force_env ); |
| 424 | $new_server_token = OsUtilHelper::generate_uuid(); |
| 425 | OsSettingsHelper::save_setting_by_name( $key, $new_server_token ); |
| 426 | return $new_server_token; |
| 427 | } |
| 428 | |
| 429 | public static function get_connect_url( string $env = '' ): string { |
| 430 | $url = LATEPOINT_RAZORPAY_CONNECT_URL . '/wp/razorpay-connection/' . $env . '/start/'; |
| 431 | $url .= self::get_server_token( $env ) . '/' . base64_encode( implode( '|||', [ get_bloginfo( 'name' ), get_site_icon_url(), OsUtilHelper::get_site_url() ] ) ); |
| 432 | return $url; |
| 433 | } |
| 434 | |
| 435 | public static function get_connect_account_id( string $env = '' ): string { |
| 436 | if ( empty( $env ) ) { |
| 437 | $env = OsSettingsHelper::get_payments_environment(); |
| 438 | } |
| 439 | return OsSettingsHelper::get_settings_value( OsSettingsHelper::append_payment_env_key( 'razorpay_connect_account_id', $env ), '' ); |
| 440 | } |
| 441 | |
| 442 | public static function get_connect_public_token( string $env = '' ): string { |
| 443 | if ( empty( $env ) ) { |
| 444 | $env = OsSettingsHelper::get_payments_environment(); |
| 445 | } |
| 446 | return OsSettingsHelper::get_settings_value( OsSettingsHelper::append_payment_env_key( 'razorpay_connect_public_token', $env ), '' ); |
| 447 | } |
| 448 | |
| 449 | public static function get_connection_buttons_and_status( string $env = '' ): string { |
| 450 | $razorpay_connect_account_id = OsSettingsHelper::get_settings_value( OsSettingsHelper::append_payment_env_key( 'razorpay_connect_account_id', $env ), false ); |
| 451 | $html = ''; |
| 452 | $duplicate_token_activations = OsSettingsHelper::get_settings_value( OsSettingsHelper::append_payment_env_key( 'razorpay_connect_duplicate_token_activations', $env ), '' ); |
| 453 | |
| 454 | if ( ! empty( $duplicate_token_activations ) ) { |
| 455 | $html .= '<div class="latepoint-fix-records-warning"><i class="latepoint-icon latepoint-icon-info"></i><div>'; |
| 456 | $html .= '<div>' . __( 'The following websites are using the same server token. This can happen if a site was cloned from one server to another. To fix this, disconnect each site and reconnect it.', 'latepoint' ) . '</div>'; |
| 457 | $html .= '<div class="latepoint-fix-records-values">' . esc_html( $duplicate_token_activations ) . '</div>'; |
| 458 | $html .= '</div></div>'; |
| 459 | } |
| 460 | |
| 461 | $html .= '<div class="payment-processor-connect-status-inner">'; |
| 462 | if ( $razorpay_connect_account_id ) { |
| 463 | $charges_enabled = OsSettingsHelper::is_on( OsSettingsHelper::append_payment_env_key( 'razorpay_connect_charges_enabled', $env ) ); |
| 464 | $disconnect_link = '<a class="payment-processor-disconnect-link" href="#" |
| 465 | data-os-pass-response="yes" |
| 466 | data-os-pass-this="yes" |
| 467 | data-os-before-after="none" |
| 468 | data-os-after-call="latepointRazorpayConnectAdmin.reload_connect_status_wrapper" |
| 469 | data-os-params="' . esc_attr( OsUtilHelper::build_os_params( [ 'env' => $env ], 'razorpay_disconnect_account' ) ) . '" |
| 470 | data-os-action="' . OsRouterHelper::build_route_name( 'razorpay_connect', 'disconnect_connect_account' ) . '" |
| 471 | ><i class="latepoint-icon latepoint-icon-x"></i><span>' . __( 'disconnect', 'latepoint' ) . '</span></a>'; |
| 472 | if ( $charges_enabled ) { |
| 473 | $html .= '<div class="payment-processor-status-connected"><i class="latepoint-icon latepoint-icon-check"></i><span>' . __( 'Connected', 'latepoint' ) . '</span></div>'; |
| 474 | $html .= $disconnect_link; |
| 475 | $html .= '<div class="razorpay-connect-account-info">' . __( 'Account: ', 'latepoint' ) . esc_html( $razorpay_connect_account_id ) . '</div>'; |
| 476 | if ( $env === LATEPOINT_PAYMENTS_ENV_LIVE && ! empty( OsSettingsHelper::get_settings_value( 'razorpay_connect_transaction_fee_info', '' ) ) ) { |
| 477 | $html .= '<div class="fee-disclosure-wrapper"> |
| 478 | <div class="fee-disclosure">' . esc_html( OsSettingsHelper::get_settings_value( 'razorpay_connect_transaction_fee_info', '' ) ) . ' transaction fee. <a target="_blank" href="https://wpdocs.latepoint.com/understanding-payment-processing-fees/"><span>Pricing</span> <i class="latepoint-icon latepoint-icon-external-link"></i></a></div> |
| 479 | </div>'; |
| 480 | } |
| 481 | } else { |
| 482 | $html .= '<div class="payment-processor-status-charges-disabled"><i class="latepoint-icon latepoint-icon-clock"></i><span>' . __( 'Pending Action', 'latepoint' ) . '</span></div>'; |
| 483 | $html .= '<a data-env="' . esc_attr( $env ) . '" data-route-name="' . esc_attr( OsRouterHelper::build_route_name( 'razorpay_connect', 'start_connect_process' ) ) . '" href="#" class="payment-start-connecting"><span>' . __( 'Continue Setup', 'latepoint' ) . '</span><i class="latepoint-icon latepoint-icon-arrow-right"></i></a>'; |
| 484 | $html .= '<div class="razorpay-connect-account-info">'; |
| 485 | $html .= '<div>' . esc_html( $razorpay_connect_account_id ) . '</div>'; |
| 486 | $html .= $disconnect_link; |
| 487 | $html .= '</div>'; |
| 488 | } |
| 489 | } else { |
| 490 | $html .= '<a data-env="' . esc_attr( $env ) . '" data-route-name="' . esc_attr( OsRouterHelper::build_route_name( 'razorpay_connect', 'start_connect_process' ) ) . '" href="#" class="payment-start-connecting"><span>' . __( 'Start Connecting', 'latepoint' ) . '</span><i class="latepoint-icon latepoint-icon-arrow-right"></i></a>'; |
| 491 | } |
| 492 | $html .= '</div>'; |
| 493 | return $html; |
| 494 | } |
| 495 | |
| 496 | // ------------------------------------------------------------------------- |
| 497 | // Settings helpers |
| 498 | // ------------------------------------------------------------------------- |
| 499 | |
| 500 | public static function get_currency_iso_code(): string { |
| 501 | return OsSettingsHelper::get_settings_value( 'razorpay_connect_currency_iso_code', self::$default_currency_iso_code ); |
| 502 | } |
| 503 | |
| 504 | public static function load_countries_list(): array { |
| 505 | return [ 'IN' => 'India' ]; |
| 506 | } |
| 507 | |
| 508 | public static function load_currencies_list(): array { |
| 509 | return [ |
| 510 | 'AED' => 'United Arab Emirates Dirham', |
| 511 | 'ALL' => 'Albanian Lek', |
| 512 | 'AMD' => 'Armenian Dram', |
| 513 | 'ARS' => 'Argentine Peso', |
| 514 | 'AUD' => 'Australian Dollar', |
| 515 | 'AWG' => 'Aruban Florin', |
| 516 | 'BBD' => 'Barbadian Dollar', |
| 517 | 'BDT' => 'Bangladeshi Taka', |
| 518 | 'BMD' => 'Bermudian Dollar', |
| 519 | 'BND' => 'Brunei Dollar', |
| 520 | 'BOB' => 'Bolivian Boliviano', |
| 521 | 'BSD' => 'Bahamian Dollar', |
| 522 | 'BWP' => 'Botswana Pula', |
| 523 | 'BZD' => 'Belize Dollar', |
| 524 | 'CAD' => 'Canadian Dollar', |
| 525 | 'CHF' => 'Swiss Franc', |
| 526 | 'CNY' => 'Chinese Yuan', |
| 527 | 'COP' => 'Colombian Peso', |
| 528 | 'CRC' => 'Costa Rican Colón', |
| 529 | 'CUP' => 'Cuban Peso', |
| 530 | 'CZK' => 'Czech Koruna', |
| 531 | 'DKK' => 'Danish Krone', |
| 532 | 'DOP' => 'Dominican Peso', |
| 533 | 'DZD' => 'Algerian Dinar', |
| 534 | 'EGP' => 'Egyptian Pound', |
| 535 | 'ETB' => 'Ethiopian Birr', |
| 536 | 'EUR' => 'Euro', |
| 537 | 'FJD' => 'Fijian Dollar', |
| 538 | 'GBP' => 'Pound Sterling', |
| 539 | 'GHS' => 'Ghanaian Cedi', |
| 540 | 'GIP' => 'Gibraltar Pound', |
| 541 | 'GMD' => 'Gambian Dalasi', |
| 542 | 'GTQ' => 'Guatemalan Quetzal', |
| 543 | 'GYD' => 'Guyanese Dollar', |
| 544 | 'HKD' => 'Hong Kong Dollar', |
| 545 | 'HNL' => 'Honduran Lempira', |
| 546 | 'HRK' => 'Croatian Kuna', |
| 547 | 'HTG' => 'Haitian Gourde', |
| 548 | 'HUF' => 'Hungarian Forint', |
| 549 | 'IDR' => 'Indonesian Rupiah', |
| 550 | 'ILS' => 'Israeli New Shekel', |
| 551 | 'INR' => 'Indian Rupee', |
| 552 | 'JMD' => 'Jamaican Dollar', |
| 553 | 'KES' => 'Kenyan Shilling', |
| 554 | 'KGS' => 'Kyrgyzstani Som', |
| 555 | 'KHR' => 'Cambodian Riel', |
| 556 | 'KYD' => 'Cayman Islands Dollar', |
| 557 | 'KZT' => 'Kazakhstani Tenge', |
| 558 | 'LAK' => 'Lao Kip', |
| 559 | 'LBP' => 'Lebanese Pound', |
| 560 | 'LKR' => 'Sri Lankan Rupee', |
| 561 | 'LRD' => 'Liberian Dollar', |
| 562 | 'LSL' => 'Lesotho Loti', |
| 563 | 'MAD' => 'Moroccan Dirham', |
| 564 | 'MDL' => 'Moldovan Leu', |
| 565 | 'MKD' => 'Macedonian Denar', |
| 566 | 'MMK' => 'Myanmar Kyat', |
| 567 | 'MNT' => 'Mongolian Tögrög', |
| 568 | 'MOP' => 'Macanese Pataca', |
| 569 | 'MUR' => 'Mauritian Rupee', |
| 570 | 'MVR' => 'Maldivian Rufiyaa', |
| 571 | 'MWK' => 'Malawian Kwacha', |
| 572 | 'MXN' => 'Mexican Peso', |
| 573 | 'MYR' => 'Malaysian Ringgit', |
| 574 | 'NAD' => 'Namibian Dollar', |
| 575 | 'NGN' => 'Nigerian Naira', |
| 576 | 'NIO' => 'Nicaraguan Córdoba', |
| 577 | 'NOK' => 'Norwegian Krone', |
| 578 | 'NPR' => 'Nepalese Rupee', |
| 579 | 'NZD' => 'New Zealand Dollar', |
| 580 | 'PEN' => 'Peruvian Sol', |
| 581 | 'PGK' => 'Papua New Guinean Kina', |
| 582 | 'PHP' => 'Philippine Peso', |
| 583 | 'PKR' => 'Pakistani Rupee', |
| 584 | 'QAR' => 'Qatari Riyal', |
| 585 | 'RUB' => 'Russian Ruble', |
| 586 | 'SAR' => 'Saudi Riyal', |
| 587 | 'SCR' => 'Seychellois Rupee', |
| 588 | 'SEK' => 'Swedish Krona', |
| 589 | 'SGD' => 'Singapore Dollar', |
| 590 | 'SLL' => 'Sierra Leonean Leone', |
| 591 | 'SOS' => 'Somali Shilling', |
| 592 | 'SSP' => 'South Sudanese Pound', |
| 593 | 'SVC' => 'Salvadoran Colón', |
| 594 | 'SZL' => 'Swazi Lilangeni', |
| 595 | 'THB' => 'Thai Baht', |
| 596 | 'TTD' => 'Trinidad and Tobago Dollar', |
| 597 | 'TZS' => 'Tanzanian Shilling', |
| 598 | 'USD' => 'United States Dollar', |
| 599 | 'UYU' => 'Uruguayan Peso', |
| 600 | 'UZS' => 'Uzbekistani Som', |
| 601 | 'YER' => 'Yemeni Rial', |
| 602 | 'ZAR' => 'South African Rand', |
| 603 | ]; |
| 604 | } |
| 605 | } |
| 606 |