配置修改
This commit is contained in:
parent
c4c8bea3c1
commit
8c40855c4e
@ -1,74 +1,43 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"common/connection"
|
||||||
"github.com/wechatpay-apiv3/wechatpay-go/core"
|
"sync"
|
||||||
"github.com/wechatpay-apiv3/wechatpay-go/core/option"
|
|
||||||
"github.com/wechatpay-apiv3/wechatpay-go/services/payments/jsapi"
|
_ "common/resultStatus"
|
||||||
"github.com/wechatpay-apiv3/wechatpay-go/services/payments/native"
|
"common/webServer"
|
||||||
"github.com/wechatpay-apiv3/wechatpay-go/utils"
|
_ "paycenter/internal"
|
||||||
"log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
wg sync.WaitGroup
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
// 设置WaitGroup需要等待的数量,只要有一个服务器出现错误都停止服务器
|
||||||
|
wg.Add(1)
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var (
|
|
||||||
mchID string = "190000****" // 商户号
|
|
||||||
mchCertificateSerialNumber string = "3775B6A45ACD588826D15E583A95F5DD********" // 商户证书序列号
|
|
||||||
mchAPIv3Key string = "2ab9****************************" // 商户APIv3密钥
|
|
||||||
)
|
|
||||||
// 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
|
|
||||||
mchPrivateKey, err := utils.LoadPrivateKeyWithPath("/path/to/merchant/apiclient_key.pem")
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal("load merchant private key error")
|
|
||||||
}
|
|
||||||
ctx := context.Background()
|
|
||||||
// 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
|
|
||||||
opts := []core.ClientOption{
|
|
||||||
option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
|
|
||||||
}
|
|
||||||
client, err := core.NewClient(ctx, opts...)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("new wechat pay client err:%s", err)
|
|
||||||
}
|
|
||||||
// 以 Native 支付为例
|
|
||||||
svc := native.NativeApiService{Client: client}
|
|
||||||
// 发送请求
|
|
||||||
resp, result, err := svc.Prepay(ctx,
|
|
||||||
native.PrepayRequest{
|
|
||||||
Appid: core.String("wxd678efh567hg6787"),
|
|
||||||
Mchid: core.String("1900009191"),
|
|
||||||
Description: core.String("Image形象店-深圳腾大-QQ公仔"),
|
|
||||||
OutTradeNo: core.String("1217752501201407033233368018"),
|
|
||||||
Attach: core.String("自定义数据说明"),
|
|
||||||
NotifyUrl: core.String("https://www.weixin.qq.com/wxpay/pay.php"),
|
|
||||||
Amount: &native.Amount{
|
|
||||||
Total: core.Int64(100),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
// 使用微信扫描 resp.code_url 对应的二维码,即可体验Native支付
|
//加载配置
|
||||||
log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
|
loadConfig()
|
||||||
|
|
||||||
svc1 := jsapi.JsapiApiService{Client: client}
|
// 启动webserver
|
||||||
// 得到prepay_id,以及调起支付所需的参数和签名
|
go webServer.Start(&wg)
|
||||||
resp1, result, err := svc1.PrepayWithRequestPayment(ctx,
|
|
||||||
jsapi.PrepayRequest{
|
|
||||||
Appid: core.String("wxd678efh567hg6787"),
|
|
||||||
Mchid: core.String("1900009191"),
|
|
||||||
Description: core.String("Image形象店-深圳腾大-QQ公仔"),
|
|
||||||
OutTradeNo: core.String("1217752501201407033233368018"),
|
|
||||||
Attach: core.String("自定义数据说明"),
|
|
||||||
NotifyUrl: core.String("https://www.weixin.qq.com/wxpay/pay.php"),
|
|
||||||
Amount: &jsapi.Amount{
|
|
||||||
Total: core.Int64(100),
|
|
||||||
},
|
|
||||||
Payer: &jsapi.Payer{
|
|
||||||
Openid: core.String("oUpF8uMuAJO_M2pxb1Q9zNjWeS6o"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
|
|
||||||
// 使用微信扫描 resp.code_url 对应的二维码,即可体验Native支付
|
// 阻塞等待,以免main线程退出
|
||||||
log.Printf("status=%d resp=%s", result.Response.StatusCode, resp1)
|
wg.Wait()
|
||||||
|
}
|
||||||
|
|
||||||
|
// loadConfig 用于加载配置信息。
|
||||||
|
// 该函数会读取配置文件或环境变量中的设置,并根据这些设置初始化程序所需的配置。
|
||||||
|
// 目前函数的实现为空,需要根据实际的配置加载逻辑进行填充。
|
||||||
|
func loadConfig() {
|
||||||
|
|
||||||
|
//设置数据类型
|
||||||
|
connection.SetModelDB(connection.GetPayDB())
|
||||||
|
|
||||||
|
//构建数据库
|
||||||
|
connection.BuildDB()
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
module logincenter
|
module usercenter
|
||||||
|
|
||||||
go 1.22.10
|
go 1.22.10
|
||||||
|
|
||||||
|
|||||||
@ -6,7 +6,7 @@ import (
|
|||||||
|
|
||||||
_ "common/resultStatus"
|
_ "common/resultStatus"
|
||||||
"common/webServer"
|
"common/webServer"
|
||||||
_ "logincenter/internal/user"
|
_ "usercenter/internal/user"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user