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 controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
2022-09-29 19:44:08 +08:00
|
|
|
"github.com/beego/beego/utils/pagination"
|
2022-02-05 01:18:13 +08:00
|
|
|
"github.com/casdoor/casdoor/object"
|
|
|
|
"github.com/casdoor/casdoor/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetPayments
|
|
|
|
// @Title GetPayments
|
|
|
|
// @Tag Payment API
|
|
|
|
// @Description get payments
|
|
|
|
// @Param owner query string true "The owner of payments"
|
|
|
|
// @Success 200 {array} object.Payment The Response object
|
|
|
|
// @router /get-payments [get]
|
|
|
|
func (c *ApiController) GetPayments() {
|
|
|
|
owner := c.Input().Get("owner")
|
2023-06-12 00:31:49 +08:00
|
|
|
organization := c.Input().Get("organization")
|
2022-02-05 01:18:13 +08:00
|
|
|
limit := c.Input().Get("pageSize")
|
|
|
|
page := c.Input().Get("p")
|
|
|
|
field := c.Input().Get("field")
|
|
|
|
value := c.Input().Get("value")
|
|
|
|
sortField := c.Input().Get("sortField")
|
|
|
|
sortOrder := c.Input().Get("sortOrder")
|
2023-05-30 15:49:39 +08:00
|
|
|
|
2022-02-05 01:18:13 +08:00
|
|
|
if limit == "" || page == "" {
|
2023-05-30 15:49:39 +08:00
|
|
|
payments, err := object.GetPayments(owner)
|
|
|
|
if err != nil {
|
2023-06-27 20:33:47 +07:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2023-05-30 15:49:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Data["json"] = payments
|
2022-02-05 01:18:13 +08:00
|
|
|
c.ServeJSON()
|
|
|
|
} else {
|
|
|
|
limit := util.ParseInt(limit)
|
2023-06-12 00:31:49 +08:00
|
|
|
count, err := object.GetPaymentCount(owner, organization, field, value)
|
2023-05-30 15:49:39 +08:00
|
|
|
if err != nil {
|
2023-06-27 20:33:47 +07:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2023-05-30 15:49:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
paginator := pagination.SetPaginator(c.Ctx, limit, count)
|
2023-06-12 00:31:49 +08:00
|
|
|
payments, err := object.GetPaginationPayments(owner, organization, paginator.Offset(), limit, field, value, sortField, sortOrder)
|
2023-05-30 15:49:39 +08:00
|
|
|
if err != nil {
|
2023-06-27 20:33:47 +07:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2023-05-30 15:49:39 +08:00
|
|
|
}
|
|
|
|
|
2022-02-05 01:18:13 +08:00
|
|
|
c.ResponseOk(payments, paginator.Nums())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-13 11:51:33 +08:00
|
|
|
// GetUserPayments
|
|
|
|
// @Title GetUserPayments
|
|
|
|
// @Tag Payment API
|
|
|
|
// @Description get payments for a user
|
|
|
|
// @Param owner query string true "The owner of payments"
|
|
|
|
// @Param organization query string true "The organization of the user"
|
|
|
|
// @Param user query string true "The username of the user"
|
|
|
|
// @Success 200 {array} object.Payment The Response object
|
|
|
|
// @router /get-user-payments [get]
|
|
|
|
func (c *ApiController) GetUserPayments() {
|
|
|
|
owner := c.Input().Get("owner")
|
|
|
|
organization := c.Input().Get("organization")
|
|
|
|
user := c.Input().Get("user")
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
payments, err := object.GetUserPayments(owner, organization, user)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-13 11:51:33 +08:00
|
|
|
c.ResponseOk(payments)
|
|
|
|
}
|
|
|
|
|
2022-08-09 16:50:49 +08:00
|
|
|
// GetPayment
|
2022-02-05 01:18:13 +08:00
|
|
|
// @Title GetPayment
|
|
|
|
// @Tag Payment API
|
|
|
|
// @Description get payment
|
2023-02-10 10:42:16 +08:00
|
|
|
// @Param id query string true "The id ( owner/name ) of the payment"
|
2022-02-05 01:18:13 +08:00
|
|
|
// @Success 200 {object} object.Payment The Response object
|
|
|
|
// @router /get-payment [get]
|
|
|
|
func (c *ApiController) GetPayment() {
|
|
|
|
id := c.Input().Get("id")
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
payment, err := object.GetPayment(id)
|
|
|
|
if err != nil {
|
2023-06-27 20:33:47 +07:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2023-05-30 15:49:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Data["json"] = payment
|
2022-02-05 01:18:13 +08:00
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
2022-08-09 16:50:49 +08:00
|
|
|
// UpdatePayment
|
2022-02-05 01:18:13 +08:00
|
|
|
// @Title UpdatePayment
|
|
|
|
// @Tag Payment API
|
|
|
|
// @Description update payment
|
2023-02-10 10:42:16 +08:00
|
|
|
// @Param id query string true "The id ( owner/name ) of the payment"
|
2022-02-05 01:18:13 +08:00
|
|
|
// @Param body body object.Payment true "The details of the payment"
|
|
|
|
// @Success 200 {object} controllers.Response The Response object
|
|
|
|
// @router /update-payment [post]
|
|
|
|
func (c *ApiController) UpdatePayment() {
|
|
|
|
id := c.Input().Get("id")
|
|
|
|
|
|
|
|
var payment object.Payment
|
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &payment)
|
|
|
|
if err != nil {
|
2022-08-20 21:09:32 +08:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2022-02-05 01:18:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Data["json"] = wrapActionResponse(object.UpdatePayment(id, &payment))
|
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
2022-08-09 16:50:49 +08:00
|
|
|
// AddPayment
|
2022-02-05 01:18:13 +08:00
|
|
|
// @Title AddPayment
|
|
|
|
// @Tag Payment API
|
|
|
|
// @Description add payment
|
|
|
|
// @Param body body object.Payment true "The details of the payment"
|
|
|
|
// @Success 200 {object} controllers.Response The Response object
|
|
|
|
// @router /add-payment [post]
|
|
|
|
func (c *ApiController) AddPayment() {
|
|
|
|
var payment object.Payment
|
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &payment)
|
|
|
|
if err != nil {
|
2022-08-20 21:09:32 +08:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2022-02-05 01:18:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Data["json"] = wrapActionResponse(object.AddPayment(&payment))
|
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
2022-08-09 16:50:49 +08:00
|
|
|
// DeletePayment
|
2022-02-05 01:18:13 +08:00
|
|
|
// @Title DeletePayment
|
|
|
|
// @Tag Payment API
|
|
|
|
// @Description delete payment
|
|
|
|
// @Param body body object.Payment true "The details of the payment"
|
|
|
|
// @Success 200 {object} controllers.Response The Response object
|
|
|
|
// @router /delete-payment [post]
|
|
|
|
func (c *ApiController) DeletePayment() {
|
|
|
|
var payment object.Payment
|
|
|
|
err := json.Unmarshal(c.Ctx.Input.RequestBody, &payment)
|
|
|
|
if err != nil {
|
2022-08-20 21:09:32 +08:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2022-02-05 01:18:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Data["json"] = wrapActionResponse(object.DeletePayment(&payment))
|
|
|
|
c.ServeJSON()
|
|
|
|
}
|
2022-03-06 22:46:02 +08:00
|
|
|
|
2022-08-09 16:50:49 +08:00
|
|
|
// NotifyPayment
|
2022-03-06 22:46:02 +08:00
|
|
|
// @Title NotifyPayment
|
|
|
|
// @Tag Payment API
|
|
|
|
// @Description notify payment
|
|
|
|
// @Param body body object.Payment true "The details of the payment"
|
|
|
|
// @Success 200 {object} controllers.Response The Response object
|
|
|
|
// @router /notify-payment [post]
|
|
|
|
func (c *ApiController) NotifyPayment() {
|
2022-03-14 02:07:55 +08:00
|
|
|
owner := c.Ctx.Input.Param(":owner")
|
|
|
|
providerName := c.Ctx.Input.Param(":provider")
|
|
|
|
productName := c.Ctx.Input.Param(":product")
|
|
|
|
paymentName := c.Ctx.Input.Param(":payment")
|
2023-06-12 13:43:37 +08:00
|
|
|
orderId := c.Ctx.Input.Param("order")
|
2022-03-14 02:07:55 +08:00
|
|
|
|
|
|
|
body := c.Ctx.Input.RequestBody
|
2022-03-06 22:46:02 +08:00
|
|
|
|
2023-06-12 13:43:37 +08:00
|
|
|
err, errorResponse := object.NotifyPayment(c.Ctx.Request, body, owner, providerName, productName, paymentName, orderId)
|
2023-05-26 21:26:41 +08:00
|
|
|
|
|
|
|
_, err2 := c.Ctx.ResponseWriter.Write([]byte(errorResponse))
|
|
|
|
if err2 != nil {
|
|
|
|
panic(err2)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2023-06-27 20:33:47 +07:00
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
2022-03-07 00:33:45 +08:00
|
|
|
}
|
2022-03-06 22:46:02 +08:00
|
|
|
}
|
2022-04-26 22:17:45 +08:00
|
|
|
|
2022-08-09 16:50:49 +08:00
|
|
|
// InvoicePayment
|
2022-04-26 22:17:45 +08:00
|
|
|
// @Title InvoicePayment
|
|
|
|
// @Tag Payment API
|
|
|
|
// @Description invoice payment
|
2023-02-10 10:42:16 +08:00
|
|
|
// @Param id query string true "The id ( owner/name ) of the payment"
|
2022-04-26 22:17:45 +08:00
|
|
|
// @Success 200 {object} controllers.Response The Response object
|
|
|
|
// @router /invoice-payment [post]
|
|
|
|
func (c *ApiController) InvoicePayment() {
|
|
|
|
id := c.Input().Get("id")
|
|
|
|
|
2023-05-30 15:49:39 +08:00
|
|
|
payment, err := object.GetPayment(id)
|
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-28 15:07:57 +08:00
|
|
|
invoiceUrl, err := object.InvoicePayment(payment)
|
2022-04-27 00:24:48 +08:00
|
|
|
if err != nil {
|
|
|
|
c.ResponseError(err.Error())
|
|
|
|
}
|
2022-04-28 15:07:57 +08:00
|
|
|
c.ResponseOk(invoiceUrl)
|
2022-04-26 22:17:45 +08:00
|
|
|
}
|