notice.go 4.96 KB
Newer Older
1 2 3
package pay

import (
wangp's avatar
wangp committed
4 5 6 7 8 9 10
	"crypto"
	"crypto/rsa"
	"crypto/sha256"
	"crypto/x509"
	"encoding/base64"
	"encoding/pem"
	"errors"
wangp's avatar
wangp committed
11
	"fmt"
12
	"github.com/gin-gonic/gin"
wangp's avatar
wangp committed
13
	"io/ioutil"
14
	"system_pay/controller/base"
wangp's avatar
wangp committed
15 16
	"system_pay/models"
	"system_pay/repository/pay"
17 18 19 20 21 22 23 24 25 26 27 28
)

// 卡拉卡统一支付回调
type NoticeController struct {
}

// WxNotice 拉卡拉统一支付微信回调
// @Summary 拉卡拉统一支付微信回调
// @Description 拉卡拉统一支付微信回调
// @Tags 拉卡拉统一支付回调
// @Accept application/json
// @Produce application/json
wangp's avatar
wangp committed
29
// @Param   body    body  models.WxNoticeInput   true	 "参数"
30 31 32 33 34
// @Param language header string ture "语言类型 zh-CN简体中文 en-US英文 ja 日文 默认中文"
// @Success 200
// @router /api/v1/pay/wx_notice [post]
func (l *PayController) WxNotice(c *gin.Context) {

wangp's avatar
wangp committed
35 36
	fmt.Println("拉卡拉微信回调start")

wangp's avatar
wangp committed
37 38 39 40 41 42 43 44 45 46 47
	//ph2 := make(map[string]interface{}, 0)
	////ph2 = new(interface{})
	//err := c.ShouldBind(ph2)
	//if err != nil {
	//	response := new(base.ResponseDataWxNotice)
	//	response.Code = "FAIL"
	//	response.Message = "执行失败1"
	//	base.ResponseWxNotice(c, response)
	//	return
	//}
	//fmt.Println(ph2)
wangp's avatar
wangp committed
48

wangp's avatar
wangp committed
49
	//验证签名
wangp's avatar
wangp committed
50 51 52 53 54 55 56 57 58
	//_, err := verySignByPublicKey(c)
	//if err != nil {
	//	fmt.Println("verySignByPublicKey err: ", err)
	//	response := new(base.ResponseDataWxNotice)
	//	response.Code = "FAIL"
	//	response.Message = "验证签名失败"
	//	base.ResponseWxNotice(c, response)
	//	return
	//}
wangp's avatar
wangp committed
59

wangp's avatar
wangp committed
60
	ph := new(models.WxNoticeInput)
wangp's avatar
wangp committed
61
	err := c.ShouldBindJSON(ph)
wangp's avatar
wangp committed
62 63 64 65 66 67 68
	if err != nil {
		response := new(base.ResponseDataWxNotice)
		response.Code = "FAIL"
		response.Message = "执行失败2"
		base.ResponseWxNotice(c, response)
		return
	}
wangp's avatar
wangp committed
69
	fmt.Println(ph)
wangp's avatar
wangp committed
70 71 72 73 74

	// 拉卡拉统一支付微信回调
	response, err := pay.WxNotice(ph)

	fmt.Println("拉卡拉微信回调end")
75 76
	base.ResponseWxNotice(c, response)
}
wangp's avatar
wangp committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

//func verify(c *gin.Context) error {
//	//String appid = getHeadValue(response, "Lklapi-Appid");
//	//String lklapiSerial = getHeadValue(response, "Lklapi-Serial");
//	//String timestamp = getHeadValue(response, "Lklapi-Timestamp");
//	//String nonce = getHeadValue(response, "Lklapi-Nonce");
//	//String signature = getHeadValue(response, "Lklapi-Signature");
//	//String responseStr = IOUtils.toString(response.getEntity().getContent(), ENCODING);
//	//
//	//System.out.println("responseStr  " + responseStr);
//	//
//	//String source = appid + "\n" + lklapiSerial + "\n" + timestamp + "\n" + nonce + "\n" + responseStr + "\n";
//
//	appid := c.GetHeader("Lklapi-Appid")
//	lklapiSerial := c.GetHeader("Lklapi-Serial")
//	timestamp := c.GetHeader("Lklapi-Timestamp")
//	nonce := c.GetHeader("Lklapi-Nonce")
//	signature := c.GetHeader("Lklapi-Signature")
//	responseStr := c.GetHeader("")
//
//	source := appid + "\n" + lklapiSerial + "\n" + timestamp + "\n" + nonce + "\n" + responseStr + "\n";
//
//	rtn, err := utils.VerifyDerCert([]byte(source), "./cert/lkl-apigw-v2.cer")
//	if err == nil {
//		fmt.Println("验证签名成功!")
//		return errors.New("验证签名成功!")
//	} else {
//		fmt.Println("验证签名失败!")
//		return err
//	}
//	fmt.Println(rtn)
//	return errors.New("wangpei签名测试中。。。。。")
//}

wangp's avatar
wangp committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
func verySignByPublicKey(c *gin.Context) (bool, error) {

	//publicKey string, data []byte, signData []byte
	//String responseStr = IOUtils.toString(response.getEntity().getContent(), ENCODING);
	appid := c.GetHeader("Lklapi-Appid")
	lklapiSerial := c.GetHeader("Lklapi-Serial")
	timestamp := c.GetHeader("Lklapi-Timestamp")
	nonce := c.GetHeader("Lklapi-Nonce")
	signature := c.GetHeader("Lklapi-Signature")
	responseStr := c.GetHeader("")

	data := appid + "\n" + lklapiSerial + "\n" + timestamp + "\n" + nonce + "\n" + responseStr + "\n";
fmt.Println("verySignByPublicKey start...")
fmt.Println(data)
fmt.Println("signature="+signature)

	// 1、读取公钥文件,获取公钥字节
wangp's avatar
wangp committed
128 129
	//path_cert := setting.Conf.Lakala.BkPathCert
	publicKey, err := ioutil.ReadFile("./cert/dev/lkl-apigw-v2.cer")
wangp's avatar
wangp committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
	if err != nil {
		return false, err
	}
	// 2、解码私钥字节,生成加密对象
	block, _ := pem.Decode(publicKey)
	if block == nil {
		return false, errors.New("私钥信息错误!")
	}
	// 3、解析DER编码的私钥,生成私钥对象
	pubKey, err := x509.ParsePKIXPublicKey(block.Bytes)
	if err != nil {
		return false, err
	}

	hashed := sha256.Sum256([]byte(data))

	fmt.Println("pubKey.(*rsa.PublicKey): ", pubKey.(*rsa.PublicKey))

	pubK := pubKey.(*rsa.PublicKey)

	sig, err := base64.StdEncoding.DecodeString(signature)

	err = rsa.VerifyPKCS1v15(pubK, crypto.SHA256, hashed[:], sig)
	if err != nil {
		return false, err
	}

	return true, nil
}

wangp's avatar
wangp committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173
//const (
//	PEM_BEGIN = "-----BEGIN RSA PRIVATE KEY-----\n"
//	PEM_END   = "\n-----END RSA PRIVATE KEY-----"
//)
//
//func FormatPrivateKey(privateKey string) string {
//	if !strings.HasPrefix(privateKey, PEM_BEGIN) {
//		privateKey = PEM_BEGIN + privateKey
//	}
//	if !strings.HasSuffix(privateKey, PEM_END) {
//		privateKey = privateKey + PEM_END
//	}
//	return privateKey
//}