@@ -24,6 +24,7 @@ type DefaultAlipayClient struct {
2424 ClientId string
2525 MerchantPrivateKey string
2626 AlipayPublicKey string
27+ AgentToken string
2728 IsSandboxMode bool
2829}
2930
@@ -33,20 +34,22 @@ type DefaultAlipayClient struct {
3334 * @param clientId 应用ID
3435 * @param merchantPrivateKey 商户私钥
3536 * @param alipayPublicKey 支付宝公钥
37+ * @param agentTokenOpt 代理token
3638 */
37- func NewDefaultAlipayClient (gatewayUrl string , clientId string , merchantPrivateKey string , alipayPublicKey string ) * DefaultAlipayClient {
38- isSandboxMode := false
39- if strings .HasPrefix (clientId , "SANDBOX_" ) {
40- isSandboxMode = true
41- }
39+ func NewDefaultAlipayClient (gatewayUrl , clientId , merchantPrivateKey , alipayPublicKey string , agentTokenOpt ... string ) * DefaultAlipayClient {
40+ isSandboxMode := strings .HasPrefix (clientId , "SANDBOX_" )
4241
43- return & DefaultAlipayClient {
42+ c := & DefaultAlipayClient {
4443 GatewayUrl : gatewayUrl ,
4544 ClientId : clientId ,
4645 MerchantPrivateKey : merchantPrivateKey ,
4746 AlipayPublicKey : alipayPublicKey ,
4847 IsSandboxMode : isSandboxMode ,
4948 }
49+ if len (agentTokenOpt ) > 0 {
50+ c .AgentToken = agentTokenOpt [0 ]
51+ }
52+ return c
5053}
5154
5255// @Description 支持任意方式的http请求
@@ -127,7 +130,7 @@ func (alipayClient *DefaultAlipayClient) Execute(alipayRequest *request.AlipayRe
127130 if err != nil {
128131 return nil , err
129132 }
130- header := buildBaseHeader (reqTime , alipayClient .ClientId , alipayRequest .KeyVersion , sign )
133+ header := buildBaseHeader (reqTime , alipayClient .ClientId , alipayRequest .KeyVersion , sign , alipayClient . AgentToken )
131134 alipayResponse , err := alipayClient .httpDo (alipayClient .GatewayUrl + path , httpMethod , map [string ]string {}, header , reqPayload , alipayRequest .AlipayResponse )
132135 if err != nil {
133136 return nil , err
@@ -148,7 +151,7 @@ func checkRspSign(httpMethod string, path string, clientId string, responseTime
148151 }
149152}
150153
151- func buildBaseHeader (reqTime string , clientId string , keyVersion string , signatureValue string ) map [string ]string {
154+ func buildBaseHeader (reqTime string , clientId string , keyVersion string , signatureValue string , agentToken string ) map [string ]string {
152155 if keyVersion == "" {
153156 keyVersion = "1"
154157 }
@@ -159,6 +162,8 @@ func buildBaseHeader(reqTime string, clientId string, keyVersion string, signatu
159162 "Client-Id" : clientId ,
160163 "Key-Version" : keyVersion ,
161164 "Signature" : signatureValue ,
165+ "SDK-VERSION" : "global-open-sdk-go" ,
166+ "agent-token" : agentToken ,
162167 }
163168
164169}
0 commit comments