1
+ package com .example .networker_test .controller .order ;
2
+
3
+ import org .json .simple .JSONObject ;
4
+ import org .json .simple .parser .JSONParser ;
5
+ import org .json .simple .parser .ParseException ;
6
+ import org .slf4j .Logger ;
7
+ import org .slf4j .LoggerFactory ;
8
+ import org .springframework .stereotype .Controller ;
9
+ import org .springframework .http .ResponseEntity ;
10
+ import org .springframework .web .bind .annotation .RequestBody ;
11
+ import org .springframework .web .bind .annotation .RequestMapping ;
12
+
13
+ import java .io .*;
14
+ import java .net .HttpURLConnection ;
15
+ import java .net .URL ;
16
+ import java .nio .charset .StandardCharsets ;
17
+ import java .util .Base64 ;
18
+
19
+ @ Controller
20
+ public class OrderController {
21
+
22
+ private final Logger logger = LoggerFactory .getLogger (this .getClass ());
23
+
24
+ @ RequestMapping (value = "/confirm" )
25
+ public ResponseEntity <JSONObject > confirmPayment (@ RequestBody String jsonBody ) throws Exception {
26
+
27
+ JSONParser parser = new JSONParser ();
28
+ String orderId ;
29
+ String amount ;
30
+ String paymentKey ;
31
+ try {
32
+ // 클라이언트에서 받은 JSON 요청 바디입니다.
33
+ JSONObject requestData = (JSONObject ) parser .parse (jsonBody );
34
+ paymentKey = (String ) requestData .get ("paymentKey" );
35
+ orderId = (String ) requestData .get ("orderId" );
36
+ amount = (String ) requestData .get ("amount" );
37
+ } catch (ParseException e ) {
38
+ throw new RuntimeException (e );
39
+ }
40
+ ;
41
+ JSONObject obj = new JSONObject ();
42
+ obj .put ("orderId" , orderId );
43
+ obj .put ("amount" , amount );
44
+ obj .put ("paymentKey" , paymentKey );
45
+
46
+ // 토스페이먼츠 API는 시크릿 키를 사용자 ID로 사용하고, 비밀번호는 사용하지 않습니다.
47
+ // 비밀번호가 없다는 것을 알리기 위해 시크릿 키 뒤에 콜론을 추가합니다.
48
+ String widgetSecretKey = "test_gsk_docs_OaPz8L5KdmQXkzRz3y47BMw6" ;
49
+ Base64 .Encoder encoder = Base64 .getEncoder ();
50
+ byte [] encodedBytes = encoder .encode ((widgetSecretKey + ":" ).getBytes (StandardCharsets .UTF_8 ));
51
+ String authorizations = "Basic " + new String (encodedBytes );
52
+
53
+ // 결제를 승인하면 결제수단에서 금액이 차감돼요.
54
+ URL url = new URL ("https://api.tosspayments.com/v1/payments/confirm" );
55
+ HttpURLConnection connection = (HttpURLConnection ) url .openConnection ();
56
+ connection .setRequestProperty ("Authorization" , authorizations );
57
+ connection .setRequestProperty ("Content-Type" , "application/json" );
58
+ connection .setRequestMethod ("POST" );
59
+ connection .setDoOutput (true );
60
+
61
+ OutputStream outputStream = connection .getOutputStream ();
62
+ outputStream .write (obj .toString ().getBytes ("UTF-8" ));
63
+
64
+ int code = connection .getResponseCode ();
65
+ boolean isSuccess = code == 200 ;
66
+
67
+ InputStream responseStream = isSuccess ? connection .getInputStream () : connection .getErrorStream ();
68
+
69
+ // 결제 성공 및 실패 비즈니스 로직을 구현하세요.
70
+ Reader reader = new InputStreamReader (responseStream , StandardCharsets .UTF_8 );
71
+ JSONObject jsonObject = (JSONObject ) parser .parse (reader );
72
+ responseStream .close ();
73
+
74
+ return ResponseEntity .status (code ).body (jsonObject );
75
+ }
76
+ }
0 commit comments