Mhamdans17 commited on
Commit
6fd3b19
·
1 Parent(s): 9ae6db1

feat: support dynamic tenant Tripay credentials and proxy via database

Browse files
controllers/company_controller.go CHANGED
@@ -42,6 +42,10 @@ type CompanyCreateRequest struct {
42
  Phone string `json:"phone"`
43
  LogoURL string `json:"logo_url"`
44
  FeePercentage float64 `json:"fee_percentage"`
 
 
 
 
45
  }
46
 
47
  type BranchCreateRequest struct {
@@ -243,6 +247,10 @@ func CreateCompany(c *gin.Context) {
243
  LogoURL: req.LogoURL,
244
  IsActive: true,
245
  FeePercentage: req.FeePercentage,
 
 
 
 
246
  }
247
 
248
  if err := config.DB.Create(&newComp).Error; err != nil {
@@ -290,6 +298,10 @@ func UpdateCompany(c *gin.Context) {
290
  company.Phone = req.Phone
291
  company.LogoURL = req.LogoURL
292
  company.FeePercentage = req.FeePercentage
 
 
 
 
293
 
294
  if err := config.DB.Save(&company).Error; err != nil {
295
  c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal meng-update perusahaan"})
 
42
  Phone string `json:"phone"`
43
  LogoURL string `json:"logo_url"`
44
  FeePercentage float64 `json:"fee_percentage"`
45
+ TripayMerchantCode string `json:"tripay_merchant_code"`
46
+ TripayAPIKey string `json:"tripay_api_key"`
47
+ TripayPrivateKey string `json:"tripay_private_key"`
48
+ TripayProxyURL string `json:"tripay_proxy_url"`
49
  }
50
 
51
  type BranchCreateRequest struct {
 
247
  LogoURL: req.LogoURL,
248
  IsActive: true,
249
  FeePercentage: req.FeePercentage,
250
+ TripayMerchantCode: req.TripayMerchantCode,
251
+ TripayAPIKey: req.TripayAPIKey,
252
+ TripayPrivateKey: req.TripayPrivateKey,
253
+ TripayProxyURL: req.TripayProxyURL,
254
  }
255
 
256
  if err := config.DB.Create(&newComp).Error; err != nil {
 
298
  company.Phone = req.Phone
299
  company.LogoURL = req.LogoURL
300
  company.FeePercentage = req.FeePercentage
301
+ company.TripayMerchantCode = req.TripayMerchantCode
302
+ company.TripayAPIKey = req.TripayAPIKey
303
+ company.TripayPrivateKey = req.TripayPrivateKey
304
+ company.TripayProxyURL = req.TripayProxyURL
305
 
306
  if err := config.DB.Save(&company).Error; err != nil {
307
  c.JSON(http.StatusInternalServerError, gin.H{"detail": "Gagal meng-update perusahaan"})
controllers/order_controller.go CHANGED
@@ -487,6 +487,7 @@ func PayTripay(c *gin.Context) {
487
  req.CustomerEmail,
488
  req.CustomerPhone,
489
  tripayItems,
 
490
  )
491
  if err != nil {
492
  c.JSON(http.StatusBadRequest, gin.H{"detail": fmt.Sprintf("Gagal membuat transaksi Tripay: %v", err)})
 
487
  req.CustomerEmail,
488
  req.CustomerPhone,
489
  tripayItems,
490
+ order.CompanyID,
491
  )
492
  if err != nil {
493
  c.JSON(http.StatusBadRequest, gin.H{"detail": fmt.Sprintf("Gagal membuat transaksi Tripay: %v", err)})
models/models.go CHANGED
@@ -16,6 +16,10 @@ type Company struct {
16
  IsActive bool `json:"is_active"`
17
  FeePercentage float64 `gorm:"type:float;default:0.0;not null" json:"fee_percentage"`
18
  ApprovedAt *time.Time `json:"approved_at"`
 
 
 
 
19
  CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
20
  Branches []Branch `gorm:"foreignKey:CompanyID;constraint:OnDelete:CASCADE" json:"-"`
21
  Users []User `gorm:"foreignKey:CompanyID;constraint:OnDelete:CASCADE" json:"-"`
 
16
  IsActive bool `json:"is_active"`
17
  FeePercentage float64 `gorm:"type:float;default:0.0;not null" json:"fee_percentage"`
18
  ApprovedAt *time.Time `json:"approved_at"`
19
+ TripayMerchantCode string `gorm:"type:varchar(100)" json:"tripay_merchant_code"`
20
+ TripayAPIKey string `gorm:"type:text" json:"tripay_api_key"`
21
+ TripayPrivateKey string `gorm:"type:text" json:"tripay_private_key"`
22
+ TripayProxyURL string `gorm:"type:text" json:"tripay_proxy_url"`
23
  CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
24
  Branches []Branch `gorm:"foreignKey:CompanyID;constraint:OnDelete:CASCADE" json:"-"`
25
  Users []User `gorm:"foreignKey:CompanyID;constraint:OnDelete:CASCADE" json:"-"`
services/tripay.go CHANGED
@@ -14,6 +14,7 @@ import (
14
  "time"
15
 
16
  "service-warungpos-go/config"
 
17
  )
18
 
19
  type TripayItem struct {
@@ -58,9 +59,15 @@ func getTripayBaseURL() string {
58
  }
59
 
60
  // CalculateSignature menghitung signature HMAC-SHA256 untuk request transaksi ke Tripay
61
- func CalculateSignature(merchantRef string, amount int) string {
62
- merchantCode := config.GlobalConfig.TripayMerchantCode
63
- privateKey := config.GlobalConfig.TripayPrivateKey
 
 
 
 
 
 
64
 
65
  // Format: merchant_code + merchant_ref + amount
66
  data := merchantCode + merchantRef + fmt.Sprintf("%d", amount)
@@ -79,6 +86,7 @@ func CreateTripayTransaction(
79
  customerEmail string,
80
  customerPhone string,
81
  items []TripayItem,
 
82
  ) (*TripayCreateResponse, error) {
83
  // Fallback input default
84
  if paymentMethod == "" {
@@ -94,8 +102,31 @@ func CreateTripayTransaction(
94
  customerPhone = "081234567890"
95
  }
96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  // Hitung signature
98
- signature := CalculateSignature(orderNumber, amount)
99
 
100
  // Persiapkan payload request
101
  payload := TripayCreateRequest{
@@ -115,14 +146,14 @@ func CreateTripayTransaction(
115
  }
116
 
117
  apiURL := getTripayBaseURL() + "/transaction/create"
118
- log.Printf("[TRIPAY] Mengirim request tagihan ke: %s", apiURL)
119
 
120
  transport := &http.Transport{}
121
- if config.GlobalConfig.TripayProxyURL != "" {
122
- proxyURL, err := url.Parse(config.GlobalConfig.TripayProxyURL)
123
  if err == nil {
124
  transport.Proxy = http.ProxyURL(proxyURL)
125
- log.Printf("[TRIPAY] Menggunakan proxy outbound: %s", config.GlobalConfig.TripayProxyURL)
126
  } else {
127
  log.Printf("[TRIPAY] Gagal memproses format proxy URL: %v", err)
128
  }
@@ -139,7 +170,7 @@ func CreateTripayTransaction(
139
 
140
  // Atur headers
141
  req.Header.Set("Content-Type", "application/json")
142
- req.Header.Set("Authorization", "Bearer "+config.GlobalConfig.TripayAPIKey)
143
 
144
  resp, err := client.Do(req)
145
  if err != nil {
 
14
  "time"
15
 
16
  "service-warungpos-go/config"
17
+ "service-warungpos-go/models"
18
  )
19
 
20
  type TripayItem struct {
 
59
  }
60
 
61
  // CalculateSignature menghitung signature HMAC-SHA256 untuk request transaksi ke Tripay
62
+ func CalculateSignature(merchantRef string, amount int, customMerchantCode string, customPrivateKey string) string {
63
+ merchantCode := customMerchantCode
64
+ if merchantCode == "" {
65
+ merchantCode = config.GlobalConfig.TripayMerchantCode
66
+ }
67
+ privateKey := customPrivateKey
68
+ if privateKey == "" {
69
+ privateKey = config.GlobalConfig.TripayPrivateKey
70
+ }
71
 
72
  // Format: merchant_code + merchant_ref + amount
73
  data := merchantCode + merchantRef + fmt.Sprintf("%d", amount)
 
86
  customerEmail string,
87
  customerPhone string,
88
  items []TripayItem,
89
+ companyID uint,
90
  ) (*TripayCreateResponse, error) {
91
  // Fallback input default
92
  if paymentMethod == "" {
 
102
  customerPhone = "081234567890"
103
  }
104
 
105
+ // Load company-specific settings jika ada
106
+ var company models.Company
107
+ if companyID > 0 {
108
+ config.DB.First(&company, companyID)
109
+ }
110
+
111
+ mCode := company.TripayMerchantCode
112
+ if mCode == "" {
113
+ mCode = config.GlobalConfig.TripayMerchantCode
114
+ }
115
+ apiKey := company.TripayAPIKey
116
+ if apiKey == "" {
117
+ apiKey = config.GlobalConfig.TripayAPIKey
118
+ }
119
+ privKey := company.TripayPrivateKey
120
+ if privKey == "" {
121
+ privKey = config.GlobalConfig.TripayPrivateKey
122
+ }
123
+ proxyURLStr := company.TripayProxyURL
124
+ if proxyURLStr == "" {
125
+ proxyURLStr = config.GlobalConfig.TripayProxyURL
126
+ }
127
+
128
  // Hitung signature
129
+ signature := CalculateSignature(orderNumber, amount, mCode, privKey)
130
 
131
  // Persiapkan payload request
132
  payload := TripayCreateRequest{
 
146
  }
147
 
148
  apiURL := getTripayBaseURL() + "/transaction/create"
149
+ log.Printf("[TRIPAY] Mengirim request tagihan ke: %s (CompanyID=%d)", apiURL, companyID)
150
 
151
  transport := &http.Transport{}
152
+ if proxyURLStr != "" {
153
+ proxyURL, err := url.Parse(proxyURLStr)
154
  if err == nil {
155
  transport.Proxy = http.ProxyURL(proxyURL)
156
+ log.Printf("[TRIPAY] Menggunakan proxy outbound: %s", proxyURLStr)
157
  } else {
158
  log.Printf("[TRIPAY] Gagal memproses format proxy URL: %v", err)
159
  }
 
170
 
171
  // Atur headers
172
  req.Header.Set("Content-Type", "application/json")
173
+ req.Header.Set("Authorization", "Bearer "+apiKey)
174
 
175
  resp, err := client.Do(req)
176
  if err != nil {