优化结构
This commit is contained in:
@@ -12,39 +12,37 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"kra/pkg/paymentkit"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const (
|
||||
PaymentAlipay = "alipay"
|
||||
PaymentAlipayV3 = "alipay-v3"
|
||||
PaymentWechatV2 = "wechat-v2"
|
||||
PaymentWechatV3 = "wechat-v3"
|
||||
PaymentApple = "apple-iap"
|
||||
PaymentChinaums = "chinaums"
|
||||
PaymentSFT = "sft"
|
||||
PaymentSuperPay = "supper-pay"
|
||||
PaymentWechatGame = "wechat-game-pay"
|
||||
PaymentDouyinGame = "douyin-game-pay"
|
||||
PaymentAlipay = paymentutil.ProviderAlipay
|
||||
PaymentAlipayV3 = paymentutil.ProviderAlipayV3
|
||||
PaymentWechatV2 = paymentutil.ProviderWechatV2
|
||||
PaymentWechatV3 = paymentutil.ProviderWechatV3
|
||||
PaymentApple = paymentutil.ProviderApple
|
||||
PaymentChinaums = paymentutil.ProviderChinaums
|
||||
PaymentSFT = paymentutil.ProviderSFT
|
||||
PaymentSuperPay = paymentutil.ProviderSuperPay
|
||||
PaymentWechatGame = paymentutil.ProviderWechatGame
|
||||
PaymentDouyinGame = paymentutil.ProviderDouyinGame
|
||||
// GoPay-backed providers. These are kept separate from the existing
|
||||
// configuration-driven game/aggregator protocols above.
|
||||
PaymentDouyin = "douyin"
|
||||
PaymentQQ = "qq"
|
||||
PaymentAllinPay = "allinpay"
|
||||
PaymentLakala = "lakala"
|
||||
PaymentPayPal = "paypal"
|
||||
PaymentSaobei = "saobei"
|
||||
PaymentInternal = "internal"
|
||||
PaymentDouyin = paymentutil.ProviderDouyin
|
||||
PaymentQQ = paymentutil.ProviderQQ
|
||||
PaymentAllinPay = paymentutil.ProviderAllinPay
|
||||
PaymentLakala = paymentutil.ProviderLakala
|
||||
PaymentPayPal = paymentutil.ProviderPayPal
|
||||
PaymentSaobei = paymentutil.ProviderSaobei
|
||||
PaymentInternal = paymentutil.ProviderInternal
|
||||
|
||||
PaymentModeExternal = "external"
|
||||
PaymentModeInternal = "internal"
|
||||
PaymentModeExternal = paymentutil.ModeExternal
|
||||
PaymentModeInternal = paymentutil.ModeInternal
|
||||
)
|
||||
|
||||
var SupportedPaymentProviders = []string{
|
||||
PaymentAlipay, PaymentAlipayV3, PaymentWechatV2, PaymentWechatV3, PaymentApple,
|
||||
PaymentChinaums, PaymentSFT, PaymentSuperPay, PaymentWechatGame, PaymentDouyinGame,
|
||||
PaymentDouyin, PaymentQQ, PaymentAllinPay, PaymentLakala, PaymentPayPal, PaymentSaobei,
|
||||
}
|
||||
var SupportedPaymentProviders = paymentutil.SupportedProviders()
|
||||
|
||||
var (
|
||||
ErrPaymentProviderNotFound = errors.New("支付渠道未配置")
|
||||
@@ -190,74 +188,15 @@ type PaymentResult struct {
|
||||
FailureAck PaymentCallbackAck `json:"-"`
|
||||
}
|
||||
|
||||
type PaymentCallbackAck struct {
|
||||
StatusCode int
|
||||
ContentType string
|
||||
Body []byte
|
||||
}
|
||||
|
||||
type PaymentCallbackError struct {
|
||||
Cause error
|
||||
Ack PaymentCallbackAck
|
||||
}
|
||||
|
||||
func (e *PaymentCallbackError) Error() string {
|
||||
if e == nil || e.Cause == nil {
|
||||
return "支付回调处理失败"
|
||||
}
|
||||
return e.Cause.Error()
|
||||
}
|
||||
|
||||
func (e *PaymentCallbackError) Unwrap() error {
|
||||
if e == nil {
|
||||
return nil
|
||||
}
|
||||
return e.Cause
|
||||
}
|
||||
type PaymentCallbackAck = paymentutil.CallbackAck
|
||||
type PaymentCallbackError = paymentutil.CallbackError
|
||||
|
||||
func CallbackFailure(err error, fallback PaymentCallbackAck) PaymentCallbackAck {
|
||||
var callbackErr *PaymentCallbackError
|
||||
if errors.As(err, &callbackErr) && callbackErr.Ack.StatusCode != 0 {
|
||||
return callbackErr.Ack
|
||||
}
|
||||
return fallback
|
||||
return paymentutil.CallbackFailure(err, fallback)
|
||||
}
|
||||
|
||||
func DefaultPaymentCallbackAck(provider string, success bool) PaymentCallbackAck {
|
||||
status := 200
|
||||
if !success {
|
||||
status = 500
|
||||
}
|
||||
switch provider {
|
||||
case PaymentAlipay, PaymentAlipayV3:
|
||||
body := "success"
|
||||
if !success {
|
||||
body = "failure"
|
||||
}
|
||||
return PaymentCallbackAck{StatusCode: status, ContentType: "text/plain; charset=utf-8", Body: []byte(body)}
|
||||
case PaymentWechatV2:
|
||||
code, message := "SUCCESS", "OK"
|
||||
if !success {
|
||||
code, message = "FAIL", "FAIL"
|
||||
}
|
||||
body := fmt.Sprintf("<xml><return_code><![CDATA[%s]]></return_code><return_msg><![CDATA[%s]]></return_msg></xml>", code, message)
|
||||
return PaymentCallbackAck{StatusCode: status, ContentType: "application/xml; charset=utf-8", Body: []byte(body)}
|
||||
case PaymentWechatV3:
|
||||
code, message := "SUCCESS", "成功"
|
||||
if !success {
|
||||
code, message = "FAIL", "失败"
|
||||
}
|
||||
body, _ := json.Marshal(map[string]string{"code": code, "message": message})
|
||||
return PaymentCallbackAck{StatusCode: status, ContentType: "application/json; charset=utf-8", Body: body}
|
||||
case PaymentApple:
|
||||
return PaymentCallbackAck{StatusCode: status}
|
||||
default:
|
||||
body := "success"
|
||||
if !success {
|
||||
body = "failure"
|
||||
}
|
||||
return PaymentCallbackAck{StatusCode: status, ContentType: "text/plain; charset=utf-8", Body: []byte(body)}
|
||||
}
|
||||
return paymentutil.DefaultCallbackAck(provider, success)
|
||||
}
|
||||
|
||||
type PaymentCallback struct {
|
||||
|
||||
Reference in New Issue
Block a user