import gradio as gr import requests from bs4 import BeautifulSoup def extract_info_from_html(html_content): soup = BeautifulSoup(html_content, 'html.parser') # Example: Extract order number and buyer name order_number = soup.find('input', {'name': 'ordr_idxx'})['value'] if soup.find('input', {'name': 'ordr_idxx'}) else 'Not found' buyer_name = soup.find('input', {'name': 'buyr_name'})['value'] if soup.find('input', {'name': 'buyr_name'}) else 'Not found' return { "order_number": order_number, "buyer_name": buyer_name, # Add more extracted fields as needed } def request_batch_key(onfftid, pay_type, method, cert_type, order_no, user_nm, user_phone2, product_nm, card_user_type, card_no, expire_date, auth_value, password, tot_amt): url = "https://store.onoffkorea.co.kr/fix/index.php" payload = { "onfftid": onfftid, "pay_type": pay_type, "method": method, "cert_type": cert_type, "order_no": order_no, "user_nm": user_nm, "user_phone2": user_phone2, "product_nm": product_nm, "card_user_type": card_user_type, "card_no": card_no, "expire_date": expire_date, "auth_value": auth_value, "password": password, "tot_amt": tot_amt } headers = { 'Accept': 'application/json', # 요청이 JSON을 선호한다고 서버에 알립니다 'Content-Type': 'application/x-www-form-urlencoded', # 폼 데이터로 전송 } try: response = requests.post(url, data=payload, headers=headers) response.raise_for_status() # Check if the response is JSON try: return response.json() except requests.exceptions.JSONDecodeError: # If not JSON, assume it's HTML and extract information extracted_info = extract_info_from_html(response.text) return { "status": "HTML response received", "extracted_info": extracted_info, "raw_html": response.text[:1000] # First 1000 characters of HTML for reference } except requests.exceptions.RequestException as e: return { "error": str(e), "status_code": getattr(e.response, 'status_code', None), "raw_response": getattr(e.response, 'text', None) } # The request_payment function can be updated similarly with gr.Blocks() as demo: gr.Markdown("# API 연동 테스트") with gr.Tab("카드 배치키 발급 요청"): onfftid = gr.Textbox(label="온오프코리아 TID") pay_type = gr.Textbox(label="결제 타입", value="fixKey") method = gr.Textbox(label="메소드", value="request") cert_type = gr.Radio(["0", "1"], label="인증 여부", value="0") order_no = gr.Textbox(label="주문번호") user_nm = gr.Textbox(label="주문자 이름") user_phone2 = gr.Textbox(label="주문자 연락처") product_nm = gr.Textbox(label="상품명") card_user_type = gr.Radio(["0", "1"], label="카드 타입", value="0") card_no = gr.Textbox(label="카드번호") expire_date = gr.Textbox(label="유효기간(YYMM)") auth_value = gr.Textbox(label="주민(사업자)등록번호") password = gr.Textbox(label="카드비밀번호 앞 2자리") tot_amt = gr.Number(label="결제금액") batch_key_button = gr.Button("배치키 발급 요청") batch_key_output = gr.JSON(label="응답 결과") batch_key_button.click( request_batch_key, inputs=[onfftid, pay_type, method, cert_type, order_no, user_nm, user_phone2, product_nm, card_user_type, card_no, expire_date, auth_value, password, tot_amt], outputs=batch_key_output ) # The "카드 배치키 결제 승인 요청" tab can be updated similarly demo.launch()