Add user and state to payment pages.

This commit is contained in:
Yang Luo
2022-03-12 20:03:48 +08:00
parent 37a26e2a91
commit c906f1e5d2
6 changed files with 70 additions and 11 deletions

View File

@ -16,6 +16,7 @@ package controllers
import ( import (
"encoding/json" "encoding/json"
"fmt"
"github.com/astaxie/beego/utils/pagination" "github.com/astaxie/beego/utils/pagination"
"github.com/casdoor/casdoor/object" "github.com/casdoor/casdoor/object"
@ -127,7 +128,19 @@ func (c *ApiController) BuyProduct() {
providerId := c.Input().Get("providerId") providerId := c.Input().Get("providerId")
host := c.Ctx.Request.Host 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 { if err != nil {
c.ResponseError(err.Error()) c.ResponseError(err.Error())
return return

View File

@ -38,7 +38,8 @@ type Payment struct {
Price float64 `json:"price"` Price float64 `json:"price"`
Currency string `xorm:"varchar(100)" json:"currency"` 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 { func GetPaymentCount(owner, field, value string) int {

View File

@ -152,7 +152,7 @@ func (product *Product) getProvider(providerId string) (*Provider, error) {
return provider, nil 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) product := GetProduct(id)
if product == nil { if product == nil {
return "", fmt.Errorf("the product: %s does not exist", id) 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() paymentId := util.GenerateTimeId()
productName := product.DisplayName
productId := product.Name
originFrontend, originBackend := getOriginFromHost(host) originFrontend, originBackend := getOriginFromHost(host)
returnUrl := fmt.Sprintf("%s/payments/%s", originFrontend, paymentId) returnUrl := fmt.Sprintf("%s/payments/%s", originFrontend, paymentId)
notifyUrl := fmt.Sprintf("%s/api/notify-payment", originBackend) 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 return payUrl, err
} }

View File

@ -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) { 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"), ".") priceString := strings.TrimRight(strings.TrimRight(fmt.Sprintf("%.2f", price), "0"), ".")

View File

@ -150,6 +150,16 @@ class PaymentEditPage extends React.Component {
}} /> }} />
</Col> </Col>
</Row> </Row>
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={(Setting.isMobile()) ? 22 : 2}>
{Setting.getLabel(i18next.t("payment:State"), i18next.t("payment:State - Tooltip"))} :
</Col>
<Col span={22} >
<Input value={this.state.payment.state} onChange={e => {
// this.updatePaymentField('state', e.target.value);
}} />
</Col>
</Row>
</Card> </Card>
) )
} }

View File

@ -73,11 +73,11 @@ class PaymentListPage extends BaseListPage {
const columns = [ const columns = [
{ {
title: i18next.t("general:Organization"), title: i18next.t("general:Organization"),
dataIndex: 'owner', dataIndex: 'organization',
key: 'owner', key: 'organization',
width: '120px', width: '120px',
sorter: true, sorter: true,
...this.getColumnSearchProps('owner'), ...this.getColumnSearchProps('organization'),
render: (text, record, index) => { render: (text, record, index) => {
return ( return (
<Link to={`/organizations/${text}`}> <Link to={`/organizations/${text}`}>
@ -105,7 +105,7 @@ class PaymentListPage extends BaseListPage {
title: i18next.t("general:Name"), title: i18next.t("general:Name"),
dataIndex: 'name', dataIndex: 'name',
key: 'name', key: 'name',
width: '150px', width: '180px',
fixed: 'left', fixed: 'left',
sorter: true, sorter: true,
...this.getColumnSearchProps('name'), ...this.getColumnSearchProps('name'),
@ -155,7 +155,7 @@ class PaymentListPage extends BaseListPage {
title: i18next.t("payment:Type"), title: i18next.t("payment:Type"),
dataIndex: 'type', dataIndex: 'type',
key: 'type', key: 'type',
width: '110px', width: '140px',
align: 'center', align: 'center',
filterMultiple: false, filterMultiple: false,
filters: Setting.getProviderTypeOptions('Payment').map((o) => {return {text:o.id, value:o.name}}), 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"), title: i18next.t("payment:Product"),
dataIndex: 'productName', dataIndex: 'productName',
key: 'productName', key: 'productName',
width: '160px', // width: '160px',
sorter: true, sorter: true,
...this.getColumnSearchProps('productName'), ...this.getColumnSearchProps('productName'),
}, },
@ -189,6 +189,14 @@ class PaymentListPage extends BaseListPage {
sorter: true, sorter: true,
...this.getColumnSearchProps('currency'), ...this.getColumnSearchProps('currency'),
}, },
{
title: i18next.t("payment:State"),
dataIndex: 'state',
key: 'state',
width: '120px',
sorter: true,
...this.getColumnSearchProps('state'),
},
{ {
title: i18next.t("general:Action"), title: i18next.t("general:Action"),
dataIndex: '', dataIndex: '',