12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- package api
- import (
- "encoding/json"
- logger "eta/eta_mini_ht_api/common/component/log"
- "eta/eta_mini_ht_api/common/exception"
- "io"
- "sync"
- )
- const (
- queryPaymentUrl = "/paygateway/query.json"
- createPaymentUrl = "/paygateway/createpay.json"
- )
- var (
- paymentFacadeOnce sync.Once
- paymentFacade *HTPaymentApi
- )
- type HTPaymentApi struct {
- *HTApi
- }
- func GetPaymentInstance() *HTPaymentApi {
- paymentFacadeOnce.Do(func() {
- paymentFacade = &HTPaymentApi{
- GetInstance(),
- }
- })
- return paymentFacade
- }
- type PaymentOrderRequest struct {
- AppId string `json:"appId"`
- Sign string `json:"sign"`
- ChannelTypeCode string `json:"channelTypeCode"`
- Subject string `json:"subject"`
- Body string `json:"body"`
- TotalFee string `json:"totalFee"`
- BillNo string `json:"billNo"`
- BillTypeCode string `json:"billTypeCode"`
- BillType string `json:"billType"`
- TimeoutExpress string `json:"timeoutExpress"`
- Optional string `json:"optional"`
- Mobile string `json:"mobile"`
- IapProductId string `json:"iapProductId"`
- }
- type PaymentOrderResponse struct {
- Success bool `json:"success"`
- ResultCode string `json:"resultCode"`
- ResultDesc string `json:"resultDesc"`
- ResultView string `json:"resultView"`
- PrePayStr string `json:"prePayStr"`
- }
- func (f *HTPaymentApi) CreatePaymentOrder(req PaymentOrderRequest) (tradeNo string, err error) {
- url := f.htConfig.GetPaymentApiUrl() + createPaymentUrl
- req.AppId = f.htConfig.GetPaymentAppId()
- req.Sign = f.htConfig.GetPaymentSign()
- resp, err := f.client.Post(url, req)
- if err != nil {
- logger.Error("调用支付创单接口失败:[%v]", err)
- return
- }
- defer func(Body io.ReadCloser) {
- closeErr := Body.Close()
- if closeErr != nil {
- logger.Error("关闭Response失败:%v", closeErr)
- }
- }(resp.Body)
- body, _ := io.ReadAll(resp.Body)
- var paymentOrderResponse PaymentOrderResponse
- err = json.Unmarshal(body, &paymentOrderResponse)
- if err != nil {
- logger.Warn("[支付接口调用]解析支付创建订单接口应答失败:%v", err)
- err = exception.New(exception.CreatePaymentOrderFailed)
- return
- }
- if !paymentOrderResponse.Success {
- logger.Warn("[支付接口调用] 创建订单失败:[code:%v, msg:%v]", paymentOrderResponse.ResultCode, paymentOrderResponse.ResultDesc)
- err = exception.NewWithException(exception.CreatePaymentOrderFailed, paymentOrderResponse.ResultView)
- return
- }
- logger.Info("[支付接口调用] 创建订单成功:[%v]", paymentOrderResponse.PrePayStr)
- tradeNo = paymentOrderResponse.PrePayStr
- return
- }
|