부모 티켓
1개 티켓

백로그

0
티켓 없음

할 일

0
티켓 없음

진행 중

0
티켓 없음

리뷰

1
높음 c42fc5e8
서브 티켓 [P4] Toss::Client 서비스 구현

Toss::Client — Faraday HTTP 클라이언트 + confirm/cancel/get_payment + Basic Auth + 에러 처리 + 테스트

## 목표 PRD Section 11.1 기반 Toss::Client 서비스. Faraday HTTP 클라이언트. confirm_payment, cancel_payment, get_payment. ## PRD 코드 (Section 11.1) ```ruby class Toss::Client BASE_URL = "https://api.tosspayments.com/v1".freeze def initialize @secret_key = ENV["TOSS_SECRET_KEY"] @conn = build_connection end def confirm_payment(payment_key:, order_id:, amount:) post("/payments/confirm", { paymentKey: payment_key, orderId: order_id, amount: amount }) end def cancel_payment(payment_key:, cancel_reason:) post("/payments/#{payment_key}/cancel", { cancelReason: cancel_reason }) end def get_payment(payment_key) get("/payments/#{payment_key}") end private def post(path, body) response = @conn.post(path) { |req| req.body = body.to_json } handle_response(response) end def get(path) response = @conn.get(path) handle_response(response) end def build_connection Faraday.new(url: BASE_URL) do |f| f.request :json f.response :json f.request :retry, max: 3, interval: 0.5 f.headers["Authorization"] = "Basic #{encoded_key}" f.headers["Content-Type"] = "application/json" end end def encoded_key Base64.strict_encode64("#{@secret_key}:") end def handle_response(response) body = response.body if response.status == 200 OpenStruct.new(success?: true, data: body) else Rails.logger.error("Toss API Error: #{body}") OpenStruct.new(success?: false, error_code: body["code"], error_message: body["message"]) end end end ``` ## 구현 사항 ### 1. app/services/toss/client.rb - PRD 코드 그대로 구현 - Faraday + JSON + retry 미들웨어 - Basic Auth: Base64(secret_key + ":") - handle_response: 200 → success, else → error with code/message ### 2. 에러 처리 - ENV["TOSS_SECRET_KEY"] 미설정 시 에러 - Faraday::ConnectionFailed 등 네트워크 에러 처리 - 응답 body 파싱 실패 처리 ### 3. 테스트 (WebMock/stub) - confirm_payment 성공/실패 - cancel_payment 성공/실패 - get_payment 성공/실패 - Basic Auth 헤더 확인 - retry 미들웨어 설정 확인 - 비밀키 미설정 에러 ### ⚠️ 주의 - services/toss/ 범위만 (developer-1은 build/specs/ 작업 중) - Payment 모델 건드리지 않기 - Faraday gem은 이미 Gemfile에 포함 - faraday-retry gem 확인 (없으면 추가) ### 완료 기준 - confirm_payment, cancel_payment, get_payment 구현 - Basic Auth 헤더 설정 - 에러 응답 파싱 - Faraday retry 미들웨어 - 서비스 테스트 통과 (HTTP stub) - bin/rails test 전체 통과

D
developer-2
25 days

완료 (전체)

0
티켓 없음