Skip to content

Commit

Permalink
[add] Added payment api.
Browse files Browse the repository at this point in the history
  • Loading branch information
yoneyan committed May 22, 2023
1 parent d871a44 commit ba6addf
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
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
74 changes: 74 additions & 0 deletions pkg/api/core/payment/v0/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,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})
}
6 changes: 6 additions & 0 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 @@ -51,6 +53,7 @@ type Web struct {
type Stripe struct {
WebhookSecretKey string `json:"webhook_secret_key"`
SecretKey string `json:"secret_key"`
IsTest bool `json:"is_test"`
}

type AdminAuth struct {
Expand Down Expand Up @@ -181,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 ba6addf

Please sign in to comment.