smepay

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

smepay-go

Official Go SDK for the SMEPay External API. Accept UPI payments in three calls; login, tokens and errors are handled for you.

Go 1.21+ · standard library only · safe for concurrent use.

go get github.com/Typof-Technologies-Pvt-Ltd/smepay-go

Usage

import smepay "github.com/Typof-Technologies-Pvt-Ltd/smepay-go"

client, err := smepay.New(smepay.Config{
	ClientID:     os.Getenv("SMEPAY_CLIENT_ID"), // from the SMEPay dashboard
	ClientSecret: os.Getenv("SMEPAY_CLIENT_SECRET"),
	Environment:  smepay.Test, // smepay.Test (default) or smepay.Live
})
if err != nil { /* invalid_config */ }

// 1. Create an order
order, err := client.Payment.Create(ctx, smepay.CreateParams{
	Amount:          "100.50",   // string, in rupees
	OrderID:         "ORD-1001", // your reference, unique per order
	CallbackURL:     "https://yoursite.com/smepay/callback",
	CustomerDetails: smepay.CustomerDetails{Name: "Asha", Mobile: "9876543210"}, // email or mobile required
})

// 2. Start the payment: show payment.QRCode (base64 PNG) or payment.Intents (UPI app links)
payment, err := client.Payment.Initiate(ctx, smepay.InitiateParams{Slug: order.OrderSlug})

// 3. Check the status (or use OrderID: order.OrderID)
status, err := client.Payment.GetStatus(ctx, smepay.GetStatusParams{ExternalReferenceID: "ORD-1001"})
if status.PaymentStatus == smepay.StatusSuccess { /* paid; status.UTR has the bank reference */ }

Create one Client and reuse it. Every call takes a context.Context; each request waits at most 60 seconds.

Method Returns
Payment.Create OrderSlug, OrderID (SMEPay's), RefID (yours), PaymentURL, PaymentStatus
Payment.Initiate PaymentLink, QRCode, Intents, ExpiresAt, PaymentStatus
Payment.GetStatus PaymentStatus, Amount, CreatedAt, ProcessedAt (nil until processed), UTR

Initiate can be called again if a payment failed or expired. Alternatively, skip Initiate and redirect the customer to PaymentURL (the hosted page); use one or the other per order.

Statuses (smepay.Status… constants): CREATED → INITIATED / PENDING → SUCCESS or FAILED / EXPIRED; also SETTLED, REFUNDED, REFUND_PENDING. Test-mode businesses get TEST_SUCCESS, TEST_FAILED, TEST_PENDING.

Errors

Every failure is a *smepay.Error with Code, Message, StatusCode (0 if no reply) and Raw (API reply).

var e *smepay.Error
if errors.As(err, &e) && e.Code == "duplicate_order_id" { /* ... */ }
Code Meaning
invalid_config Missing ClientID / ClientSecret, or bad Environment
invalid_client_id, invalid_credentials Wrong credentials (the Client Secret changes when you go live)
invalid_endpoint Environment doesn't match your business's mode (see the Test/Live badge on API Credentials)
ip_not_whitelisted Live: your server IP isn't whitelisted (ask SMEPay support)
api_not_enabled External API not enabled for your business (contact SMEPay)
insufficient_wallet_balance, wallet_validation_failed Wallet balance too low (live needs ≥ ₹10)
invalid_request, validation_error Missing or invalid field
amount_below_minimum, amount_exceeds_maximum Amount outside your allowed range
duplicate_order_id OrderID already used
transaction_not_found No such order for your business
payment_already_processed Initiate on an order already paid / in progress
network_error, timeout, invalid_response Couldn't reach SMEPay / no reply in time / unreadable reply

Calls are never retried automatically, so a failed Create is never sent twice. A cancelled context gives network_error with errors.Is(err, context.Canceled) true.

Test vs live

smepay.Test smepay.Live
Server staging.smepay.in extranet.smepay.in
Money / fees None Real; fees from your SMEPay wallet
Requirements — Server IP whitelisted, wallet ≥ ₹10

Webhooks

When a payment finishes, SMEPay POSTs JSON to your CallbackURL: ref_id (your order ID), status, transaction_id, amount (string, e.g. "100.00"), created_at / processed_at (ISO-8601) and utr (when available). Reply with any 2xx quickly; other replies are retried, so handle duplicates. Confirm with Payment.GetStatus before marking an order paid.

Security

Use the SDK only on your server. Keep ClientSecret in an environment variable, never in code. The SDK never logs your secret or token, and printing a Client doesn't show them.

Example

examples/full-flow: put SMEPAY_CLIENT_ID / SMEPAY_CLIENT_SECRET in .env, then set -a; source .env; set +a; go run ./examples/full-flow.

License

MIT

Documentation

Overview

Package smepay is the official Go SDK for the SMEPay External API.

It wraps the three payment calls (create, initiate, get status) and handles authentication, token reuse and errors for you.

client, err := smepay.New(smepay.Config{
	ClientID:     os.Getenv("SMEPAY_CLIENT_ID"),
	ClientSecret: os.Getenv("SMEPAY_CLIENT_SECRET"),
	Environment:  smepay.Test, // or smepay.Live
})

Use the SDK only on your server; never expose the client secret.

Index

Constants

View Source
const Version = "0.2.0"

Version is the SDK version, sent in the User-Agent header. Keep in sync with the git release tag (vX.Y.Z).

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// Payment holds the payment calls: Create → Initiate → GetStatus.
	Payment *PaymentService
	// contains filtered or unexported fields
}

