1.引入的包

import ("context""crypto/x509""encoding/json""github.com/wechatpay-apiv3/wechatpay-go/core""github.com/wechatpay-apiv3/wechatpay-go/core/option"wxutils "github.com/wechatpay-apiv3/wechatpay-go/utils"
)

2.建立请求vx的client

type OrderService struct{}func (orderService *OrderService)SetUp()(opt []core.ClientOption, err error){//商户号mchID := global.GVA_CONFIG.VX.Payid//商户证书序列号mchCertSerialNumber := global.GVA_CONFIG.VX.CerSerial//商户私钥文件路径privateKeyPath := global.GVA_CONFIG.VX.PrivateKey//平台证书文件路径wechatCertificatePath := global.GVA_CONFIG.VX.Certificate// 加载商户私钥privateKey, err := wxutils.LoadPrivateKeyWithPath(privateKeyPath)if err != nil {fmt.Printf("load private err:%s", err.Error())return nil, err}// 加载微信支付平台证书wechatPayCertificate, err := wxutils.LoadCertificateWithPath(wechatCertificatePath)if err != nil {fmt.Printf("load certificate err:%s",err)return nil, err}//设置header头中authorization信息opts := []core.ClientOption{option.WithMerchantCredential(mchID, mchCertSerialNumber, privateKey), // 设置商户相关配置option.WithWechatPayCertificate([]*x509.Certificate{wechatPayCertificate}), // 设置微信支付平台证书,用于校验回包信息用option.WithoutValidator(),//跳过证书的效验}return opts, nil
}

3.生成预支付请求,并生成签名信息


func (orderService *OrderService)VxCreateOrder(openid string,mineOrder Order)(error, map[string]string) {// 初始化客户端ctx := context.TODO()opts, err := orderService.SetUp() //第二段代码中的函数if err != nil {return err, nil}client, err := core.NewClient(ctx, opts...)//_, err = core.NewClient(ctx, opts...)if err != nil{fmt.Printf("init client err:%s",err)return err, nil}//设置请求地址URL := "https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi"//设置请求信息,此处也可以使用结构体来进行请求mapInfo := map[string]interface{}{"mchid": global.GVA_CONFIG.VX.Payid,"out_trade_no": mineOrder.OrderId,"appid": global.GVA_CONFIG.VX.AppId,"description": mineOrder.BuyGoodsInfo(),"notify_url": global.GVA_CONFIG.VX.NotifyUrl,"amount": map[string]interface{}{"total": mineOrder.CurPriceCent(),"currency": "CNY",},"payer": map[string]interface{}{"openid": openid,},}//fmt.Print(URL,mapInfo)// 发起请求response, err := client.Post(ctx, URL, mapInfo)if err != nil{fmt.Printf("client post err:%s",err)return err, nil}// 校验回包内容是否有逻辑错误err = core.CheckResponse(response.Response)if err != nil{fmt.Printf("check response err:%s",err)return err, nil}// 读取回包信息body, err := ioutil.ReadAll(response.Response.Body)if err != nil{fmt.Printf("read response body err:%s",err)return err, nil}return nil, orderService.MakeReturnMsg(body)
}func (orderService *OrderService)MakeReturnMsg(body []byte)map[string]string{var prepaymap map[string]string_ = json.Unmarshal(body,&prepaymap)// 时间戳timeStamp:=strconv.FormatInt(time.Now().Unix(),10)// 任意长度的随机字符串即可nonceStr:=comFunc.RandStr(16)packageStr:="prepay_id="+prepaymap["prepay_id"]sb := strings.Builder{}sb.WriteString(global.GVA_CONFIG.VX.AppId+"\n")sb.WriteString(timeStamp+"\n")sb.WriteString(nonceStr+"\n")sb.WriteString(packageStr+"\n")privateKey, err := wxutils.LoadPrivateKeyWithPath(global.GVA_CONFIG.VX.PrivateKey)if err != nil{log.Println("load err:",err)}sign,err:= wxutils.SignSHA256WithRSA(sb.String(), privateKey)if err != nil{log.Println("sign error :",err)}resultMap:=make(map[string]string)resultMap["timeStamp"]=timeStampresultMap["nonceStr"]=nonceStrresultMap["package"]=packageStrresultMap["signType"]="RSA"resultMap["paySign"]=signresultMap["prepay_id"]=prepaymap["prepay_id"]return resultMap
}

4.收到vx的订单回传信息,注意是使用post回传的

// 使用gin的外部接口
func (orderApi *OrderApi) NotifyOrder(c *gin.Context) {body,_ := ioutil.ReadAll(c.Request.Body)b,s := orderService.DecodeNotify(body)log.Println(b,s)response.OkWithMessage("收到", c)
}// 直接使用接口解密
func (orderService *OrderService)DecodeNotify(body []byte)(bool, string){var prepaymap map[string]interface{}_ = json.Unmarshal(body,&prepaymap)var prepaymap2 = prepaymap["resource"].(map[string]interface{})nonce := prepaymap2["nonce"].(string)associatedData := prepaymap2["associated_data"].(string)ciphertext := prepaymap2["ciphertext"].(string)apiV3key := global.GVA_CONFIG.VX.Apiv3Keytx,err := wxutils.DecryptAES256GCM(apiV3key, associatedData,nonce,ciphertext)if err != nil{log.Println(err)return false,""}var datamap map[string]string_ = json.Unmarshal([]byte(tx),&datamap)return true, datamap["out_trade_no"]}

