From a66bcfba00d8de1c5ea9c2511354560a529e3d92 Mon Sep 17 00:00:00 2001 From: yoneyan Date: Thu, 4 Aug 2022 20:21:18 +0900 Subject: [PATCH] [fix] #144, #145 --- pkg/api/api.go | 9 +- pkg/api/core/interface.go | 18 +- pkg/api/core/payment/v0/payment.go | 59 +---- pkg/api/core/payment/v0/websocket.go | 201 +++++++++++------- pkg/api/core/template/interface.go | 2 - pkg/api/core/template/v0/admin.go | 7 - pkg/api/core/template/v0/template.go | 7 - pkg/api/core/tool/config/config.go | 4 + pkg/api/store/payment/v0/payment.go | 2 +- pkg/api/store/store.go | 1 - .../payment_donate/v0/payment_donate.go | 107 ---------- 11 files changed, 136 insertions(+), 281 deletions(-) delete mode 100644 pkg/api/store/template/payment_donate/v0/payment_donate.go diff --git a/pkg/api/api.go b/pkg/api/api.go index 4b68c8c8..85f6fac6 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -298,7 +298,6 @@ func UserRestAPI() { // Payment // v1.POST("/payment/membership", payment.MembershipPayment) - v1.POST("/payment/donate", payment.DonatePayment) v1.PUT("/payment/card", payment.ChangeCardPayment) v1.GET("/payment/card", payment.ChangeCardPaymentInit) @@ -309,9 +308,6 @@ func UserRestAPI() { v1.POST("/request", ticket.Request) v1.PUT("/support/:id", ticket.Update) - // User Delete - // User ID Get - // Group Delete //v1.DELETE("/group", group.Delete) @@ -319,6 +315,11 @@ func UserRestAPI() { } } + // + // Stripe + // + router.POST("/stripe", payment.GetStripeWebHook) + ws := router.Group("/ws") { v1 := ws.Group("/v1") diff --git a/pkg/api/core/interface.go b/pkg/api/core/interface.go index 6d4f8b16..e590c1fb 100644 --- a/pkg/api/core/interface.go +++ b/pkg/api/core/interface.go @@ -5,13 +5,16 @@ import ( "time" ) +// Payment Type +const PaymentMembership = 1 +const PaymentDonate = 2 + type User struct { gorm.Model Tokens []*Token `json:"tokens"` Notice []*Notice `json:"notice" gorm:"many2many:user_notice;"` Ticket []Ticket `json:"tickets"` Group *Group `json:"group"` - Payment []Payment `json:"payment_membership"` GroupID *uint `json:"group_id"` Name string `json:"name"` NameEn string `json:"name_en"` @@ -25,12 +28,10 @@ type User struct { type Payment struct { gorm.Model - User *User `json:"user"` Group *Group `json:"group"` - UserID uint `json:"user_id"` GroupID *uint `json:"group_id"` PaymentIntentID string `json:"payment_intent_id"` - IsMembership *bool `json:"is_membership"` + Type uint `json:"type"` Paid *bool `json:"paid"` Refund *bool `json:"refund"` Fee uint `json:"fee"` @@ -40,7 +41,7 @@ type Payment struct { type Group struct { gorm.Model Users []User `json:"users"` - Payment []Payment `json:"payment_membership"` + Payment Payment `json:"payment_membership"` Services []Service `json:"services"` Tickets []Ticket `json:"tickets"` Memos []Memo `json:"memos"` @@ -272,13 +273,6 @@ type PaymentCouponTemplate struct { Comment string `json:"comment"` } -type PaymentDonateTemplate struct { - gorm.Model - Name string `json:"name"` - Fee uint `json:"fee"` - Comment string `json:"comment"` -} - type ServiceTemplate struct { gorm.Model Hidden *bool `json:"hidden"` diff --git a/pkg/api/core/payment/v0/payment.go b/pkg/api/core/payment/v0/payment.go index 1a4ad171..a071cb9a 100644 --- a/pkg/api/core/payment/v0/payment.go +++ b/pkg/api/core/payment/v0/payment.go @@ -10,11 +10,9 @@ import ( "github.com/homenoc/dsbd-backend/pkg/api/core/tool/config" dbGroup "github.com/homenoc/dsbd-backend/pkg/api/store/group/v0" dbPayment "github.com/homenoc/dsbd-backend/pkg/api/store/payment/v0" - dbPaymentDonateTemplate "github.com/homenoc/dsbd-backend/pkg/api/store/template/payment_donate/v0" dbPaymentMembershipTemplate "github.com/homenoc/dsbd-backend/pkg/api/store/template/payment_membership/v0" "github.com/stripe/stripe-go/v72" "github.com/stripe/stripe-go/v72/customer" - "github.com/stripe/stripe-go/v72/paymentintent" "github.com/stripe/stripe-go/v72/paymentmethod" "github.com/stripe/stripe-go/v72/setupintent" "github.com/stripe/stripe-go/v72/sub" @@ -84,10 +82,9 @@ func MembershipPayment(c *gin.Context) { } dbPayment.Create(&core.Payment{ - UserID: result.User.ID, GroupID: result.User.GroupID, PaymentIntentID: pi.LatestInvoice.PaymentIntent.ID, - IsMembership: &[]bool{true}[0], + Type: core.PaymentMembership, Paid: &[]bool{false}[0], Fee: resultTemplate.Fee, }) @@ -106,60 +103,6 @@ func MembershipPayment(c *gin.Context) { }) } -func DonatePayment(c *gin.Context) { - var input payment.Input - userToken := c.Request.Header.Get("USER_TOKEN") - accessToken := c.Request.Header.Get("ACCESS_TOKEN") - - err := c.BindJSON(&input) - if err != nil { - log.Println(err) - c.JSON(http.StatusBadRequest, common.Error{Error: err.Error()}) - return - } - - result := auth.UserAuthorization(core.Token{UserToken: userToken, AccessToken: accessToken}) - if result.Err != nil { - c.JSON(http.StatusUnauthorized, common.Error{Error: result.Err.Error()}) - return - } - - resultTemplate, err := dbPaymentDonateTemplate.Get(input.ItemID) - if err != nil { - c.JSON(http.StatusBadRequest, common.Error{Error: "template is not found..."}) - return - } - - stripe.Key = config.Conf.Stripe.SecretKey - - params := &stripe.PaymentIntentParams{ - Amount: stripe.Int64(int64(resultTemplate.Fee)), - Currency: stripe.String(string(stripe.CurrencyJPY)), - } - - pi, err := paymentintent.New(params) - log.Printf("pi.New: %v\n", pi.ClientSecret) - if err != nil { - log.Printf("pi.New: %v", err) - c.JSON(http.StatusInternalServerError, common.Error{Error: "payment_membership system error"}) - return - } - - dbPayment.Create(&core.Payment{ - UserID: result.User.ID, - PaymentIntentID: pi.ID, - IsMembership: &[]bool{false}[0], - Paid: &[]bool{false}[0], - Fee: resultTemplate.Fee, - }) - - go noticeSlackPaymentDonatePayment(result.User.ID, resultTemplate.Fee, pi.ID) - - c.JSON(http.StatusOK, payment.ResultByUser{ - ClientSecret: pi.ClientSecret, - }) -} - func ChangeCardPayment(c *gin.Context) { var input payment.ChangeCardPaymentInit userToken := c.Request.Header.Get("USER_TOKEN") diff --git a/pkg/api/core/payment/v0/websocket.go b/pkg/api/core/payment/v0/websocket.go index c7b30b6b..2f263a2e 100644 --- a/pkg/api/core/payment/v0/websocket.go +++ b/pkg/api/core/payment/v0/websocket.go @@ -3,22 +3,20 @@ package v0 import ( "encoding/json" "fmt" + "github.com/ashwanthkumar/slack-go-webhook" "github.com/gin-gonic/gin" "github.com/homenoc/dsbd-backend/pkg/api/core" "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/payment" "github.com/homenoc/dsbd-backend/pkg/api/core/tool/config" - dbGroup "github.com/homenoc/dsbd-backend/pkg/api/store/group/v0" + "github.com/homenoc/dsbd-backend/pkg/api/core/tool/notification" dbPayment "github.com/homenoc/dsbd-backend/pkg/api/store/payment/v0" "github.com/stripe/stripe-go/v72" "github.com/stripe/stripe-go/v72/webhook" - "gorm.io/gorm" "io/ioutil" "log" "net/http" "os" - "time" + "strconv" ) func GetStripeWebHook(c *gin.Context) { @@ -50,87 +48,126 @@ func GetStripeWebHook(c *gin.Context) { log.Println(event.Type) - if event.Type == "checkout.session.completed" { - log.Println("user", event.Data.Object["metadata"].(map[string]interface{})["user"].(string)) - } else if event.Type == "customer.subscription.updated" { - log.Println("customer.subscription.updated: " + event.Data.Object["id"].(string)) - } else if event.Type == "invoice.paid" { - log.Println("invoice.paid: " + event.Data.Object["id"].(string)) - } else if event.Type == "invoice.updated" { - log.Println("invoice.updated: " + event.Data.Object["id"].(string)) - } else if event.Type == "invoice.payment_succeeded" { - log.Println("invoice.payment_succeeded: " + event.Data.Object["id"].(string)) - } else if event.Type == "payment_intent.created" { - log.Println("payment_intent.created: " + event.Data.Object["id"].(string)) - } else if event.Type == "payment_intent.succeeded" { - log.Println("payment_intent.successed: " + event.Data.Object["id"].(string)) - resultPayment, err := dbPayment.Get(payment.PaymentIntentID, core.Payment{PaymentIntentID: event.Data.Object["id"].(string)}) - if err != nil { - log.Println(err) - } - - if resultPayment[0].Group == nil { - log.Println("[paid] error: GroupID is not exists....") - return - } - - // Membership - if *resultPayment[0].IsMembership { - resultGroup := dbGroup.Get(group.ID, &core.Group{Model: gorm.Model{ID: *resultPayment[0].GroupID}}) - if resultGroup.Err != nil { - log.Println(resultGroup.Err) - return - } - - // now time - now := time.Now() - - if resultGroup.Group[0].MemberExpired != nil { - now = *resultGroup.Group[0].MemberExpired - } + // slack notify(payment log) + attachment := slack.Attachment{} + attachment.Text = &[]string{"Payment Log"}[0] + attachment.AddField(slack.Field{Title: "Type", Value: event.Type}). + AddField(slack.Field{Title: "ID", Value: event.ID}). + AddField(slack.Field{Title: "Created", Value: strconv.FormatInt(event.Created, 10)}) + notification.SendSlack(notification.Slack{Attachment: attachment, ID: config.ToPaymentLogSlackNotify, Status: true}) - if resultGroup.Group[0].PaymentMembershipTemplate.Yearly { - // membership yearly - now = now.AddDate(1, 0, 0) - } else if resultGroup.Group[0].PaymentMembershipTemplate.Monthly { - // membership monthly - now = now.AddDate(0, 1, 0) - } else { - log.Println("error:") - return - } - - dbGroup.Update(group.UpdateMembership, core.Group{ - Model: gorm.Model{ID: *resultPayment[0].GroupID}, - MemberExpired: &now, + if event.Type == "checkout.session.completed" { + // meta + dataType := event.Data.Object["metadata"].(map[string]interface{})["type"].(string) + name := event.Data.Object["metadata"].(map[string]interface{})["name"].(string) + groupID := event.Data.Object["metadata"].(map[string]interface{})["group_id"].(string) + etc := "" + // stripe standard data + amountTotal := event.Data.Object["amount_total"].(float64) + paymentIntent := event.Data.Object["payment_intent"].(string) + if dataType == "donate" { + dbPayment.Create(&core.Payment{ + Type: core.PaymentMembership, + GroupID: nil, + Refund: &[]bool{false}[0], + PaymentIntentID: paymentIntent, + Fee: uint(amountTotal), }) + etc += "UserName: " + name + } else if dataType == "membership" { + etc += "GroupID: " + groupID } - err = dbPayment.Update(payment.UpdatePaid, &core.Payment{ - PaymentIntentID: event.Data.Object["id"].(string), - Paid: &[]bool{true}[0], - }) - if err != nil { - log.Println(err) - } - - noticeSlackPaymentPaid(event.Data.Object["id"].(string)) - - } else if event.Type == "charge.succeeded" { - log.Printf("charge.succeeded: " + event.Data.Object["id"].(string)) - } else if event.Type == "charge.refunded" { - log.Printf("charge.succeeded: " + event.Data.Object["id"].(string) + "| " + event.Data.Object["payment_intent"].(string)) - result, err := dbPayment.Get(payment.PaymentIntentID, core.Payment{PaymentIntentID: event.Data.Object["payment_intent"].(string)}) - if err != nil { - log.Println(err) - } - err = dbPayment.Update(payment.UpdateAll, &core.Payment{ - Model: gorm.Model{ID: result[0].ID}, - Refund: &[]bool{true}[0], - }) - if err != nil { - log.Println(err) - } + // slack notify(payment log) + attachment = slack.Attachment{} + attachment.Text = &[]string{"Payment"}[0] + attachment.AddField(slack.Field{Title: "Type", Value: dataType}). + AddField(slack.Field{Title: "ID", Value: event.ID}). + AddField(slack.Field{Title: "PaymentIntent", Value: paymentIntent}). + AddField(slack.Field{Title: "Etc", Value: etc}). + AddField(slack.Field{Title: "Fee", Value: strconv.Itoa(int(uint(amountTotal))) + "円"}) + notification.SendSlack(notification.Slack{Attachment: attachment, ID: config.ToPaymentSlackNotify, Status: true}) + + //log.Println("user", event.Data.Object["metadata"].(map[string]interface{})["user"].(string)) + //} else if event.Type == "customer.subscription.updated" { + // log.Println("customer.subscription.updated: " + event.Data.Object["id"].(string)) + //} else if event.Type == "invoice.paid" { + // log.Println("invoice.paid: " + event.Data.Object["id"].(string)) + //} else if event.Type == "invoice.updated" { + // log.Println("invoice.updated: " + event.Data.Object["id"].(string)) + //} else if event.Type == "invoice.payment_succeeded" { + // log.Println("invoice.payment_succeeded: " + event.Data.Object["id"].(string)) + //} else if event.Type == "payment_intent.created" { + // log.Println("payment_intent.created: " + event.Data.Object["id"].(string)) + //} else if event.Type == "payment_intent.succeeded" { + // log.Println("payment_intent.successed: " + event.Data.Object["id"].(string)) + // resultPayment, err := dbPayment.Get(payment.PaymentIntentID, core.Payment{PaymentIntentID: event.Data.Object["id"].(string)}) + // if err != nil { + // log.Println(err) + // } + // + // // Membership + // if *resultPayment[0].IsMembership { + // if resultPayment[0].Group == nil { + // log.Println("[paid] error: GroupID is not exists....") + // return + // } + // + // resultGroup := dbGroup.Get(group.ID, &core.Group{Model: gorm.Model{ID: *resultPayment[0].GroupID}}) + // if resultGroup.Err != nil { + // log.Println(resultGroup.Err) + // return + // } + // + // // now time + // now := time.Now() + // + // if resultGroup.Group[0].MemberExpired != nil { + // now = *resultGroup.Group[0].MemberExpired + // } + // + // if resultGroup.Group[0].PaymentMembershipTemplate.Yearly { + // // membership yearly + // now = now.AddDate(1, 0, 0) + // } else if resultGroup.Group[0].PaymentMembershipTemplate.Monthly { + // // membership monthly + // now = now.AddDate(0, 1, 0) + // } else { + // log.Println("error:") + // return + // } + // + // dbGroup.Update(group.UpdateMembership, core.Group{ + // Model: gorm.Model{ID: *resultPayment[0].GroupID}, + // MemberExpired: &now, + // }) + // } + // + // err = dbPayment.Update(payment.UpdatePaid, &core.Payment{ + // PaymentIntentID: event.Data.Object["id"].(string), + // Paid: &[]bool{true}[0], + // }) + // if err != nil { + // log.Println(err) + // } + // + // noticeSlackPaymentPaid(event.Data.Object["id"].(string)) + // + //} else if event.Type == "charge.succeeded" { + // log.Printf("charge.succeeded: " + event.Data.Object["id"].(string)) + //} else if event.Type == "charge.refunded" { + // log.Printf("charge.succeeded: " + event.Data.Object["id"].(string) + "| " + event.Data.Object["payment_intent"].(string)) + // result, err := dbPayment.Get(payment.PaymentIntentID, core.Payment{PaymentIntentID: event.Data.Object["payment_intent"].(string)}) + // if err != nil { + // log.Println(err) + // } + // err = dbPayment.Update(payment.UpdateAll, &core.Payment{ + // Model: gorm.Model{ID: result[0].ID}, + // Refund: &[]bool{true}[0], + // }) + // if err != nil { + // log.Println(err) + // } } diff --git a/pkg/api/core/template/interface.go b/pkg/api/core/template/interface.go index a53c77f2..a675e362 100644 --- a/pkg/api/core/template/interface.go +++ b/pkg/api/core/template/interface.go @@ -26,7 +26,6 @@ type Result struct { IPv4Route []core.IPv4RouteTemplate `json:"ipv4_route"` IPv6Route []core.IPv6RouteTemplate `json:"ipv6_route"` PaymentMembershipTemplate []core.PaymentMembershipTemplate `json:"payment_membership_template"` - PaymentDonateTemplate []core.PaymentDonateTemplate `json:"payment_donate_template"` PaymentCouponTemplate []core.PaymentCouponTemplate `json:"payment_coupon_template"` } @@ -45,7 +44,6 @@ type ResultAdmin struct { User []core.User `json:"user"` Group []core.Group `json:"group"` PaymentMembershipTemplate []core.PaymentMembershipTemplate `json:"payment_membership_template"` - PaymentDonateTemplate []core.PaymentDonateTemplate `json:"payment_donate_template"` PaymentCouponTemplate []core.PaymentCouponTemplate `json:"payment_coupon_template"` MailTemplate []core.MailTemplate `json:"mail_template"` } diff --git a/pkg/api/core/template/v0/admin.go b/pkg/api/core/template/v0/admin.go index 7bb148aa..35c45f0e 100644 --- a/pkg/api/core/template/v0/admin.go +++ b/pkg/api/core/template/v0/admin.go @@ -17,7 +17,6 @@ import ( dbMailTemplate "github.com/homenoc/dsbd-backend/pkg/api/store/template/mail/v0" dbNTTTemplate "github.com/homenoc/dsbd-backend/pkg/api/store/template/ntt/v0" dbPaymentCouponTemplate "github.com/homenoc/dsbd-backend/pkg/api/store/template/payment_coupon/v0" - dbPaymentDonateTemplate "github.com/homenoc/dsbd-backend/pkg/api/store/template/payment_donate/v0" dbPaymentMembershipTemplate "github.com/homenoc/dsbd-backend/pkg/api/store/template/payment_membership/v0" dbServiceTemplate "github.com/homenoc/dsbd-backend/pkg/api/store/template/service/v0" dbUser "github.com/homenoc/dsbd-backend/pkg/api/store/user/v0" @@ -104,11 +103,6 @@ func GetByAdmin(c *gin.Context) { c.JSON(http.StatusInternalServerError, common.Error{Error: err.Error()}) return } - resultDonateMembership, err := dbPaymentDonateTemplate.GetAll() - if err != nil { - c.JSON(http.StatusInternalServerError, common.Error{Error: err.Error()}) - return - } resultPaymentCoupon, err := dbPaymentCouponTemplate.GetAll() if err != nil { c.JSON(http.StatusInternalServerError, common.Error{Error: err.Error()}) @@ -135,7 +129,6 @@ func GetByAdmin(c *gin.Context) { User: resultUser.User, Group: resultGroup.Group, PaymentMembershipTemplate: resultPaymentMembership, - PaymentDonateTemplate: resultDonateMembership, PaymentCouponTemplate: resultPaymentCoupon, MailTemplate: resultMailTemplate, }) diff --git a/pkg/api/core/template/v0/template.go b/pkg/api/core/template/v0/template.go index 28bfa49d..db054440 100644 --- a/pkg/api/core/template/v0/template.go +++ b/pkg/api/core/template/v0/template.go @@ -14,7 +14,6 @@ import ( dbIPv6RouteTemplate "github.com/homenoc/dsbd-backend/pkg/api/store/template/ipv6_route/v0" dbNTTTemplate "github.com/homenoc/dsbd-backend/pkg/api/store/template/ntt/v0" dbPaymentCouponTemplate "github.com/homenoc/dsbd-backend/pkg/api/store/template/payment_coupon/v0" - dbPaymentDonateTemplate "github.com/homenoc/dsbd-backend/pkg/api/store/template/payment_donate/v0" dbPaymentMembershipTemplate "github.com/homenoc/dsbd-backend/pkg/api/store/template/payment_membership/v0" dbServiceTemplate "github.com/homenoc/dsbd-backend/pkg/api/store/template/service/v0" "net/http" @@ -78,11 +77,6 @@ func Get(c *gin.Context) { c.JSON(http.StatusInternalServerError, common.Error{Error: err.Error()}) return } - resultDonateMembership, err := dbPaymentDonateTemplate.GetAll() - if err != nil { - c.JSON(http.StatusInternalServerError, common.Error{Error: err.Error()}) - return - } resultPaymentCoupon, err := dbPaymentCouponTemplate.GetAll() if err != nil { c.JSON(http.StatusInternalServerError, common.Error{Error: err.Error()}) @@ -99,7 +93,6 @@ func Get(c *gin.Context) { IPv4Route: resultIPv4Route, IPv6Route: resultIPv6Route, PaymentMembershipTemplate: resultPaymentMembership, - PaymentDonateTemplate: resultDonateMembership, PaymentCouponTemplate: resultPaymentCoupon, }) } diff --git a/pkg/api/core/tool/config/config.go b/pkg/api/core/tool/config/config.go index 20af6376..225b8c20 100644 --- a/pkg/api/core/tool/config/config.go +++ b/pkg/api/core/tool/config/config.go @@ -5,6 +5,10 @@ import ( "io/ioutil" ) +const ToMainSlackNotify = "main" +const ToPaymentSlackNotify = "payment" +const ToPaymentLogSlackNotify = "payment_log" + type Config struct { Controller Controller `json:"controller"` Web Web `json:"web"` diff --git a/pkg/api/store/payment/v0/payment.go b/pkg/api/store/payment/v0/payment.go index 18bd5ed4..4744e80c 100644 --- a/pkg/api/store/payment/v0/payment.go +++ b/pkg/api/store/payment/v0/payment.go @@ -59,7 +59,7 @@ func Update(base int, input *core.Payment) error { err = nil if payment.UpdatePaid == base { - err = db.Model(&core.Payment{PaymentIntentID: input.PaymentIntentID}).Updates(core.Payment{Paid: input.Paid}).Error + err = db.Model(&core.Payment{PaymentIntentID: input.PaymentIntentID}).Where("payment_intent_id", input.PaymentIntentID).Update("paid", input.Paid).Error } else if payment.UpdateAll == base { err = db.Model(&core.Payment{Model: gorm.Model{ID: input.ID}}).Updates(&input).Error } else { diff --git a/pkg/api/store/store.go b/pkg/api/store/store.go index 873205e5..d5328252 100644 --- a/pkg/api/store/store.go +++ b/pkg/api/store/store.go @@ -55,7 +55,6 @@ func InitDB() { &core.Payment{}, &core.PaymentCouponTemplate{}, &core.PaymentMembershipTemplate{}, - &core.PaymentDonateTemplate{}, &core.MailTemplate{}, ) log.Println(result) diff --git a/pkg/api/store/template/payment_donate/v0/payment_donate.go b/pkg/api/store/template/payment_donate/v0/payment_donate.go deleted file mode 100644 index bf57874a..00000000 --- a/pkg/api/store/template/payment_donate/v0/payment_donate.go +++ /dev/null @@ -1,107 +0,0 @@ -package v0 - -import ( - "fmt" - "github.com/homenoc/dsbd-backend/pkg/api/core" - "github.com/homenoc/dsbd-backend/pkg/api/store" - "gorm.io/gorm" - "log" - "time" -) - -func Create(connection *core.PaymentDonateTemplate) (*core.PaymentDonateTemplate, error) { - db, err := store.ConnectDB() - if err != nil { - log.Println("database connection error") - return connection, fmt.Errorf("(%s)error: %s\n", time.Now(), err.Error()) - } - dbSQL, err := db.DB() - if err != nil { - log.Printf("database error: %v", err) - return nil, fmt.Errorf("(%s)error: %s\n", time.Now(), err.Error()) - } - defer dbSQL.Close() - - err = db.Create(&connection).Error - return connection, err -} - -func Delete(connection *core.PaymentDonateTemplate) error { - db, err := store.ConnectDB() - if err != nil { - log.Println("database connection error") - return fmt.Errorf("(%s)error: %s\n", time.Now(), err.Error()) - } - dbSQL, err := db.DB() - if err != nil { - log.Printf("database error: %v", err) - return fmt.Errorf("(%s)error: %s\n", time.Now(), err.Error()) - } - defer dbSQL.Close() - - return db.Delete(connection).Error -} - -func Update(c core.PaymentDonateTemplate) error { - db, err := store.ConnectDB() - if err != nil { - log.Println("database connection error") - return fmt.Errorf("(%s)error: %s\n", time.Now(), err.Error()) - } - dbSQL, err := db.DB() - if err != nil { - log.Printf("database error: %v", err) - return fmt.Errorf("(%s)error: %s\n", time.Now(), err.Error()) - } - defer dbSQL.Close() - - err = nil - - err = db.Model(&core.PaymentDonateTemplate{Model: gorm.Model{ID: c.ID}}).Updates(core.PaymentDonateTemplate{ - Name: c.Name, - Fee: c.Fee, - Comment: c.Comment, - }).Error - - return err -} - -func Get(id uint) (core.PaymentDonateTemplate, error) { - var payment core.PaymentDonateTemplate - - db, err := store.ConnectDB() - if err != nil { - log.Println("database connection error") - return payment, fmt.Errorf("(%s)error: %s\n", time.Now(), err.Error()) - } - dbSQL, err := db.DB() - if err != nil { - log.Printf("database error: %v", err) - return payment, fmt.Errorf("(%s)error: %s\n", time.Now(), err.Error()) - } - defer dbSQL.Close() - - err = db.First(&payment, id).Error - - return payment, err -} - -func GetAll() ([]core.PaymentDonateTemplate, error) { - var payments []core.PaymentDonateTemplate - - db, err := store.ConnectDB() - if err != nil { - log.Println("database connection error") - return payments, fmt.Errorf("(%s)error: %s\n", time.Now(), err.Error()) - } - dbSQL, err := db.DB() - if err != nil { - log.Printf("database error: %v", err) - return payments, fmt.Errorf("(%s)error: %s\n", time.Now(), err.Error()) - } - defer dbSQL.Close() - - err = db.Find(&payments).Error - - return payments, err -}