From c906f1e5d2dae01a9badabd5ea9327a8584c1e93 Mon Sep 17 00:00:00 2001 From: Yang Luo Date: Sat, 12 Mar 2022 20:03:48 +0800 Subject: [PATCH] Add user and state to payment pages. --- controllers/product.go | 15 ++++++++++++++- object/payment.go | 3 ++- object/product.go | 31 +++++++++++++++++++++++++++++-- pp/alipay.go | 2 +- web/src/PaymentEditPage.js | 10 ++++++++++ web/src/PaymentListPage.js | 20 ++++++++++++++------ 6 files changed, 70 insertions(+), 11 deletions(-) diff --git a/controllers/product.go b/controllers/product.go index 27f6a04b..9bd0eb07 100644 --- a/controllers/product.go +++ b/controllers/product.go @@ -16,6 +16,7 @@ package controllers import ( "encoding/json" + "fmt" "github.com/astaxie/beego/utils/pagination" "github.com/casdoor/casdoor/object" @@ -127,7 +128,19 @@ func (c *ApiController) BuyProduct() { providerId := c.Input().Get("providerId") host := c.Ctx.Request.Host - payUrl, err := object.BuyProduct(id, providerId, host) + userId := c.GetSessionUsername() + if userId == "" { + c.ResponseError("Please login first") + return + } + + user := object.GetUser(userId) + if user == nil { + c.ResponseError(fmt.Sprintf("The user: %s doesn't exist", userId)) + return + } + + payUrl, err := object.BuyProduct(id, providerId, user, host) if err != nil { c.ResponseError(err.Error()) return diff --git a/object/payment.go b/object/payment.go index a1a3edce..784f721e 100644 --- a/object/payment.go +++ b/object/payment.go @@ -38,7 +38,8 @@ type Payment struct { Price float64 `json:"price"` Currency string `xorm:"varchar(100)" json:"currency"` - State string `xorm:"varchar(100)" json:"state"` + PayUrl string `xorm:"varchar(2000)" json:"payUrl"` + State string `xorm:"varchar(100)" json:"state"` } func GetPaymentCount(owner, field, value string) int { diff --git a/object/product.go b/object/product.go index ab095e07..8890f20e 100644 --- a/object/product.go +++ b/object/product.go @@ -152,7 +152,7 @@ func (product *Product) getProvider(providerId string) (*Provider, error) { return provider, nil } -func BuyProduct(id string, providerId string, host string) (string, error) { +func BuyProduct(id string, providerId string, user *User, host string) (string, error) { product := GetProduct(id) if product == nil { return "", fmt.Errorf("the product: %s does not exist", id) @@ -174,11 +174,38 @@ func BuyProduct(id string, providerId string, host string) (string, error) { } paymentId := util.GenerateTimeId() + productName := product.DisplayName + productId := product.Name originFrontend, originBackend := getOriginFromHost(host) returnUrl := fmt.Sprintf("%s/payments/%s", originFrontend, paymentId) notifyUrl := fmt.Sprintf("%s/api/notify-payment", originBackend) - payUrl, err := pProvider.Pay(product.DisplayName, product.Name, provider.Name, paymentId, product.Price, returnUrl, notifyUrl) + payUrl, err := pProvider.Pay(productName, productId, providerId, paymentId, product.Price, returnUrl, notifyUrl) + if err != nil { + return "", err + } + + payment := Payment{ + Owner: product.Owner, + Name: paymentId, + CreatedTime: util.GetCurrentTime(), + DisplayName: paymentId, + Provider: provider.Name, + Type: provider.Type, + Organization: user.Owner, + User: user.Name, + ProductId: productId, + ProductName: productName, + Price: product.Price, + Currency: product.Currency, + PayUrl: payUrl, + State: "Created", + } + affected := AddPayment(&payment) + if !affected { + return "", fmt.Errorf("failed to add payment: %s", util.StructToJson(payment)) + } + return payUrl, err } diff --git a/pp/alipay.go b/pp/alipay.go index f043aeb3..1e526bc7 100644 --- a/pp/alipay.go +++ b/pp/alipay.go @@ -45,7 +45,7 @@ func NewAlipayPaymentProvider(appId string, appPublicKey string, appPrivateKey s } func (pp *AlipayPaymentProvider) Pay(productName string, productId string, providerId string, paymentId string, price float64, returnUrl string, notifyUrl string) (string, error) { - pp.Client.DebugSwitch = gopay.DebugOn + //pp.Client.DebugSwitch = gopay.DebugOn priceString := strings.TrimRight(strings.TrimRight(fmt.Sprintf("%.2f", price), "0"), ".") diff --git a/web/src/PaymentEditPage.js b/web/src/PaymentEditPage.js index 219ddd03..cc60f0ac 100644 --- a/web/src/PaymentEditPage.js +++ b/web/src/PaymentEditPage.js @@ -150,6 +150,16 @@ class PaymentEditPage extends React.Component { }} /> + + + {Setting.getLabel(i18next.t("payment:State"), i18next.t("payment:State - Tooltip"))} : + + + { + // this.updatePaymentField('state', e.target.value); + }} /> + + ) } diff --git a/web/src/PaymentListPage.js b/web/src/PaymentListPage.js index 7b27eb39..fded5d9e 100644 --- a/web/src/PaymentListPage.js +++ b/web/src/PaymentListPage.js @@ -73,11 +73,11 @@ class PaymentListPage extends BaseListPage { const columns = [ { title: i18next.t("general:Organization"), - dataIndex: 'owner', - key: 'owner', + dataIndex: 'organization', + key: 'organization', width: '120px', sorter: true, - ...this.getColumnSearchProps('owner'), + ...this.getColumnSearchProps('organization'), render: (text, record, index) => { return ( @@ -105,7 +105,7 @@ class PaymentListPage extends BaseListPage { title: i18next.t("general:Name"), dataIndex: 'name', key: 'name', - width: '150px', + width: '180px', fixed: 'left', sorter: true, ...this.getColumnSearchProps('name'), @@ -155,7 +155,7 @@ class PaymentListPage extends BaseListPage { title: i18next.t("payment:Type"), dataIndex: 'type', key: 'type', - width: '110px', + width: '140px', align: 'center', filterMultiple: false, filters: Setting.getProviderTypeOptions('Payment').map((o) => {return {text:o.id, value:o.name}}), @@ -169,7 +169,7 @@ class PaymentListPage extends BaseListPage { title: i18next.t("payment:Product"), dataIndex: 'productName', key: 'productName', - width: '160px', + // width: '160px', sorter: true, ...this.getColumnSearchProps('productName'), }, @@ -189,6 +189,14 @@ class PaymentListPage extends BaseListPage { sorter: true, ...this.getColumnSearchProps('currency'), }, + { + title: i18next.t("payment:State"), + dataIndex: 'state', + key: 'state', + width: '120px', + sorter: true, + ...this.getColumnSearchProps('state'), + }, { title: i18next.t("general:Action"), dataIndex: '',