From 3acf3a19fed9c9b9ff4ab9d427cb3e8b2ef34dca Mon Sep 17 00:00:00 2001 From: AH-dark Date: Sun, 10 Sep 2023 12:26:58 +0800 Subject: [PATCH 1/4] refactor(actions/migrate): use json file instead of variable config --- .goreleaser.yml | 8 ++ actions/migrate.go | 148 +++++++++++++++++++------------- data/default_config.json | 57 ++++++++++++ data/default_payment_types.json | 30 +++++++ pkg/utils/path.go | 20 +++++ 5 files changed, 202 insertions(+), 61 deletions(-) create mode 100644 data/default_config.json create mode 100644 data/default_payment_types.json create mode 100644 pkg/utils/path.go diff --git a/.goreleaser.yml b/.goreleaser.yml index 23e0540..d74e287 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -49,6 +49,14 @@ archives: format_overrides: - goos: windows format: zip + files: + - src: data/* + dst: data + info: + owner: root + group: root + mtime: "{{ .CommitDate }}" + mode: 0644 release: github: diff --git a/actions/migrate.go b/actions/migrate.go index 999b920..46a1786 100644 --- a/actions/migrate.go +++ b/actions/migrate.go @@ -1,8 +1,10 @@ package actions import ( + "encoding/json" "errors" "fmt" + "os" "strconv" "time" @@ -87,73 +89,84 @@ func MigrateCommand() *cli.Command { EnvVars: []string{"APP_CRONKEY", "APP_CRON_KEY"}, Value: strconv.Itoa(utils.RandInt(100000, 999999)), }, + &cli.StringFlag{ + Name: "migrate.default_config", + Usage: "migrate default config", + EnvVars: []string{"MIGRATE_DEFAULT_CONFIG", "MIGRATION_DEFAULT_CONFIG"}, + Value: "./data/default_config.json", + }, + &cli.StringFlag{ + Name: "migrate.default_payment_types", + Usage: "migrate default payment types", + EnvVars: []string{"MIGRATE_DEFAULT_PAYMENT_TYPES", "MIGRATION_DEFAULT_PAYMENT_TYPES"}, + Value: "./data/default_payment_types.json", + }, }, } } -var defaultDatabaseConfig = map[string]string{ - "version": "2024", - "admin_user": "admin", - "admin_pwd": "123456", - "admin_paypwd": "123456", - "homepage": "0", - "sitename": "聚合易支付", - "title": "聚合易支付 - 行业领先的免签约支付平台", - "keywords": "聚合易支付,支付宝免签约即时到账,财付通免签约,微信免签约支付,QQ钱包免签约,免签约支付", - "description": "聚合易支付是XX公司旗下的免签约支付产品,完美解决支付难题,一站式接入支付宝,微信,财付通,QQ钱包,微信wap,帮助开发者快速集成到自己相应产品,效率高,见效快,费率低!", - "orgname": "XX公司", - "kfqq": "123456789", - "template": "index1", - "pre_maxmoney": "1000", - "blockname": "云盘|网盘|Q币", - "blockalert": "温馨提醒该商品禁止出售,如有疑问请联系网站客服!", - "settle_open": "1", - "settle_type": "1", - "settle_money": "30", - "settle_rate": "0.5", - "settle_fee_min": "0.1", - "settle_fee_max": "20", - "settle_alipay": "1", - "settle_wxpay": "1", - "settle_qqpay": "1", - "settle_bank": "0", - "transfer_alipay": "0", - "transfer_wxpay": "0", - "transfer_qqpay": "0", - "transfer_name": "聚合易支付", - "transfer_desc": "聚合易支付自动结算", - "login_qq": "0", - "login_alipay": "0", - "login_alipay_channel": "0", - "login_wx": "0", - "login_wx_channel": "0", - "reg_open": "1", - "reg_pay": "1", - "reg_pre_uid": "1000", - "reg_pre_price": "5", - "verifytype": "1", - "test_open": "1", - "test_pre_uid": "1000", - "mail_cloud": "0", - "mail_smtp": "smtp.qq.com", - "mail_port": "465", - "mail_name": "", - "mail_pwd": "", - "sms_api": "0", - "captcha_open": "1", - "captcha_id": "", - "captcha_key": "", - "onecode": "1", - "recharge": "1", - "pageordername": "1", - "notifyordername": "1", +func getDefaultDatabaseConfig(c *cli.Context) (map[string]string, error) { + var data map[string]string + + logger := log.Log(c.Context) + + path := utils.GetAbsolutePath(c.String("migrate.default_config")) + if _, err := os.Stat(path); os.IsNotExist(err) { + logger.WithError(err).Error("default config file not exists") + return data, nil + } + + logger = logger.WithField("path", path) + + f, err := os.OpenFile(path, os.O_RDONLY, 0644) + if err != nil { + logger.WithError(err).Error("open default config file failed") + return data, err + } + defer func(f *os.File) { + if err := f.Close(); err != nil { + logger.WithError(err).Warn("close default config file failed") + } + }(f) + + if err := json.NewDecoder(f).Decode(&data); err != nil { + logger.WithError(err).Error("decode default config file failed") + return data, err + } + + return data, nil } -var defaultPaymentTypes = []model.Type{ - {ID: 1, Name: "alipay", Device: 0, ShowName: "支付宝", Status: 1}, - {ID: 2, Name: "wxpay", Device: 0, ShowName: "微信支付", Status: 1}, - {ID: 3, Name: "qqpay", Device: 0, ShowName: "QQ钱包", Status: 1}, - {ID: 4, Name: "bank", Device: 0, ShowName: "网银支付", Status: 0}, +func getDefaultPaymentTypes(c *cli.Context) ([]model.Type, error) { + var data []model.Type + + logger := log.Log(c.Context) + + path := utils.GetAbsolutePath(c.String("migrate.default_payment_types")) + if _, err := os.Stat(path); os.IsNotExist(err) { + logger.WithError(err).Error("default payment types file not exists") + return data, nil + } + + logger = logger.WithField("path", path) + + f, err := os.OpenFile(path, os.O_RDONLY, 0644) + if err != nil { + logger.WithError(err).Error("open default payment types file failed") + return data, err + } + defer func(f *os.File) { + if err := f.Close(); err != nil { + logger.WithError(err).Warn("close default payment types file failed") + } + }(f) + + if err := json.NewDecoder(f).Decode(&data); err != nil { + logger.WithError(err).Error("decode default payment types file failed") + return data, err + } + + return data, nil } var defaultGroup = model.Group{ @@ -239,6 +252,13 @@ func MigrateAction(c *cli.Context) error { } } + log.Log(ctx).Info("get default config") + defaultDatabaseConfig, err := getDefaultDatabaseConfig(c) + if err != nil { + log.Log(ctx).WithError(err).Error("get default config failed") + return err + } + log.Log(ctx).Info("create default config") if err := createDatabaseConfig(c, defaultDatabaseConfig); err != nil { log.Log(ctx).WithError(err).Panic("create default config failed") @@ -257,6 +277,12 @@ func MigrateAction(c *cli.Context) error { } log.Log(ctx).Info("create default payment types") + defaultPaymentTypes, err := getDefaultPaymentTypes(c) + if err != nil { + log.Log(ctx).WithError(err).Error("get default payment types failed") + return err + } + for _, t := range defaultPaymentTypes { if err := db.Model(&model.Type{}). Where("name = ?", t.Name). diff --git a/data/default_config.json b/data/default_config.json new file mode 100644 index 0000000..828e722 --- /dev/null +++ b/data/default_config.json @@ -0,0 +1,57 @@ +{ + "admin_paypwd": "123456", + "admin_pwd": "123456", + "admin_user": "admin", + "blockalert": "温馨提醒该商品禁止出售,如有疑问请联系网站客服!", + "blockname": "云盘|网盘|Q币", + "captcha_id": "", + "captcha_key": "", + "captcha_open": "1", + "description": "聚合易支付是XX公司旗下的免签约支付产品,完美解决支付难题,一站式接入支付宝,微信,财付通,QQ钱包,微信wap,帮助开发者快速集成到自己相应产品,效率高,见效快,费率低!", + "homepage": "0", + "keywords": "聚合易支付,支付宝免签约即时到账,财付通免签约,微信免签约支付,QQ钱包免签约,免签约支付", + "kfqq": "123456789", + "login_alipay": "0", + "login_alipay_channel": "0", + "login_qq": "0", + "login_wx": "0", + "login_wx_channel": "0", + "mail_cloud": "0", + "mail_name": "", + "mail_port": "465", + "mail_pwd": "", + "mail_smtp": "smtp.qq.com", + "notifyordername": "1", + "onecode": "1", + "orgname": "XX公司", + "pageordername": "1", + "pre_maxmoney": "1000", + "recharge": "1", + "reg_open": "1", + "reg_pay": "1", + "reg_pre_price": "5", + "reg_pre_uid": "1000", + "settle_alipay": "1", + "settle_bank": "0", + "settle_fee_max": "20", + "settle_fee_min": "0.1", + "settle_money": "30", + "settle_open": "1", + "settle_qqpay": "1", + "settle_rate": "0.5", + "settle_type": "1", + "settle_wxpay": "1", + "sitename": "聚合易支付", + "sms_api": "0", + "template": "index1", + "test_open": "1", + "test_pre_uid": "1000", + "title": "聚合易支付 - 行业领先的免签约支付平台", + "transfer_alipay": "0", + "transfer_desc": "聚合易支付自动结算", + "transfer_name": "聚合易支付", + "transfer_qqpay": "0", + "transfer_wxpay": "0", + "verifytype": "1", + "version": "2024" +} diff --git a/data/default_payment_types.json b/data/default_payment_types.json new file mode 100644 index 0000000..acb0e5a --- /dev/null +++ b/data/default_payment_types.json @@ -0,0 +1,30 @@ +[ + { + "id": 1, + "name": "alipay", + "device": 0, + "showname": "支付宝", + "status": 1 + }, + { + "id": 2, + "name": "wxpay", + "device": 0, + "showname": "微信支付", + "status": 1 + }, + { + "id": 3, + "name": "qqpay", + "device": 0, + "showname": "QQ钱包", + "status": 1 + }, + { + "id": 4, + "name": "bank", + "device": 0, + "showname": "网银支付", + "status": 0 + } +] diff --git a/pkg/utils/path.go b/pkg/utils/path.go new file mode 100644 index 0000000..7187309 --- /dev/null +++ b/pkg/utils/path.go @@ -0,0 +1,20 @@ +package utils + +import ( + "os" + "path/filepath" +) + +func GetAbsolutePath(path string) string { + if filepath.IsAbs(path) { + return path + } + + exe, err := os.Executable() + if err != nil { + panic(err) + } + + dir := filepath.Dir(exe) + return filepath.Join(dir, path) +} From d873f760daeb61eb73ca0a503a4ba62fefcfc739 Mon Sep 17 00:00:00 2001 From: AH-dark Date: Sun, 10 Sep 2023 12:30:32 +0800 Subject: [PATCH 2/4] ci(release): no need to upload assets --- .github/workflows/release.yml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 9c3456e..8ffa437 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -52,9 +52,3 @@ jobs: GPG_FINGERPRINT: ${{ steps.import_gpg.outputs.fingerprint }} DOCKER_REGISTRY: ghcr.io/ah-dark DOCKER_IMAGE_NAME: epay-cli - - - name: Upload assets - uses: actions/upload-artifact@v3 - with: - name: migrator - path: dist/* From 6ddfb884264390320ff3a582b103a300b1f3f679 Mon Sep 17 00:00:00 2001 From: AH-dark Date: Sun, 10 Sep 2023 12:45:32 +0800 Subject: [PATCH 3/4] refactor(actions): use factory mode instead of functions --- actions/factory/action.go | 8 + .../{submit.go => implements/mapi/command.go} | 73 +++------ actions/implements/mapi/service.go | 72 +++++++++ actions/implements/migrate/command.go | 97 ++++++++++++ .../migrate/service.go} | 122 +++------------ actions/implements/submit/command.go | 25 +++ actions/implements/submit/service.go | 57 +++++++ actions/mapi.go | 146 ------------------ main.go | 10 +- 9 files changed, 312 insertions(+), 298 deletions(-) create mode 100644 actions/factory/action.go rename actions/{submit.go => implements/mapi/command.go} (60%) create mode 100644 actions/implements/mapi/service.go create mode 100644 actions/implements/migrate/command.go rename actions/{migrate.go => implements/migrate/service.go} (62%) create mode 100644 actions/implements/submit/command.go create mode 100644 actions/implements/submit/service.go delete mode 100644 actions/mapi.go diff --git a/actions/factory/action.go b/actions/factory/action.go new file mode 100644 index 0000000..2a70f81 --- /dev/null +++ b/actions/factory/action.go @@ -0,0 +1,8 @@ +package factory + +import "github.com/urfave/cli/v2" + +type ActionService interface { + Command() *cli.Command + Do(c *cli.Context) error +} diff --git a/actions/submit.go b/actions/implements/mapi/command.go similarity index 60% rename from actions/submit.go rename to actions/implements/mapi/command.go index fec4493..807b909 100644 --- a/actions/submit.go +++ b/actions/implements/mapi/command.go @@ -1,19 +1,15 @@ -package actions +package mapi import ( - "fmt" - "net/url" - - "github.com/AH-dark/epay-cli/pkg/epay" "github.com/AH-dark/epay-cli/pkg/utils" "github.com/urfave/cli/v2" ) -func SubmitCommand() *cli.Command { +func (svc *service) Command() *cli.Command { return &cli.Command{ - Name: "submit", - Usage: "get submit.php url and args", - Action: SubmitAction, + Name: "mapi", + Usage: "mapi submit", + Action: svc.Do, Flags: []cli.Flag{ &cli.IntFlag{ Name: "pid", @@ -73,45 +69,26 @@ func SubmitCommand() *cli.Command { Required: false, EnvVars: []string{"EPAY_RETURN_URL"}, }, + &cli.StringFlag{ + Name: "client-ip", + Usage: "client ip", + Value: "127.0.0.1", + Required: false, + EnvVars: []string{"EPAY_CLIENT_IP"}, + }, + &cli.StringFlag{ + Name: "device", + Usage: "device", + Value: "pc", + Required: false, + EnvVars: []string{"EPAY_DEVICE"}, + }, + &cli.StringFlag{ + Name: "param", + Usage: "param", + Required: false, + EnvVars: []string{"EPAY_PARAM"}, + }, }, } } - -func SubmitAction(c *cli.Context) error { - client, err := epay.NewClient(&epay.Config{ - PartnerID: c.Int("pid"), - AppSecret: c.String("secret"), - Endpoint: c.String("endpoint"), - }) - if err != nil { - return err - } - - u, args, err := client.Submit(c.Context, &epay.SubmitArgs{ - Type: epay.PaymentType(c.String("type")), - ServiceTradeNo: c.String("trade-no"), - Name: c.String("name"), - Money: c.String("money"), - NotifyUrl: c.String("notify-url"), - ReturnUrl: c.String("return-url"), - }) - if err != nil { - return err - } - - values := url.Values{} - for k, v := range args { - values.Add(k, v) - } - - urlObj, err := url.Parse(u) - if err != nil { - return err - } - - urlObj.RawQuery = values.Encode() - - fmt.Println(urlObj.String()) - - return nil -} diff --git a/actions/implements/mapi/service.go b/actions/implements/mapi/service.go new file mode 100644 index 0000000..ae3e626 --- /dev/null +++ b/actions/implements/mapi/service.go @@ -0,0 +1,72 @@ +package mapi + +import ( + "fmt" + "strconv" + + "github.com/urfave/cli/v2" + + "github.com/AH-dark/epay-cli/actions/factory" + "github.com/AH-dark/epay-cli/pkg/epay" + "github.com/AH-dark/epay-cli/pkg/utils" +) + +type service struct { +} + +func NewService() factory.ActionService { + return &service{} +} + +func (svc *service) getSign(c *cli.Context) string { + sign := utils.CalculateEPaySign(map[string]string{ + "pid": strconv.Itoa(c.Int("pid")), + "type": c.String("type"), + "out_trade_no": c.String("trade-no"), + "notify_url": c.String("notify-url"), + "return_url": c.String("return-url"), + "name": c.String("name"), + "money": c.String("money"), + "clientip": c.String("client-ip"), + "device": c.String("device"), + "param": c.String("param"), + }, c.String("secret")) + + return sign +} + +func (svc *service) Do(c *cli.Context) error { + client, err := epay.NewClient(&epay.Config{ + PartnerID: c.Int("pid"), + AppSecret: c.String("secret"), + Endpoint: c.String("endpoint"), + }) + if err != nil { + return err + } + + sign := svc.getSign(c) + fmt.Println("Sign:", sign) + + url, args, err := client.MApiSubmit(c.Context, &epay.MApiSubmitArgs{ + Type: epay.PaymentType(c.String("type")), + OutTradeNo: c.String("trade-no"), + Name: c.String("name"), + Money: c.String("money"), + NotifyUrl: c.String("notify-url"), + ReturnUrl: utils.EmptyPtr(c.String("return-url")), + ClientIP: c.String("client-ip"), + Device: utils.EmptyPtr(epay.DeviceType(c.String("device"))), + Param: utils.EmptyPtr(c.String("param")), + Sign: sign, + SignType: "MD5", + }) + if err != nil { + return err + } + + fmt.Println("URL:", url) + fmt.Println("Args:", args) + + return nil +} diff --git a/actions/implements/migrate/command.go b/actions/implements/migrate/command.go new file mode 100644 index 0000000..f1e92de --- /dev/null +++ b/actions/implements/migrate/command.go @@ -0,0 +1,97 @@ +package migrate + +import ( + "strconv" + + "github.com/urfave/cli/v2" + + "github.com/AH-dark/epay-cli/pkg/utils" +) + +func (svc *service) Command() *cli.Command { + return &cli.Command{ + Name: "migrate", + Usage: "generate sql schema", + Action: svc.Do, + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "database.driver", + Usage: "database driver", + EnvVars: []string{"DATABASE_DRIVER", "DB_DRIVER"}, + Value: "mysql", + }, + &cli.StringFlag{ + Name: "database.host", + Usage: "database host", + EnvVars: []string{"DATABASE_HOST", "DB_HOST"}, + Value: "localhost", + }, + &cli.IntFlag{ + Name: "database.port", + Usage: "database port", + EnvVars: []string{"DATABASE_PORT", "DB_PORT"}, + Value: 3306, + }, + &cli.StringFlag{ + Name: "database.name", + Usage: "database name", + EnvVars: []string{"DATABASE_NAME", "DB_NAME"}, + Value: "epay", + }, + &cli.StringFlag{ + Name: "database.user", + Usage: "database user", + EnvVars: []string{"DATABASE_USER", "DB_USER"}, + Value: "root", + }, + &cli.StringFlag{ + Name: "database.password", + Usage: "database password", + EnvVars: []string{"DATABASE_PASSWORD", "DB_PASSWORD"}, + Value: "", + }, + &cli.StringFlag{ + Name: "database.prefix", + Usage: "database prefix", + EnvVars: []string{"DATABASE_PREFIX", "DB_PREFIX"}, + Value: "pay_", + }, + &cli.StringFlag{ + Name: "database.sslmode", + Usage: "database sslmode", + EnvVars: []string{"DATABASE_SSL_MODE", "DB_SSL_MODE"}, + Value: "disable", + }, + &cli.StringFlag{ + Name: "database.charset", + Usage: "database charset", + EnvVars: []string{"DATABASE_CHARSET", "DB_CHARSET"}, + Value: "utf8mb4", + }, + &cli.StringFlag{ + Name: "app.syskey", + Usage: "app syskey", + EnvVars: []string{"APP_SYSKEY", "APP_SYS_KEY"}, + Value: utils.RandString(32), + }, + &cli.StringFlag{ + Name: "app.cronkey", + Usage: "app cronkey", + EnvVars: []string{"APP_CRONKEY", "APP_CRON_KEY"}, + Value: strconv.Itoa(utils.RandInt(100000, 999999)), + }, + &cli.StringFlag{ + Name: "migrate.default_config", + Usage: "migrate default config", + EnvVars: []string{"MIGRATE_DEFAULT_CONFIG", "MIGRATION_DEFAULT_CONFIG"}, + Value: "./data/default_config.json", + }, + &cli.StringFlag{ + Name: "migrate.default_payment_types", + Usage: "migrate default payment types", + EnvVars: []string{"MIGRATE_DEFAULT_PAYMENT_TYPES", "MIGRATION_DEFAULT_PAYMENT_TYPES"}, + Value: "./data/default_payment_types.json", + }, + }, + } +} diff --git a/actions/migrate.go b/actions/implements/migrate/service.go similarity index 62% rename from actions/migrate.go rename to actions/implements/migrate/service.go index 46a1786..5b7b09e 100644 --- a/actions/migrate.go +++ b/actions/implements/migrate/service.go @@ -1,111 +1,30 @@ -package actions +package migrate import ( "encoding/json" "errors" "fmt" "os" - "strconv" "time" "github.com/urfave/cli/v2" "gorm.io/gorm" + "github.com/AH-dark/epay-cli/actions/factory" "github.com/AH-dark/epay-cli/database/conn" "github.com/AH-dark/epay-cli/database/model" "github.com/AH-dark/epay-cli/pkg/log" "github.com/AH-dark/epay-cli/pkg/utils" ) -func MigrateCommand() *cli.Command { - return &cli.Command{ - Name: "migrate", - Usage: "generate sql schema", - Action: MigrateAction, - Flags: []cli.Flag{ - &cli.StringFlag{ - Name: "database.driver", - Usage: "database driver", - EnvVars: []string{"DATABASE_DRIVER", "DB_DRIVER"}, - Value: "mysql", - }, - &cli.StringFlag{ - Name: "database.host", - Usage: "database host", - EnvVars: []string{"DATABASE_HOST", "DB_HOST"}, - Value: "localhost", - }, - &cli.IntFlag{ - Name: "database.port", - Usage: "database port", - EnvVars: []string{"DATABASE_PORT", "DB_PORT"}, - Value: 3306, - }, - &cli.StringFlag{ - Name: "database.name", - Usage: "database name", - EnvVars: []string{"DATABASE_NAME", "DB_NAME"}, - Value: "epay", - }, - &cli.StringFlag{ - Name: "database.user", - Usage: "database user", - EnvVars: []string{"DATABASE_USER", "DB_USER"}, - Value: "root", - }, - &cli.StringFlag{ - Name: "database.password", - Usage: "database password", - EnvVars: []string{"DATABASE_PASSWORD", "DB_PASSWORD"}, - Value: "", - }, - &cli.StringFlag{ - Name: "database.prefix", - Usage: "database prefix", - EnvVars: []string{"DATABASE_PREFIX", "DB_PREFIX"}, - Value: "pay_", - }, - &cli.StringFlag{ - Name: "database.sslmode", - Usage: "database sslmode", - EnvVars: []string{"DATABASE_SSL_MODE", "DB_SSL_MODE"}, - Value: "disable", - }, - &cli.StringFlag{ - Name: "database.charset", - Usage: "database charset", - EnvVars: []string{"DATABASE_CHARSET", "DB_CHARSET"}, - Value: "utf8mb4", - }, - &cli.StringFlag{ - Name: "app.syskey", - Usage: "app syskey", - EnvVars: []string{"APP_SYSKEY", "APP_SYS_KEY"}, - Value: utils.RandString(32), - }, - &cli.StringFlag{ - Name: "app.cronkey", - Usage: "app cronkey", - EnvVars: []string{"APP_CRONKEY", "APP_CRON_KEY"}, - Value: strconv.Itoa(utils.RandInt(100000, 999999)), - }, - &cli.StringFlag{ - Name: "migrate.default_config", - Usage: "migrate default config", - EnvVars: []string{"MIGRATE_DEFAULT_CONFIG", "MIGRATION_DEFAULT_CONFIG"}, - Value: "./data/default_config.json", - }, - &cli.StringFlag{ - Name: "migrate.default_payment_types", - Usage: "migrate default payment types", - EnvVars: []string{"MIGRATE_DEFAULT_PAYMENT_TYPES", "MIGRATION_DEFAULT_PAYMENT_TYPES"}, - Value: "./data/default_payment_types.json", - }, - }, - } +type service struct { +} + +func NewService() factory.ActionService { + return &service{} } -func getDefaultDatabaseConfig(c *cli.Context) (map[string]string, error) { +func (*service) getDefaultDatabaseConfig(c *cli.Context) (map[string]string, error) { var data map[string]string logger := log.Log(c.Context) @@ -137,7 +56,7 @@ func getDefaultDatabaseConfig(c *cli.Context) (map[string]string, error) { return data, nil } -func getDefaultPaymentTypes(c *cli.Context) ([]model.Type, error) { +func (*service) getDefaultPaymentTypes(c *cli.Context) ([]model.Type, error) { var data []model.Type logger := log.Log(c.Context) @@ -169,13 +88,15 @@ func getDefaultPaymentTypes(c *cli.Context) ([]model.Type, error) { return data, nil } -var defaultGroup = model.Group{ - GID: 0, - Name: "默认分组", - Info: `{"1":{"type":"","channel":"-1","rate":""},"2":{"type":"","channel":"-1","rate":""},"3":{"type":"","channel":"-1","rate":""}}`, +func (*service) getDefaultGroup(_ *cli.Context) *model.Group { + return &model.Group{ + GID: 0, + Name: "默认分组", + Info: `{"1":{"type":"","channel":"-1","rate":""},"2":{"type":"","channel":"-1","rate":""},"3":{"type":"","channel":"-1","rate":""}}`, + } } -func createDatabaseConfig(c *cli.Context, configs map[string]string) error { +func (*service) createDatabaseConfig(c *cli.Context, configs map[string]string) error { db := conn.GetDB(c) ctx := c.Context @@ -210,7 +131,7 @@ func createDatabaseConfig(c *cli.Context, configs map[string]string) error { return nil } -func MigrateAction(c *cli.Context) error { +func (svc *service) Do(c *cli.Context) error { ctx := c.Context db := conn.GetDB(c) @@ -253,14 +174,14 @@ func MigrateAction(c *cli.Context) error { } log.Log(ctx).Info("get default config") - defaultDatabaseConfig, err := getDefaultDatabaseConfig(c) + defaultDatabaseConfig, err := svc.getDefaultDatabaseConfig(c) if err != nil { log.Log(ctx).WithError(err).Error("get default config failed") return err } log.Log(ctx).Info("create default config") - if err := createDatabaseConfig(c, defaultDatabaseConfig); err != nil { + if err := svc.createDatabaseConfig(c, defaultDatabaseConfig); err != nil { log.Log(ctx).WithError(err).Panic("create default config failed") return err } @@ -271,13 +192,13 @@ func MigrateAction(c *cli.Context) error { "build": time.Now().Format("2006-01-02"), "cronkey": c.String("app.cronkey"), } - if err := createDatabaseConfig(c, initData); err != nil { + if err := svc.createDatabaseConfig(c, initData); err != nil { log.Log(ctx).WithError(err).Panic("create app init config failed") return err } log.Log(ctx).Info("create default payment types") - defaultPaymentTypes, err := getDefaultPaymentTypes(c) + defaultPaymentTypes, err := svc.getDefaultPaymentTypes(c) if err != nil { log.Log(ctx).WithError(err).Error("get default payment types failed") return err @@ -300,6 +221,7 @@ func MigrateAction(c *cli.Context) error { } log.Log(ctx).Info("create default group") + defaultGroup := svc.getDefaultGroup(c) if err := db.Model(&model.Group{}). Where("gid = ?", defaultGroup.GID). First(&model.Group{}).Error; err == nil { diff --git a/actions/implements/submit/command.go b/actions/implements/submit/command.go new file mode 100644 index 0000000..cfaee63 --- /dev/null +++ b/actions/implements/submit/command.go @@ -0,0 +1,25 @@ +package submit + +import ( + "github.com/AH-dark/epay-cli/pkg/utils" + "github.com/urfave/cli/v2" +) + +func (svc *service) Command() *cli.Command { + return &cli.Command{ + Name: "submit", + Usage: "get submit.php url and args", + Action: svc.Do, + Flags: []cli.Flag{ + &cli.IntFlag{Name: "pid", Required: true, Usage: "partner id", EnvVars: []string{"EPAY_PID"}}, + &cli.StringFlag{Name: "secret", Required: true, Usage: "app secret", EnvVars: []string{"EPAY_SECRET"}}, + &cli.StringFlag{Name: "endpoint", Required: true, Usage: "endpoint", EnvVars: []string{"EPAY_ENDPOINT"}}, + &cli.StringFlag{Name: "type", Value: "alipay", Usage: "payment type", Required: false, EnvVars: []string{"EPAY_TYPE"}}, + &cli.StringFlag{Name: "trade-no", Usage: "service trade no", Value: utils.RandString(32), Required: false, EnvVars: []string{"EPAY_TRADE_NO"}}, + &cli.StringFlag{Name: "name", Usage: "name", Value: "cli 测试商品", Required: false, EnvVars: []string{"EPAY_NAME"}}, + &cli.StringFlag{Name: "money", Usage: "money", Value: "1.00", Required: false, EnvVars: []string{"EPAY_MONEY"}}, + &cli.StringFlag{Name: "notify-url", Usage: "notify url", Required: false, EnvVars: []string{"EPAY_NOTIFY_URL"}}, + &cli.StringFlag{Name: "return-url", Usage: "return url", Required: false, EnvVars: []string{"EPAY_RETURN_URL"}}, + }, + } +} diff --git a/actions/implements/submit/service.go b/actions/implements/submit/service.go new file mode 100644 index 0000000..7a70a49 --- /dev/null +++ b/actions/implements/submit/service.go @@ -0,0 +1,57 @@ +package submit + +import ( + "fmt" + "net/url" + + "github.com/urfave/cli/v2" + + "github.com/AH-dark/epay-cli/actions/factory" + "github.com/AH-dark/epay-cli/pkg/epay" +) + +type service struct { +} + +func NewService() factory.ActionService { + return &service{} +} + +func (svc *service) Do(c *cli.Context) error { + client, err := epay.NewClient(&epay.Config{ + PartnerID: c.Int("pid"), + AppSecret: c.String("secret"), + Endpoint: c.String("endpoint"), + }) + if err != nil { + return err + } + + u, args, err := client.Submit(c.Context, &epay.SubmitArgs{ + Type: epay.PaymentType(c.String("type")), + ServiceTradeNo: c.String("trade-no"), + Name: c.String("name"), + Money: c.String("money"), + NotifyUrl: c.String("notify-url"), + ReturnUrl: c.String("return-url"), + }) + if err != nil { + return err + } + + values := url.Values{} + for k, v := range args { + values.Add(k, v) + } + + urlObj, err := url.Parse(u) + if err != nil { + return err + } + + urlObj.RawQuery = values.Encode() + + fmt.Println(urlObj.String()) + + return nil +} diff --git a/actions/mapi.go b/actions/mapi.go deleted file mode 100644 index 6af187d..0000000 --- a/actions/mapi.go +++ /dev/null @@ -1,146 +0,0 @@ -package actions - -import ( - "fmt" - "strconv" - - "github.com/urfave/cli/v2" - - "github.com/AH-dark/epay-cli/pkg/epay" - "github.com/AH-dark/epay-cli/pkg/utils" -) - -func MapiCommand() *cli.Command { - return &cli.Command{ - Name: "mapi", - Usage: "mapi submit", - Action: MapiAction, - Flags: []cli.Flag{ - &cli.IntFlag{ - Name: "pid", - Required: true, - Usage: "partner id", - EnvVars: []string{"EPAY_PID"}, - }, - &cli.StringFlag{ - Name: "secret", - Required: true, - Usage: "app secret", - EnvVars: []string{"EPAY_SECRET"}, - }, - &cli.StringFlag{ - Name: "endpoint", - Required: true, - Usage: "endpoint", - EnvVars: []string{"EPAY_ENDPOINT"}, - }, - &cli.StringFlag{ - Name: "type", - Value: "alipay", - Usage: "payment type", - Required: false, - EnvVars: []string{"EPAY_TYPE"}, - }, - &cli.StringFlag{ - Name: "trade-no", - Usage: "service trade no", - Value: utils.RandString(32), - Required: false, - EnvVars: []string{"EPAY_TRADE_NO"}, - }, - &cli.StringFlag{ - Name: "name", - Usage: "name", - Value: "cli 测试商品", - Required: false, - EnvVars: []string{"EPAY_NAME"}, - }, - &cli.StringFlag{ - Name: "money", - Usage: "money", - Value: "1.00", - Required: false, - EnvVars: []string{"EPAY_MONEY"}, - }, - &cli.StringFlag{ - Name: "notify-url", - Usage: "notify url", - Required: false, - EnvVars: []string{"EPAY_NOTIFY_URL"}, - }, - &cli.StringFlag{ - Name: "return-url", - Usage: "return url", - Required: false, - EnvVars: []string{"EPAY_RETURN_URL"}, - }, - &cli.StringFlag{ - Name: "client-ip", - Usage: "client ip", - Value: "127.0.0.1", - Required: false, - EnvVars: []string{"EPAY_CLIENT_IP"}, - }, - &cli.StringFlag{ - Name: "device", - Usage: "device", - Value: "pc", - Required: false, - EnvVars: []string{"EPAY_DEVICE"}, - }, - &cli.StringFlag{ - Name: "param", - Usage: "param", - Required: false, - EnvVars: []string{"EPAY_PARAM"}, - }, - }, - } -} - -func MapiAction(c *cli.Context) error { - client, err := epay.NewClient(&epay.Config{ - PartnerID: c.Int("pid"), - AppSecret: c.String("secret"), - Endpoint: c.String("endpoint"), - }) - if err != nil { - return err - } - - sign := utils.CalculateEPaySign(map[string]string{ - "pid": strconv.Itoa(c.Int("pid")), - "type": c.String("type"), - "out_trade_no": c.String("trade-no"), - "notify_url": c.String("notify-url"), - "return_url": c.String("return-url"), - "name": c.String("name"), - "money": c.String("money"), - "clientip": c.String("client-ip"), - "device": c.String("device"), - "param": c.String("param"), - }, c.String("secret")) - fmt.Println("Sign:", sign) - - url, args, err := client.MApiSubmit(c.Context, &epay.MApiSubmitArgs{ - Type: epay.PaymentType(c.String("type")), - OutTradeNo: c.String("trade-no"), - Name: c.String("name"), - Money: c.String("money"), - NotifyUrl: c.String("notify-url"), - ReturnUrl: utils.EmptyPtr(c.String("return-url")), - ClientIP: c.String("client-ip"), - Device: utils.EmptyPtr(epay.DeviceType(c.String("device"))), - Param: utils.EmptyPtr(c.String("param")), - Sign: sign, - SignType: "MD5", - }) - if err != nil { - return err - } - - fmt.Println("URL:", url) - fmt.Println("Args:", args) - - return nil -} diff --git a/main.go b/main.go index 65b2cac..edb3f18 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,9 @@ import ( "github.com/urfave/cli/v2" - "github.com/AH-dark/epay-cli/actions" + "github.com/AH-dark/epay-cli/actions/implements/mapi" + "github.com/AH-dark/epay-cli/actions/implements/migrate" + "github.com/AH-dark/epay-cli/actions/implements/submit" ) var ( @@ -30,13 +32,13 @@ func main() { "date": date, }, Commands: []*cli.Command{ - actions.MigrateCommand(), + migrate.NewService().Command(), { Name: "test", Usage: "test epay", Subcommands: []*cli.Command{ - actions.SubmitCommand(), - actions.MapiCommand(), + submit.NewService().Command(), + mapi.NewService().Command(), }, }, }, From d5b3190296ac78ff9da34ff297951e232795ba66 Mon Sep 17 00:00:00 2001 From: AH-dark Date: Sun, 10 Sep 2023 12:51:19 +0800 Subject: [PATCH 4/4] ci(codeql): install go in version 1.21 --- .github/workflows/codeql.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 1b6c23f..2708521 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -36,6 +36,11 @@ jobs: - name: Checkout repository uses: actions/checkout@v3 + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '1.21' + # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL uses: github/codeql-action/init@v2