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 (
"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

View File

@ -38,6 +38,7 @@ type Payment struct {
Price float64 `json:"price"`
Currency string `xorm:"varchar(100)" json:"currency"`
PayUrl string `xorm:"varchar(2000)" json:"payUrl"`
State string `xorm:"varchar(100)" json:"state"`
}

View File

@ -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
}

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) {
pp.Client.DebugSwitch = gopay.DebugOn
//pp.Client.DebugSwitch = gopay.DebugOn
priceString := strings.TrimRight(strings.TrimRight(fmt.Sprintf("%.2f", price), "0"), ".")

View File

@ -150,6 +150,16 @@ class PaymentEditPage extends React.Component {
}} />
</Col>
</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>
)
}

View File

@ -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 (
<Link to={`/organizations/${text}`}>
@ -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: '',