Client calls the SMEPay External API. It is safe for concurrent use; create one and reuse it.

func New

func New(cfg Config) (*Client, error)

New creates a Client. Environment defaults to Test. A missing ClientID/ClientSecret or unknown Environment returns *Error with code "invalid_config".

func (*Client) Environment

func (c *Client) Environment() Environment

Environment reports which SMEPay environment the client uses.

func (*Client) GoString

func (c *Client) GoString() string

func (*Client) String

func (c *Client) String() string

String and GoString keep credentials out of logs and fmt output.

type Config

type Config struct {
	ClientID     string
	ClientSecret string // keep in an environment variable, never in code
	Environment  Environment
}

Config holds the credentials from the SMEPay dashboard.

type CreateParams

type CreateParams struct {
	Amount          string          `json:"amount"`       // in rupees, as a string, e.g. "100.50"
	OrderID         string          `json:"order_id"`     // your own reference; unique for your business
	CallbackURL     string          `json:"callback_url"` // receives the payment result webhook
	CustomerDetails CustomerDetails `json:"customer_details"`
}

CreateParams for Payment.Create (POST /api/external/order/create).

type CreateResponse

type CreateResponse struct {
	Status          bool            `json:"status"`
	OrderSlug       string          `json:"order_slug"` // pass to Payment.Initiate
	OrderID         string          `json:"order_id"`   // SMEPay's order ID
	Amount          float64         `json:"amount"`
	PaymentStatus   PaymentStatus   `json:"payment_status"`
	PaymentURL      string          `json:"payment_url"` // hosted SMEPay payment page
	RefID           string          `json:"ref_id"`      // your OrderID, echoed back
	CustomerDetails CustomerDetails `json:"customer_details"`
	Message         string          `json:"message"`
	CallbackURL     string          `json:"callback_url,omitempty"`
}

CreateResponse from Payment.Create.

type CustomerDetails

type CustomerDetails struct {
	Name   string `json:"name,omitempty"`
	Email  string `json:"email,omitempty"`
	Mobile string `json:"mobile,omitempty"`
}

CustomerDetails of the payer; at least Email or Mobile is required.

type Environment

type Environment string

Environment selects the SMEPay server. It must match your business's mode on SMEPay.

const (
	// Test uses staging.smepay.in: no real money, no fees.
	Test Environment = "test"
	// Live uses extranet.smepay.in: real payments.
	Live Environment = "live"
)

type Error

type Error struct {
	Code       string
	Message    string
	StatusCode int
	Raw        []byte
	// contains filtered or unexported fields
}

Error is returned for every failed SDK call. Check it with errors.As:

var e *smepay.Error
if errors.As(err, &e) && e.Code == "duplicate_order_id" { ... }

