PayRouter.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package router
  2. import (
  3. "context"
  4. "demo/configs"
  5. "github.com/wechatpay-apiv3/wechatpay-go/core"
  6. "github.com/wechatpay-apiv3/wechatpay-go/core/option"
  7. "github.com/wechatpay-apiv3/wechatpay-go/services/payments/native"
  8. "log"
  9. )
  10. func main() {
  11. ctx := context.Background()
  12. // 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
  13. opts := []core.ClientOption{
  14. option.WithWechatPayAutoAuthCipher(configs.WxPay.MchID, configs.WxPay.MchCertificateSerialNumber, configs.WxPay.PrivateKey, configs.WxPay.MchAPIv3Key),
  15. }
  16. client, err := core.NewClient(ctx, opts...)
  17. if err != nil {
  18. log.Fatalf("new wechat pay client err:%s", err)
  19. }
  20. // 以 Native 支付为例
  21. svc := native.NativeApiService{Client: client}
  22. // 发送请求
  23. resp, result, err := svc.Prepay(ctx,
  24. native.PrepayRequest{
  25. Appid: core.String("wxd678efh567hg6787"),
  26. Mchid: core.String("1900009191"),
  27. Description: core.String("Image形象店-深圳腾大-QQ公仔"),
  28. OutTradeNo: core.String("1217752501201407033233368018"),
  29. Attach: core.String("自定义数据说明"),
  30. NotifyUrl: core.String("https://www.weixin.qq.com/wxpay/pay.php"),
  31. Amount: &native.Amount{
  32. Total: core.Int64(100),
  33. },
  34. },
  35. )
  36. // 使用微信扫描 resp.code_url 对应的二维码,即可体验Native支付
  37. log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
  38. }