Skip to content

Commit

Permalink
Merge pull request #191 from homenoc/develop
Browse files Browse the repository at this point in the history
Fix
  • Loading branch information
yoneyan authored May 22, 2023
2 parents 776a1a5 + 6bee69a commit a9c319d
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 30 deletions.
41 changes: 21 additions & 20 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/homenoc/dsbd-backend

go 1.18
go 1.20

require (
github.com/gin-gonic/gin v1.9.0
Expand All @@ -11,46 +11,47 @@ require (
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.15.0
github.com/stripe/stripe-go/v73 v73.16.0
gorm.io/driver/mysql v1.5.0
gorm.io/gorm v1.25.0
gorm.io/driver/mysql v1.5.1
gorm.io/gorm v1.25.1
)

require (
github.com/bytedance/sonic v1.8.0 // indirect
github.com/bytedance/sonic v1.8.9 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.11.2 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/goccy/go-json v0.10.0 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/go-sql-driver/mysql v1.7.1 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.7 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.9 // indirect
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
golang.org/x/crypto v0.5.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
51 changes: 51 additions & 0 deletions go.sum

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ func AdminRestAPI() {
// Payment
//
v1.POST("/group/:id/payment/subscribe", payment.PostAdminSubscribeGettingURL)
v1.GET("/group/:id/payment/subscribe", payment.GetAdminDashboardSubscribeURL)
v1.GET("/group/:id/payment", payment.GetAdminBillingPortalURL)
v1.GET("/group/:id/payment/customer", payment.GetAdminDashboardCustomerURL)

//
// JPNIC ByAdmin
Expand Down
79 changes: 76 additions & 3 deletions pkg/api/core/payment/v0/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,8 @@ func GetAdminBillingPortalURL(c *gin.Context) {
}

params := &stripe.BillingPortalSessionParams{
Customer: stripe.String(*result.Group[0].StripeCustomerID),
Configuration: stripe.String(config.Conf.Stripe.MembershipConfiguration),
ReturnURL: stripe.String(config.Conf.Controller.Admin.ReturnURL),
Customer: stripe.String(*result.Group[0].StripeCustomerID),
ReturnURL: stripe.String(config.Conf.Controller.Admin.ReturnURL),
}

s, err := billingSession.New(params)
Expand All @@ -163,3 +162,77 @@ func GetAdminBillingPortalURL(c *gin.Context) {

c.JSON(http.StatusOK, map[string]string{"url": s.URL})
}

func GetAdminDashboardCustomerURL(c *gin.Context) {
// ID取得
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()})
return
}

// Admin authentication
resultAdmin := auth.AdminAuthorization(c.Request.Header.Get("ACCESS_TOKEN"))
if resultAdmin.Err != nil {
c.JSON(http.StatusUnauthorized, common.Error{Error: resultAdmin.Err.Error()})
return
}

// serviceIDが0の時エラー処理
if id == 0 {
c.JSON(http.StatusBadRequest, common.Error{Error: fmt.Sprintf("ID is wrong... ")})
return
}

result := dbGroup.Get(group.ID, &core.Group{Model: gorm.Model{ID: uint(id)}})
if result.Err != nil {
c.JSON(http.StatusInternalServerError, common.Error{Error: result.Err.Error()})
return
}

// exist check: stripeCustomerID
if result.Group[0].StripeCustomerID == nil || *result.Group[0].StripeCustomerID == "" {
c.JSON(http.StatusNotFound, common.Error{Error: "CustomerID is not found..."})
return
}

url := config.BaseStripeUrl + "/customers/" + *result.Group[0].StripeCustomerID
c.JSON(http.StatusOK, map[string]string{"url": url})
}

func GetAdminDashboardSubscribeURL(c *gin.Context) {
// ID取得
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()})
return
}

// Admin authentication
resultAdmin := auth.AdminAuthorization(c.Request.Header.Get("ACCESS_TOKEN"))
if resultAdmin.Err != nil {
c.JSON(http.StatusUnauthorized, common.Error{Error: resultAdmin.Err.Error()})
return
}

