Created
November 13, 2017 04:25
-
-
Save hyukkwonepic/e05b6041a861a799eb9babbd3a841f93 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from flask import Flask, request, jsonify # flask 사용 | |
import json | |
import requests # requests 사용 | |
app = Flask(__name__) | |
def validate(imp_uid): | |
# 1. 인증 토큰 발급 받기 | |
access_token = "" | |
try: | |
get_access_token = requests.post("https://api.iamport.kr/users/getToken", data={ | |
"imp_key": "imp_apikey", | |
"imp_secret": "ekKoeW8RyKuT0zgaZsUtXXTLQ4AhPFW3ZGseDA6bkA5lamv9OqDMnxyeB9wqOsuO9W3Mx9YSJ4dTqJ3f" | |
}) | |
access_token = get_access_token.json()["response"]["access_token"] # 인증 토큰 | |
except requests.exceptions.RequestException as e: | |
print("Error: ", e) | |
# 2. imp_uid로 결제 정보 조회 | |
payment_amount = None | |
payment_status = "" | |
try: | |
get_payment_info = requests.get( | |
"https://api.iamport.kr/payments/{imp_uid}".format(imp_uid = imp_uid), | |
headers={ "Authorization": access_token } # 인증 토큰 Authorization header에 추가 | |
) | |
payment_amount = get_payment_info.json()["response"]["amount"] # 결제 가격 | |
payment_status = get_payment_info.json()["response"]["status"] # 결제 상태 | |
except requests.exceptions.RequestException as e: | |
print("Error: ", e) | |
# 3. 내 서버 DB 결제 정보 조회 | |
payment_amount_myserver = "" | |
try: | |
get_payment_info_myserver = requests.get( | |
"https://www.myawesomeservice.com/payment/{imp_uid}".format(imp_uid = imp_uid) | |
) | |
payment_amount_myserver = get_payment_info_myserver.json()["amount"] # 내 서버의 결제 가격 | |
except requests.exceptions.RequestException as e: | |
print("Error: ", e) | |
# 4. 결제 정보 비교하기 | |
if payment_status == "paid" and payment_amount == payment_amount_myserver: # 결제 완료 | |
return { "status": "success", "type": "payment" } | |
elif payment_status == "ready" and payment_amount == payment_amount_myserver: # 가상계좌 결제 완료 | |
return { "status": "success", "type": "vbank "} | |
else: | |
return { "status": "Payment info doesn't match" } | |
@app.route("/payment/success", methods=["POST"]) | |
def payment_succes(): | |
request_body = json.loads(request.data) # POST request body의 json을 decoding | |
imp_uid = request_body["imp_uid"] # imp_uid 추출 | |
return jsonify(validate(imp_uid)) # validate 함수에 imp_uid 전달 후 검증 결과 반환 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment