|
@@ -1,5 +1,10 @@
|
|
|
+import dataset
|
|
|
import uuid
|
|
|
-
|
|
|
+from datetime import datetime
|
|
|
+from linebot.models import (
|
|
|
+ MessageEvent, TextMessage, TextSendMessage, FollowEvent,
|
|
|
+ TemplateSendMessage, ButtonsTemplate, URITemplateAction,
|
|
|
+)
|
|
|
from fastapi import APIRouter
|
|
|
from fastapi.param_functions import Depends
|
|
|
from linepay import LinePayApi
|
|
@@ -7,7 +12,16 @@ from fastapi.templating import Jinja2Templates
|
|
|
from sqlalchemy.orm.session import Session
|
|
|
from app import crud, schemas
|
|
|
from app.api import deps
|
|
|
+from fastapi.encoders import jsonable_encoder
|
|
|
+import requests
|
|
|
+from app.core.config import settings
|
|
|
+
|
|
|
|
|
|
+baseUrl = "https://nft-api-staging.joyso.io/api/v1/"
|
|
|
+headers = {
|
|
|
+ 'Authorization':
|
|
|
+ 'Basic %s' % 'bmZ0OmMxOTEzOWMzYjM3YjdjZWU3ZmY3OTFiZGU3NzdjZWNl'
|
|
|
+}
|
|
|
# template
|
|
|
templates = Jinja2Templates(directory="templates")
|
|
|
|
|
@@ -29,17 +43,20 @@ line = LinePayApi(
|
|
|
# CACHE
|
|
|
CACHE = {}
|
|
|
|
|
|
-
|
|
|
+def push_text(user, message):
|
|
|
+ settings.line_bot_api.push_message(
|
|
|
+ user, TextSendMessage(text=message)
|
|
|
+ )
|
|
|
# Request
|
|
|
@router.post('/request')
|
|
|
async def pay_request(
|
|
|
- line_id:str, nft_id: str, db: Session = Depends(deps.get_db)
|
|
|
+ line_id:str, nft_id: str, amount: int,
|
|
|
+ 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,
|
|
@@ -47,11 +64,11 @@ async def pay_request(
|
|
|
"packages": [
|
|
|
{
|
|
|
"id": nft.id,
|
|
|
- "amount": 1,
|
|
|
+ "amount": amount,
|
|
|
"products": [
|
|
|
{
|
|
|
"name": nft.title,
|
|
|
- "quantity": 1,
|
|
|
+ "quantity": amount,
|
|
|
"price": 1,
|
|
|
"imageUrl": nft.imgurl
|
|
|
}
|
|
@@ -61,7 +78,8 @@ async def pay_request(
|
|
|
],
|
|
|
"redirectUrls": {
|
|
|
# "confirmUrl": LINE_PAY_REQEST_BASE_URL + "/confirm/"
|
|
|
- "confirmUrl": "https://ark.cards/collect.html"
|
|
|
+ "confirmUrl": "https://ark.cards/confirm.html",
|
|
|
+ "cancelUrl" : "https://ark.cards/confirm.html"
|
|
|
# "cancelUrl": LINE_PAY_REQEST_BASE_URL + "/cancel/"
|
|
|
}
|
|
|
}
|
|
@@ -78,10 +96,11 @@ async def pay_request(
|
|
|
)
|
|
|
payment_obj = schemas.payment.PaymentBase(
|
|
|
order_id=order_id, transaction_id=transaction_id,
|
|
|
- payload=str(request_options), line_id=line_id
|
|
|
+ amount=amount, nft_id=nft_id,
|
|
|
+ payment_status="establish the order", 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"]
|
|
|
|
|
@@ -89,12 +108,16 @@ async def pay_request(
|
|
|
# Confirm
|
|
|
@router.get('/confirm')
|
|
|
async def pay_confirm(
|
|
|
- transactionId: int = "transactionId",
|
|
|
+ transactionId: int = 123456789,
|
|
|
+ db: Session = Depends(deps.get_db)
|
|
|
):
|
|
|
- CACHE["transaction_id"] = transactionId
|
|
|
+ db_ = dataset.connect(
|
|
|
+ 'mysql://choozmo:pAssw0rd@db.ptt.cx:3306/arkcard?charset=utf8mb4'
|
|
|
+ )
|
|
|
+ transaction_table = db_['transaction']
|
|
|
+ payment = crud.payment.get_by_transaction(db, transaction_id=transactionId)
|
|
|
response = line.confirm(
|
|
|
- transactionId, float(CACHE.get("amount", 1)),
|
|
|
- CACHE.get("currency", "TWD"))
|
|
|
+ transactionId, float(payment.amount), "TWD")
|
|
|
check_result = line.check_payment_status(transactionId)
|
|
|
payment_details = line.payment_details(transaction_id=transactionId)
|
|
|
response["transaction_id"] = transactionId
|
|
@@ -106,7 +129,52 @@ async def pay_confirm(
|
|
|
)
|
|
|
response["payment_details"] = payment_details
|
|
|
if(response["paymentStatusCheckReturnCode"] == '0123'):
|
|
|
- # return response
|
|
|
- return "OK"
|
|
|
+ to = crud.user.get_by_lineid(db, lineid=payment.line_id).__dict__['useraddress']
|
|
|
+ payload = {
|
|
|
+ "txid": transactionId,
|
|
|
+ # "contract": "0xe0d9102c88b09369df99b1c126fb2eebc13804f8",
|
|
|
+ "contract": "0x8d5a7d8ca33fd9edca4b871cf5fb2a9cb5091505",
|
|
|
+ "to": to,
|
|
|
+ "uid": crud.nft.get(db, id=payment.nft_id).__dict__['uid'],
|
|
|
+ "value": payment.amount
|
|
|
+ }
|
|
|
+ r = requests.post(baseUrl + "accounts/U9dc55544ecca3a95b170bdf2a30e3691/erc1155/transfer_by_admin",
|
|
|
+ headers=headers, data=payload)
|
|
|
+ payment_update = schemas.payment.PaymentUpdate(**jsonable_encoder(payment))
|
|
|
+ if r.status_code == 200:
|
|
|
+ title = crud.nft.get(db, id=payment.nft_id).__dict__['title']
|
|
|
+ message = "您的NFT : " + title + ", 已劃轉!"
|
|
|
+ push_text("U9dc55544ecca3a95b170bdf2a30e3691", message)
|
|
|
+ transaction_table.insert({'txid':transactionId,'tfrom':"U9dc55544ecca3a95b170bdf2a30e3691",'to':payment.line_id,'nft':crud.nft.get(db, id=payment.nft_id).__dict__['uid'],'transaction_at':datetime.now()})
|
|
|
+ from_name = settings.line_bot_api.get_profile("U9dc55544ecca3a95b170bdf2a30e3691").as_json_dict()['displayName']
|
|
|
+ try:
|
|
|
+ to_name = settings.line_bot_api.get_profile(payment.line_id).as_json_dict()['displayName']
|
|
|
+ except:
|
|
|
+ to_name = "外部"
|
|
|
+ fr = "您賣給"+ to_name +"的NFT(" + title + "), 已發送,請靜待網路確認!"
|
|
|
+ to_message = "您購買的NFT("+title+"), 已成功,請靜待網路確認!"
|
|
|
+ push_text("U9dc55544ecca3a95b170bdf2a30e3691", fr)
|
|
|
+ try:
|
|
|
+ push_text(payment.line_id, to_message)
|
|
|
+ except:
|
|
|
+ pass
|
|
|
+ payment_update.payment_status = "success"
|
|
|
+ crud.payment.update(db, db_obj=payment, obj_in=payment_update)
|
|
|
+ return "OK"
|
|
|
+ else:
|
|
|
+ # push訊息
|
|
|
+ payment_update.payment_status = "success get payment, but transfer failed, error message is" + r.content
|
|
|
+ crud.payment.update(db, db_obj=payment, obj_in=payment_update)
|
|
|
+ message = str(transactionId) + " 收款成功,轉移失敗, error message is" + r.content
|
|
|
+ to_message = str(transactionId)+ " 收款成功,轉移失敗,請洽客服"
|
|
|
+ try:
|
|
|
+ push_text(payment.line_id, to_message)
|
|
|
+ except:
|
|
|
+ pass
|
|
|
+ push_text("U9dc55544ecca3a95b170bdf2a30e3691", message)
|
|
|
+ return {'msg': "收款成功,轉移失敗,請洽客服"}
|
|
|
else:
|
|
|
- return "Not found"
|
|
|
+ payment_update = schemas.payment.PaymentUpdate(**jsonable_encoder(payment))
|
|
|
+ payment_update.payment_status = "cancel"
|
|
|
+ crud.payment.update(db, db_obj=payment, obj_in=payment_update)
|
|
|
+ return "交易取消"
|