golang 微信商户平台支付平台V3

微信V3 普通商户支付

  1. V3介绍
    在这里插入图片描述

  2. 开发文档

    微信官方商户平平台开发者文档
    https://pay.weixin.qq.com/wiki/doc/apiv3/index.shtml

  3. 如何获取证书序列号
    获取证书序列号

    使用 openssl 获取序列号如下:

    openssl x509 -in apiclient_cert.pem -noout -serial

    获取序列号

package main

import (
    "context"
    "crypto/x509"
    "io/ioutil"
    "log"
    "net/http"
    "time"

    "github.com/wechatpay-apiv3/wechatpay-go/core"
    "github.com/wechatpay-apiv3/wechatpay-go/core/option"
    "github.com/wechatpay-apiv3/wechatpay-go/utils"
)

const (
    mchID                      = "" // 商户ID
    mchCertificateSerialNumber = "" // 证书序列号
)

func main() {
    // 通过私钥的文件路径内容加载私钥
    privateKey, err := utils.LoadPrivateKeyWithPath("apiclient_key.pem")
    public, err := utils.LoadCertificateWithPath("apiclient_cert.pem")

    // 增加客户端配置
    ctx := context.Background()
    opts := []option.ClientOption{
        option.WithMerchant(mchID, mchCertificateSerialNumber, privateKey), // 设置商户信息,用于生成签名信息
        option.WithWechatPay([]*x509.Certificate{public}),                  // 设置微信支付平台证书信息,对回包进行校验
        option.WithHTTPClient(&http.Client{}),                              // 可以不设置
        option.WithTimeout(2 * time.Second),                                // 自行进行超时时间配置
    }
    client, err := core.NewClient(ctx, opts...)
    if err != nil {
        log.Printf("new wechat pay client err:%s", err.Error())
        return
    }
    // 后面可以开始写你的逻辑
    var H5 = map[string]interface{}{
        "appid":     "wx862e63bb06c70539",
        "mchid":     mchID,
        "description":  "商品支付",
        "out_trade_no": time.Now().Format("20060102150405"),
        "notify_url":   "http://127.0.0.1/v1/orde",
        "amount": map[string]interface{}{
            "total":100, // 支付单位分
            "currency": "CNY",
        },
        "scene_info": map[string]interface{}{
            "device_id":"12346785",
            "payer_client_ip":"192.168.8.123",
            "h5_info": map[string]interface{}{
                "type": "iOSAndroidWap",
            },
        },

    }
	// h5支付为例
    url := "https://api.mch.weixin.qq.com/v3/pay/transactions/h5"
    response, err := client.Post(ctx, url, H5)
    if err != nil {
        log.Printf("weChatPayHttpClient Get:%s err:%s", url, err.Error())
        return
    }
    if response.Body != nil {
        defer response.Body.Close()
    }
    body, err := ioutil.ReadAll(response.Body)
    if err != nil {
        log.Printf("read response body err:%s", err.Error())
        return
    }
    log.Printf("response body:%s", string(body))

}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值