Browse Source

unionpay test

ls 5 years ago
parent
commit
44526cfc1f
4 changed files with 25 additions and 18 deletions
  1. 5 5
      http.go
  2. 15 13
      unionpay/cert.go
  3. 3 0
      unionpay/charging.go
  4. 2 0
      unionpay/v3.go

+ 5 - 5
http.go

@@ -40,10 +40,10 @@ type Message struct {
 	Cookies    []*http.Cookie
 }
 
-func header(w http.ResponseWriter, contentType string) {
-	w.Header().Set(`Content-Type`, contentType)
-	w.Header().Set(`X-Powered-By`, LibName+`/`+LibVersion)
-	w.WriteHeader(http.StatusOK)
+func header(w io.Writer, contentType string) {
+	w.(http.ResponseWriter).Header().Set(`Content-Type`, contentType)
+	w.(http.ResponseWriter).Header().Set(`X-Powered-By`, LibName+`/`+LibVersion)
+	w.(http.ResponseWriter).WriteHeader(http.StatusOK)
 }
 
 // Redirect redirect
@@ -61,7 +61,7 @@ func GetRealIP(req *http.Request) (ip string) {
 }
 
 // WriteJSON response JSON data.
-func WriteJSON(w http.ResponseWriter, response interface{}) error {
+func WriteJSON(w io.Writer, response interface{}) error {
 	header(w, `application/json; charset=utf-8`)
 	return json.NewEncoder(w).Encode(response)
 }

+ 15 - 13
unionpay/cert.go

@@ -7,6 +7,8 @@ import (
 	"errors"
 	"fmt"
 	"io/ioutil"
+
+	"golang.org/x/crypto/pkcs12"
 )
 
 // getPublicCert 获取加密证书信息
@@ -37,23 +39,23 @@ func getPrivateCert(filename, password string) (key *rsa.PrivateKey, cert *x509.
 		fmt.Println(err)
 		return
 	}
-	//*
-	block, _ := pem.Decode(bs)
-	if block == nil {
-		err = errors.New(`PrivateKey pem Decode error`)
-		return
-	}
-	cert = &x509.Certificate{}
-	private, err = x509.ParsePKCS8PrivateKey(block.Bytes)
-	// */
-
 	/*
-		private, cert, err = pkcs12.Decode(bs, password)
-		if err != nil {
+		block, _ := pem.Decode(bs)
+		if block == nil {
+			err = errors.New(`PrivateKey pem Decode error`)
 			return
 		}
-
+		cert = &x509.Certificate{}
+		private, err = x509.ParsePKCS8PrivateKey(block.Bytes)
 		// */
+
+	//*
+	private, cert, err = pkcs12.Decode(bs, password)
+	if err != nil {
+		return
+	}
+	//fmt.Println(cert.SerialNumber)
+	// */
 	key = private.(*rsa.PrivateKey)
 	return
 }

+ 3 - 0
unionpay/charging.go

@@ -2,6 +2,8 @@
 
 package unionpay
 
+import "fmt"
+
 // EntrustCharge 代扣费用
 func EntrustCharge(mi *MerchantInfo, oi OrderInfo, ci CustomerInfo) (res ReqRespose, err error) {
 	var body string
@@ -16,6 +18,7 @@ func EntrustCharge(mi *MerchantInfo, oi OrderInfo, ci CustomerInfo) (res ReqResp
 	if err != nil {
 		return
 	}
+	fmt.Println(body)
 	res, err = post(BaseHost+ReqBackTrans, []byte(body))
 	if err != nil {
 		return

+ 2 - 0
unionpay/v3.go

@@ -149,9 +149,11 @@ func V3Remove(mi *MerchantInfo, ci CustomerInfo, oi OrderInfo) (res ReqRespose,
 
 	s := createLinkString(args, true, false)
 	sha1 := util.SHA1(s)
+	//sha1 := util.SHA256(s)
 
 	var data []byte
 	data, err = signsha1(mi.PrivateKey, []byte(sha1))
+	//data, err = sign(mi.PrivateKey, []byte(sha1))
 	if err != nil {
 		return
 	}