/
gateway.go
50 lines (39 loc) · 1.11 KB
/
gateway.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package midtrans
import (
"os"
"github.com/midtrans/midtrans-go"
"github.com/midtrans/midtrans-go/snap"
"github.com/imrenagi/go-payment/util/localconfig"
)
// NewGateway creates new midtrans payment gateway
func NewGateway(creds localconfig.APICredential) *Gateway {
snapClient := &snap.Client{
ServerKey: creds.SecretKey,
}
switch os.Getenv("ENVIRONMENT") {
case "prod":
snapClient.Env = midtrans.Production
snapClient.HttpClient = midtrans.GetHttpClient(midtrans.Production)
default:
snapClient.Env = midtrans.Sandbox
snapClient.HttpClient = midtrans.GetHttpClient(midtrans.Sandbox)
}
gateway := Gateway{
serverKey: creds.SecretKey,
SnapV2Gateway: snapClient,
}
return &gateway
}
// Gateway stores go-midtrans gateway and client
type Gateway struct {
serverKey string
SnapV2Gateway SnapGateway
}
// NotificationValidationKey returns midtrans server key used for validating
// midtransa transaction status
func (g Gateway) NotificationValidationKey() string {
return g.serverKey
}
type SnapGateway interface {
CreateTransaction(req *snap.Request) (*snap.Response, *midtrans.Error)
}