71 lines
2.4 KiB
Go
71 lines
2.4 KiB
Go
package recharge
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
bizpayment "kra/internal/biz/payment"
|
|
)
|
|
|
|
type repoStub struct {
|
|
order *Order
|
|
paidCall int
|
|
}
|
|
|
|
func (r *repoStub) Plans(context.Context) ([]*Plan, error) { return nil, nil }
|
|
func (r *repoStub) SavePlans(context.Context, []*Plan) error { return nil }
|
|
func (r *repoStub) List(context.Context, Filter, ListOptions) ([]*Order, int64, *Summary, error) {
|
|
return nil, 0, &Summary{}, nil
|
|
}
|
|
func (r *repoStub) Get(context.Context, string) (*Order, error) { return r.order, nil }
|
|
func (r *repoStub) Create(context.Context, uint, uint) (*Order, error) { return r.order, nil }
|
|
func (r *repoStub) MarkPaid(context.Context, string) (*Order, error) {
|
|
r.paidCall++
|
|
return r.order, nil
|
|
}
|
|
|
|
func newTestUsecase(t *testing.T, repo Repo) *Usecase {
|
|
t.Helper()
|
|
payments := bizpayment.NewPaymentUsecase(nil, nil, bizpayment.NewPaymentHooks(), bizpayment.NewPaymentOrderSourceRegistry(), bizpayment.NewPaymentFulfillmentRegistry(), nil)
|
|
uc, err := NewUsecase(repo, payments)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return uc
|
|
}
|
|
|
|
func TestPreparePaymentUsesRechargeOrderFacts(t *testing.T) {
|
|
repo := &repoStub{order: &Order{OrderNo: "CZ-1", UserID: 7, Diamonds: 300, AmountCents: 3000}}
|
|
uc := newTestUsecase(t, repo)
|
|
intent, err := uc.PreparePayment(context.Background(), bizpayment.PaymentWechatV3, "CZ-1", "CZ-1")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if intent.Amount != 3000 || intent.OriginalAmount != 3000 || intent.Currency != "CNY" || intent.Subject != "充值 300 钻石" {
|
|
t.Fatalf("unexpected payment intent: %+v", intent)
|
|
}
|
|
if string(intent.Extra) != `{"diamonds":300,"userId":7}` {
|
|
t.Fatalf("unexpected payment extra: %s", intent.Extra)
|
|
}
|
|
}
|
|
|
|
func TestFulfillCreditsMatchingRechargeOnly(t *testing.T) {
|
|
repo := &repoStub{order: &Order{OrderNo: "CZ-1", Diamonds: 60, AmountCents: 600}}
|
|
uc := newTestUsecase(t, repo)
|
|
err := uc.Fulfill(context.Background(), &bizpayment.PaymentConfirmation{
|
|
TradeNo: "CZ-1", BusinessType: BusinessType, BusinessID: "CZ-1", Amount: 600, Currency: "CNY",
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if repo.paidCall != 1 {
|
|
t.Fatalf("paid calls = %d, want 1", repo.paidCall)
|
|
}
|
|
err = uc.Fulfill(context.Background(), &bizpayment.PaymentConfirmation{
|
|
TradeNo: "CZ-1", BusinessType: BusinessType, BusinessID: "CZ-1", Amount: 601, Currency: "CNY",
|
|
})
|
|
if err == nil || repo.paidCall != 1 {
|
|
t.Fatalf("mismatched payment was credited: err=%v calls=%d", err, repo.paidCall)
|
|
}
|
|
}
|