testapi / app.py
aiqtech's picture
Update app.py
570234d verified
import gradio as gr
import requests
import xml.etree.ElementTree as ET
def card_authorization(onfftid, tot_amt, com_tax_amt, com_free_amt, com_vat_amt, card_no, install_period, user_nm, user_phone2, product_nm, expire_date, cert_type, card_user_type, auth_value, password, card_nm, order_no, pay_type):
url = "https://store.onoffkorea.co.kr/payment/index.php"
# ํ•„์ˆ˜ ํŒŒ๋ผ๋ฏธํ„ฐ ํ™•์ธ
if not all([onfftid, tot_amt, card_no, install_period, user_nm, user_phone2, product_nm, expire_date, cert_type, card_user_type]):
return {"error": "ํ•„์ˆ˜ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ๋ˆ„๋ฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค."}
# ์ธ์ฆ ๊ด€๋ จ ํŒŒ๋ผ๋ฏธํ„ฐ ์ฒ˜๋ฆฌ
if cert_type == "0 - ์ธ์ฆ":
if not auth_value or not password:
return {"error": "์ธ์ฆ ๊ฒฐ์ œ ์‹œ ์ธ์ฆ๋ฒˆํ˜ธ์™€ ๋น„๋ฐ€๋ฒˆํ˜ธ๋Š” ํ•„์ˆ˜์ž…๋‹ˆ๋‹ค."}
cert_type = "0"
elif cert_type == "1 - ๋น„์ธ์ฆ":
cert_type = "1"
else:
return {"error": "์ž˜๋ชป๋œ ์ธ์ฆ ์œ ํ˜•์ž…๋‹ˆ๋‹ค."}
# ์นด๋“œ ์œ ํ˜• ์ฒ˜๋ฆฌ
if card_user_type == "0 - ๊ฐœ์ธ์นด๋“œ":
card_user_type = "0"
elif card_user_type == "1 - ๋ฒ•์ธ์นด๋“œ":
card_user_type = "1"
else:
return {"error": "์ž˜๋ชป๋œ ์นด๋“œ ์œ ํ˜•์ž…๋‹ˆ๋‹ค."}
# ํ• ๋ถ€๊ธฐ๊ฐ„ ์ฒ˜๋ฆฌ
install_period = install_period.split(' - ')[0] # "00 - ์ผ์‹œ๋ถˆ"์—์„œ "00"๋งŒ ์ถ”์ถœ
data = {
"onfftid": onfftid,
"tot_amt": tot_amt,
"com_tax_amt": com_tax_amt if com_tax_amt is not None else '',
"com_free_amt": com_free_amt if com_free_amt is not None else '',
"com_vat_amt": com_vat_amt if com_vat_amt is not None else '',
"card_no": card_no,
"install_period": install_period,
"user_nm": user_nm,
"user_phone2": user_phone2,
"product_nm": product_nm,
"expire_date": expire_date,
"cert_type": cert_type,
"card_user_type": card_user_type,
"auth_value": auth_value,
"password": password,
"card_nm": card_nm or '',
"order_no": order_no or '',
"pay_type": pay_type or 'card'
}
response = requests.post(url, data=data)
if response.status_code == 200:
try:
root = ET.fromstring(response.text)
result = {}
for child in root:
result[child.tag] = child.text
# ๊ฒฐ๊ณผ ์ฝ”๋“œ ํ™•์ธ
if result.get('result_cd') == '0000':
result['status'] = '์„ฑ๊ณต'
else:
result['status'] = '์‹คํŒจ'
return result
except ET.ParseError:
return {"error": "์‘๋‹ต ํŒŒ์‹ฑ ์˜ค๋ฅ˜", "details": response.text}
else:
return {"error": "์š”์ฒญ ์‹คํŒจ", "status_code": response.status_code}
with gr.Blocks() as app:
with gr.Tab("์นด๋“œ ๊ฒฐ์ œ ์Šน์ธ ์š”์ฒญ"):
with gr.Row():
with gr.Column(scale=1):
onfftid = gr.Textbox(label="์˜จ์˜คํ”„์ฝ”๋ฆฌ์•„ TID", value="OFPT000000011017")
tot_amt = gr.Number(label="๊ฒฐ์ œ๊ธˆ์•ก")
com_tax_amt = gr.Number(label="๊ณผ์„ธ์Šน์ธ๊ธˆ์•ก")
com_free_amt = gr.Number(label="๋น„๊ณผ์„ธ์Šน์ธ๊ธˆ์•ก")
com_vat_amt = gr.Number(label="๋ถ€๊ฐ€์„ธ")
card_no = gr.Textbox(label="์นด๋“œ๋ฒˆํ˜ธ")
install_period = gr.Dropdown(
choices=[f"{str(i).zfill(2)} - {'์ผ์‹œ๋ถˆ' if i == 0 else f'{i}๊ฐœ์›”'}" for i in range(13)],
label="ํ• ๋ถ€๊ธฐ๊ฐ„ ์„ ํƒ",
value="00 - ์ผ์‹œ๋ถˆ"
)
user_nm = gr.Textbox(label="๊ฒฐ์ œ์ž๋ช…")
user_phone2 = gr.Textbox(label="๊ฒฐ์ œ์ž ์—ฐ๋ฝ์ฒ˜")
product_nm = gr.Textbox(label="์ƒํ’ˆ๋ช…")
expire_date = gr.Textbox(label="์œ ํšจ๊ธฐ๊ฐ„(YYMM)")
cert_type = gr.Radio(choices=["0 - ์ธ์ฆ", "1 - ๋น„์ธ์ฆ"], label="์ธ์ฆ์—ฌ๋ถ€ ์„ ํƒ", value="0 - ์ธ์ฆ")
card_user_type = gr.Radio(choices=["0 - ๊ฐœ์ธ์นด๋“œ", "1 - ๋ฒ•์ธ์นด๋“œ"], label="์นด๋“œ์œ ํ˜• ์„ ํƒ", value="0 - ๊ฐœ์ธ์นด๋“œ")
auth_value = gr.Textbox(label="์ธ์ฆ๋ฒˆํ˜ธ (์ธ์ฆ ์‹œ ํ•„์ˆ˜)")
password = gr.Textbox(label="์นด๋“œ ๋น„๋ฐ€๋ฒˆํ˜ธ ์•ž 2์ž๋ฆฌ (์ธ์ฆ ์‹œ ํ•„์ˆ˜)")
card_nm = gr.Textbox(label="์นด๋“œ์‚ฌ๋ช…")
order_no = gr.Textbox(label="์ฃผ๋ฌธ๋ฒˆํ˜ธ")
pay_type = gr.Textbox(label="๊ฒฐ์ œํƒ€์ž…", value="card")
submit_button = gr.Button("๊ฒฐ์ œ ์Šน์ธ ์š”์ฒญ")
result_area = gr.JSON(label="๊ฒฐ๊ณผ")
submit_button.click(
fn=card_authorization,
inputs=[onfftid, tot_amt, com_tax_amt, com_free_amt, com_vat_amt, card_no, install_period, user_nm, user_phone2, product_nm, expire_date, cert_type, card_user_type, auth_value, password, card_nm, order_no, pay_type],
outputs=result_area
)
app.launch()