Adds /payments/chapa/direct initiate and authorize endpoints alongside existing hosted checkout. Includes 3DES OTP authorization, verify routing for CHAPA_DIRECT payments, tests, and client app integration guide. Co-authored-by: Cursor <cursoragent@cursor.com>
65 lines
1.5 KiB
Go
65 lines
1.5 KiB
Go
package chapa
|
|
|
|
import (
|
|
"encoding/json"
|
|
"testing"
|
|
)
|
|
|
|
func TestParseDirectChargeData_flatReference(t *testing.T) {
|
|
raw := []byte(`{"reference":"CHcuKjgnN0Dk0","status":"pending"}`)
|
|
parsed, err := parseDirectChargeData(raw)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if got := parsed.reference(); got != "CHcuKjgnN0Dk0" {
|
|
t.Fatalf("reference=%q", got)
|
|
}
|
|
}
|
|
|
|
func TestParseDirectChargeData_nestedMeta(t *testing.T) {
|
|
raw := []byte(`{
|
|
"auth_type":"otp",
|
|
"meta":{
|
|
"message":"OTP sent",
|
|
"ref_id":"CHmetaRef",
|
|
"verification_type":"otp",
|
|
"data":"request-token-123"
|
|
}
|
|
}`)
|
|
parsed, err := parseDirectChargeData(json.RawMessage(raw))
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if got := parsed.reference(); got != "CHmetaRef" {
|
|
t.Fatalf("reference=%q", got)
|
|
}
|
|
if got := parsed.requestID(); got != "request-token-123" {
|
|
t.Fatalf("requestID=%q", got)
|
|
}
|
|
if !parsed.requiresAuthorize() {
|
|
t.Fatal("expected authorize required")
|
|
}
|
|
}
|
|
|
|
func (p parsedDirectChargeData) requiresAuthorize() bool {
|
|
authType := p.authType()
|
|
if authType == "otp" {
|
|
return true
|
|
}
|
|
return p.metaVerificationType() == "otp"
|
|
}
|
|
|
|
func TestEncryptChapaDirectChargePayload_requiresKey(t *testing.T) {
|
|
_, err := encryptChapaDirectChargePayload("", "req", "1234")
|
|
if err == nil {
|
|
t.Fatal("expected error for missing key")
|
|
}
|
|
}
|
|
|
|
func TestPkcs7Pad(t *testing.T) {
|
|
padded := pkcs7Pad([]byte("hello"), 8)
|
|
if len(padded)%8 != 0 {
|
|
t.Fatalf("expected multiple of block size, got %d", len(padded))
|
|
}
|
|
}
|