*获取商家序列号,参考文档 证书查看

$ openssl x509 -in xxx_xxxx_cert.pem -noout -serial

go接入微信小程序支付相关推荐

  1. spring boot接入微信小程序支付流程

    前言 正好最近项目中有需要做微信支付,跟着官方文档写下来坑还是踩了不少,于是写了这篇流程给自己长长记性,代码比较粗糙大家图一乐就好. 官方文档 官方接入指引--微信支付开发者文档 所用依赖 <d ...

  2. 新手指南:顶象验证码如何接入微信小程序?

    自2017年小程序发布以来,经过4年的快速发展,小程序已然成为企业互联网布局不可或缺的一环.无论是互联网企业还是拥抱互联网的传统企业,无论是服务导向型企业还是产品导向型企业,小程序都为用户提供了一种轻 ...

  3. 微擎支付返回商户单号_微信小程序支付流程

    微信支付之小程序支付 微信的支付方式有以下几种,不同的支付方式适用于不同的支付场景,而今天要给大家讲的就是 小程序支付 方式 说到支付功能就要涉及到金钱交易,必定是有比较严格的规范及流程,如要求小程序 ...

  4. 微信小程序支付java服务端集成采坑总结

    先上个微信小程序支付官方文档地址: https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_7&index=8 重点看 ...

  5. SpringBoot对接微信小程序支付功能开发(一,下单功能)

    1,接入前准备: 接入模式选择直连模式: 申请小程序,得到APPID,并开通微信支付: 申请微信商户号,得到mchid,并绑定APPID: 配置商户API key,下载并配置商户证书,根据微信官方文档 ...

  6. uniapp开发微信小程序支付

    uniapp开发微信小程序支付 1.申请接入微信支付 打开微信公众平台申请接入微信支付:https://mp.weixin.qq.com/ 2.调用统一下单和微信支付接口 uni.login({suc ...

  7. 微信小程序支付流程详解

    原创 Dr Hydra 码农参上 2020-11-22 11:00 收录于合集#微信开发技术3个 最近在工作中接入了一下微信小程序支付的功能,虽然说官方文档已经比较详细了,但在使用过程中还是踩了不少的 ...

  8. 微信小程序支付(java后端)

    微信支付文档传送门:https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_3 1.开发工具: idea+springclou ...

  9. 关于使用nodejs搭建微信小程序支付接口

    前言 前段时间在开发一个微信小程序的时候需要用到支付功能,我就去看了下微信支付的官方文档,好家伙,微信官方只提供了java.php还有Go语言的sdk.PHP我会点吧,但又不是很会,做为一个菜鸡前端, ...

最新文章

  1. 大数据竞赛平台——Kaggle 入门篇
  2. java饿汉式有啥作用,Java面试 - 什么是单例设计模式,为什么要使用单例设计模式,如何实现单例设计模式(饿汉式和懒汉式)?...
  3. PYTHON -MYSQLDB安装遇到的问题和解决办法
  4. python3 赋值与内存空间
  5. noip考python吗_青少年信奥联赛(NOIP)改名重推实锤!带着“不建议”硬刚到底?...
  6. 项目经理应该把30%的时间用在编程上
  7. UML 类图几种关系的总结
  8. 实战-130W表增加字段耗时
  9. 互联网晚报 | 8月12日 星期四 | 苏宁易购零售云将迈入“万店时代”;理想汽车今日港股上市;好未来励步推素质教育新产品...
  10. 嫁人要嫁IT男 ,嫁对了人天天都是情人节
  11. STM32之输入捕获
  12. 成为一个优秀网络工程师的条件
  13. 例2.1 排序 - 九度教程第1题(排序)
  14. ELMo ,LM:一串词序列的概率分布probability distribution over sequences of words
  15. Excel 连接 MySQL 导入数据 自定义 SQL (Excel 2016 + 适用)
  16. can bus测试工具
  17. Delphi XE10.4字体字号对应的Font Size的点或像素换算
  18. linux格式化挂载的硬盘,linux格式化和挂载硬盘
  19. SAP资产负债表实现方案探索 - 基于 VBA 自定义函数方法
  20. Linux input子系统上报键值失败问题

热门文章

  1. Azure Kinect DK + Ubuntu 18.04,从相机获取图像和点云数据
  2. DebugView使用说明
  3. Win10环境下Ctrl+C无法复制,Ctrl+V无法粘贴等问题解决方法
  4. 哔哩哔哩一直显示服务器繁忙,b站code504,为什么b站登录错误504
  5. 摄像头感应距离不到3米,这样的PSVR你会买吗?
  6. 手机通过USB3.0扩展坞也能连接到显示器、电视呢
  7. Cartoon CG:卡通渲染(tone-based-shading)
  8. 如何在Instagram上存档帖子(不删除它们)
  9. Java的四种引用类型
  10. 从零开发微信公众号系列 【一】账号准备及公众号类型简介