Parcourir la source

Use linepay sdk

Conrad il y a 3 ans
Parent
commit
4a99b67a88
3 fichiers modifiés avec 101 ajouts et 2 suppressions
  1. BIN
      __pycache__/main.cpython-310.pyc
  2. 79 2
      main.py
  3. 22 0
      templates/request.html

BIN
__pycache__/main.cpython-310.pyc


+ 79 - 2
main.py

@@ -1,10 +1,87 @@
+import dotenv
+import uuid
+import os
+import logging
 from fastapi import FastAPI
+from fastapi.templating import Jinja2Templates
 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.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

+ 22 - 0
templates/request.html

@@ -0,0 +1,22 @@
+<!DOCTYPE html>
+<html>
+    <head>
+		<title>LINE Pay Request API</title>
+    </head>
+    <body>
+		<dl>
+			<dt>paymentAccessToken</dt>
+			<dd>{{ result.info.paymentAccessToken }}</dd>
+			<dt>transactionId</dt>
+			<dd>{{ result.info.transactionId }}</dd>
+			<dt>Payment Status Check Return Code</dt>
+			<dd>{{ result.paymentStatusCheckReturnCode }}</dd>
+			<dt>Payment Status Check Return Message</dt>
+			<dd>{{ result.paymentStatusCheckReturnMessage }}</dd>
+			<dt>Access to below paymentUrl to confirm your payment</dt>
+			<dd>
+				<a href="{{ result.info.paymentUrl.web }}" rel="noopener noreferrer">paymentUrl</a>
+			</dd>
+		</dl>
+    </body>
+</html>