127 lines
3.0 KiB
Go
127 lines
3.0 KiB
Go
package mdtrns
|
|
|
|
import (
|
|
"enaklo-pos-be/internal/common/logger"
|
|
"enaklo-pos-be/internal/entity"
|
|
"fmt"
|
|
"log"
|
|
"strconv"
|
|
|
|
"github.com/veritrans/go-midtrans"
|
|
)
|
|
|
|
type MidtransConfig interface {
|
|
MidtransServerKey() string
|
|
MidtransClientKey() string
|
|
MidtranEnvType() int
|
|
}
|
|
|
|
type ClientService struct {
|
|
client midtrans.Client
|
|
midtransConfig MidtransConfig
|
|
}
|
|
|
|
func New(midtransConfig MidtransConfig) *ClientService {
|
|
midclient := midtrans.NewClient()
|
|
midclient.ServerKey = midtransConfig.MidtransServerKey()
|
|
midclient.ClientKey = midtransConfig.MidtransClientKey()
|
|
midclient.APIEnvType = midtrans.EnvironmentType(midtransConfig.MidtranEnvType())
|
|
|
|
return &ClientService{
|
|
client: midclient,
|
|
midtransConfig: midtransConfig,
|
|
}
|
|
}
|
|
|
|
func (c *ClientService) CreatePayment(order entity.MidtransRequest) (*entity.MidtransResponse, error) {
|
|
var snapGateway midtrans.SnapGateway
|
|
snapGateway = midtrans.SnapGateway{
|
|
Client: c.client,
|
|
}
|
|
|
|
var paymentMethod []midtrans.PaymentType
|
|
|
|
if order.PaymentMethod == "QRIS" {
|
|
paymentMethod = []midtrans.PaymentType{midtrans.SourceGopay}
|
|
}
|
|
|
|
snapReq := &midtrans.SnapReq{
|
|
EnabledPayments: paymentMethod,
|
|
TransactionDetails: midtrans.TransactionDetails{
|
|
OrderID: order.PaymentReferenceID,
|
|
GrossAmt: order.TotalAmount,
|
|
},
|
|
}
|
|
|
|
log.Println("GetToken:")
|
|
snapTokenResp, err := snapGateway.GetToken(snapReq)
|
|
|
|
if err != nil {
|
|
logger.GetLogger().Error(fmt.Sprintf("error when create midtrans payment %v", err))
|
|
return nil, err
|
|
}
|
|
|
|
return &entity.MidtransResponse{
|
|
Token: snapTokenResp.Token,
|
|
RedirectURL: snapTokenResp.RedirectURL,
|
|
}, nil
|
|
|
|
}
|
|
|
|
func (c ClientService) getProductItems(products []entity.OrderItem) []midtrans.ItemDetail {
|
|
var items []midtrans.ItemDetail
|
|
|
|
for _, product := range products {
|
|
item := midtrans.ItemDetail{
|
|
ID: strconv.FormatInt(product.ID, 10),
|
|
Name: product.ItemType,
|
|
Qty: int32(product.Quantity),
|
|
Price: int64(product.Price),
|
|
}
|
|
items = append(items, item)
|
|
}
|
|
|
|
return items
|
|
}
|
|
|
|
func (c *ClientService) CreateQrisPayment(order entity.MidtransRequest) (*entity.MidtransQrisResponse, error) {
|
|
coreGateway := midtrans.CoreGateway{
|
|
Client: c.client,
|
|
}
|
|
|
|
req := &midtrans.ChargeReq{
|
|
PaymentType: midtrans.SourceGopay,
|
|
TransactionDetails: midtrans.TransactionDetails{
|
|
OrderID: order.PaymentReferenceID,
|
|
GrossAmt: order.TotalAmount,
|
|
},
|
|
}
|
|
|
|
// Request charge and retrieve response
|
|
resp, err := coreGateway.Charge(req)
|
|
if err != nil {
|
|
logger.GetLogger().Error(fmt.Sprintf("error when creating QRIS payment: %v", err))
|
|
return nil, err
|
|
}
|
|
|
|
// Extract QR code URL from response actions
|
|
var qrCodeURL string
|
|
for _, action := range resp.Actions {
|
|
if action.Name == "generate-qr-code" {
|
|
qrCodeURL = action.URL
|
|
break
|
|
}
|
|
}
|
|
|
|
if qrCodeURL == "" {
|
|
logger.GetLogger().Error("error: QR code URL not provided in response")
|
|
return nil, fmt.Errorf("QR code URL not provided in response")
|
|
}
|
|
|
|
return &entity.MidtransQrisResponse{
|
|
QrCodeUrl: qrCodeURL,
|
|
OrderID: order.PaymentReferenceID,
|
|
Amount: order.TotalAmount,
|
|
}, nil
|
|
}
|