core

package
v2.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 2 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.

Documentation

Overview

Package core defines the contracts for Nepal payment provider implementations. It is intentionally dependency-free, using only the standard library.

Index

Constants

View Source
const (
	StatusComplete  = "COMPLETE"
	StatusPending   = "PENDING"
	StatusFailed    = "FAILED"
	StatusRefunded  = "REFUNDED"
	StatusCancelled = "CANCELLED"
	StatusExpired   = "EXPIRED"
)

Normalized payment status values.

Variables

View Source
var (
	ErrPaymentNotFound   = errors.New("payment: transaction not found")
	ErrSignatureMismatch = errors.New("payment: signature verification failed")
	ErrAmountMismatch    = errors.New("payment: amount mismatch")
	ErrProviderError     = errors.New("payment: provider error")
	ErrInvalidConfig     = errors.New("payment: invalid provider configuration")
	ErrInvalidRequest    = errors.New("payment: invalid request")
	ErrRateLimited       = errors.New("payment: rate limit exceeded")
	ErrTimeout           = errors.New("payment: operation timed out")
)

Sentinel errors for consistent error handling across providers. Use errors.Is() to check; providers should wrap these with context.

Functions

This section is empty.

Types

type MockProvider

type MockProvider struct {
	InitiateFunc func(ctx context.Context, req *PaymentRequest) (*PaymentResponse, error)
	VerifyFunc   func(ctx context.Context, req *VerifyRequest) (*VerifyResponse, error)
}

MockProvider implements Provider for testing. Set the function fields to control behavior in tests.

func (*MockProvider) InitiatePayment

func (m *MockProvider) InitiatePayment(ctx context.Context, req *PaymentRequest) (*PaymentResponse, error)

func (*MockProvider) VerifyPayment

func (m *MockProvider) VerifyPayment(ctx context.Context, req *VerifyRequest) (*VerifyResponse, error)

type PaymentRequest

type PaymentRequest struct {
	// Amount in paisa (1/100 rupee) for every provider. Must be positive.
	Amount int64

	// Currency code (ISO 4217, e.g., "NPR").
	Currency string

	// OrderID is your unique transaction identifier.
	OrderID string

	// Description shown to customer during checkout.
	Description string

	// CallbackURL where provider redirects after payment.
	CallbackURL string

	// CustomerEmail for receipts and fraud detection (optional).
	CustomerEmail string

	// CustomerPhone for authentication (optional).
	CustomerPhone string
}

PaymentRequest contains the data needed to initiate a payment.

type PaymentResponse

type PaymentResponse struct {
	// IsFormPOST indicates whether to use form POST (eSewa) vs redirect (Khalti).
	IsFormPOST bool

	// TargetURL is the payment gateway endpoint.
	TargetURL string

	// FormFields contains hidden form inputs (only when IsFormPOST is true).
	FormFields map[string]string

	// TransactionID from the provider (may be empty at initiation).
	// For Khalti this is the pidx. Store it and pass it as VerifyRequest.ProviderRef.
	TransactionID string
}

PaymentResponse contains the result of initiating a payment.

Check IsFormPOST to determine the integration flow:

  • false: Redirect user directly to TargetURL
  • true: Generate HTML form with FormFields and POST to TargetURL

type Provider

type Provider interface {
	// InitiatePayment creates a payment session with the provider.
	// Returns ErrInvalidRequest for validation failures, ErrProviderError for API failures.
	InitiatePayment(ctx context.Context, req *PaymentRequest) (*PaymentResponse, error)

	// VerifyPayment validates a callback and confirms payment status.
	// Returns ErrSignatureMismatch for tampering, ErrAmountMismatch for manipulation.
	VerifyPayment(ctx context.Context, req *VerifyRequest) (*VerifyResponse, error)
}

Provider defines the contract for payment gateway implementations. All methods must be safe for concurrent use.

type StatusChecker added in v2.1.0

type StatusChecker interface {
	CheckStatus(ctx context.Context, req *StatusRequest) (*VerifyResponse, error)
}

StatusChecker is implemented by providers that can ask the gateway for a payment's status without callback data (the customer never came back).

type StatusRequest added in v2.1.0

type StatusRequest struct {
	OrderID     string // the order ID sent to the gateway in InitiatePayment
	ProviderRef string // PaymentResponse.TransactionID from InitiatePayment (Khalti: pidx)
	Amount      int64  // paisa; the amount of the payment
}

StatusRequest identifies a payment for a status check without callback data.

type VerifyRequest

type VerifyRequest struct {
	// EncodedParams contains raw callback parameters from the provider.
	EncodedParams map[string]string

	// OriginalOrderID: when set, a callback with a different order ID is rejected.
	// Always set it. Not checked for Khalti (its lookup has no order ID): set ProviderRef instead.
	OriginalOrderID string

	// ProviderRef is the PaymentResponse.TransactionID returned by InitiatePayment for this order.
	// When set, a callback that carries a different provider reference is rejected.
	ProviderRef string

	// ExpectedAmount in paisa. 0 skips the check, except connectIPS where it is required.
	ExpectedAmount int64
}

VerifyRequest contains callback data for payment verification.

type VerifyResponse

type VerifyResponse struct {
	// Success indicates payment was verified and completed.
	Success bool

	// TransactionID confirmed by the provider.
	TransactionID string

	// OrderID echoed from your original request.
	OrderID string

	// VerifiedAmount confirmed by the provider, in paisa.
	VerifiedAmount int64

	// Currency of the payment.
	Currency string

	// Status normalized to: COMPLETE, PENDING, FAILED, REFUNDED, CANCELLED.
	Status string

	// ProviderResponse contains raw response for debugging.
	ProviderResponse map[string]string

	// VerifiedAt timestamp (Unix seconds).
	VerifiedAt int64
}

VerifyResponse contains the verified payment details.

Jump to

Keyboard shortcuts

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