123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- import uuid
- from fastapi import APIRouter
- from fastapi.param_functions import Depends
- from linepay import LinePayApi
- from fastapi.templating import Jinja2Templates
- from sqlalchemy.orm.session import Session
- from app import crud, schemas
- from app.api import deps
- # template
- templates = Jinja2Templates(directory="templates")
- router = APIRouter()
- LINE_PAY_CHANNEL_ID = "1656387996"
- SECRET_KEY = "37336af5452f74ee871a9fa38d81602e"
- HOST_NAME = "https://api.ptt.cx:8750"
- # Line Pay Config
- LINE_PAY_CHANNEL_ID = LINE_PAY_CHANNEL_ID
- LINE_PAY_CHANNEL_SECRET = "37336af5452f74ee871a9fa38d81602e"
- # LINE_PAY_REQEST_BASE_URL = "https://{}".format(HOST_NAME)
- LINE_PAY_REQEST_BASE_URL = "https://api.ptt.cx:8750/api/v1/linepay"
- line = LinePayApi(
- LINE_PAY_CHANNEL_ID, LINE_PAY_CHANNEL_SECRET, is_sandbox=True
- )
- # CACHE
- CACHE = {}
- # Request
- @router.post('/request')
- async def pay_request(
- line_id:str, nft_id: str, db: Session = Depends(deps.get_db)
- ):
- order_id = str(uuid.uuid4())
- amount = "1"
- currency = "TWD"
- nft = crud.nft.get(db=db, id=nft_id)
- request_options = {
- "amount": amount,
- "currency": currency,
- "orderId": order_id,
- "packages": [
- {
- "id": nft.id,
- "amount": 1,
- "products": [
- {
- "name": nft.title,
- "quantity": 1,
- "price": 1,
- "imageUrl": nft.imgurl
- }
- ]
- }
- ],
- "redirectUrls": {
- # "confirmUrl": LINE_PAY_REQEST_BASE_URL + "/confirm/"
- "confirmUrl": "https://ark.cards/collect.html"
- # "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
- )
- payment_obj = schemas.payment.PaymentBase(
- order_id=order_id, transaction_id=transaction_id,
- payload=str(request_options), line_id=line_id
- )
- payment = crud.payment.create(db=db, obj_in=payment_obj)
- # return response
- return {"web":response["info"]["paymentUrl"]['web'], "app":response["info"]["paymentUrl"]['web']}
- return response["info"]["paymentUrl"]
- # Confirm
- @router.get('/confirm')
- async def pay_confirm(
- transactionId: int = "transactionId",
- ):
- CACHE["transaction_id"] = transactionId
- response = line.confirm(
- transactionId, float(CACHE.get("amount", 1)),
- CACHE.get("currency", "TWD"))
- check_result = line.check_payment_status(transactionId)
- payment_details = line.payment_details(transaction_id=transactionId)
- response["transaction_id"] = transactionId
- response["paymentStatusCheckReturnCode"] = check_result.get(
- "returnCode", None
- )
- response["paymentStatusCheckReturnMessage"] = check_result.get(
- "returnMessage", None
- )
- response["payment_details"] = payment_details
- if(response["paymentStatusCheckReturnCode"] == '0123'):
- # return response
- return "OK"
- else:
- return "Not found"
|