casdoor/object/payment.go

216 lines
5.5 KiB
Go
Raw Normal View History

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-03-13 19:57:23 +08:00
Message string `xorm:"varchar(1000)" json:"message"`
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-13 19:57:23 +08:00
func notifyPayment(bm gopay.BodyMap) (*Payment, error) {
2022-03-07 00:33:45 +08:00
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 {
2022-03-13 19:57:23 +08:00
return nil, fmt.Errorf("the product: %s does not exist", productId)
2022-03-07 00:33:45 +08:00
}
if productName != product.DisplayName {
2022-03-13 19:57:23 +08:00
return nil, fmt.Errorf("the payment's product name: %s doesn't equal to the expected product name: %s", productName, product.DisplayName)
2022-03-07 00:33:45 +08:00
}
if price != product.Price {
2022-03-13 19:57:23 +08:00
return nil, fmt.Errorf("the payment's price: %f doesn't equal to the expected price: %f", price, product.Price)
2022-03-07 00:33:45 +08:00
}
payment := getPayment(owner, paymentId)
2022-03-06 22:46:02 +08:00
if payment == nil {
2022-03-13 19:57:23 +08:00
return nil, 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 {
2022-03-13 19:57:23 +08:00
return payment, err
2022-03-07 00:33:45 +08:00
}
2022-03-06 22:46:02 +08:00
2022-03-07 00:33:45 +08:00
cert := getCert(owner, provider.Cert)
if cert == nil {
2022-03-13 19:57:23 +08:00
return payment, fmt.Errorf("the cert: %s does not exist", provider.Cert)
2022-03-07 00:33:45 +08:00
}
ok, err := alipay.VerifySignWithCert(cert.AuthorityPublicKey, bm)
if err != nil {
2022-03-13 19:57:23 +08:00
return payment, err
2022-03-07 00:33:45 +08:00
}
2022-03-13 19:57:23 +08:00
if !ok {
return payment, fmt.Errorf("VerifySignWithCert() failed: %v", ok)
2022-03-07 00:33:45 +08:00
}
2022-03-13 19:57:23 +08:00
return payment, nil
}
func NotifyPayment(bm gopay.BodyMap) bool {
payment, err := notifyPayment(bm)
if payment != nil {
if err != nil {
payment.State = "Error"
payment.Message = err.Error()
} else {
payment.State = "Paid"
}
UpdatePayment(payment.GetId(), payment)
2022-03-06 22:46:02 +08:00
}
2022-03-13 19:57:23 +08:00
ok := err == nil
return ok
2022-03-06 22:46:02 +08:00
}
2022-02-05 01:18:13 +08:00
func (payment *Payment) GetId() string {
return fmt.Sprintf("%s/%s", payment.Owner, payment.Name)
}