-
Notifications
You must be signed in to change notification settings - Fork 25
/
sample.go
132 lines (111 loc) · 3.52 KB
/
sample.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"context"
"fmt"
"github.com/midtrans/midtrans-go"
"github.com/midtrans/midtrans-go/example"
"github.com/midtrans/midtrans-go/snap"
)
var s snap.Client
func setupGlobalMidtransConfig() {
midtrans.ServerKey = example.SandboxServerKey1
midtrans.Environment = midtrans.Sandbox
// Optional : here is how if you want to set append payment notification globally
midtrans.SetPaymentAppendNotification("https://example.com/append")
// Optional : here is how if you want to set override payment notification globally
midtrans.SetPaymentOverrideNotification("https://example.com/override")
//// remove the comment bellow, in cases you need to change the default for Log Level
// midtrans.DefaultLoggerLevel = &midtrans.LoggerImplementation{
// LogLevel: midtrans.LogInfo,
// }
}
func initializeSnapClient() {
s.New(example.SandboxServerKey1, midtrans.Sandbox)
}
func createTransactionWithGlobalConfig() {
res, err := snap.CreateTransactionWithMap(example.SnapParamWithMap())
if err != nil {
fmt.Println("Snap Request Error", err.GetMessage())
}
fmt.Println("Snap response", res)
}
func createTransaction() {
// Optional : here is how if you want to set append payment notification for this request
s.Options.SetPaymentAppendNotification("https://example.com/append")
// Optional : here is how if you want to set override payment notification for this request
s.Options.SetPaymentOverrideNotification("https://example.com/override")
// Send request to Midtrans Snap API
resp, err := s.CreateTransaction(GenerateSnapReq())
if err != nil {
fmt.Println("Error :", err.GetMessage())
}
fmt.Println("Response : ", resp)
}
func createTokenTransactionWithGateway() {
s.Options.SetPaymentOverrideNotification("https://example.com/url2")
resp, err := s.CreateTransactionToken(GenerateSnapReq())
if err != nil {
fmt.Println("Error :", err.GetMessage())
}
fmt.Println("Response : ", resp)
}
func createUrlTransactionWithGateway() {
s.Options.SetContext(context.Background())
resp, err := s.CreateTransactionUrl(GenerateSnapReq())
if err != nil {
fmt.Println("Error :", err.GetMessage())
}
fmt.Println("Response : ", resp)
}
func main() {
fmt.Println("================ Request with global config ================")
setupGlobalMidtransConfig()
createTransactionWithGlobalConfig()
fmt.Println("================ Request with Snap Client ================")
initializeSnapClient()
createTransaction()
fmt.Println("================ Request Snap token ================")
createTokenTransactionWithGateway()
fmt.Println("================ Request Snap URL ================")
createUrlTransactionWithGateway()
}
func GenerateSnapReq() *snap.Request {
// Initiate Customer address
custAddress := &midtrans.CustomerAddress{
FName: "John",
LName: "Doe",
Phone: "081234567890",
Address: "Baker Street 97th",
City: "Jakarta",
Postcode: "16000",
CountryCode: "IDN",
}
// Initiate Snap Request
snapReq := &snap.Request{
TransactionDetails: midtrans.TransactionDetails{
OrderID: "MID-GO-ID-" + example.Random(),
GrossAmt: 200000,
},
CreditCard: &snap.CreditCardDetails{
Secure: true,
},
CustomerDetail: &midtrans.CustomerDetails{
FName: "John",
LName: "Doe",
Email: "[email protected]",
Phone: "081234567890",
BillAddr: custAddress,
ShipAddr: custAddress,
},
EnabledPayments: snap.AllSnapPaymentType,
Items: &[]midtrans.ItemDetails{
{
ID: "ITEM1",
Price: 200000,
Qty: 1,
Name: "Someitem",
},
},
}
return snapReq
}