go实现微信支付

package api

import (
    "encoding/json"
    "encoding/xml"
    "fmt"
    "io/ioutil"
    "net/http"
    "strconv"
    "strings"
    "time"

    "zhoujinhui/model"

    "zhoujinhui/util"

    "github.com/globalsign/mgo/bson"
)

const APPID, SECRET = "wxxxxxxxxxx86", "06xxxxxxxxxxx08"
const WX_LOGIN_API = "https://api.weixin.qq.com/sns/jscode2session?grant_type=authorization_code&appid=" + APPID + "&secret=" + SECRET

const MCH_ID, MCH_KEY, TRADE_TYPE = "1xxxxxx2", "34xxxxxxxxx87", "JSAPI"
const NOTIFY_URL, WX_PAY_API = "https://admin.qhx-xhx.com/api/notifyRrl", "https://api.mch.weixin.qq.com/pay/unifiedorder"

func GetOpenId(w http.ResponseWriter, r *http.Request) {
    r.ParseForm()

    resp, err := http.Get(WX_LOGIN_API + "&js_code=" + r.Form.Get("res_code"))

    defer resp.Body.Close()
    if err == nil {
        body, _ := ioutil.ReadAll(resp.Body)

        var respStr = string(body)

        var jm map[string]string
        json.Unmarshal([]byte(respStr), &jm)

        if jm["openid"] != "" {
            if model.IsExistsWxUserByOpenId(jm["openid"]) {
                model.EditWxUser(jm["openid"])
            } else {
                model.InsertWxUser(jm["openid"])
            }
        }

        fmt.Fprint(w, respStr)
    }
}

func UpdateUserInfo(w http.ResponseWriter, r *http.Request) {
    r.ParseForm()
    openId := r.Form.Get("open_id")

    if openId != "" {
        var wxUser model.WU
        json.Unmarshal([]byte(r.Form.Get("user_info")), &

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
wxpay 是一个使用Go语言编写的微信支付商户平台SDK。举个栗子以查询企业付款API为栗:package main import (     "log"     "github.com/go-with/wxpay" ) const (     appId  = "" // 微信公众平台应用ID     mchId  = "" // 微信支付商户平台商户号     apiKey = "" // 微信支付商户平台API密钥     // 微信支付商户平台证书路径     certFile   = "cert/apiclient_cert.pem"     keyFile    = "cert/apiclient_key.pem"     rootcaFile = "cert/rootca.pem" ) func main() {     c := wxpay.NewClient(appId, mchId, apiKey)     // 附着商户证书     err := c.WithCert(certFile, keyFile, rootcaFile)     if err != nil {         log.Fatal(err)     }     params := make(wxpay.Params)     // 查询企业付款接口请求参数     params.SetString("appid", c.AppId)     params.SetString("mch_id", c.MchId)     params.SetString("nonce_str", "5K8264ILTKCH16CQ2502SI8ZNMTM67VS")  // 随机字符串     params.SetString("partner_trade_no", "10000098201411111234567890") // 商户订单号     params.SetString("sign", c.Sign(params))                           // 签名     // 查询企业付款接口请求URL     url := "https://api.mch.weixin.qq.com/mmpaymkttransfers/gettransferinfo"     // 发送查询企业付款请求     ret, err := c.Post(url, params, true)     if err != nil {         log.Fatal(err)     }     log.Print(ret) } 标签:wxpay

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值