82 lines
2.3 KiB
Go
82 lines
2.3 KiB
Go
package hyperswitch
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestClient_CreatePayment(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost || r.URL.Path != "/payments" {
|
|
t.Errorf("unexpected request: %s %s", r.Method, r.URL.Path)
|
|
}
|
|
if r.Header.Get("api-key") == "" {
|
|
t.Error("missing api-key header")
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(map[string]string{
|
|
"payment_id": "pay_test_123",
|
|
"client_secret": "pi_test_secret_xxx",
|
|
"status": "requires_payment_method",
|
|
})
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClient(server.URL, "test_api_key")
|
|
paymentID, clientSecret, err := client.CreatePaymentSimple(
|
|
context.Background(),
|
|
6540,
|
|
"EUR",
|
|
"order-123",
|
|
"https://example.com/success",
|
|
map[string]string{"key": "value"},
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("CreatePaymentSimple: %v", err)
|
|
}
|
|
if paymentID != "pay_test_123" {
|
|
t.Errorf("payment_id = %q, want pay_test_123", paymentID)
|
|
}
|
|
if clientSecret != "pi_test_secret_xxx" {
|
|
t.Errorf("client_secret = %q, want pi_test_secret_xxx", clientSecret)
|
|
}
|
|
}
|
|
|
|
func TestClient_CreatePayment_HTTPError(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClient(server.URL, "test_api_key")
|
|
_, _, err := client.CreatePaymentSimple(context.Background(), 100, "EUR", "", "", nil)
|
|
if err == nil {
|
|
t.Fatal("expected error for 400 response")
|
|
}
|
|
}
|
|
|
|
func TestClient_GetPaymentStatus(t *testing.T) {
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/payments/pay_123" {
|
|
t.Errorf("unexpected path: %s", r.URL.Path)
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(map[string]string{
|
|
"payment_id": "pay_123",
|
|
"status": "succeeded",
|
|
})
|
|
}))
|
|
defer server.Close()
|
|
|
|
client := NewClient(server.URL, "test_api_key")
|
|
status, err := client.GetPaymentStatus(context.Background(), "pay_123")
|
|
if err != nil {
|
|
t.Fatalf("GetPaymentStatus: %v", err)
|
|
}
|
|
if status != "succeeded" {
|
|
t.Errorf("status = %q, want succeeded", status)
|
|
}
|
|
}
|