linepay.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import uuid
  2. from fastapi import APIRouter
  3. from fastapi.param_functions import Depends
  4. from linepay import LinePayApi
  5. from fastapi.templating import Jinja2Templates
  6. from sqlalchemy.orm.session import Session
  7. from app import crud, models, schemas
  8. from app.api import deps
  9. from app.db import session
  10. # template
  11. templates = Jinja2Templates(directory="templates")
  12. router = APIRouter()
  13. LINE_PAY_CHANNEL_ID = "1656387996"
  14. SECRET_KEY = "37336af5452f74ee871a9fa38d81602e"
  15. HOST_NAME = "https://api.ptt.cx:8750"
  16. # Line Pay Config
  17. LINE_PAY_CHANNEL_ID = LINE_PAY_CHANNEL_ID
  18. LINE_PAY_CHANNEL_SECRET = "37336af5452f74ee871a9fa38d81602e"
  19. # LINE_PAY_REQEST_BASE_URL = "https://{}".format(HOST_NAME)
  20. LINE_PAY_REQEST_BASE_URL = "https://api.ptt.cx:8750"
  21. line = LinePayApi(
  22. LINE_PAY_CHANNEL_ID, LINE_PAY_CHANNEL_SECRET, is_sandbox=True
  23. )
  24. # CACHE
  25. CACHE = {}
  26. # Request
  27. @router.post('/request')
  28. async def pay_request(nft_id: str, db: Session = Depends(deps.get_db)):
  29. order_id = str(uuid.uuid4())
  30. amount = "1"
  31. currency = "TWD"
  32. nft = crud.nft.get(db=db, id=nft_id)
  33. request_options = {
  34. "amount": amount,
  35. "currency": currency,
  36. "orderId": order_id,
  37. "packages": [
  38. {
  39. "id": "NFT",
  40. "amount": 1,
  41. "products": [
  42. {
  43. "name": nft.title,
  44. "quantity": 1,
  45. "price": 1,
  46. "imageUrl": nft.imgurl
  47. }
  48. ]
  49. }
  50. ],
  51. "redirectUrls": {
  52. "confirmUrl": LINE_PAY_REQEST_BASE_URL + "/confirm/"
  53. # "cancelUrl": LINE_PAY_REQEST_BASE_URL + "/cancel/"
  54. }
  55. }
  56. response = line.request(request_options)
  57. transaction_id = int(response.get("info", {}).get("transactionId", 0))
  58. check_result = line.check_payment_status(transaction_id)
  59. response["transaction_id"] = transaction_id
  60. response["paymentStatusCheckReturnCode"] = check_result.get(
  61. "returnCode", None
  62. )
  63. response["paymentStatusCheckReturnMessage"] = check_result.get(
  64. "returnMessage", None
  65. )
  66. # return response
  67. return response
  68. # Confirm
  69. @router.get('/confirm/')
  70. async def pay_confirm(
  71. transactionId: int = "transactionId",
  72. ):
  73. CACHE["transaction_id"] = transactionId
  74. response = line.confirm(
  75. transactionId, float(CACHE.get("amount", 0)),
  76. CACHE.get("currency", "TWD"))
  77. check_result = line.check_payment_status(transactionId)
  78. payment_details = line.payment_details(transaction_id=transactionId)
  79. response["transaction_id"] = transactionId
  80. response["paymentStatusCheckReturnCode"] = check_result.get(
  81. "returnCode", None
  82. )
  83. response["paymentStatusCheckReturnMessage"] = check_result.get(
  84. "returnMessage", None
  85. )
  86. response["payment_details"] = payment_details
  87. if(response["paymentStatusCheckReturnCode"] == '0123'):
  88. # return response
  89. return "confirm.html", {"request": response}
  90. else:
  91. return "Not found"