-
Notifications
You must be signed in to change notification settings - Fork 50
/
pay.go
67 lines (61 loc) · 1.33 KB
/
pay.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package gopay
import (
"errors"
"github.com/guidao/gopay/client"
"github.com/guidao/gopay/common"
"github.com/guidao/gopay/constant"
//"github.com/guidao/gopay/util"
"strconv"
)
func Pay(charge *common.Charge) (string, error) {
err := checkCharge(charge)
if err != nil {
//log.Error(err, charge)
return "", err
}
ct := getPayClient(charge.PayMethod)
re, err := ct.Pay(charge)
if err != nil {
//log.Error("支付失败:", err, charge)
return "", err
}
return re, err
}
func checkCharge(charge *common.Charge) error {
var id uint64
var err error
if charge.UserID == "" {
id = 0
} else {
id, err = strconv.ParseUint(charge.UserID, 10, -1)
if err != nil {
return err
}
}
if id < 0 {
return errors.New("userID less than 0")
}
if charge.PayMethod < 0 {
return errors.New("payMethod less than 0")
}
if charge.MoneyFee < 0 {
return errors.New("totalFee less than 0")
}
if charge.CallbackURL == "" {
return errors.New("callbackURL is NULL")
}
return nil
}
// getPayClient 得到需要支付的客户端
func getPayClient(payMethod int64) common.PayClient {
//如果使用余额支付
switch payMethod {
case constant.ALI_WEB:
return client.DefaultAliWebClient()
case constant.ALI_APP:
return client.DefaultAliAppClient()
case constant.WECHAT:
return client.DefaultWechatAppClient()
}
return nil
}