ShopPlatform.Web
1.0.0.0
농산물, 소프트웨어 라이선스와 개발 용역을 직접 판매하는 CodeMaru 직영 쇼핑몰입니다.
Toggle main menu visibility
로딩중...
검색중...
일치하는것 없음
TossPaymentGateway.cs
이 파일의 문서화 페이지로 가기
1
using
System.Net.Http.Headers;
2
using
System.Text;
3
using
System.Text.Json;
4
using
ShopPlatform.Models
;
5
using
ShopPlatform.Services
;
6
7
namespace
ShopPlatform.Payments
;
8
17
public
sealed
class
TossPaymentGateway
:
IPaymentGateway
18
{
27
private
const
string
ApiBase
=
"https://api.tosspayments.com/v1"
;
28
37
private
readonly HttpClient
_http
;
46
private
readonly
ShopPaymentConfig
_config
;
55
private
readonly
PaymentKeyProtector
_protector
;
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
}
ShopPlatform.Models
Definition
CartItem.cs:1
ShopPlatform.Payments
Definition
DummyPaymentGateway.cs:1
ShopPlatform.Services
Definition
CartService.cs:3
ShopPlatform.Models.ShopPaymentConfig
Definition
ShopConfig.cs:458
ShopPlatform.Payments.PaymentStartResult
Definition
IPaymentGateway.cs:12
ShopPlatform.Payments.PaymentConfirmResult
Definition
IPaymentGateway.cs:78
ShopPlatform.Payments.IPaymentGateway
Definition
IPaymentGateway.cs:117
ShopPlatform.Payments.TossPaymentGateway.ConfirmAsync
async Task< PaymentConfirmResult > ConfirmAsync(string paymentKey, string orderId, int amount)
Definition
TossPaymentGateway.cs:189
ShopPlatform.Payments.TossPaymentGateway.StartAsync
Task< PaymentStartResult > StartAsync(string orderNo, decimal amount, string customerName)
Definition
TossPaymentGateway.cs:136
ShopPlatform.Payments.TossPaymentGateway._config
readonly ShopPaymentConfig _config
Definition
TossPaymentGateway.cs:46
ShopPlatform.Payments.TossPaymentGateway.ApiBase
const string ApiBase
Definition
TossPaymentGateway.cs:27
ShopPlatform.Payments.TossPaymentGateway._http
readonly HttpClient _http
Definition
TossPaymentGateway.cs:37
ShopPlatform.Payments.TossPaymentGateway._protector
readonly PaymentKeyProtector _protector
Definition
TossPaymentGateway.cs:55
ShopPlatform.Payments.TossPaymentGateway.TossPaymentGateway
TossPaymentGateway(HttpClient http, ShopPaymentConfig config, PaymentKeyProtector protector)
Definition
TossPaymentGateway.cs:89
ShopPlatform.Services.PaymentKeyProtector
Definition
PaymentKeyProtector.cs:14
Payments
TossPaymentGateway.cs
다음에 의해 생성됨 :
1.17.0