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

78 lines
2.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package payment
import (
"encoding/json"
"fmt"
"time"
)
// OrderQuery 查询订单支付状态
// 对齐 YouChuKoffee 的 OrderQuery 流程:
// 1. 构建完整 {head, body} 结构
// 2. 整个 {head, body} 一起加密EncryptRequest
// 3. head 同时作为 HTTP Header 明文传输
func (c *Client) OrderQuery(orderNo string) (*OrderQueryResponse, error) {
now := time.Now().Format("20060102150405")
BusiMainId := now + RandomNumber(10)
// 构建完整 requesthead+body 一起加密,对齐 YouChuKoffee
requestBody := map[string]interface{}{
"head": map[string]string{
"partnerTxSriNo": BusiMainId,
"reqTime": now,
"method": "b2c.gatewaypay.orderQuery",
"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": "1004",
"sourceId": "16",
"reqTraceId": now + RandomNumber(10),
"reqDate": now, // 14 位 YYYYMMDDHHmmss
"txnDt": time.Now().Format("20060102"), // 8 位 YYYYMMDD对齐 YouChuKoffee
"mchtNo": c.cfg.MchtNo,
"oldSeqNo": orderNo,
},
},
}
// 整个 {head, body} 一起加密
encryptedReq, err := EncryptRequest(requestBody, c.cfg)
if err != nil {
return nil, fmt.Errorf("加密请求失败: %v", err)
}
BusiMainId = "202607301754241710448735"
encryptedBytes, _ := json.Marshal(encryptedReq)
url := c.cfg.OrderHost + c.cfg.MerchantId + ".htm?partnerTxSriNo=" + BusiMainId
// head 同时作为 HTTP Header 明文传输(对齐 YouChuKoffee 的 doPost 方式)
body, err := c.doPost(url, BusiMainId, "b2c.gatewaypay.orderQuery", encryptedBytes)
if err != nil {
return nil, err
}
responseData, err := c.DecryptResponse(string(body), false)
if err != nil {
return nil, fmt.Errorf("解密响应失败: %v原始响应: %s", err, string(body))
}
var result OrderQueryResponse
if err := json.Unmarshal([]byte(responseData), &result); err != nil {
return nil, fmt.Errorf("解析响应失败: %v响应内容: %s", err, responseData)
}
if result.RespCode != "" && result.RespCode != "0000" && result.RespCode != "00" {
return nil, fmt.Errorf("订单查询失败,错误码: %s错误信息: %s", result.RespCode, result.RespMsg)
}
return &result, nil
}