casdoor/object/payment.go

251 lines
7.1 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"
2022-03-14 02:07:55 +08:00
"net/http"
2022-02-05 01:18:13 +08:00
"github.com/casdoor/casdoor/util"
"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-14 02:07:55 +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"`
ProductName string `xorm:"varchar(100)" json:"productName"`
ProductDisplayName string `xorm:"varchar(100)" json:"productDisplayName"`
2022-03-13 11:51:33 +08:00
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-04-25 20:40:50 +08:00
PersonName string `xorm:"varchar(100)" json:"personName"`
PersonIdCard string `xorm:"varchar(100)" json:"personIdCard"`
PersonEmail string `xorm:"varchar(100)" json:"personEmail"`
PersonPhone string `xorm:"varchar(100)" json:"personPhone"`
2022-04-25 20:58:53 +08:00
InvoiceType string `xorm:"varchar(100)" json:"invoiceType"`
2022-04-25 20:40:50 +08:00
InvoiceTitle string `xorm:"varchar(100)" json:"invoiceTitle"`
InvoiceTaxId string `xorm:"varchar(100)" json:"invoiceTaxId"`
InvoiceRemark string `xorm:"varchar(100)" json:"invoiceRemark"`
2022-04-27 01:06:54 +08:00
InvoiceUrl string `xorm:"varchar(255)" json:"invoiceUrl"`
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-14 02:07:55 +08:00
func notifyPayment(request *http.Request, body []byte, owner string, providerName string, productName string, paymentName string) (*Payment, error) {
payment := getPayment(owner, paymentName)
if payment == nil {
return nil, fmt.Errorf("the payment: %s does not exist", paymentName)
2022-03-07 00:33:45 +08:00
}
2022-03-14 02:07:55 +08:00
product := getProduct(owner, productName)
if product == nil {
return nil, fmt.Errorf("the product: %s does not exist", productName)
2022-03-06 22:46:02 +08:00
}
2022-03-14 02:07:55 +08:00
provider, err := product.getProvider(providerName)
2022-03-07 00:33:45 +08:00
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-14 02:07:55 +08:00
pProvider, cert, err := provider.getPaymentProvider()
if err != nil {
return payment, err
2022-03-07 00:33:45 +08:00
}
2022-03-14 02:07:55 +08:00
productDisplayName, paymentName, price, productName, providerName, err := pProvider.Notify(request, body, cert.AuthorityPublicKey)
2022-03-07 00:33:45 +08:00
if err != nil {
2022-03-13 19:57:23 +08:00
return payment, err
2022-03-07 00:33:45 +08:00
}
2022-03-14 02:07:55 +08:00
if productDisplayName != "" && productDisplayName != product.DisplayName {
return nil, fmt.Errorf("the payment's product name: %s doesn't equal to the expected product name: %s", productDisplayName, product.DisplayName)
}
if price != product.Price {
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
}
2022-03-13 19:57:23 +08:00
return payment, nil
}
2022-03-14 02:07:55 +08:00
func NotifyPayment(request *http.Request, body []byte, owner string, providerName string, productName string, paymentName string) bool {
payment, err := notifyPayment(request, body, owner, providerName, productName, paymentName)
2022-03-13 19:57:23 +08:00
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-04-26 22:17:45 +08:00
func invoicePayment(payment *Payment) (string, error) {
provider := getProvider(payment.Owner, payment.Provider)
if provider == nil {
return "", fmt.Errorf("the payment provider: %s does not exist", payment.Provider)
}
pProvider, _, err := provider.getPaymentProvider()
if err != nil {
return "", err
}
invoiceUrl, err := pProvider.GetInvoice(payment.Name, payment.PersonName, payment.PersonIdCard, payment.PersonEmail, payment.PersonPhone, payment.InvoiceType, payment.InvoiceTitle, payment.InvoiceTaxId)
if err != nil {
return "", err
}
return invoiceUrl, nil
}
func InvoicePayment(payment *Payment) (string, error) {
2022-04-26 22:17:45 +08:00
if payment.State != "Paid" {
return "", fmt.Errorf("the payment state is supposed to be: \"%s\", got: \"%s\"", "Paid", payment.State)
2022-04-26 22:17:45 +08:00
}
invoiceUrl, err := invoicePayment(payment)
if err != nil {
return "", err
2022-04-26 22:17:45 +08:00
}
payment.InvoiceUrl = invoiceUrl
affected := UpdatePayment(payment.GetId(), payment)
if !affected {
return "", fmt.Errorf("failed to update the payment: %s", payment.Name)
}
2022-04-26 22:17:45 +08:00
return invoiceUrl, nil
2022-04-26 22:17:45 +08:00
}
2022-02-05 01:18:13 +08:00
func (payment *Payment) GetId() string {
return fmt.Sprintf("%s/%s", payment.Owner, payment.Name)
}