add order and payment
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
package mdtrns
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"furtuna-be/internal/common/logger"
|
||||
"furtuna-be/internal/entity"
|
||||
"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,
|
||||
}
|
||||
|
||||
snapReq := &midtrans.SnapReq{
|
||||
EnabledPayments: []midtrans.PaymentType{
|
||||
midtrans.SourceGopay,
|
||||
},
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user