Release / update-version (push) Has been cancelled
Release / build-frontend (push) Has been cancelled
Release / release (push) Has been cancelled
Release / sync-version-file (push) Has been cancelled
CI / shell (push) Canceled after 0s
CI / test (push) Canceled after 0s
CI / frontend (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
Security Scan / backend-security (push) Canceled after 0s
Security Scan / frontend-security (push) Canceled after 0s
49 lines
1.5 KiB
Go
49 lines
1.5 KiB
Go
package service
|
|
|
|
import (
|
|
"math"
|
|
|
|
"github.com/Wei-Shaw/sub2api/internal/payment"
|
|
"github.com/shopspring/decimal"
|
|
)
|
|
|
|
const defaultBalanceRechargeMultiplier = 1.0
|
|
|
|
func normalizeBalanceRechargeMultiplier(multiplier float64) float64 {
|
|
if math.IsNaN(multiplier) || math.IsInf(multiplier, 0) || multiplier <= 0 {
|
|
return defaultBalanceRechargeMultiplier
|
|
}
|
|
return multiplier
|
|
}
|
|
|
|
// normalizeSubscriptionUSDToCNYRate 将非法值归一为 0(换算关闭)。
|
|
// 与余额倍率不同,0 是合法状态:表示订阅保持 price 直付的存量行为。
|
|
func normalizeSubscriptionUSDToCNYRate(rate float64) float64 {
|
|
if math.IsNaN(rate) || math.IsInf(rate, 0) || rate < 0 {
|
|
return 0
|
|
}
|
|
return rate
|
|
}
|
|
|
|
func calculateCreditedBalance(paymentAmount, multiplier float64) float64 {
|
|
return decimal.NewFromFloat(paymentAmount).
|
|
Mul(decimal.NewFromFloat(normalizeBalanceRechargeMultiplier(multiplier))).
|
|
Round(2).
|
|
InexactFloat64()
|
|
}
|
|
|
|
func calculateGatewayRefundAmount(orderAmount, payAmount, refundAmount float64, currency string) float64 {
|
|
if orderAmount <= 0 || payAmount <= 0 || refundAmount <= 0 {
|
|
return 0
|
|
}
|
|
fractionDigits := int32(payment.CurrencyMaxFractionDigits(currency))
|
|
if math.Abs(refundAmount-orderAmount) <= paymentAmountToleranceForCurrency(currency) {
|
|
return decimal.NewFromFloat(payAmount).Round(fractionDigits).InexactFloat64()
|
|
}
|
|
return decimal.NewFromFloat(payAmount).
|
|
Mul(decimal.NewFromFloat(refundAmount)).
|
|
Div(decimal.NewFromFloat(orderAmount)).
|
|
Round(fractionDigits).
|
|
InexactFloat64()
|
|
}
|