Mhamdans17 commited on
Commit ·
4e8a43c
1
Parent(s): 6fd3b19
feat: migrate global Tripay credentials to system settings table in DB
Browse files- controllers/auth_controller.go +22 -0
- main.go +2 -0
- models/models.go +9 -0
- services/tripay.go +7 -9
controllers/auth_controller.go
CHANGED
|
@@ -42,6 +42,28 @@ func SeedDefaultAdmin() {
|
|
| 42 |
}
|
| 43 |
}
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
// Request & Response DTOs
|
| 46 |
type LoginRequest struct {
|
| 47 |
Email string `json:"email" binding:"required"`
|
|
|
|
| 42 |
}
|
| 43 |
}
|
| 44 |
|
| 45 |
+
// SeedDefaultSetting inisialisasi setting bawaan di database dari konfigurasi .env
|
| 46 |
+
func SeedDefaultSetting() {
|
| 47 |
+
var count int64
|
| 48 |
+
config.DB.Model(&models.Setting{}).Count(&count)
|
| 49 |
+
if count == 0 {
|
| 50 |
+
setting := models.Setting{
|
| 51 |
+
ID: 1,
|
| 52 |
+
TripayMerchantCode: config.GlobalConfig.TripayMerchantCode,
|
| 53 |
+
TripayAPIKey: config.GlobalConfig.TripayAPIKey,
|
| 54 |
+
TripayPrivateKey: config.GlobalConfig.TripayPrivateKey,
|
| 55 |
+
TripayProxyURL: config.GlobalConfig.TripayProxyURL,
|
| 56 |
+
}
|
| 57 |
+
if err := config.DB.Create(&setting).Error; err != nil {
|
| 58 |
+
log.Printf("[SEED] Gagal membuat default setting: %v", err)
|
| 59 |
+
} else {
|
| 60 |
+
log.Println("[SEED] Sukses membuat default setting dari file .env!")
|
| 61 |
+
}
|
| 62 |
+
} else {
|
| 63 |
+
log.Println("[SEED] Setting default sudah terbuat di DB.")
|
| 64 |
+
}
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
// Request & Response DTOs
|
| 68 |
type LoginRequest struct {
|
| 69 |
Email string `json:"email" binding:"required"`
|
main.go
CHANGED
|
@@ -33,6 +33,7 @@ func main() {
|
|
| 33 |
&models.Order{},
|
| 34 |
&models.OrderItem{},
|
| 35 |
&models.OTP{},
|
|
|
|
| 36 |
)
|
| 37 |
if err != nil {
|
| 38 |
log.Fatalf("Gagal melakukan Auto-Migration: %v", err)
|
|
@@ -41,6 +42,7 @@ func main() {
|
|
| 41 |
|
| 42 |
// 4. Seeding super_admin default jika belum ada
|
| 43 |
controllers.SeedDefaultAdmin()
|
|
|
|
| 44 |
|
| 45 |
// 5. Tripay integration (No SDK init needed, we use plain HTTP Client!)
|
| 46 |
|
|
|
|
| 33 |
&models.Order{},
|
| 34 |
&models.OrderItem{},
|
| 35 |
&models.OTP{},
|
| 36 |
+
&models.Setting{},
|
| 37 |
)
|
| 38 |
if err != nil {
|
| 39 |
log.Fatalf("Gagal melakukan Auto-Migration: %v", err)
|
|
|
|
| 42 |
|
| 43 |
// 4. Seeding super_admin default jika belum ada
|
| 44 |
controllers.SeedDefaultAdmin()
|
| 45 |
+
controllers.SeedDefaultSetting()
|
| 46 |
|
| 47 |
// 5. Tripay integration (No SDK init needed, we use plain HTTP Client!)
|
| 48 |
|
models/models.go
CHANGED
|
@@ -131,3 +131,12 @@ type OTP struct {
|
|
| 131 |
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
| 132 |
ExpiresAt time.Time `gorm:"not null" json:"expires_at"`
|
| 133 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
| 132 |
ExpiresAt time.Time `gorm:"not null" json:"expires_at"`
|
| 133 |
}
|
| 134 |
+
|
| 135 |
+
type Setting struct {
|
| 136 |
+
ID uint `gorm:"primaryKey" json:"id"`
|
| 137 |
+
TripayMerchantCode string `gorm:"type:varchar(100)" json:"tripay_merchant_code"`
|
| 138 |
+
TripayAPIKey string `gorm:"type:text" json:"tripay_api_key"`
|
| 139 |
+
TripayPrivateKey string `gorm:"type:text" json:"tripay_private_key"`
|
| 140 |
+
TripayProxyURL string `gorm:"type:text" json:"tripay_proxy_url"`
|
| 141 |
+
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
| 142 |
+
}
|
services/tripay.go
CHANGED
|
@@ -102,25 +102,23 @@ func CreateTripayTransaction(
|
|
| 102 |
customerPhone = "081234567890"
|
| 103 |
}
|
| 104 |
|
| 105 |
-
// Load
|
| 106 |
-
var
|
| 107 |
-
|
| 108 |
-
config.DB.First(&company, companyID)
|
| 109 |
-
}
|
| 110 |
|
| 111 |
-
mCode :=
|
| 112 |
if mCode == "" {
|
| 113 |
mCode = config.GlobalConfig.TripayMerchantCode
|
| 114 |
}
|
| 115 |
-
apiKey :=
|
| 116 |
if apiKey == "" {
|
| 117 |
apiKey = config.GlobalConfig.TripayAPIKey
|
| 118 |
}
|
| 119 |
-
privKey :=
|
| 120 |
if privKey == "" {
|
| 121 |
privKey = config.GlobalConfig.TripayPrivateKey
|
| 122 |
}
|
| 123 |
-
proxyURLStr :=
|
| 124 |
if proxyURLStr == "" {
|
| 125 |
proxyURLStr = config.GlobalConfig.TripayProxyURL
|
| 126 |
}
|
|
|
|
| 102 |
customerPhone = "081234567890"
|
| 103 |
}
|
| 104 |
|
| 105 |
+
// Load global Tripay settings dari database (ID = 1)
|
| 106 |
+
var setting models.Setting
|
| 107 |
+
config.DB.First(&setting, 1)
|
|
|
|
|
|
|
| 108 |
|
| 109 |
+
mCode := setting.TripayMerchantCode
|
| 110 |
if mCode == "" {
|
| 111 |
mCode = config.GlobalConfig.TripayMerchantCode
|
| 112 |
}
|
| 113 |
+
apiKey := setting.TripayAPIKey
|
| 114 |
if apiKey == "" {
|
| 115 |
apiKey = config.GlobalConfig.TripayAPIKey
|
| 116 |
}
|
| 117 |
+
privKey := setting.TripayPrivateKey
|
| 118 |
if privKey == "" {
|
| 119 |
privKey = config.GlobalConfig.TripayPrivateKey
|
| 120 |
}
|
| 121 |
+
proxyURLStr := setting.TripayProxyURL
|
| 122 |
if proxyURLStr == "" {
|
| 123 |
proxyURLStr = config.GlobalConfig.TripayProxyURL
|
| 124 |
}
|