This commit is contained in:
邹宗楠
2022-11-24 13:47:14 +08:00
parent 1ecff0fb9b
commit 32e4ef5e54
5 changed files with 208 additions and 27 deletions

View File

@@ -0,0 +1,116 @@
package recharge_phone_bill
import (
"encoding/json"
"errors"
"git.rosy.net.cn/baseapi/utils"
"net/http"
)
// 充值类型
const (
ChargeTypeDefault = 0 //快充(默认)
ChargeTypeAuto = 1 // 自动慢充
ChargeTypeManual = 2 // 手动慢充
)
// 产品编码
const (
FlowCodeY10Y100 = "Y10Y100" // Y10Y100 全国 全省 中国移动 100
FlowCodeY10Y200 = "Y10Y200" // Y10Y200 全国 全省 中国移动 200
FlowCodeD10Y50 = "D10Y50" // D10Y50 全国 全省 中国电信 50
FlowCodeD10Y100 = "D10Y100" // D10Y100 全国 全省 中国电信 100
FlowCodeD10Y200 = "D10Y200" // D10Y200 全国 全省 中国电信 200
FlowCodeL10Y50 = "L10Y50" // L10Y50 全国 全省 中国联通 50
FlowCodeL10Y100 = "L10Y100" // L10Y100 全国 全省 中国联通 100
FlowCodeL10Y200 = "L10Y200" // L10Y200 全国 全省 中国联通 200
)
const (
BaseUrl = "http://39.108.16.170/index.php/" // 基础访问路由
CreateOrder = "Api/Order/flow" // 话费充值
QueryOrder = "Api/Order/order_search" // 订单查询
QueryAccountBill = "Api/Order/balance_search" // 余额查询
)
type RechargePhoneBillBase struct {
FlowCode string `json:"flowCode"` // 产品编码
Mobile string `json:"mobile"` // 手机号码
OrderNumber string `json:"orderNumber"` // 商户订单号
CallbackURL string `json:"callbackURL"` // 回调地址,不传不回调
ChargeType string `json:"chargeType"` // 充值类型 0 快充(默认) 1 自动慢充 2 手动慢充
}
var FlowCode = map[string]bool{FlowCodeY10Y100: true, FlowCodeY10Y200: true, FlowCodeD10Y50: true, FlowCodeD10Y100: true, FlowCodeD10Y200: true, FlowCodeL10Y50: true, FlowCodeL10Y100: true, FlowCodeL10Y200: true}
// RechargePhoneBill 话费充值接口
func (a *API) RechargePhoneBill(params *RechargePhoneBillBase) (string, error) {
if !FlowCode[params.FlowCode] {
return "", errors.New("不存在的充值类型")
}
data, err := a.AccessAPI(BaseUrl, CreateOrder, http.MethodPost, utils.Struct2MapByJson(params))
if err != nil {
return "", err
}
if data["code"].(string) != "2000" {
return "", errors.New(data["msg"].(string))
}
return data["order_number"].(string), nil
}
type QueryOrderDetailRes struct {
Code string `json:"code"`
Msg string `json:"msg"`
Data []QueryOrderDetailResList `json:"data"`
}
type QueryOrderDetailResList struct {
OrderNumber string `json:"order_number"` // 平台订单号
UserOrdernum string `json:"user_ordernum"` // 商户订单号
OrderStatus string `json:"order_status"` // 状态0未提交1充值中2已充值-1失败
Mobile string `json:"mobile"` // 手机号码
Ctime string `json:"ctime"` // 时间戳
Voucher string `json:"voucher"` // 透传流水号
}
// QueryOrderDetail 查询充值订单详情
func (a *API) QueryOrderDetail(orderNumber, userOrderNum string) ([]QueryOrderDetailResList, error) {
params := make(map[string]interface{}, 0)
if orderNumber == "" && userOrderNum == "" {
return nil, errors.New("订单号不能为空")
}
if orderNumber != "" {
params["order_number"] = orderNumber
} else {
params["user_ordernum"] = userOrderNum
}
data, err := a.AccessAPI(BaseUrl, QueryOrder, http.MethodPost, params)
if err != nil {
return nil, err
}
if data["code"].(string) != "2000" {
return nil, errors.New(data["msg"].(string))
}
var result []QueryOrderDetailResList
if err := json.Unmarshal([]byte(utils.Interface2String(data["data"])), &result); err != nil {
return nil, err
}
return result, nil
}
// QueryAccountBill 余额查询
func (a *API) QueryAccountBill() (string, error) {
data, err := a.AccessAPI(BaseUrl, QueryAccountBill, http.MethodPost, nil)
if err != nil {
return "", err
}
if data["code"].(string) != "2000" {
return "", errors.New(data["msg"].(string))
}
return data["balance"].(string), nil
}