$params * @return array{ok:bool, data:array, error?:string, code?:int} */ public function post(string $path, array $params, ?string $idempotencyKey = null): array { $headers = [ 'Authorization' => 'Bearer ' . $this->secretKey, 'Stripe-Version' => self::API_VERSION, 'Content-Type' => 'application/x-www-form-urlencoded', ]; if ($idempotencyKey !== null) { $headers['Idempotency-Key'] = $idempotencyKey; } $response = wp_remote_post(self::BASE . $path, [ 'headers' => $headers, 'body' => $this->encode($params), 'timeout' => 30, ]); return $this->parse($response); } /** * @return array{ok:bool, data:array, error?:string, code?:int} */ public function get(string $path): array { $response = wp_remote_get(self::BASE . $path, [ 'headers' => [ 'Authorization' => 'Bearer ' . $this->secretKey, 'Stripe-Version' => self::API_VERSION, ], 'timeout' => 30, ]); return $this->parse($response); } /** @param \WP_Error|array $response */ private function parse($response): array { if (is_wp_error($response)) { return ['ok' => false, 'data' => [], 'error' => $response->get_error_message()]; } $code = (int) wp_remote_retrieve_response_code($response); $body = json_decode((string) wp_remote_retrieve_body($response), true); if (!is_array($body)) { $body = []; } if ($code >= 200 && $code < 300) { return ['ok' => true, 'data' => $body, 'code' => $code]; } $msg = $body['error']['message'] ?? ('Stripe HTTP ' . $code); return ['ok' => false, 'data' => $body, 'error' => (string) $msg, 'code' => $code]; } /** Stripe expects PHP-style bracket encoding for nested params. */ private function encode(array $params): string { return http_build_query($params, '', '&', PHP_QUERY_RFC3986); } }