ShopPlatform.Web 1.0.0.0
농산물, 소프트웨어 라이선스와 개발 용역을 직접 판매하는 CodeMaru 직영 쇼핑몰입니다.
로딩중...
검색중...
일치하는것 없음
TossPaymentGateway.cs
이 파일의 문서화 페이지로 가기
1using System.Net.Http.Headers;
2using System.Text;
3using System.Text.Json;
6
8
18{
27 private const string ApiBase = "https://api.tosspayments.com/v1";
28
37 private readonly HttpClient _http;
46 private readonly ShopPaymentConfig _config;
56
89 public TossPaymentGateway(HttpClient http, ShopPaymentConfig config, PaymentKeyProtector protector)
90 {
91 _http = http;
92 _config = config;
93 _protector = protector;
94 }
95
136 public Task<PaymentStartResult> StartAsync(string orderNo, decimal amount, string customerName)
137 {
138 return Task.FromResult(new PaymentStartResult
139 {
140 OrderId = orderNo,
141 OrderNo = orderNo,
142 Amount = amount,
143 ClientKey = _config.TossClientKey,
144 SuccessUrl = _config.SuccessReturnUrl,
145 FailUrl = _config.FailReturnUrl
146 });
147 }
148
189 public async Task<PaymentConfirmResult> ConfirmAsync(string paymentKey, string orderId, int amount)
190 {
191 var secretKey = _protector.Unprotect(_config.TossSecretKeyEncrypted);
192 if (string.IsNullOrEmpty(secretKey))
193 return new PaymentConfirmResult { Succeeded = false, Error = "결제 키가 설정되지 않았습니다." };
194
195 var encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{secretKey}:"));
196 var req = new HttpRequestMessage(HttpMethod.Post, $"{ApiBase}/payments/confirm");
197 req.Headers.Authorization = new AuthenticationHeaderValue("Basic", encoded);
198 req.Content = new StringContent(
199 JsonSerializer.Serialize(new { paymentKey, orderId, amount }),
200 Encoding.UTF8, "application/json");
201
202 var resp = await _http.SendAsync(req);
203 var body = await resp.Content.ReadAsStringAsync();
204
205 if (!resp.IsSuccessStatusCode)
206 return new PaymentConfirmResult { Succeeded = false, Error = body };
207
208 using var doc = JsonDocument.Parse(body);
209 var txId = doc.RootElement.TryGetProperty("paymentKey", out var pk) ? pk.GetString() : null;
210 return new PaymentConfirmResult { Succeeded = true, TransactionId = txId };
211 }
212}
async Task< PaymentConfirmResult > ConfirmAsync(string paymentKey, string orderId, int amount)
Task< PaymentStartResult > StartAsync(string orderNo, decimal amount, string customerName)
readonly PaymentKeyProtector _protector
TossPaymentGateway(HttpClient http, ShopPaymentConfig config, PaymentKeyProtector protector)