Skip to content

Commit

Permalink
fix bug and optimized code
Browse files Browse the repository at this point in the history
Signed-off-by: xy <[email protected]>
  • Loading branch information
geniuxy authored and bxy4543 committed Oct 13, 2023
1 parent df72d74 commit b93cf4c
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 77 deletions.
1 change: 1 addition & 0 deletions service/pay/api/paysession.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ func GetSession(c *gin.Context, client *mongo.Client) {
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("paymethod is illegal: %v", request.PayMethod)})
}
// TODO At present, the Currency of wechat and stripe seems to be CNY, and then if there are other currencies, here needs to be changed
// TODO To prevent multiple orders from the same IP address, you need to add IP restrictions
}
14 changes: 3 additions & 11 deletions service/pay/handler/appdetail.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,7 @@ import (
"go.mongodb.org/mongo-driver/mongo"
)

type PMDetail struct {
PayMethod string `bson:"payMethod"`
Currency string `bson:"currency"`
AmountOptions []string `bson:"amountOptions"`
ExchangeRate float64 `bson:"exchangeRate"`
TaxRate float64 `bson:"taxRate"`
}

func GetAppDetails(request *helper.Request, client *mongo.Client) ([]PMDetail, error) {
func GetAppDetails(request *helper.Request, client *mongo.Client) ([]helper.PayMethodDetail, error) {
appID := request.AppID
filter := bson.D{{Key: "appID", Value: appID}}
appColl := helper.InitDBAndColl(client, helper.Database, helper.AppColl)
Expand All @@ -30,7 +22,7 @@ func GetAppDetails(request *helper.Request, client *mongo.Client) ([]PMDetail, e
if !ok {
return nil, fmt.Errorf("methods type assertion failed")
}
var payDetails []PMDetail
var payDetails []helper.PayMethodDetail
for _, method := range methods {
pmColl := helper.InitDBAndColl(client, helper.Database, helper.PayMethodColl)
filter := bson.D{{Key: "payMethod", Value: method}}
Expand All @@ -41,7 +33,7 @@ func GetAppDetails(request *helper.Request, client *mongo.Client) ([]PMDetail, e
return nil, fmt.Errorf("query error: %v", err)
}
// Retrieve documents for the current payment method
var methodPayDetails []PMDetail
var methodPayDetails []helper.PayMethodDetail
if err := cursor.All(context.TODO(), &methodPayDetails); err != nil {
return nil, fmt.Errorf("cursor error: %v", err)
}
Expand Down
13 changes: 2 additions & 11 deletions service/pay/handler/bill.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,7 @@ import (
"go.mongodb.org/mongo-driver/mongo"
)

type BillDetail struct {
OrderID string `bson:"orderID"`
Amount string `bson:"amount"`
Currency string `bson:"currency"`
PayTime string `bson:"payTime"`
PayMethod string `bson:"payMethod"`
Status string `bson:"status"`
}

func GetBillDetails(request *helper.Request, client *mongo.Client) ([]BillDetail, error) {
func GetBillDetails(request *helper.Request, client *mongo.Client) ([]helper.BillDetail, error) {
coll := helper.InitDBAndColl(client, helper.Database, helper.PaymentDetailsColl)
filter := bson.D{
{Key: "user", Value: request.User},
Expand All @@ -31,7 +22,7 @@ func GetBillDetails(request *helper.Request, client *mongo.Client) ([]BillDetail
defer cursor.Close(context.Background())

// init result array
var billDetails []BillDetail
var billDetails []helper.BillDetail

if err := cursor.All(context.Background(), &billDetails); err != nil {
return nil, err
Expand Down
9 changes: 1 addition & 8 deletions service/pay/handler/payapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,10 @@ import (
"go.mongodb.org/mongo-driver/mongo"
)

type App struct {
AppID int64 `bson:"appID"`
Sign string `bson:"sign"`
PayAppName string `bson:"payAppName"`
Methods []string `bson:"methods"`
}

func InsertApp(client *mongo.Client, appID int64, sign, appName string, methods []string) (*mongo.InsertManyResult, error) {
coll := helper.InitDBAndColl(client, helper.Database, helper.AppColl)
docs := []interface{}{
App{
helper.App{
AppID: appID,
Sign: sign,
PayAppName: appName,
Expand Down
2 changes: 1 addition & 1 deletion service/pay/handler/paymethod.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func InsertPayMethod(request *helper.Request, client *mongo.Client) (*mongo.Inse
coll := helper.InitDBAndColl(client, helper.Database, helper.PayMethodColl)

docs := []interface{}{
PMDetail{
helper.PayMethodDetail{
PayMethod: payMethod,
Currency: currency,
AmountOptions: amountOptions,
Expand Down
29 changes: 5 additions & 24 deletions service/pay/handler/paysession.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,6 @@ import (
"go.mongodb.org/mongo-driver/mongo/options"
)

type PaymentDetails struct {
OrderID string `bson:"orderID"`
User string `bson:"user"`
Amount string `bson:"amount"`
Currency string `bson:"currency"`
PayTime string `bson:"payTime"`
PayMethod string `bson:"payMethod"`
AppID int64 `bson:"appID"`
Status string `bson:"status"`
}

type OrderDetails struct {
OrderID string `bson:"orderID"`
Amount string `bson:"amount"`
PayTime string `bson:"paytime"`
PayMethod string `bson:"payMethod"`
AppID int64 `bson:"appID"`
DetailsData map[string]interface{} `bson:"detailsdata"`
}

// InsertDetails inserts data into both the payment_details and order_details tables and ensures transactionality
func InsertDetails(client *mongo.Client, user, payMethod, amount, currency string, appID int64, details map[string]interface{}) (string, error) {
// create transaction options
Expand All @@ -56,7 +36,7 @@ func InsertDetails(client *mongo.Client, user, payMethod, amount, currency strin
}

// the insertion of data to the orderDetailsColl in a transaction
if err := InsertOrderDetails(client, appID, orderID, amount, payMethod, currency, details); err != nil {
if err := InsertOrderDetails(client, appID, user, orderID, amount, payMethod, currency, details); err != nil {
fmt.Println("insert order details failed:", err)
return nil, fmt.Errorf("insert order details failed: %v", err)
}
Expand All @@ -82,7 +62,7 @@ func InsertPaymentDetails(client *mongo.Client, user, payMethod, amount, currenc
return "", fmt.Errorf("create order id failed: %w", err)
}
docs := []interface{}{
PaymentDetails{
helper.PaymentDetails{
OrderID: orderID,
User: user,
Amount: amount,
Expand All @@ -102,7 +82,7 @@ func InsertPaymentDetails(client *mongo.Client, user, payMethod, amount, currenc
return orderID, nil
}

func InsertOrderDetails(client *mongo.Client, appID int64, orderID, amount, payMethod, currency string, details map[string]interface{}) error {
func InsertOrderDetails(client *mongo.Client, appID int64, user, orderID, amount, payMethod, currency string, details map[string]interface{}) error {
coll := helper.InitDBAndColl(client, helper.Database, helper.OrderDetailsColl)
// switched to the Chinese time zone and optimized the format
payTime := time.Now().UTC().In(time.FixedZone("CST", 8*60*60)).Format("2006-01-02 15:04:05")
Expand All @@ -122,9 +102,10 @@ func InsertOrderDetails(client *mongo.Client, appID int64, orderID, amount, payM
newAmountStr := strconv.FormatFloat(newAmount, 'f', 2, 64)

docs := []interface{}{
OrderDetails{
helper.OrderDetails{
OrderID: orderID,
Amount: newAmountStr,
User: user,
PayTime: payTime,
PayMethod: payMethod,
AppID: appID,
Expand Down
4 changes: 3 additions & 1 deletion service/pay/handler/paystatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,16 @@ func CheckOrderExistOrNot(client *mongo.Client, request *helper.Request) error {
orderID := request.OrderID
payMethod := request.PayMethod
appID := request.AppID
user := request.User
coll := helper.InitDBAndColl(client, helper.Database, helper.OrderDetailsColl)
filter := bson.D{
{Key: "orderID", Value: orderID},
{Key: "payMethod", Value: payMethod},
{Key: "user", Value: user},
{Key: "appID", Value: appID},
}
// Execute the MongoDB query
var result OrderDetails
var result helper.OrderDetails
err := coll.FindOne(context.Background(), filter).Decode(&result)
if err != nil {
if err == mongo.ErrNoDocuments {
Expand Down
24 changes: 4 additions & 20 deletions service/pay/helper/common.go → service/pay/helper/constant.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
package helper

type Request struct {
AppID int64 `json:"appID"`
Sign string `json:"sign"`
PayMethod string `json:"payMethod"`
Amount string `json:"amount"`
User string `json:"user"`
PayAppName string `json:"payAppName,omitempty"`
Currency string `json:"currency,omitempty"`
AmountOptions []string `json:"amountOptions,omitempty"`
ExchangeRate float64 `json:"exchangeRate,omitempty"`
TaxRate float64 `json:"taxRate,omitempty"`
TradeNO string `json:"tradeNO,omitempty"`
SessionID string `json:"sessionID,omitempty"`
OrderID string `json:"orderID,omitempty"`
}

// DB
const (
DBURI = "dburi"
Expand Down Expand Up @@ -64,8 +48,8 @@ const (
GetBill = "/bill"
TestAppID = 66683568733697785
TestSign = "597d7f10a27219"
TestUser = "cx"
TestOrderID = "VS4gw76Ej-wIzGa1p2"
TestSessionID = "cs_test_a1GdmdhVBHivyUqtLdl9ILv1fXneuPIY5XdtlTpljHYInH1fPhGCN8KmWt"
TestTradeNO = "db27af04c65bd27bb3c3708addbafc01"
TestUser = "xy"
TestOrderID = "8QC6mu7vSNJckVhpKv"
TestSessionID = "cs_test_a1vbMHEx4iVfIWPoiJVVbBd7eecKDw8CDdJoLc7KRpahkMXYJ51EIlA1x5"
TestTradeNO = "049dfbf0b96ae9e2fa54a4b8eed6ea34"
)
17 changes: 17 additions & 0 deletions service/pay/helper/dto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package helper

type Request struct {
AppID int64 `json:"appID"`
Sign string `json:"sign"`
PayMethod string `json:"payMethod"`
Amount string `json:"amount"`
User string `json:"user"`
PayAppName string `json:"payAppName,omitempty"`
Currency string `json:"currency,omitempty"`
AmountOptions []string `json:"amountOptions,omitempty"`
ExchangeRate float64 `json:"exchangeRate,omitempty"`
TaxRate float64 `json:"taxRate,omitempty"`
TradeNO string `json:"tradeNO,omitempty"`
SessionID string `json:"sessionID,omitempty"`
OrderID string `json:"orderID,omitempty"`
}
46 changes: 46 additions & 0 deletions service/pay/helper/entity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package helper

type PaymentDetails struct {
OrderID string `bson:"orderID"`
User string `bson:"user"`
Amount string `bson:"amount"`
Currency string `bson:"currency"`
PayTime string `bson:"payTime"`
PayMethod string `bson:"payMethod"`
AppID int64 `bson:"appID"`
Status string `bson:"status"`
}

type OrderDetails struct {
OrderID string `bson:"orderID"`
User string `bson:"user"`
Amount string `bson:"amount"`
PayTime string `bson:"paytime"`
PayMethod string `bson:"payMethod"`
AppID int64 `bson:"appID"`
DetailsData map[string]interface{} `bson:"detailsdata"`
}

type App struct {
AppID int64 `bson:"appID"`
Sign string `bson:"sign"`
PayAppName string `bson:"payAppName"`
Methods []string `bson:"methods"`
}

type BillDetail struct {
OrderID string `bson:"orderID"`
Amount string `bson:"amount"`
Currency string `bson:"currency"`
PayTime string `bson:"payTime"`
PayMethod string `bson:"payMethod"`
Status string `bson:"status"`
}

type PayMethodDetail struct {
PayMethod string `bson:"payMethod"`
Currency string `bson:"currency"`
AmountOptions []string `bson:"amountOptions"`
ExchangeRate float64 `bson:"exchangeRate"`
TaxRate float64 `bson:"taxRate"`
}
19 changes: 18 additions & 1 deletion service/pay/pay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ func TestGetPayStatus(t *testing.T) {
"sign": helper.TestSign,
"orderID": helper.TestOrderID,
"payMethod": helper.Stripe,
"user": helper.TestUser,
"sessionID": helper.TestSessionID,
// "TradeNO": "db27af04c65bd27bb3c3708addbafc01",
//"TradeNO": helper.TestTradeNO,
}
createPayTest(t, data, http.MethodGet, helper.GetPayStatus)
}
Expand All @@ -105,6 +106,7 @@ func TestGetPayStatus_seesionIDLXD(t *testing.T) {
"sign": helper.TestSign,
"orderID": helper.TestOrderID,
"payMethod": helper.Stripe,
"user": helper.TestUser,
"sessionID": helper.TestSessionID + "abc",
// "TradeNO": "db27af04c65bd27bb3c3708addbafc01",
}
Expand All @@ -118,6 +120,7 @@ func TestGetPayStatus_orderIDLXD(t *testing.T) {
"sign": helper.TestSign,
"orderID": helper.TestOrderID + "abc",
"payMethod": helper.Stripe,
"user": helper.TestUser,
"sessionID": helper.TestSessionID,
// "TradeNO": "db27af04c65bd27bb3c3708addbafc01",
}
Expand All @@ -131,6 +134,20 @@ func TestGetPayStatus_payMethodLXD(t *testing.T) {
"sign": helper.TestSign,
"orderID": helper.TestOrderID,
"payMethod": helper.Wechat,
"user": helper.TestUser,
"sessionID": helper.TestSessionID,
// "TradeNO": "db27af04c65bd27bb3c3708addbafc01",
}
createPayTest(t, data, http.MethodGet, helper.GetPayStatus)
}

func TestGetPayStatus_userLXD(t *testing.T) {
data := map[string]interface{}{
"appID": helper.TestAppID,
"sign": helper.TestSign,
"orderID": helper.TestOrderID,
"payMethod": helper.Wechat,
"user": helper.TestUser + "123",
"sessionID": helper.TestSessionID,
// "TradeNO": "db27af04c65bd27bb3c3708addbafc01",
}
Expand Down

0 comments on commit b93cf4c

Please sign in to comment.