linepay.py 3.4 KB

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