PaymentCenter/app/third/paymentService/psbc/refund.go

76 lines
1.9 KiB
Go

package payment
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)
// Refund 申请退款
func (c *Client) Refund(req RefundRequest) (*RefundResponse, error) {
now := time.Now().Format("20060102150405")
BusiMainId := now + RandomNumber(10)
requestBody := map[string]interface{}{
"head": map[string]string{
"partnerTxSriNo": BusiMainId,
"reqTime": now,
"method": "b2c.gatewaypay.orderRefund",
"version": "1",
"merchantId": c.cfg.MerchantId,
"appID": c.cfg.AppID,
"accessType": "API",
"reserve": "",
},
"body": map[string]interface{}{
"busiMainId": BusiMainId,
"reqTransTime": now,
"data": map[string]string{
"txnCode": "1003",
"sourceId": "16",
"reqTraceId": req.OrderNo + now[:8],
"reqDate": now[:8],
"mchtNo": c.cfg.MchtNo,
"orgTxnSeq": req.OrgTxnSeq,
"txnAmt": req.Price,
"orgTxnAmt": req.Price,
"refundDesc": "用户主动退款",
},
},
}
encryptedReq, err := EncryptRequest(requestBody, c.cfg)
if err != nil {
return nil, fmt.Errorf("加密请求失败: %v", err)
}
encryptedBytes, _ := json.Marshal(encryptedReq)
url := c.cfg.OrderHost + c.cfg.MerchantId + ".htm?partnerTxSriNo=" + BusiMainId
resp, err := http.Post(url, "application/json", bytes.NewReader(encryptedBytes))
if err != nil {
return nil, fmt.Errorf("请求失败: %v", err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("读取响应失败: %v", err)
}
responseData, err := c.DecryptResponse(string(body), false)
if err != nil {
return nil, fmt.Errorf("解密响应失败: %v", err)
}
var result RefundResponse
if err := json.Unmarshal([]byte(responseData), &result); err != nil {
return nil, fmt.Errorf("解析响应失败: %v", err)
}
return &result, nil
}