forked from veritrans/go-midtrans
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change simplepay main.go and some tests on how they import midtrans pkg
- Loading branch information
1 parent
feb9e89
commit b5881d5
Showing
4 changed files
with
113 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
package midtrans_test | ||
|
||
import ( | ||
"testing" | ||
"github.com/cheekybits/is" | ||
"midtrans" | ||
"testing" | ||
|
||
midtrans "github.com/veritrans/go-midtrans" | ||
|
||
"github.com/cheekybits/is" | ||
) | ||
|
||
func TestDefaultEnvironmentType(t *testing.T) { | ||
is := is.New(t) | ||
is := is.New(t) | ||
|
||
midclient := midtrans.NewClient() | ||
is.Equal(midtrans.Sandbox, midclient.ApiEnvType) | ||
} | ||
midclient := midtrans.NewClient() | ||
is.Equal(midtrans.Sandbox, midclient.ApiEnvType) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,80 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/saveav/go-midtrans" | ||
"flag" | ||
"fmt" | ||
"net/http" | ||
"log" | ||
"time" | ||
"strconv" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"strconv" | ||
"time" | ||
|
||
midtrans "github.com/veritrans/go-midtrans" | ||
) | ||
|
||
var midclient midtrans.Client | ||
var coreGateway midtrans.CoreGateway | ||
var snapGateway midtrans.SnapGateway | ||
|
||
func main() { | ||
setupMidtrans() | ||
var addr = flag.String("port", ":1234", "The address of the application") | ||
flag.Parse() | ||
fmt.Println("Server started on port: ", *addr) | ||
setupMidtrans() | ||
var addr = flag.String("port", ":1234", "The address of the application") | ||
flag.Parse() | ||
fmt.Println("Server started on port: ", *addr) | ||
|
||
http.Handle("/", &templateHandler{filename: "index.html"}) | ||
http.Handle("/snap", &templateHandler{ | ||
filename: "snap_index.html", | ||
dataInitializer: func (t *templateHandler) { | ||
snapResp, err := snapGateway.GetTokenQuick(generateOrderId(), 200000) | ||
t.data = make(map[string]interface{}) | ||
http.Handle("/", &templateHandler{filename: "index.html"}) | ||
http.Handle("/snap", &templateHandler{ | ||
filename: "snap_index.html", | ||
dataInitializer: func(t *templateHandler) { | ||
snapResp, err := snapGateway.GetTokenQuick(generateOrderId(), 200000) | ||
t.data = make(map[string]interface{}) | ||
|
||
if err != nil { | ||
log.Fatal("Error generating snap token: ", err) | ||
t.data["Token"] = "" | ||
} else { | ||
t.data["Token"] = snapResp.Token | ||
} | ||
}, | ||
}) | ||
http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets")))) | ||
http.HandleFunc("/chargeDirect", chargeDirect) | ||
if err != nil { | ||
log.Fatal("Error generating snap token: ", err) | ||
t.data["Token"] = "" | ||
} else { | ||
t.data["Token"] = snapResp.Token | ||
} | ||
}, | ||
}) | ||
http.Handle("/assets/", http.StripPrefix("/assets/", http.FileServer(http.Dir("assets")))) | ||
http.HandleFunc("/chargeDirect", chargeDirect) | ||
|
||
if err := http.ListenAndServe(*addr, nil); err != nil { | ||
log.Fatal("Failed starting server: ", err) | ||
} | ||
if err := http.ListenAndServe(*addr, nil); err != nil { | ||
log.Fatal("Failed starting server: ", err) | ||
} | ||
} | ||
|
||
func setupMidtrans() { | ||
midclient = midtrans.NewClient() | ||
midclient.ServerKey = "VT-server-7CVlR3AJ8Dpkez3k_TeGJQZU" | ||
midclient.ClientKey = "VT-client-IKktHiy3aRYHljsw" | ||
midclient.ApiEnvType = midtrans.Sandbox | ||
midclient = midtrans.NewClient() | ||
midclient.ServerKey = "VT-server-7CVlR3AJ8Dpkez3k_TeGJQZU" | ||
midclient.ClientKey = "VT-client-IKktHiy3aRYHljsw" | ||
midclient.ApiEnvType = midtrans.Sandbox | ||
|
||
coreGateway = midtrans.CoreGateway{ | ||
Client: midclient, | ||
} | ||
coreGateway = midtrans.CoreGateway{ | ||
Client: midclient, | ||
} | ||
|
||
snapGateway = midtrans.SnapGateway{ | ||
Client: midclient, | ||
} | ||
snapGateway = midtrans.SnapGateway{ | ||
Client: midclient, | ||
} | ||
} | ||
|
||
func chargeDirect(w http.ResponseWriter, r *http.Request) { | ||
chargeResp, _ := coreGateway.Charge(&midtrans.ChargeReq{ | ||
PaymentType: midtrans.SourceCreditCard, | ||
CreditCard: &midtrans.CreditCardDetail{ | ||
TokenID: r.FormValue("card-token"), | ||
}, | ||
TransactionDetails: midtrans.TransactionDetails{ | ||
OrderID: generateOrderId(), | ||
GrossAmt: 200000, | ||
}, | ||
}) | ||
chargeResp, _ := coreGateway.Charge(&midtrans.ChargeReq{ | ||
PaymentType: midtrans.SourceCreditCard, | ||
CreditCard: &midtrans.CreditCardDetail{ | ||
TokenID: r.FormValue("card-token"), | ||
}, | ||
TransactionDetails: midtrans.TransactionDetails{ | ||
OrderID: generateOrderId(), | ||
GrossAmt: 200000, | ||
}, | ||
}) | ||
|
||
fmt.Println(chargeResp.ValMessages) | ||
fmt.Println(chargeResp.StatusMessage) | ||
fmt.Println(chargeResp.ValMessages) | ||
fmt.Println(chargeResp.StatusMessage) | ||
} | ||
|
||
func generateOrderId() string { | ||
return strconv.FormatInt(time.Now().UnixNano(), 10) | ||
} | ||
return strconv.FormatInt(time.Now().UnixNano(), 10) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,24 @@ | ||
package midtrans_test | ||
|
||
import ( | ||
"github.com/cheekybits/is" | ||
"testing" | ||
"midtrans" | ||
"testing" | ||
|
||
"github.com/cheekybits/is" | ||
midtrans "github.com/veritrans/go-midtrans" | ||
) | ||
|
||
func TestPaymentType(t *testing.T) { | ||
is := is.New(t) | ||
is.Equal("credit_card", midtrans.SourceCreditCard) | ||
is.Equal("bank_transfer", midtrans.SourceBankTransfer) | ||
is.Equal("cimb_clicks", midtrans.SourceCimbClicks) | ||
is.Equal("mandiri_clickpay", midtrans.SourceMandiriClickpay) | ||
is.Equal("bri_epay", midtrans.SourceBriEpay) | ||
is.Equal("telkomsel_cash", midtrans.SourceTelkomselCash) | ||
is.Equal("xl_tunai", midtrans.SourceXlTunai) | ||
is.Equal("bbm_money", midtrans.SourceBbmMoney) | ||
is.Equal("echannel", midtrans.SourceEchannel) | ||
is.Equal("cstore", midtrans.SourceConvStore) | ||
is.Equal("bca_klikbca", midtrans.SourceKlikBca) | ||
is.Equal("bca_klikpay", midtrans.SourceBcaKlikpay) | ||
} | ||
is := is.New(t) | ||
is.Equal("credit_card", midtrans.SourceCreditCard) | ||
is.Equal("bank_transfer", midtrans.SourceBankTransfer) | ||
is.Equal("cimb_clicks", midtrans.SourceCimbClicks) | ||
is.Equal("mandiri_clickpay", midtrans.SourceMandiriClickpay) | ||
is.Equal("bri_epay", midtrans.SourceBriEpay) | ||
is.Equal("telkomsel_cash", midtrans.SourceTelkomselCash) | ||
is.Equal("xl_tunai", midtrans.SourceXlTunai) | ||
is.Equal("bbm_money", midtrans.SourceBbmMoney) | ||
is.Equal("echannel", midtrans.SourceEchannel) | ||
is.Equal("cstore", midtrans.SourceConvStore) | ||
is.Equal("bca_klikbca", midtrans.SourceKlikBca) | ||
is.Equal("bca_klikpay", midtrans.SourceBcaKlikpay) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,43 @@ | ||
package midtrans_test | ||
|
||
import ( | ||
"midtrans" | ||
"testing" | ||
"github.com/cheekybits/is" | ||
"encoding/json" | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/cheekybits/is" | ||
midtrans "github.com/veritrans/go-midtrans" | ||
) | ||
|
||
func TestCustomerFieldOmmittable(t *testing.T) { | ||
is := is.New(t) | ||
is := is.New(t) | ||
|
||
req := midtrans.ChargeReq{ | ||
PaymentType: midtrans.SourceCreditCard, | ||
} | ||
req := midtrans.ChargeReq{ | ||
PaymentType: midtrans.SourceCreditCard, | ||
} | ||
|
||
reqJSON, _ := json.Marshal(req) | ||
is.Equal(string(reqJSON), `{"payment_type":"credit_card","transaction_details":{"order_id":"","gross_amount":0}}`) | ||
reqJSON, _ := json.Marshal(req) | ||
is.Equal(string(reqJSON), `{"payment_type":"credit_card","transaction_details":{"order_id":"","gross_amount":0}}`) | ||
|
||
req.CustField1 = "f1" | ||
req.CustField2 = "f2" | ||
req.CustField3 = "f3" | ||
reqJSON, _ = json.Marshal(req) | ||
is.Equal(string(reqJSON), `{"payment_type":"credit_card","transaction_details":{"order_id":"","gross_amount":0},"custom_field1":"f1","custom_field2":"f2","custom_field3":"f3"}`) | ||
req.CustField1 = "f1" | ||
req.CustField2 = "f2" | ||
req.CustField3 = "f3" | ||
reqJSON, _ = json.Marshal(req) | ||
is.Equal(string(reqJSON), `{"payment_type":"credit_card","transaction_details":{"order_id":"","gross_amount":0},"custom_field1":"f1","custom_field2":"f2","custom_field3":"f3"}`) | ||
} | ||
|
||
func TestBankTransferMandiriBill(t *testing.T) { | ||
is := is.New(t) | ||
|
||
req := midtrans.ChargeReq{ | ||
PaymentType: midtrans.SourceBankTransfer, | ||
BankTransfer: &midtrans.BankTransferDetail{ | ||
MandiriBillBankTransferDetail: &midtrans.MandiriBillBankTransferDetail{ | ||
BillInfo1: "Silahkan transfer", | ||
BillInfo2: "Untuk pembelian pulsa", | ||
}, | ||
}, | ||
} | ||
|
||
reqJSON, _ := json.Marshal(req) | ||
is.Equal(string(reqJSON), `{"payment_type":"bank_transfer","transaction_details":{"order_id":"","gross_amount":0},"bank_transfer":{"bill_info1":"Silahkan transfer","bill_info2":"Untuk pembelian pulsa"}}`) | ||
} | ||
is := is.New(t) | ||
|
||
req := midtrans.ChargeReq{ | ||
PaymentType: midtrans.SourceBankTransfer, | ||
BankTransfer: &midtrans.BankTransferDetail{ | ||
MandiriBillBankTransferDetail: &midtrans.MandiriBillBankTransferDetail{ | ||
BillInfo1: "Silahkan transfer", | ||
BillInfo2: "Untuk pembelian pulsa", | ||
}, | ||
}, | ||
} | ||
|
||
reqJSON, _ := json.Marshal(req) | ||
is.Equal(string(reqJSON), `{"payment_type":"bank_transfer","transaction_details":{"order_id":"","gross_amount":0},"bank_transfer":{"bill_info1":"Silahkan transfer","bill_info2":"Untuk pembelian pulsa"}}`) | ||
} |