Skip to content

Commit

Permalink
[fix] #144 #145
Browse files Browse the repository at this point in the history
  • Loading branch information
yoneyan committed Aug 13, 2022
1 parent ae47db7 commit 64216e6
Show file tree
Hide file tree
Showing 17 changed files with 447 additions and 302 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ require (
github.com/slack-go/slack v0.11.2
github.com/spf13/cobra v1.5.0
github.com/spf13/viper v1.12.0
github.com/stripe/stripe-go/v72 v72.122.0
github.com/stripe/stripe-go/v73 v73.0.1
gorm.io/driver/mysql v1.3.5
gorm.io/gorm v1.23.8
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ func AdminRestAPI() {
v1.DELETE("/group", group.DeleteByAdmin)
// Group Update
v1.PUT("/group/:id", group.UpdateByAdmin)
v1.DELETE("/group/:id/subscription", group.CancelSubscription)
v1.GET("/group", group.GetAllByAdmin)
v1.GET("/group/:id", group.GetByAdmin)

Expand Down Expand Up @@ -168,7 +167,8 @@ func AdminRestAPI() {
//
// Payment
//
//v1.POST("/group/:id/service", service.AddByAdmin)
v1.POST("/group/:id/payment/subscribe", payment.PostAdminSubscribeGettingURL)
v1.GET("/group/:id/payment", payment.GetAdminBillingPortalURL)
// Delete
v1.DELETE("/payment/:id", payment.DeleteByAdmin)
v1.POST("/payment/:id/refund", payment.RefundByAdmin)
Expand Down
13 changes: 13 additions & 0 deletions pkg/api/core/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ package core
//

// Membership
// 1-49 一般支払い
// 70-89 特別枠(無料)
// 90-99 特別枠(運営)
// 1: 一般会員
// 70: 学生会員
// 90: 運営委員
// 99: ""
var MemberTypes = []ConstantMembership{
MemberTypeStandard,
MemberTypeStudent,
MemberTypeCommittee,
MemberTypeDisable,
}
var MemberTypeStandard = ConstantMembership{ID: 1, Name: "一般会員"}
var MemberTypeStudent = ConstantMembership{ID: 70, Name: "学生会員"}
var MemberTypeCommittee = ConstantMembership{ID: 90, Name: "運営委員"}
Expand Down
12 changes: 12 additions & 0 deletions pkg/api/core/get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package core

import "fmt"

func GetMembershipTypeID(id uint) (ConstantMembership, error) {
for _, memberType := range MemberTypes {
if memberType.ID == id {
return memberType, nil
}
}
return ConstantMembership{}, fmt.Errorf("error: getting membership")
}
41 changes: 20 additions & 21 deletions pkg/api/core/group/info/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,26 @@ type User struct {
}

type Group struct {
ID uint `json:"id"`
PaymentMembershipTemplate string `json:"payment_membership_template"`
Agree *bool `json:"agree"`
Question string `json:"question"`
Org string `json:"org"`
OrgEn string `json:"org_en"`
PostCode string `json:"postcode"`
Address string `json:"address"`
AddressEn string `json:"address_en"`
Tel string `json:"tel"`
Country string `json:"country"`
Contract string `json:"contract"`
Paid *bool `json:"paid"`
MemberInfo string `json:"member_info"`
MemberExpired *time.Time `json:"member_expired"`
Student *bool `json:"student"`
StudentExpired *time.Time `json:"student_expired"`
Pass *bool `json:"pass"`
Lock *bool `json:"lock"`
ExpiredStatus *uint `json:"expired_status"`
AddAllow *bool `json:"add_allow"`
ID uint `json:"id"`
Agree *bool `json:"agree"`
Question string `json:"question"`
Org string `json:"org"`
OrgEn string `json:"org_en"`
PostCode string `json:"postcode"`
Address string `json:"address"`
AddressEn string `json:"address_en"`
Tel string `json:"tel"`
Country string `json:"country"`
Contract string `json:"contract"`
CouponID string `json:"coupon_id"`
MemberTypeID uint `json:"member_type_id"`
MemberType string `json:"member_type"`
MemberExpired *time.Time `json:"member_expired"`
IsExpired bool `json:"is_expired"`
IsStripeID bool `json:"is_stripe_id"`
Pass *bool `json:"pass"`
ExpiredStatus *uint `json:"expired_status"`
AddAllow *bool `json:"add_allow"`
}

type Notice struct {
Expand Down
54 changes: 43 additions & 11 deletions pkg/api/core/group/info/v0/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"net/http"
"sort"
"strconv"
"time"
)

func Get(c *gin.Context) {
Expand Down Expand Up @@ -60,19 +61,50 @@ func Get(c *gin.Context) {

if authResult.User.GroupID != nil {
// Membership Info
membershipInfo := "一般会員"
membershipPlan := "未設定"
paid := false
membership, err := core.GetMembershipTypeID(authResult.User.Group.MemberType)
if err != nil {
c.JSON(http.StatusInternalServerError, common.Error{Error: err.Error()})
return
}

// isExpired(課金確認)
isExpired := false
if authResult.User.Group.MemberType < 50 && authResult.User.Group.MemberExpired != nil {
jst, err := time.LoadLocation("Asia/Tokyo")
if err != nil {
panic(err)
}
nowJST := time.Now().In(jst)
if nowJST.Unix() > authResult.User.Group.MemberExpired.Add(time.Hour*24).Unix() {
isExpired = true
}
} else if authResult.User.Group.MemberType < 50 && authResult.User.Group.MemberExpired == nil {
isExpired = true
}

// isStripeID
isStripeID := true
if authResult.User.Group.StripeCustomerID == nil || *authResult.User.Group.StripeCustomerID == "" ||
authResult.User.Group.StripeSubscriptionID == nil || *authResult.User.Group.StripeSubscriptionID == "" {
isStripeID = false
}

// coupon
couponID := ""
if authResult.User.Group.CouponID != nil {
couponID = *authResult.User.Group.CouponID
}

resultGroup = info.Group{
ID: authResult.User.Group.ID,
Student: &[]bool{authResult.User.Group.MemberType == core.MemberTypeStudent.ID}[0],
Pass: authResult.User.Group.Pass,
ExpiredStatus: authResult.User.Group.ExpiredStatus,
MemberInfo: membershipInfo,
Paid: &paid,
PaymentMembershipTemplate: membershipPlan,
MemberExpired: authResult.User.Group.MemberExpired,
ID: authResult.User.Group.ID,
Pass: authResult.User.Group.Pass,
ExpiredStatus: authResult.User.Group.ExpiredStatus,
IsExpired: isExpired,
IsStripeID: isStripeID,
MemberTypeID: membership.ID,
MemberType: membership.Name,
MemberExpired: authResult.User.Group.MemberExpired,
CouponID: couponID,
}
if authResult.User.Level < 3 {
resultGroup.Agree = dbUserResult.User[0].Group.Agree
Expand Down
57 changes: 0 additions & 57 deletions pkg/api/core/group/v0/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import (
auth "github.com/homenoc/dsbd-backend/pkg/api/core/auth/v0"
"github.com/homenoc/dsbd-backend/pkg/api/core/common"
"github.com/homenoc/dsbd-backend/pkg/api/core/group"
"github.com/homenoc/dsbd-backend/pkg/api/core/tool/config"
dbGroup "github.com/homenoc/dsbd-backend/pkg/api/store/group/v0"
"github.com/stripe/stripe-go/v72"
"github.com/stripe/stripe-go/v72/sub"
"gorm.io/gorm"
"log"
"net/http"
Expand Down Expand Up @@ -142,57 +139,3 @@ func GetAllByAdmin(c *gin.Context) {
c.JSON(http.StatusOK, group.ResultAdminAll{Group: result.Group})
}
}

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

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

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

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

if resultGroup.Group[0].StripeSubscriptionID == nil {
c.JSON(http.StatusBadRequest, common.Error{Error: "Subscription ID is not exists."})
return
}

noticeCancelSubscriptionByAdmin(resultGroup.Group[0])

stripe.Key = config.Conf.Stripe.SecretKey

_, err = sub.Cancel(*resultGroup.Group[0].StripeSubscriptionID, nil)
if err != nil {
log.Printf("pi.New: %v", err)
c.JSON(http.StatusInternalServerError, common.Error{Error: err.Error()})
return
}

err = dbGroup.Update(group.UpdateAll, core.Group{
StripeSubscriptionID: &[]string{""}[0],
})
if err != nil {
log.Printf("Error: %v", err)
c.JSON(http.StatusInternalServerError, common.Error{Error: err.Error()})
return
}

c.JSON(http.StatusOK, common.Result{})
}
45 changes: 15 additions & 30 deletions pkg/api/core/group/v0/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ import (
auth "github.com/homenoc/dsbd-backend/pkg/api/core/auth/v0"
"github.com/homenoc/dsbd-backend/pkg/api/core/common"
"github.com/homenoc/dsbd-backend/pkg/api/core/group"
"github.com/homenoc/dsbd-backend/pkg/api/core/tool/config"
"github.com/homenoc/dsbd-backend/pkg/api/core/user"
dbGroup "github.com/homenoc/dsbd-backend/pkg/api/store/group/v0"
dbUser "github.com/homenoc/dsbd-backend/pkg/api/store/user/v0"
"github.com/stripe/stripe-go/v72"
"github.com/stripe/stripe-go/v72/customer"
"gorm.io/gorm"
"log"
"net/http"
Expand Down Expand Up @@ -72,34 +69,22 @@ func Add(c *gin.Context) {
memberType = core.MemberTypeStudent.ID
}

// added customer (stripe)
stripe.Key = config.Conf.Stripe.SecretKey

params := &stripe.CustomerParams{
Description: stripe.String("Org: " + input.Org + "(" + input.OrgEn + ")"),
}
cus, err := customer.New(params)
if err != nil {
log.Println("Error: " + err.Error())
}

groupData := core.Group{
Agree: &[]bool{*input.Agree}[0],
StripeCustomerID: &cus.ID,
Question: input.Question,
Org: input.Org,
OrgEn: input.OrgEn,
PostCode: input.PostCode,
Address: input.Address,
AddressEn: input.AddressEn,
Tel: input.Tel,
Country: input.Country,
ExpiredStatus: &[]uint{0}[0],
Contract: input.Contract,
MemberType: memberType,
MemberExpired: memberExpired,
Pass: &[]bool{false}[0],
AddAllow: &[]bool{true}[0],
Agree: &[]bool{*input.Agree}[0],
Question: input.Question,
Org: input.Org,
OrgEn: input.OrgEn,
PostCode: input.PostCode,
Address: input.Address,
AddressEn: input.AddressEn,
Tel: input.Tel,
Country: input.Country,
ExpiredStatus: &[]uint{0}[0],
Contract: input.Contract,
MemberType: memberType,
MemberExpired: memberExpired,
Pass: &[]bool{false}[0],
AddAllow: &[]bool{true}[0],
}

_, err = dbGroup.Create(&groupData)
Expand Down
Loading

0 comments on commit 64216e6

Please sign in to comment.