API errors: Code is the API's "error" field, StatusCode the HTTP status and Raw the API reply body. SDK-side errors use the codes "invalid_config", "network_error", "timeout" and "invalid_response"; StatusCode is 0 when no reply was received.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns the underlying cause (e.g. a network or context error), if any.

type GetStatusParams

type GetStatusParams struct {
	OrderID             string `json:"order_id,omitempty"`
	ExternalReferenceID string `json:"external_reference_id,omitempty"`
	CallbackURL         string `json:"callback_url,omitempty"` // saved only if the order has none yet
}

GetStatusParams for Payment.GetStatus (POST /api/external/order/status). Set OrderID (SMEPay's) or ExternalReferenceID (yours); ExternalReferenceID is used first if both are set.

type GetStatusResponse

type GetStatusResponse struct {
	Status              bool          `json:"status"`
	ExternalReferenceID string        `json:"external_reference_id"` // your OrderID
	OrderID             string        `json:"order_id"`              // SMEPay's order ID
	PaymentStatus       PaymentStatus `json:"payment_status"`
	Amount              float64       `json:"amount"`
	CreatedAt           int64         `json:"created_at"`    // Unix seconds
	ProcessedAt         *int64        `json:"processed_at"`  // Unix seconds; nil until processed
	UTR                 string        `json:"utr,omitempty"` // bank reference, when available
}

GetStatusResponse from Payment.GetStatus.

type InitiateParams

type InitiateParams struct {
	Slug string `json:"slug"` // OrderSlug from Payment.Create
}

InitiateParams for Payment.Initiate (POST /api/external/order/initiate).

type InitiateResponse

type InitiateResponse struct {
	Status              bool              `json:"status"`
	OrderID             string            `json:"order_id"`              // SMEPay's order ID
	ExternalReferenceID string            `json:"external_reference_id"` // your OrderID
	PaymentLink         string            `json:"payment_link"`          // UPI payment link
	TransactionID       string            `json:"transaction_id"`
	QRCode              string            `json:"qr_code"` // base64-encoded PNG
	PaymentStatus       PaymentStatus     `json:"payment_status"`
	ExpiresAt           int64             `json:"expires_at"` // Unix seconds
	Intents             map[string]string `json:"intents"`    // UPI app key -> app payment link
	Message             string            `json:"message"`
}

InitiateResponse from Payment.Initiate.

type PaymentService

type PaymentService struct {
	// contains filtered or unexported fields
}

PaymentService groups the payment calls: Create → Initiate → GetStatus.

func (*PaymentService) Create

func (s *PaymentService) Create(ctx context.Context, params CreateParams) (*CreateResponse, error)

Create creates a payment order. Next step: Initiate with the returned OrderSlug.

func (*PaymentService) GetStatus

func (s *PaymentService) GetStatus(ctx context.Context, params GetStatusParams) (*GetStatusResponse, error)

GetStatus fetches the current payment status of an order.

func (*PaymentService) Initiate

func (s *PaymentService) Initiate(ctx context.Context, params InitiateParams) (*InitiateResponse, error)

Initiate starts the payment for an order and returns the UPI link, QR code and app intents.

type PaymentStatus

type PaymentStatus string

PaymentStatus is a payment status returned by the API. Compare against the constants below; any new value the API adds is still accepted.

const (
	StatusCreated       PaymentStatus = "CREATED"
	StatusInitiated     PaymentStatus = "INITIATED"
	StatusPending       PaymentStatus = "PENDING"
	StatusSuccess       PaymentStatus = "SUCCESS"
	StatusFailed        PaymentStatus = "FAILED"
	StatusExpired       PaymentStatus = "EXPIRED"
	StatusSettled       PaymentStatus = "SETTLED"
	StatusRefunded      PaymentStatus = "REFUNDED"
	StatusRefundPending PaymentStatus = "REFUND_PENDING"
	// Test-mode businesses get these instead of SUCCESS / FAILED / PENDING.
	StatusTestSuccess PaymentStatus = "TEST_SUCCESS"
	StatusTestFailed  PaymentStatus = "TEST_FAILED"
	StatusTestPending PaymentStatus = "TEST_PENDING"
)

Directories

Path Synopsis
examples
full-flow command
Full flow: Create → Initiate → GetStatus.
Full flow: Create → Initiate → GetStatus.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL