diff --git a/api/h5/index.go b/api/h5/index.go index 4972351..80d8534 100644 --- a/api/h5/index.go +++ b/api/h5/index.go @@ -8,6 +8,7 @@ import ( "gold-shop/global" "gold-shop/request" "gold-shop/service" + "gold-shop/utils/alipay" "gold-shop/utils/result" "gold-shop/utils/tlpay/notify" "gold-shop/utils/weixin" @@ -142,6 +143,16 @@ func GetWxOpenId(c *gin.Context) (result.Data, error) { return data, nil } +func GetAliOpenId(c *gin.Context) (result.Data, error) { + code := c.Query("code") + res, err := alipay.AlipayApi.GetAuthToken(code) + if err != nil { + return nil, err + } + data := result.Data{"openId": res.OpenID} + return data, nil +} + func SaveAlipayAccount(c *gin.Context) (result.Data, error) { alipayAccount := c.PostForm("alipayAccount") err := service.UserService.SaveAlipayAccount(alipayAccount, user(c)) diff --git a/config.yaml b/config.yaml index 1e73e7d..f1d9f09 100644 --- a/config.yaml +++ b/config.yaml @@ -26,6 +26,9 @@ payment: auth-redirect-uri: "http://mall.wrtcjt.com/pages/wx/callback" sub-app-id: "wx939c26e4e09ce9ee" sub-app-secret: "0d365a525ec9bf2c055d735ced98a6c1" + ali-pay: + sub-app-id: "wx939c26e4e09ce9ee" + sub-pri-pem-file: "" tl-pay: base-url: "https://vsp.allinpay.com" app-id: "00307595" diff --git a/config/payment.go b/config/payment.go index 6d2928b..8194321 100644 --- a/config/payment.go +++ b/config/payment.go @@ -5,6 +5,7 @@ type Payment struct { ApiKey string `yaml:"api-key"` SignKey string `yaml:"sign-key"` WxPay WxPay `yaml:"wx-pay"` + AliPay AliPay `yaml:"ali-pay"` } type WxPay struct { @@ -12,3 +13,8 @@ type WxPay struct { SubAppSecret string `yaml:"sub-app-secret"` AuthRedirectUri string `yaml:"auth-redirect-uri"` } + +type AliPay struct { + SubAppId string `yaml:"sub-app-id"` + SubPriPemFile string `yaml:"sub-pri-pem-file"` +} diff --git a/goldshop b/goldshop deleted file mode 100755 index c2f157c..0000000 Binary files a/goldshop and /dev/null differ diff --git a/goshop b/goshop deleted file mode 100755 index 0e89dbb..0000000 Binary files a/goshop and /dev/null differ diff --git a/route/h5.go b/route/h5.go index 565a685..fd4a372 100644 --- a/route/h5.go +++ b/route/h5.go @@ -31,6 +31,7 @@ func h5RouteInit(r *gin.Engine) { authGroup.Match([]string{"POST", "OPTIONS"}, "/payment", result.Json(h5.Payment)) authGroup.Match([]string{"GET", "OPTIONS"}, "/get-wx-open-id", result.Json(h5.GetWxOpenId)) authGroup.Match([]string{"GET", "OPTIONS"}, "/get-wx-auth-url", result.Json(h5.GetWxAuthUrl)) + authGroup.Match([]string{"GET", "OPTIONS"}, "/get-ali-open-id", result.Json(h5.GetAliOpenId)) authGroup.Match([]string{"GET", "OPTIONS"}, "/get-orders", result.Json(h5.GetOrders)) authGroup.Match([]string{"GET", "OPTIONS"}, "/get-payments", result.Json(h5.GetPayments)) authGroup.Match([]string{"GET", "OPTIONS"}, "/get-user-data", result.Json(h5.GetUserData)) diff --git a/service/payment_service.go b/service/payment_service.go index 9fd2bfe..ee56e40 100644 --- a/service/payment_service.go +++ b/service/payment_service.go @@ -45,6 +45,8 @@ func (s paymentService) Payment(userID int, paymentRequest request.PaymentReques data := result.Data{} if paymentRequest.PayType == "wxgzh" { data, err = s.wxPayment(paymentRequest, p.PaymentNo) + } else if paymentRequest.PayType == "alishh" { + data, err = s.aliPayment(paymentRequest, p.PaymentNo) } else if paymentRequest.PayType == "transfer" { data, err = s.transferPayment(paymentRequest, p) } else if paymentRequest.PayType == "binding" { @@ -95,6 +97,30 @@ func (s paymentService) wxPayment(paymentRequest request.PaymentRequest, orderID return data, nil } +func (s paymentService) aliPayment(paymentRequest request.PaymentRequest, orderID string) (result.Data, error) { + param := payment.AliPayParam{} + param.Remark = "payment" + param.OrderId = orderID + param.BuyerId = paymentRequest.OpenID + param.Amount = paymentRequest.Amount + param.NotifyUrl = "http://api.wrtcjt.com/h5/payment-notify" + res, err := payment.PayApi.AliPay(param) + if err != nil { + return nil, err + } + + if res.Status != payment.Success { + return nil, errors.NewBusinessError(res.Message) + } + + data := result.Data{ + "orderId": res.OrderID, + "tradeNo": res.TradeNo, + "paymentNo": orderID, + } + return data, nil +} + func (s paymentService) transferPayment(paymentRequest request.PaymentRequest, p *model.Payment) (result.Data, error) { param := payment.TransferPayParam{} param.Remark = "payment" @@ -381,6 +407,7 @@ func (s paymentService) GatewayPay(token string) (string, error) { p.TrxAmt = strconv.FormatFloat(pm.Amount*100, 'f', 0, 64) p.LimitPay = "no_credit" p.RetUrl = "http://api.wrtcjt.com/h5/gateway-pay-ret" + //p.RetUrl = "https://www.baidu.com" p.NotifyUrl = "http://api.wrtcjt.com/h5/binding-pay-notify" p.GoodsID = "1" p.GoodsInf = "充值" diff --git a/utils/alipay/alipay_api.go b/utils/alipay/alipay_api.go new file mode 100644 index 0000000..5bc3065 --- /dev/null +++ b/utils/alipay/alipay_api.go @@ -0,0 +1,111 @@ +package alipay + +import ( + "encoding/json" + "fmt" + "gold-shop/errors" + "gold-shop/global" + "gold-shop/utils" + "io" + "net/http" + "net/url" + "strings" + "time" +) + +var AlipayApi alipayApi = alipayApi{} + +type alipayApi struct { +} + +func (api alipayApi) GetAuthToken(code string) (*AuthTokenResult, error) { + + commonParam := api.buildCommonParam("alipay.system.oauth.token") + param := AuthTokenParam{} + param.CommonParam = commonParam + param.Code = code + param.GrantType = "authorization_code" + + p, err := utils.StructToURLValues(param) + if err != nil { + return nil, err + } + + sign, err := api.sign(p) + if err != nil { + return nil, err + } + p.Set("sign", sign) + + body, err := api.post(p) + if err != nil { + return nil, err + } + result := AuthTokenResult{} + err = json.Unmarshal(body, &result) + fmt.Println(result) + if err != nil { + return nil, errors.NewBusinessError("数据解析错误") + } + if result.Code != "10000" { + return nil, errors.NewBusinessError(result.Msg) + } + return &result, nil +} + +func (api alipayApi) buildCommonParam(method string) CommonParam { + param := CommonParam{} + param.AppID = global.Config.Payment.AliPay.SubAppId + param.Method = method + param.Format = "JSON" + param.Charset = "utf-8" + param.SignType = "RSA2" + param.Timestamp = time.Now().Format("2006-01-02 15:04:05") + param.Version = "1.0" + return param +} + +func (api *alipayApi) sign(params url.Values) (string, error) { + params.Del("sign") + for key, value := range params { + if len(value) == 0 || value[0] == "" { + params.Del(key) + } + } + signStr, err := url.QueryUnescape(params.Encode()) + if err != nil { + return "", err + } + + fmt.Println("signStr: " + signStr) + return utils.RSASign([]byte(signStr), global.Config.Payment.AliPay.SubPriPemFile) +} + +func (alipayApi) post(data url.Values) ([]byte, error) { + client := &http.Client{} + body := strings.NewReader(data.Encode()) + request, err := http.NewRequest("POST", "https://openapi.alipay.com/gateway.do", body) + request.Header.Set("content-type", "application/x-www-form-urlencoded") + + if err != nil { + return nil, errors.NewBusinessError("请求错误") + } + response, err := client.Do(request) + + defer func(Body io.ReadCloser) { + err := Body.Close() + if err != nil { + + } + }(response.Body) + + bodyBytes, err := io.ReadAll(response.Body) + + fmt.Println(string(bodyBytes)) + + if err != nil { + return nil, errors.NewBusinessError("返回内容错误") + } + + return bodyBytes, nil +} diff --git a/utils/alipay/auth_token_param.go b/utils/alipay/auth_token_param.go new file mode 100644 index 0000000..3abaf11 --- /dev/null +++ b/utils/alipay/auth_token_param.go @@ -0,0 +1,8 @@ +package alipay + +type AuthTokenParam struct { + CommonParam + Code string `json:"code"` + GrantType string `json:"grant_type"` + RefreshToken string `json:"refresh_token"` +} diff --git a/utils/alipay/auth_token_result.go b/utils/alipay/auth_token_result.go new file mode 100644 index 0000000..3b37171 --- /dev/null +++ b/utils/alipay/auth_token_result.go @@ -0,0 +1,12 @@ +package alipay + +type AuthTokenResult struct { + CommonResult + AccessToken string `json:"access_token"` + ExpiresIn string `json:"expires_in"` + RefreshToken string `json:"refresh_token"` + ReExpiresIn string `json:"re_expires_in"` + OpenID string `json:"open_id"` + UserID string `json:"user_id"` + AuthStart string `json:"auth_start"` +} diff --git a/utils/alipay/common_param.go b/utils/alipay/common_param.go new file mode 100644 index 0000000..8e6452a --- /dev/null +++ b/utils/alipay/common_param.go @@ -0,0 +1,13 @@ +package alipay + +type CommonParam struct { + AppID string `json:"app_id"` + Method string `json:"method"` + Format string `json:"format"` + Charset string `json:"charset"` + SignType string `json:"sign_type"` + Sign string `json:"sign"` + Timestamp string `json:"timestamp"` + Version string `json:"version"` + AppAuthToken string `json:"app_auth_token"` +} diff --git a/utils/alipay/common_result.go b/utils/alipay/common_result.go new file mode 100644 index 0000000..ff6003d --- /dev/null +++ b/utils/alipay/common_result.go @@ -0,0 +1,9 @@ +package alipay + +type CommonResult struct { + Code string `json:"code"` + Msg string `json:"msg"` + SubCode string `json:"sub_code"` + SubMsg string `json:"sub_msg"` + Sign string `json:"sign"` +} diff --git a/utils/payment/ali_pay_param.go b/utils/payment/ali_pay_param.go new file mode 100644 index 0000000..0b1c308 --- /dev/null +++ b/utils/payment/ali_pay_param.go @@ -0,0 +1,9 @@ +package payment + +type AliPayParam struct { + BuyerId string + Amount float64 + OrderId string + NotifyUrl string + Remark string +} diff --git a/utils/payment/ali_pay_result.go b/utils/payment/ali_pay_result.go new file mode 100644 index 0000000..3f1a4ae --- /dev/null +++ b/utils/payment/ali_pay_result.go @@ -0,0 +1,14 @@ +package payment + +type AliPayResult struct { + TradeNo string `json:"trade_no"` + OrderID string `json:"orderid"` + OrderTime string `json:"order_time"` + MchOrderID string `json:"mch_orderid"` + Version string `json:"version"` + Charset string `json:"charset"` + Message string `json:"message"` + Status string `json:"status"` + SignType string `json:"sign_ype"` + Sign string `json:"sign"` +} diff --git a/utils/payment/pay_api.go b/utils/payment/pay_api.go index d3c455a..5f7d0d2 100644 --- a/utils/payment/pay_api.go +++ b/utils/payment/pay_api.go @@ -48,6 +48,36 @@ func (api *payApi) WxPay(param WxPayParam) (*WxPayResult, error) { return &result, nil } +func (api *payApi) AliPay(param AliPayParam) (*AliPayResult, error) { + payment := global.Config.Payment + data := url.Values{} + data.Set("service", "comm.js.pay") + data.Set("apikey", payment.ApiKey) + data.Set("money", fmt.Sprintf("%.2f", param.Amount)) + data.Set("sub_appid", payment.WxPay.SubAppId) + data.Set("buyer_id", param.OrderId) + data.Set("nonce_str", utils.GenerateRandomString(32)) + data.Set("mch_orderid", param.OrderId) + data.Set("notify_url", param.NotifyUrl) + data.Set("remarks", param.Remark) + sign := api.sign(data, payment.SignKey) + data.Set("sign", sign) + fmt.Println(data.Encode()) + + data.Set("notify_url", url.QueryEscape(param.NotifyUrl)) + + res, err := api.post(payment.BaseUrl+"/payapi/trade/alipay", data) + if err != nil { + return nil, err + } + result := AliPayResult{} + err = json.Unmarshal(res, &result) + if err != nil { + return nil, errors.NewBusinessError("结果解析错误") + } + return &result, nil +} + func (api *payApi) QueryOrder(param QueryOrderParam) (*QueryOrderResult, error) { payment := global.Config.Payment data := url.Values{}