|  | @@ -1,10 +1,87 @@
 | 
											
												
													
														|  | 
 |  | +import dotenv
 | 
											
												
													
														|  | 
 |  | +import uuid
 | 
											
												
													
														|  | 
 |  | +import os
 | 
											
												
													
														|  | 
 |  | +import logging
 | 
											
												
													
														|  |  from fastapi import FastAPI
 |  |  from fastapi import FastAPI
 | 
											
												
													
														|  | 
 |  | +from fastapi.templating import Jinja2Templates
 | 
											
												
													
														|  |  from pydantic import BaseModel
 |  |  from pydantic import BaseModel
 | 
											
												
													
														|  | 
 |  | +from dotenv import load_dotenv
 | 
											
												
													
														|  | 
 |  | +from os.path import join, dirname
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | 
 |  | +from linepay import LinePayApi
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | 
 |  | +# dotenv
 | 
											
												
													
														|  | 
 |  | +dotenv_path = join(dirname(__file__),'./env/.env')
 | 
											
												
													
														|  | 
 |  | +load_dotenv(dotenv_path)
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +# logger (TBD)
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +# template
 | 
											
												
													
														|  | 
 |  | +templates = Jinja2Templates(directory="templates")
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +# Line Pay Config
 | 
											
												
													
														|  | 
 |  | +LINE_PAY_CHANNEL_ID = os.environ.get("LINE_PAY_CHANNEL_ID")
 | 
											
												
													
														|  | 
 |  | +LINE_PAY_CHANNEL_SECRET = os.environ.get("LINE_PAY_CHANNEL_SECRET")
 | 
											
												
													
														|  | 
 |  | +LINE_PAY_REQEST_BASE_URL = "https://{}".format(
 | 
											
												
													
														|  | 
 |  | +    os.environ.get("HOST_NAME")
 | 
											
												
													
														|  | 
 |  | +)
 | 
											
												
													
														|  | 
 |  | +line = LinePayApi(LINE_PAY_CHANNEL_ID, LINE_PAY_CHANNEL_SECRET, is_sandbox=True)
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +# Fastapi
 | 
											
												
													
														|  |  app = FastAPI()
 |  |  app = FastAPI()
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  |  
 |  |  
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  |  @app.get('/')
 |  |  @app.get('/')
 | 
											
												
													
														|  | -async def hello_world():
 |  | 
 | 
											
												
													
														|  | -    return "Hello world"
 |  | 
 | 
											
												
													
														|  | 
 |  | +def hellow():
 | 
											
												
													
														|  | 
 |  | +    return {"Hello" : "World"}
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +## Request
 | 
											
												
													
														|  | 
 |  | +@app.get('/request')
 | 
											
												
													
														|  | 
 |  | +async def pay_request():
 | 
											
												
													
														|  | 
 |  | +    order_id = str(uuid.uuid4())
 | 
											
												
													
														|  | 
 |  | +    amount = 1200
 | 
											
												
													
														|  | 
 |  | +    currency = "TWD"
 | 
											
												
													
														|  | 
 |  | +    request_options ={
 | 
											
												
													
														|  | 
 |  | +        "amount" : amount,
 | 
											
												
													
														|  | 
 |  | +        "currency" : currency,
 | 
											
												
													
														|  | 
 |  | +        "orderId" : order_id,
 | 
											
												
													
														|  | 
 |  | +        "packages" : [
 | 
											
												
													
														|  | 
 |  | +            {
 | 
											
												
													
														|  | 
 |  | +                "id" : "早鳥方案",
 | 
											
												
													
														|  | 
 |  | +                "amount" : 1200,
 | 
											
												
													
														|  | 
 |  | +                "products" :[
 | 
											
												
													
														|  | 
 |  | +                    {
 | 
											
												
													
														|  | 
 |  | +                        # "id" : "Id_早鳥方案",
 | 
											
												
													
														|  | 
 |  | +                        "name" : "早鳥方案",
 | 
											
												
													
														|  | 
 |  | +                        "quantity" : 1,
 | 
											
												
													
														|  | 
 |  | +                        "price" : 1200,
 | 
											
												
													
														|  | 
 |  | +                        # "imageUrl" : ""
 | 
											
												
													
														|  | 
 |  | +                    }
 | 
											
												
													
														|  | 
 |  | +                ]
 | 
											
												
													
														|  | 
 |  | +            }
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +        ],
 | 
											
												
													
														|  | 
 |  | +        "redirectUrls" : {
 | 
											
												
													
														|  | 
 |  | +            "confirmUrl" : LINE_PAY_REQEST_BASE_URL + "/confirm",
 | 
											
												
													
														|  | 
 |  | +            "cancelUrl" : LINE_PAY_REQEST_BASE_URL + "/cancel"
 | 
											
												
													
														|  | 
 |  | +        }
 | 
											
												
													
														|  | 
 |  | +    }
 | 
											
												
													
														|  | 
 |  | +    response = line.request(request_options)
 | 
											
												
													
														|  | 
 |  | +    transaction_id = int(response.get("info",{}).get("transactionId",0))
 | 
											
												
													
														|  | 
 |  | +    check_result = line.check_payment_status(transaction_id)
 | 
											
												
													
														|  | 
 |  | +    response["transaction_id"] = transaction_id
 | 
											
												
													
														|  | 
 |  | +    response["paymentStatusCheckReturnCode"] = check_result.get("returnCode", None)
 | 
											
												
													
														|  | 
 |  | +    response["paymentStatusCheckReturnMessage"] = check_result.get("returnMessage", None)
 | 
											
												
													
														|  | 
 |  | +    return templates.TemplateResponse("request.html", response=response)
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +## Confirm
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +## Capture
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +## Refund
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +## Payment Details API
 | 
											
												
													
														|  | 
 |  | +
 | 
											
												
													
														|  | 
 |  | +## Pay Preapproved API
 |