Go SDK

bank.thehsi.cloud/hbank/hbank-go · v1.2.0 · Go 1.21+

Download .zip
README.md

hbank-go

Client for the HBank merchant API. Go 1.21+, standard library only.

go get bank.thehsi.cloud/hbank/hbank-go@latest

The module is served from HBank's own git repository (https://bank.thehsi.cloud/hbank/hbank-go.git), tagged v<Version>. Without network access, use the downloaded folder instead:

// go.mod of your service
require bank.thehsi.cloud/hbank/hbank-go v1.1.0
replace bank.thehsi.cloud/hbank/hbank-go => ./hbank-go
import hbank "bank.thehsi.cloud/hbank/hbank-go"

bank := hbank.New(os.Getenv("HBANK_API_KEY")) // hbank.WithBaseURL("http://localhost:8000") for local

tid, err := bank.CreatePayment(ctx, hbank.PaymentRequest{
	Amount: "24.90", Currency: "EUR", Description: "Order 1042",
	CallbackURL: "https://shop.example.com/hbank/callback",
})
checkout := bank.CheckoutURL(tid)
qr, err := bank.CreatePaymentCode(ctx, tid)

p, err := bank.GetPayment(ctx, tid)
if err == nil && p.Completed() {
	ship(order)
}

res, err := bank.Refund(ctx, tid, "10.00", "Returned one item") // "" amount = everything left

sub, err := bank.CreateSubscription(ctx, hbank.SubscriptionRequest{Amount: "9.99", Currency: "EUR", Interval: "month"})
_, err = bank.CancelSubscription(ctx, sub.ID)

// Several subscriptions in one checkout: offers you made and new ones, one payment starts them all
both, err := bank.CreateSubscriptionsCheckout(ctx, hbank.SubscriptionsCheckoutRequest{Subscriptions: []hbank.SubscriptionCheckoutItem{
	hbank.ExistingSubscription(sub.ID),
	hbank.NewSubscription(hbank.SubscriptionRequest{Amount: "2.50", Currency: "EUR", Interval: "week"}),
}})

Amounts are decimal strings in, json.Number out (p.Amount.String() == "24.90"), so nothing goes through float64.

Errors

var e *hbank.Error
if errors.As(err, &e) {
	log.Println(e.Code, e.Status, e.Message) // not_refundable 409 …
}
if hbank.IsCode(err, "connection_failed") { /* may or may not have reached HBank */ }

Pay button endpoint + callback

CallbackHandler is a plain http.Handler:

mux.HandleFunc("GET /checkout/hbank", func(w http.ResponseWriter, r *http.Request) {
	cart := carts.Get(r.URL.Query().Get("cart"))
	tid, err := bank.CreatePayment(r.Context(), hbank.PaymentRequest{Amount: cart.Total, Currency: "EUR",
		CallbackURL: "https://shop.example.com/hbank/callback"})
	if err != nil {
		http.Error(w, "payment unavailable", http.StatusBadGateway)
		return
	}
	http.Redirect(w, r, bank.CheckoutURL(tid), http.StatusFound)
})

mux.Handle("POST /hbank/callback", hbank.CallbackHandler(
	func(r *http.Request, cb *hbank.Callback) (hbank.CallbackAnswer, error) {
		if orders.Matches(r.Context(), cb.ID, cb.Amount.String()) {
			return hbank.Approve(), nil
		}
		return hbank.Refuse("This order is no longer available"), nil
	}))
  • chi: r.Method("POST", "/hbank/callback", hbank.CallbackHandler(decide))
  • Gin: r.POST("/hbank/callback", gin.WrapH(hbank.CallbackHandler(decide)))
  • Echo: e.POST("/hbank/callback", echo.WrapHandler(hbank.CallbackHandler(decide)))

An error from the decider refuses the payment. Callbacks are signed: use hbank.SignedCallbackHandler(secret, decide) with your signing secret (business settings → Callback URLs), or hbank.VerifySignature(secret, r.Header.Get("HBank-Signature"), body, time.Now()) on the raw body. Check the callback against your own order too.

Tests

go test ./...     # self-contained (httptest)