-
Notifications
You must be signed in to change notification settings - Fork 120
/
pay.go
60 lines (54 loc) · 1.37 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
package gopay
import (
"errors"
"github.com/milkbobo/gopay/client"
"github.com/milkbobo/gopay/common"
"github.com/milkbobo/gopay/constant"
)
// 用户下单支付接口
func Pay(charge *common.Charge) (map[string]string, error) {
err := checkCharge(charge)
if err != nil {
return map[string]string{}, err
}
ct := getPayType(charge.PayMethod)
re, err := ct.Pay(charge)
return re, err
}
// 付款给用户接口
func PayToClient(charge *common.Charge) (map[string]string, error) {
err := checkCharge(charge)
if err != nil {
return nil, err
}
ct := getPayType(charge.PayMethod)
re, err := ct.PayToClient(charge)
return re, err
}
// 验证支付内容
func checkCharge(charge *common.Charge) error {
if charge.PayMethod <= 0 {
return errors.New("PayMethod不能少于等于0")
}
if charge.MoneyFee <= 0 {
return errors.New("MoneyFee不能少于等于0")
}
return nil
}
// getPayType 得到需要支付的类型
func getPayType(payMethod int64) common.PayClient {
//如果使用余额支付
switch payMethod {
case constant.ALI_WEB:
return client.DefaultAliWebClient()
case constant.ALI_APP:
return client.DefaultAliAppClient()
case constant.WECHAT_WEB:
return client.DefaultWechatWebClient()
case constant.WECHAT_APP:
return client.DefaultWechatAppClient()
case constant.WECHAT_MINI_PROGRAM:
return client.DefaultWechatMiniProgramClient()
}
return nil
}