Skip to content

Commit

Permalink
Change midtrans package import
Browse files Browse the repository at this point in the history
Change simplepay main.go and some tests on how they import midtrans pkg
  • Loading branch information
haritsfahreza committed Jun 28, 2018
1 parent feb9e89 commit b5881d5
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 108 deletions.
16 changes: 9 additions & 7 deletions client_test.go
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)
}
109 changes: 55 additions & 54 deletions example/simplepay/main.go
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)
}
35 changes: 18 additions & 17 deletions paysource_test.go
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)
}
61 changes: 31 additions & 30 deletions request_test.go
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"}}`)
}

0 comments on commit b5881d5

Please sign in to comment.