COURIERAPP_API_BASE,'key'=>COURIERAPP_API_KEY,'mode'=>str_starts_with(COURIERAPP_API_KEY,'ca_live_')?'live':'sandbox']; $saved=courierapp_compat_get_option('courierapp_connect_connection',[]); if(!function_exists('openssl_decrypt'))return ['base'=>'','key'=>'']; if(!is_array($saved)||empty($saved['sealed']))return ['base'=>'','key'=>'']; $sealed=json_decode(base64_decode($saved['sealed'],true),true); if(!is_array($sealed))return ['base'=>'','key'=>'']; try{$iv=base64_decode($sealed['iv'],true);$tag=base64_decode($sealed['tag'],true);$bytes=base64_decode($sealed['data'],true); if(strlen($iv)!==12||strlen($tag)!==16)return ['base'=>'','key'=>'']; $key=openssl_decrypt($bytes,'aes-256-gcm',hash('sha256',wp_salt('auth'),true),OPENSSL_RAW_DATA,$iv,$tag,'CourierAPP-sandbox-key'); if(!is_string($key)||!preg_match('/^ca_(test|live)_[a-f0-9]{64}$/',$key))return ['base'=>'','key'=>'']; $mode=str_starts_with($key,'ca_live_')?'live':'sandbox'; return ['base'=>'https://swdjgtbhifglgnnmwthf.supabase.co/functions/v1/merchant-api'.($mode==='sandbox'?'-sandbox':''),'key'=>$key,'mode'=>$mode]; }catch(Throwable $e){return ['base'=>'','key'=>''];} } function courierapp_connect_save_sandbox_connection($token){ if(!preg_match('/^ca_test_[a-f0-9]{64}$/',$token))return new WP_Error('courierapp_test_key','A scoped sandbox key is required.'); return courierapp_connect_save_connection($token); } function courierapp_connect_save_connection($token,$public_checkout=false){ if(!current_user_can('manage_woocommerce'))return new WP_Error('courierapp_permissions','Not allowed.'); if(defined('COURIERAPP_API_KEY'))return new WP_Error('courierapp_constants','Connection is managed by server constants.'); if(!preg_match('/^ca_(test|live)_[a-f0-9]{64}$/',$token))return new WP_Error('courierapp_key','A scoped CourierAPP key is required.'); $mode=str_starts_with($token,'ca_live_')?'live':'sandbox'; if($public_checkout&&$mode!=='live')return new WP_Error('courierapp_mode','Public checkout requires a live account connection.'); if(!function_exists('openssl_encrypt'))return new WP_Error('courierapp_encryption','OpenSSL is required to protect the connection key.'); // Host is fixed. Do not send keys to arbitrary user-supplied URLs or redirects. $base='https://swdjgtbhifglgnnmwthf.supabase.co/functions/v1/merchant-api'.($mode==='sandbox'?'-sandbox':''); $response=wp_remote_get($base.'/v1/capabilities',['headers'=>['Authorization'=>'Bearer '.$token],'timeout'=>15,'redirection'=>0,'sslverify'=>true]); if(is_wp_error($response)||wp_remote_retrieve_response_code($response)!==200)return new WP_Error('courierapp_connection_failed','The sandbox connection could not be verified.'); $data=json_decode(wp_remote_retrieve_body($response),true); if(($data['mode']??'')!==$mode||($mode==='live'&&$public_checkout&&empty($data['shipment_purchase_enabled'])))return new WP_Error('courierapp_mode','Account connection mode could not be verified.'); $iv=random_bytes(12);$tag='';$encrypted=openssl_encrypt($token,'aes-256-gcm',hash('sha256',wp_salt('auth'),true),OPENSSL_RAW_DATA,$iv,$tag,'CourierAPP-sandbox-key'); if($encrypted===false)return new WP_Error('courierapp_encryption','Could not protect the connection key.'); $sealed=base64_encode(wp_json_encode(['iv'=>base64_encode($iv),'tag'=>base64_encode($tag),'data'=>base64_encode($encrypted)])); courierapp_compat_update_option('courierapp_connect_connection',['sealed'=>$sealed,'verified_at'=>gmdate('c'),'mode'=>$mode],false); courierapp_compat_update_option('courierapp_connect_public_checkout',$public_checkout&&$mode==='live',false); return true; } function courierapp_connect_connection_form(){ if(!current_user_can('manage_woocommerce'))return; if(isset($_POST['courierapp_save_connection'])){ check_admin_referer('courierapp_save_connection');$token=sanitize_text_field(wp_unslash($_POST['courierapp_sandbox_key']??'')); $result=courierapp_connect_save_connection($token,!empty($_POST['courierapp_public_checkout']));unset($token); echo '

'.esc_html(is_wp_error($result)?$result->get_error_message():'Connection verified and protected server-side.').'

'; } if(function_exists('courierapp_connect_self_service_form'))courierapp_connect_self_service_form(); if(isset($_POST['courierapp_set_checkout'])){ check_admin_referer('courierapp_set_checkout');$existing=courierapp_connect_connection(); $result=courierapp_connect_save_connection($existing['key']??'',!empty($_POST['courierapp_public_checkout'])); echo '

'.esc_html(is_wp_error($result)?$result->get_error_message():'Η ρύθμιση του checkout αποθηκεύτηκε.').'

'; } if(isset($_POST['courierapp_save_checkout_credit'])){ check_admin_referer('courierapp_checkout_credit'); courierapp_compat_update_option('courierapp_connect_checkout_credit', !empty($_POST['courierapp_checkout_credit']), false); } echo '

Προαιρετική εμφάνιση CourierAPP

'; wp_nonce_field('courierapp_checkout_credit'); echo '

Προαιρετικό σε κάθε πακέτο. Η απενεργοποίηση δεν επηρεάζει τιμές, μεταφορικές, συσκευασία ή ετικέτες.

'; submit_button('Αποθήκευση εμφάνισης','secondary','courierapp_save_checkout_credit');echo '
'; $current=courierapp_connect_connection(); if(!empty($current['key'])){ echo '

Checkout

Σύνδεση: '.esc_html($current['mode']??'').'. Η ζώνη CourierAPP και τα πραγματικά στοιχεία αποστολέα πρέπει να έχουν συμπληρωθεί στις ρυθμίσεις μεταφορικών.

';wp_nonce_field('courierapp_set_checkout'); echo ''; submit_button('Αποθήκευση λειτουργίας checkout','secondary','courierapp_set_checkout');echo '
'; } echo '

Σύνδεση CourierAPP

Το API key αποθηκεύεται κρυπτογραφημένο στον server. Η φόρμα δεν εμφανίζει αποθηκευμένη τιμή. Οι πραγματικές αποστολές χρεώνουν το wallet του συνδεδεμένου επαγγελματικού λογαριασμού. Η εγκατάσταση δεν αλλάζει τις ζώνες μεταφορικών.

'; wp_nonce_field('courierapp_save_connection');echo '
'; echo '

'; submit_button('Verify CourierAPP connection','secondary','courierapp_save_connection');echo '
'; }