feat: add online payment management and checkout
Co-Authored-By: Craft Agent <agents-noreply@craft.do>
This commit is contained in:
37
backend/internal/payments/payments_test.go
Normal file
37
backend/internal/payments/payments_test.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package payments
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"encoding/base64"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParseAlipayAmount(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
s string
|
||||
want int64
|
||||
ok bool
|
||||
}{{"1.50", 150, true}, {"0.01", 1, true}, {"1.5", 150, true}, {"-1.00", 0, false}, {"1.000", 0, false}, {"abc", 0, false}} {
|
||||
got, e := parseAlipayAmount(tc.s)
|
||||
if (e == nil) != tc.ok || e == nil && got != tc.want {
|
||||
t.Fatalf("%q got %d err %v", tc.s, got, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
func TestDecryptGCMRejectsInvalidNonce(t *testing.T) {
|
||||
if _, e := decryptGCM("01234567890123456789012345678901", "short", "", "AA=="); e == nil {
|
||||
t.Fatal("expected nonce length error")
|
||||
}
|
||||
}
|
||||
func TestDecryptGCM(t *testing.T) {
|
||||
key := []byte("01234567890123456789012345678901")
|
||||
b, _ := aes.NewCipher(key)
|
||||
g, _ := cipher.NewGCM(b)
|
||||
nonce := []byte("nonce1234567")
|
||||
ct := g.Seal(nil, nonce, []byte(`{"out_trade_no":"m-1"}`), []byte("ad"))
|
||||
got, e := decryptGCM(string(key), string(nonce), "ad", base64.StdEncoding.EncodeToString(ct))
|
||||
if e != nil || got == "" {
|
||||
t.Fatalf("decrypt failed: %q %v", got, e)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user