133 lines
3.2 KiB
Go
133 lines
3.2 KiB
Go
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||
//go:build plus
|
||
|
||
package userconfigs
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
|
||
)
|
||
|
||
type PayMethod = string
|
||
|
||
const (
|
||
PayMethodAlipay PayMethod = "alipay"
|
||
)
|
||
|
||
type PayMethodParams interface {
|
||
Init() error
|
||
}
|
||
|
||
type AlipayPayMethodParams struct {
|
||
IsSandbox bool `json:"isSandbox"` // 是否沙箱应用
|
||
AppId string `json:"appId"` // APPID
|
||
PrivateKey string `json:"privateKey"` // 私钥
|
||
AppPublicCert string `json:"appPublicCert"` // 应用公钥证书
|
||
AlipayPublicCert string `json:"alipayPublicCert"` // 支付宝公钥证书
|
||
AlipayRootCert string `json:"alipayRootCert"` // 支付宝根证书
|
||
ProductCode string `json:"productCode"` // 产品代号
|
||
}
|
||
|
||
func (this *AlipayPayMethodParams) Init() error {
|
||
return nil
|
||
}
|
||
|
||
type PayMethodDefinition struct {
|
||
Name string `json:"name"`
|
||
Code string `json:"code"`
|
||
Description string `json:"description"`
|
||
QRCodeTitle string `json:"qrcodeTitle"` // 二维码标题
|
||
NotifyURL string `json:"notifyURL"`
|
||
}
|
||
|
||
func FindAllPresetPayMethods() []*PayMethodDefinition {
|
||
return []*PayMethodDefinition{
|
||
{
|
||
Name: "支付宝",
|
||
Code: PayMethodAlipay,
|
||
Description: "支付宝平台提供的支付能力。",
|
||
QRCodeTitle: "请使用支付宝APP扫描下方二维码完成支付:",
|
||
NotifyURL: "${baseAddr}/api/pay/alipay/notify",
|
||
},
|
||
}
|
||
}
|
||
|
||
func FindPresetPayMethodWithCode(code PayMethod) *PayMethodDefinition {
|
||
for _, payMethod := range FindAllPresetPayMethods() {
|
||
if payMethod.Code == code {
|
||
return payMethod
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func DecodePayMethodParams(payMethodCode PayMethod, paramsJSON []byte) (params PayMethodParams, err error) {
|
||
switch payMethodCode {
|
||
case PayMethodAlipay:
|
||
params = &AlipayPayMethodParams{}
|
||
}
|
||
|
||
if params == nil {
|
||
return
|
||
}
|
||
|
||
err = json.Unmarshal(paramsJSON, params)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("decode params failed: %w", err)
|
||
}
|
||
|
||
err = params.Init()
|
||
return
|
||
}
|
||
|
||
type PayClientType = string
|
||
|
||
const (
|
||
PayClientTypeAll PayClientType = "all"
|
||
PayClientTypeLaptop PayClientType = "laptop"
|
||
PayClientTypeMobile PayClientType = "mobile"
|
||
)
|
||
|
||
func FindAllPayClientTypes() []*shared.Definition {
|
||
return []*shared.Definition{
|
||
{
|
||
Name: "所有终端",
|
||
Code: PayClientTypeAll,
|
||
Description: "可以在电脑浏览器和手机上完成支付。",
|
||
},
|
||
{
|
||
Name: "PC端",
|
||
Code: PayClientTypeLaptop,
|
||
Description: "需要在电脑浏览器上完成支付。",
|
||
},
|
||
{
|
||
Name: "手机端",
|
||
Code: PayClientTypeMobile,
|
||
Description: "需要在手机上完成支付。",
|
||
},
|
||
}
|
||
}
|
||
|
||
func FindPayClientType(clientType PayClientType) *shared.Definition {
|
||
var allTypes = FindAllPayClientTypes()
|
||
if len(allTypes) == 0 {
|
||
return nil
|
||
}
|
||
for _, def := range allTypes {
|
||
if def.Code == clientType {
|
||
return def
|
||
}
|
||
}
|
||
return allTypes[0]
|
||
}
|
||
|
||
func FindPayClientTypeName(clientType PayClientType) string {
|
||
for _, def := range FindAllPayClientTypes() {
|
||
if def.Code == clientType {
|
||
return def.Name
|
||
}
|
||
}
|
||
return "所有终端"
|
||
}
|