'POST', 'callback' => [ $this, 'handle_create_checkout' ], 'permission_callback' => function () { return current_user_can( abmahn_shield_capability() ); }, ] ); } public function handle_create_checkout( WP_REST_Request $request ) { $last_scan = get_option( 'abmahn_shield_last_scan', null ); if ( empty( $last_scan ) || empty( $last_scan['scanId'] ) ) { return new WP_Error( 'no_scan', 'Bitte zuerst einen Quick-Scan durchführen.', [ 'status' => 400 ] ); } $scanner = new Abmahn_Shield_Scanner(); $site_key = $scanner->get_site_key(); if ( empty( $site_key ) ) { return new WP_Error( 'no_site_key', 'Site noch nicht registriert. Bitte zuerst einen Scan durchführen.', [ 'status' => 400 ] ); } $current_user = wp_get_current_user(); $response = wp_remote_post( ABMAHN_SHIELD_API_BASE . '/api/wc/checkout', [ 'timeout' => 30, 'headers' => [ 'Content-Type' => 'application/json', 'User-Agent' => 'Abmahn-Shield-WooCommerce/' . ABMAHN_SHIELD_VERSION, ], 'body' => wp_json_encode( [ 'siteKey' => $site_key, 'scanId' => $last_scan['scanId'], 'plan' => 'single', 'email' => $current_user->user_email, ] ), ] ); if ( is_wp_error( $response ) ) { return new WP_Error( 'payment_failed', 'Verbindung zum Payment-Server fehlgeschlagen.', [ 'status' => 502 ] ); } $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( empty( $body['checkoutUrl'] ) ) { return new WP_Error( 'no_checkout', $body['error'] ?? 'Checkout konnte nicht erstellt werden.', [ 'status' => 400 ] ); } return new WP_REST_Response( [ 'checkoutUrl' => $body['checkoutUrl'], ], 200 ); } }