2022-02-13 23:39:27 +08:00
|
|
|
// Copyright 2022 The Casdoor Authors. All Rights Reserved.
|
2022-02-05 01:18:13 +08:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/casdoor/casdoor/util"
|
2022-03-07 00:33:45 +08:00
|
|
|
"github.com/go-pay/gopay"
|
|
|
|
"github.com/go-pay/gopay/alipay"
|
2022-02-05 01:18:13 +08:00
|
|
|
"xorm.io/core"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Payment struct {
|
|
|
|
Owner string `xorm:"varchar(100) notnull pk" json:"owner"`
|
|
|
|
Name string `xorm:"varchar(100) notnull pk" json:"name"`
|
|
|
|
CreatedTime string `xorm:"varchar(100)" json:"createdTime"`
|
|
|
|
DisplayName string `xorm:"varchar(100)" json:"displayName"`
|
|
|
|
|
2022-03-13 11:51:33 +08:00
|
|
|
Provider string `xorm:"varchar(100)" json:"provider"`
|
|
|
|
Type string `xorm:"varchar(100)" json:"type"`
|
|
|
|
Organization string `xorm:"varchar(100)" json:"organization"`
|
|
|
|
User string `xorm:"varchar(100)" json:"user"`
|
|
|
|
ProductId string `xorm:"varchar(100)" json:"productId"`
|
|
|
|
ProductName string `xorm:"varchar(100)" json:"productName"`
|
|
|
|
|
|
|
|
Detail string `xorm:"varchar(100)" json:"detail"`
|
|
|
|
Tag string `xorm:"varchar(100)" json:"tag"`
|
|
|
|
Currency string `xorm:"varchar(100)" json:"currency"`
|
|
|
|
Price float64 `json:"price"`
|
2022-02-05 01:18:13 +08:00
|
|
|
|
2022-03-13 16:25:54 +08:00
|
|
|
PayUrl string `xorm:"varchar(2000)" json:"payUrl"`
|
|
|
|
ReturnUrl string `xorm:"varchar(1000)" json:"returnUrl"`
|
|
|
|
State string `xorm:"varchar(100)" json:"state"`
|
2022-02-05 01:18:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetPaymentCount(owner, field, value string) int {
|
|
|
|
session := GetSession(owner, -1, -1, field, value, "", "")
|
|
|
|
count, err := session.Count(&Payment{})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return int(count)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetPayments(owner string) []*Payment {
|
|
|
|
payments := []*Payment{}
|
|
|
|
err := adapter.Engine.Desc("created_time").Find(&payments, &Payment{Owner: owner})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return payments
|
|
|
|
}
|
|
|
|
|
2022-03-13 11:51:33 +08:00
|
|
|
func GetUserPayments(owner string, organization string, user string) []*Payment {
|
|
|
|
payments := []*Payment{}
|
|
|
|
err := adapter.Engine.Desc("created_time").Find(&payments, &Payment{Owner: owner, Organization: organization, User: user})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return payments
|
|
|
|
}
|
|
|
|
|
2022-02-05 01:18:13 +08:00
|
|
|
func GetPaginationPayments(owner string, offset, limit int, field, value, sortField, sortOrder string) []*Payment {
|
|
|
|
payments := []*Payment{}
|
|
|
|
session := GetSession(owner, offset, limit, field, value, sortField, sortOrder)
|
|
|
|
err := session.Find(&payments)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return payments
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPayment(owner string, name string) *Payment {
|
|
|
|
if owner == "" || name == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
payment := Payment{Owner: owner, Name: name}
|
|
|
|
existed, err := adapter.Engine.Get(&payment)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if existed {
|
|
|
|
return &payment
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetPayment(id string) *Payment {
|
|
|
|
owner, name := util.GetOwnerAndNameFromId(id)
|
|
|
|
return getPayment(owner, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpdatePayment(id string, payment *Payment) bool {
|
|
|
|
owner, name := util.GetOwnerAndNameFromId(id)
|
|
|
|
if getPayment(owner, name) == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
affected, err := adapter.Engine.ID(core.PK{owner, name}).AllCols().Update(payment)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return affected != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func AddPayment(payment *Payment) bool {
|
|
|
|
affected, err := adapter.Engine.Insert(payment)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return affected != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeletePayment(payment *Payment) bool {
|
|
|
|
affected, err := adapter.Engine.ID(core.PK{payment.Owner, payment.Name}).Delete(&Payment{})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return affected != 0
|
|
|
|
}
|
|
|
|
|
2022-03-07 00:33:45 +08:00
|
|
|
func NotifyPayment(bm gopay.BodyMap) bool {
|
|
|
|
owner := "admin"
|
|
|
|
productName := bm.Get("subject")
|
|
|
|
paymentId := bm.Get("out_trade_no")
|
|
|
|
priceString := bm.Get("total_amount")
|
|
|
|
price := util.ParseFloat(priceString)
|
|
|
|
productId := bm.Get("productId")
|
|
|
|
providerId := bm.Get("providerId")
|
|
|
|
|
|
|
|
product := getProduct(owner, productId)
|
|
|
|
if product == nil {
|
|
|
|
panic(fmt.Errorf("the product: %s does not exist", productId))
|
|
|
|
}
|
|
|
|
|
|
|
|
if productName != product.DisplayName {
|
|
|
|
panic(fmt.Errorf("the payment's product name: %s doesn't equal to the expected product name: %s", productName, product.DisplayName))
|
|
|
|
}
|
|
|
|
|
|
|
|
if price != product.Price {
|
|
|
|
panic(fmt.Errorf("the payment's price: %f doesn't equal to the expected price: %f", price, product.Price))
|
|
|
|
}
|
|
|
|
|
|
|
|
payment := getPayment(owner, paymentId)
|
2022-03-06 22:46:02 +08:00
|
|
|
if payment == nil {
|
2022-03-07 00:33:45 +08:00
|
|
|
panic(fmt.Errorf("the payment: %s does not exist", paymentId))
|
2022-03-06 22:46:02 +08:00
|
|
|
}
|
|
|
|
|
2022-03-07 00:33:45 +08:00
|
|
|
provider, err := product.getProvider(providerId)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2022-03-06 22:46:02 +08:00
|
|
|
|
2022-03-07 00:33:45 +08:00
|
|
|
cert := getCert(owner, provider.Cert)
|
|
|
|
if cert == nil {
|
|
|
|
panic(fmt.Errorf("the cert: %s does not exist", provider.Cert))
|
|
|
|
}
|
|
|
|
|
|
|
|
ok, err := alipay.VerifySignWithCert(cert.AuthorityPublicKey, bm)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
payment.State = "Paid"
|
|
|
|
} else {
|
|
|
|
if cert == nil {
|
|
|
|
panic(fmt.Errorf("VerifySignWithCert() failed: %v", ok))
|
|
|
|
}
|
|
|
|
//payment.State = "Failed"
|
|
|
|
}
|
|
|
|
|
|
|
|
affected, err := adapter.Engine.ID(core.PK{owner, paymentId}).AllCols().Update(payment)
|
2022-03-06 22:46:02 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return affected != 0
|
|
|
|
}
|
|
|
|
|
2022-02-05 01:18:13 +08:00
|
|
|
func (payment *Payment) GetId() string {
|
|
|
|
return fmt.Sprintf("%s/%s", payment.Owner, payment.Name)
|
|
|
|
}
|