// serviceIDが0の時エラー処理
if id == 0 {
c.JSON(http.StatusBadRequest, common.Error{Error: fmt.Sprintf("ID is wrong... ")})
return
}

result := dbGroup.Get(group.ID, &core.Group{Model: gorm.Model{ID: uint(id)}})
if result.Err != nil {
c.JSON(http.StatusInternalServerError, common.Error{Error: result.Err.Error()})
return
}

// exist check: stripeCustomerID
if result.Group[0].StripeSubscriptionID == nil || *result.Group[0].StripeSubscriptionID == "" {
c.JSON(http.StatusNotFound, common.Error{Error: "SubscribeID is not found..."})
return
}

url := config.BaseStripeUrl + "/subscriptions/" + *result.Group[0].StripeSubscriptionID
c.JSON(http.StatusOK, map[string]string{"url": url})
}
5 changes: 2 additions & 3 deletions pkg/api/core/payment/v0/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,8 @@ func GetBillingPortalURL(c *gin.Context) {
}

params := &stripe.BillingPortalSessionParams{
Customer: stripe.String(*resultAuth.User.Group.StripeCustomerID),
Configuration: stripe.String(config.Conf.Stripe.MembershipConfiguration),
ReturnURL: stripe.String(config.Conf.Controller.User.ReturnURL),
Customer: stripe.String(*resultAuth.User.Group.StripeCustomerID),
ReturnURL: stripe.String(config.Conf.Controller.User.ReturnURL),
}

s, err := billingSession.New(params)
Expand Down
3 changes: 2 additions & 1 deletion pkg/api/core/payment/v0/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ func GetStripeWebHook(c *gin.Context) {
groupID, _ = strconv.Atoi(groupIDStr)

// stripe standard data
sub := event.Data.Object["id"].(string)
customer := event.Data.Object["customer"].(string)
planID := event.Data.Object["plan"].(map[string]interface{})["id"].(string)
amount := event.Data.Object["plan"].(map[string]interface{})["amount"].(float64)
Expand All @@ -177,7 +178,7 @@ func GetStripeWebHook(c *gin.Context) {
jst, _ := time.LoadLocation(config.Conf.Controller.TimeZone)
timeDate := time.Date(periodEndTime.Year(), periodEndTime.Month(), periodEndTime.Day(), 0, 0, 0, 0, jst)
if groupID != 0 {
err = dbGroup.Update(group.UpdateAll, core.Group{Model: gorm.Model{ID: uint(groupID)}, MemberExpired: &timeDate})
err = dbGroup.Update(group.UpdateAll, core.Group{Model: gorm.Model{ID: uint(groupID)}, StripeSubscriptionID: &sub, MemberExpired: &timeDate})
}

// slack notify(payment log)
Expand Down
11 changes: 8 additions & 3 deletions pkg/api/core/tool/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const ToMainSlackNotify = "main"
const ToPaymentSlackNotify = "payment"
const ToPaymentLogSlackNotify = "payment_log"

var BaseStripeUrl = "https://dashboard.stripe.com"

type Config struct {
Controller Controller `json:"controller"`
Web Web `json:"web"`
Expand Down Expand Up @@ -49,9 +51,9 @@ type Web struct {
}

type Stripe struct {
WebhookSecretKey string `json:"webhook_secret_key"`
SecretKey string `json:"secret_key"`
MembershipConfiguration string `json:"membership_configuration"`
WebhookSecretKey string `json:"webhook_secret_key"`
SecretKey string `json:"secret_key"`
IsTest bool `json:"is_test"`
}

type AdminAuth struct {
Expand Down Expand Up @@ -182,6 +184,9 @@ func GetConfig(inputConfPath string) error {
if err != nil {
log.Fatal(err)
}
if data.Stripe.IsTest {
BaseStripeUrl += "/test"
}
Conf = data
return nil
}

0 comments on commit a9c319d

Please sign in to comment.