|
@@ -5,9 +5,8 @@ from fastapi.param_functions import Depends
|
|
from linepay import LinePayApi
|
|
from linepay import LinePayApi
|
|
from fastapi.templating import Jinja2Templates
|
|
from fastapi.templating import Jinja2Templates
|
|
from sqlalchemy.orm.session import Session
|
|
from sqlalchemy.orm.session import Session
|
|
-from app import crud, models, schemas
|
|
|
|
|
|
+from app import crud, schemas
|
|
from app.api import deps
|
|
from app.api import deps
|
|
-from app.db import session
|
|
|
|
|
|
|
|
# template
|
|
# template
|
|
templates = Jinja2Templates(directory="templates")
|
|
templates = Jinja2Templates(directory="templates")
|
|
@@ -22,7 +21,7 @@ HOST_NAME = "https://api.ptt.cx:8750"
|
|
LINE_PAY_CHANNEL_ID = LINE_PAY_CHANNEL_ID
|
|
LINE_PAY_CHANNEL_ID = LINE_PAY_CHANNEL_ID
|
|
LINE_PAY_CHANNEL_SECRET = "37336af5452f74ee871a9fa38d81602e"
|
|
LINE_PAY_CHANNEL_SECRET = "37336af5452f74ee871a9fa38d81602e"
|
|
# LINE_PAY_REQEST_BASE_URL = "https://{}".format(HOST_NAME)
|
|
# LINE_PAY_REQEST_BASE_URL = "https://{}".format(HOST_NAME)
|
|
-LINE_PAY_REQEST_BASE_URL = "https://api.ptt.cx:8750"
|
|
|
|
|
|
+LINE_PAY_REQEST_BASE_URL = "https://api.ptt.cx:8750/api/v1/linepay"
|
|
line = LinePayApi(
|
|
line = LinePayApi(
|
|
LINE_PAY_CHANNEL_ID, LINE_PAY_CHANNEL_SECRET, is_sandbox=True
|
|
LINE_PAY_CHANNEL_ID, LINE_PAY_CHANNEL_SECRET, is_sandbox=True
|
|
)
|
|
)
|
|
@@ -33,20 +32,21 @@ CACHE = {}
|
|
|
|
|
|
# Request
|
|
# Request
|
|
@router.post('/request')
|
|
@router.post('/request')
|
|
-async def pay_request(nft_id: str, db: Session = Depends(deps.get_db)):
|
|
|
|
|
|
+async def pay_request(
|
|
|
|
+ line_id:str, nft_id: str, db: Session = Depends(deps.get_db)
|
|
|
|
+ ):
|
|
order_id = str(uuid.uuid4())
|
|
order_id = str(uuid.uuid4())
|
|
amount = "1"
|
|
amount = "1"
|
|
currency = "TWD"
|
|
currency = "TWD"
|
|
nft = crud.nft.get(db=db, id=nft_id)
|
|
nft = crud.nft.get(db=db, id=nft_id)
|
|
|
|
|
|
-
|
|
|
|
request_options = {
|
|
request_options = {
|
|
"amount": amount,
|
|
"amount": amount,
|
|
"currency": currency,
|
|
"currency": currency,
|
|
"orderId": order_id,
|
|
"orderId": order_id,
|
|
"packages": [
|
|
"packages": [
|
|
{
|
|
{
|
|
- "id": "NFT",
|
|
|
|
|
|
+ "id": nft.id,
|
|
"amount": 1,
|
|
"amount": 1,
|
|
"products": [
|
|
"products": [
|
|
{
|
|
{
|
|
@@ -64,6 +64,7 @@ async def pay_request(nft_id: str, db: Session = Depends(deps.get_db)):
|
|
# "cancelUrl": LINE_PAY_REQEST_BASE_URL + "/cancel/"
|
|
# "cancelUrl": LINE_PAY_REQEST_BASE_URL + "/cancel/"
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
response = line.request(request_options)
|
|
response = line.request(request_options)
|
|
transaction_id = int(response.get("info", {}).get("transactionId", 0))
|
|
transaction_id = int(response.get("info", {}).get("transactionId", 0))
|
|
check_result = line.check_payment_status(transaction_id)
|
|
check_result = line.check_payment_status(transaction_id)
|
|
@@ -74,18 +75,23 @@ async def pay_request(nft_id: str, db: Session = Depends(deps.get_db)):
|
|
response["paymentStatusCheckReturnMessage"] = check_result.get(
|
|
response["paymentStatusCheckReturnMessage"] = check_result.get(
|
|
"returnMessage", None
|
|
"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 response
|
|
return response
|
|
return response
|
|
|
|
|
|
|
|
|
|
# Confirm
|
|
# Confirm
|
|
-@router.get('/confirm/')
|
|
|
|
|
|
+@router.get('/confirm')
|
|
async def pay_confirm(
|
|
async def pay_confirm(
|
|
transactionId: int = "transactionId",
|
|
transactionId: int = "transactionId",
|
|
):
|
|
):
|
|
CACHE["transaction_id"] = transactionId
|
|
CACHE["transaction_id"] = transactionId
|
|
response = line.confirm(
|
|
response = line.confirm(
|
|
- transactionId, float(CACHE.get("amount", 0)),
|
|
|
|
|
|
+ transactionId, float(CACHE.get("amount", 1)),
|
|
CACHE.get("currency", "TWD"))
|
|
CACHE.get("currency", "TWD"))
|
|
check_result = line.check_payment_status(transactionId)
|
|
check_result = line.check_payment_status(transactionId)
|
|
payment_details = line.payment_details(transaction_id=transactionId)
|
|
payment_details = line.payment_details(transaction_id=transactionId)
|
|
@@ -99,6 +105,6 @@ async def pay_confirm(
|
|
response["payment_details"] = payment_details
|
|
response["payment_details"] = payment_details
|
|
if(response["paymentStatusCheckReturnCode"] == '0123'):
|
|
if(response["paymentStatusCheckReturnCode"] == '0123'):
|
|
# return response
|
|
# return response
|
|
- return "confirm.html", {"request": response}
|
|
|
|
|
|
+ return "OK"
|
|
else:
|
|
else:
|
|
return "Not found"
|
|
return "Not found"
|