<!DOCTYPE html>
<html lang="ko">
<head>
<title>결제하기</title>
<meta charset="utf-8">
<script src="https://js.tosspayments.com/v1"></script>
</head>
<body>
<script>
var clientKey = 'test_key'
var tossPayments = TossPayments(clientKey) // 클라이언트 키로 초기화하기
</script>
</body>
<script>
tossPayments.requestPayment('카드', {
amount: 150000,
orderId: '0299f62b-865b-4ba1-812f-15sdfs42',
orderName: '개인산책',
customerName: '홍길동',
successUrl: 'http://localhost:8080/success',
failUrl: 'http://localhost:8080/fail',
})
</script>
</html>
결제 요청이 성공하거나 실패하면 위에서 설정한 successUrl, failUrl로 이동합니다. URL에 포함된 쿼리 파라미터로 결제 승인을 요청할 수 있습니다.
successUrl로 리다이렉트 된 경우
https://{ORIGIN}/success?paymentKey={PAYMENT_KEY}&orderId={ORDER_ID}&amount={AMOUNT}
결제창을 열 때 requestPayment 메서드에 담아 보냈던 amount 값과 리다이렉트 URL에 있는 실제 결제 금액인 amount 값이 같은지 확인
curl --request POST \
--url https://api.tosspayments.com/v1/payments/confirm \
--header 'Authorization: Basic dGVzdF9za19kMjZEbGJYQWFWMG85Uk5HOWJLM3FZNTBROVJCOg==' \
--header 'Content-Type: application/json' \
--data '{"paymentKey":"P5qJ5TLLWt-yD51UZrpD6","amount":15000,"orderId":"Rjbb0lCkPeGe56cw-JmUk"}'
API 호출 결과로 HTTP 상태 코드 200이 돌아오면 결제 승인 성공
{
"mId": "tosspayments",
"version": "2022-06-08",
"paymentKey": "P5qJ5TLLWt-yD51UZrpD6",
"status": "DONE",
"transactionKey": "vwHfrGh21kF0qPlP-c3eC",
"lastTransactionKey": "JhwlGZaT_tHwJTKk4f1s-",
"orderId": "Rjbb0lCkPeGe56cw-JmUk",
"orderName": "토스 티셔츠 외 2건",
"requestedAt": "2022-06-08T15:40:09+09:00",
"approvedAt": "2022-06-08T15:40:49+09:00",
"useEscrow": false,
"cultureExpense": false,
"card": {
"company": "농협",
"number": "123456******7890",
"installmentPlanMonths": 0,
"isInterestFree": false,
"interestPayer": null,
"approveNo": "00000000",
"useCardPoint": false,
"cardType": "신용",
"ownerType": "개인",
"acquireStatus": "READY",
"receiptUrl": "https://dashboard.tosspayments.com/sales-slip?transactionId=KAgfjGxIqVVXDxOiSW1wUnRWBS1dszn3DKcuhpm7mQlKP0iOdgPCKmwEdYglIHX&ref=PX",
"amount": 15000
},
"virtualAccount": null,
"transfer": null,
"mobilePhone": null,
"giftCertificate": null,
"cashReceipt": null,
"discount": null,
"cancels": null,
"secret": null,
"type": "NORMAL",
"easyPay": null,
"country": "KR",
"failure": null,
"isPartialCancelable": true,
"receipt": {
"url": "https://dashboard.tosspayments.com/sales-slip?transactionId=KAgfjGxIqVVXDxOiSW1wUnRWBS1dszn3DKcuhpm7mQlKP0iOdgPCKmwEdYglIHX&ref=PX"
},
"currency": "KRW",
"totalAmount": 15000,
"balanceAmount": 15000,
"suppliedAmount": 13636,
"vat": 1364,
"taxFreeAmount": 0,
"method": "카드"
}
ref