stripe_manager = $stripe_manager; } public function create_checkout_session( int $article_id, string $email ): array|WP_Error { $body = array( 'mode' => 'payment', 'success_url' => $this->stripe_manager->get_success_url( $article_id ), 'cancel_url' => $this->stripe_manager->get_cancel_url( $article_id ), 'client_reference_id' => (string) $article_id, 'metadata[article_id]' => (string) $article_id, 'line_items[0][quantity]' => '1', 'line_items[0][price_data][currency]' => $this->stripe_manager->get_currency(), 'line_items[0][price_data][unit_amount]' => (string) $this->stripe_manager->get_price_cents(), 'line_items[0][price_data][product_data][name]' => get_the_title( $article_id ), ); if ( is_email( $email ) ) { $body['customer_email'] = $email; } return $this->stripe_post( '/checkout/sessions', $body ); } public function retrieve_checkout_session( string $session_id ): array|WP_Error { return $this->stripe_get( '/checkout/sessions/' . rawurlencode( $session_id ) ); } private function stripe_post( string $endpoint, array $body ): array|WP_Error { $response = wp_remote_post( Constants::STRIPE_API_BASE . $endpoint, array( 'timeout' => 30, 'headers' => array( 'Authorization' => 'Bearer ' . $this->stripe_manager->get_secret_key(), ), 'body' => $body, ) ); return $this->handle_response( $response ); } private function stripe_get( string $endpoint ): array|WP_Error { $response = wp_remote_get( Constants::STRIPE_API_BASE . $endpoint, array( 'timeout' => 30, 'headers' => array( 'Authorization' => 'Bearer ' . $this->stripe_manager->get_secret_key(), ), ) ); return $this->handle_response( $response ); } private function handle_response( $response ): array|WP_Error { if ( is_wp_error( $response ) ) { return $response; } $status_code = wp_remote_retrieve_response_code( $response ); $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( $status_code < 200 || $status_code >= 300 ) { $message = $body['error']['message'] ?? __( 'Stripe request failed.', 'aibender-articlevault-pro' ); return new WP_Error( 'articlevault_stripe_error', $message ); } return is_array( $body ) ? $body : array(); } }