Skip to content

Commit

Permalink
add snap example
Browse files Browse the repository at this point in the history
  • Loading branch information
adamnoto committed Jan 11, 2017
1 parent bdec835 commit ba426f2
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 5 deletions.
Binary file modified example/simplepay/main
Binary file not shown.
25 changes: 24 additions & 1 deletion example/simplepay/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

var midclient midtrans.Client
var coreGateway midtrans.CoreGateway
var snapGateway midtrans.SnapGateway

func main() {
setupMidtrans()
Expand All @@ -20,6 +21,20 @@ func main() {
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{})

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)

Expand All @@ -37,6 +52,10 @@ func setupMidtrans() {
coreGateway = midtrans.CoreGateway{
Client: midclient,
}

snapGateway = midtrans.SnapGateway{
Client: midclient,
}
}

func chargeDirect(w http.ResponseWriter, r *http.Request) {
Expand All @@ -46,11 +65,15 @@ func chargeDirect(w http.ResponseWriter, r *http.Request) {
TokenID: r.FormValue("card-token"),
},
TransactionDetails: midtrans.TransactionDetails{
OrderID: strconv.FormatInt(time.Now().UnixNano(), 10),
OrderID: generateOrderId(),
GrossAmt: 200000,
},
})

fmt.Println(chargeResp.ValMessages)
fmt.Println(chargeResp.StatusMessage)
}

func generateOrderId() string {
return strconv.FormatInt(time.Now().UnixNano(), 10)
}
12 changes: 9 additions & 3 deletions example/simplepay/template_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,22 @@ type templateHandler struct {
once sync.Once
filename string
templ *template.Template
data map[string]interface{}
dataInitializer func(t *templateHandler)
}

func (t *templateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
t.once.Do(func() {
t.templ = template.Must(template.ParseFiles(filepath.Join("templates", t.filename)))
})

data := map[string]interface{}{
"Host": r.Host,
if t.dataInitializer != nil {
t.dataInitializer(t)
} else {
t.data = make(map[string]interface{})
}

t.templ.Execute(w, data)
t.data["Host"] = r.Host

t.templ.Execute(w, t.data)
}
2 changes: 1 addition & 1 deletion example/simplepay/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<input type="hidden" id="card-token" name="card-token"/>

<div class='row checkout'>
<span><a href='#'>View Cart</a></span>
<span><a href='/snap'>via SNAP</a></span>
<span class='checkout-button'>Checkout</span>
</div>
</form>
Expand Down
55 changes: 55 additions & 0 deletions example/simplepay/templates/snap_index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<!doctype html>
<html>
<head>
<title>Simplepay</title>
<script src="https://code.jquery.com/jquery-2.2.4.min.js" type="text/javascript"></script>
<script src="https://api.sandbox.midtrans.com/v2/assets/js/midtrans.min.js" type="text/javascript"></script>
<script src="https://app.sandbox.midtrans.com/snap/snap.js"></script>
<link href="/assets/common.css" rel="stylesheet"/>
</head>
<body>
<div class='cart'>
<div class='popup'>
<div class='row header'>
<span>Items</span>
<span>Amount</span>
</div>

<div class='row items'>
<span>2</span>
<span>200000</span>
</div>

<input type="hidden" id="snap-token" name="snap-token" value="{{.Token}}"/>

<div class='row checkout'>
<span><a href='/snap'>via SNAP</a></span>
<span class='checkout-button'>Checkout</span>
</div>
</div>
</div>
</body>

<script type='text/javascript'>
$(document).ready(function() {
$(".checkout-button").on("click", function() {
var token = $("#snap-token").val();
console.log("Token", token);

snap.pay(token, {
onSuccess: function(res) {
alert("Payment accepted!");
},

onPending: function(res) {
alert("Payment pending", res);
},

onError: function(res) {
alert("Error", res);
}
});
});
});
</script>
</html>

0 comments on commit ba426f2

Please sign in to comment.