123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- package service
- import (
- "context"
- "fmt"
- log "github.com/sirupsen/logrus"
- "github.com/wechatpay-apiv3/wechatpay-go/core"
- "github.com/wechatpay-apiv3/wechatpay-go/core/option"
- "github.com/wechatpay-apiv3/wechatpay-go/services/payments/native"
- "github.com/wechatpay-apiv3/wechatpay-go/utils"
- "strconv"
- "time"
- "youngee_b_api/app/dao"
- "youngee_b_api/app/entity"
- "youngee_b_api/app/util"
- "youngee_b_api/app/vo"
- )
- type RechargeService struct{}
- // 充值管理——对公转账
- func (s RechargeService) TransferToPublic(param *vo.RechargeTransferParam) (*string, error) {
- var rechargeId string
- var phone string
- if param.SubAccountId == 0 {
- rechargeId = util.MakeRechargeId(param.EnterpriseId)
- phone, _ = dao.EnterpriseDao{}.GetEnterprisePhone(param.EnterpriseId)
- } else {
- rechargeId = util.MakeRechargeId(strconv.FormatInt(param.SubAccountId, 10))
- phone, _ = dao.SubAccountDao{}.GetSubAccountPhone(param.SubAccountId)
- }
- rechargeRecord := entity.RechargeRecord{
- RechargeID: rechargeId,
- EnterpriseID: param.EnterpriseId,
- RechargeAmount: param.Amount,
- TransferVoucherUrl: param.TransferVoucherUrl,
- Phone: phone,
- RechargeMethod: 1,
- Status: 1,
- InvoiceStatus: 1,
- CommitAt: time.Now(),
- }
- err := dao.RechargeRecordDao{}.Insert(&rechargeRecord)
- if err != nil {
- return nil, err
- }
- return &rechargeId, nil
- }
- func (s RechargeService) NativeApiServicePrepay(tradeId string, amount int64) (codeUrl string, err error) {
- var (
- mchID string = "1615933939" // 商户号
- mchCertificateSerialNumber string = "33DDFEC51BF5412F663B9B56510FD567B625FC68" // 商户证书序列号
- mchAPIv3Key string = "V10987654321younggee12345678910V" // 商户APIv3密钥,用于加密和解密平台证书
- )
- fmt.Println("充值的金额为:", amount)
- // 使用 utils 提供的函数从本地文件中加载商户私钥
- mchPrivateKey, err := utils.LoadPrivateKeyWithPath("./apiclient_key.pem") // 商户私钥,用于生成请求的签名
- if err != nil {
- log.Print("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.Printf("new wechat pay client err: %s", err)
- }
- // 采用 Native 支付方式
- svc := native.NativeApiService{Client: client}
- // 发送请求
- resp, result, err := svc.Prepay(ctx,
- native.PrepayRequest{
- Appid: core.String("wxac396a3be7a16844"),
- Mchid: core.String("1615933939"),
- Description: core.String("样叽微信支付充值"),
- OutTradeNo: core.String(tradeId),
- TimeExpire: core.Time(time.Now().Add(10 * time.Minute)),
- Attach: core.String("微信支付充值"),
- NotifyUrl: core.String("https://www.weixin.qq.com/wxpay/pay.php"),
- SupportFapiao: core.Bool(true),
- Amount: &native.Amount{
- Currency: core.String("CNY"),
- Total: core.Int64(amount),
- },
- },
- )
- if err != nil {
- // 处理错误
- log.Printf("call Prepay err:%s", err)
- return "", err
- } else {
- // 处理返回结果
- log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
- log.Println("codeUrl:", *resp.CodeUrl)
- }
- return *resp.CodeUrl, nil
- }
- func (s RechargeService) QueryOrderByTradeId(tradeId string) (tradeState string, err error) {
- var (
- mchID string = "1615933939" // 商户号
- mchCertificateSerialNumber string = "33DDFEC51BF5412F663B9B56510FD567B625FC68" // 商户证书序列号
- mchAPIv3Key string = "V10987654321younggee12345678910V" // 商户APIv3密钥
- )
- // 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
- mchPrivateKey, err := utils.LoadPrivateKeyWithPath("./apiclient_key.pem")
- if err != nil {
- log.Print("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.Printf("new wechat pay client err: %s", err)
- }
- svc := native.NativeApiService{Client: client}
- resp, result, err := svc.QueryOrderByOutTradeNo(ctx,
- native.QueryOrderByOutTradeNoRequest{
- OutTradeNo: core.String(tradeId),
- Mchid: core.String("1615933939"),
- },
- )
- fmt.Printf("支付 %+v\n", resp)
- if err != nil {
- // 处理错误
- log.Printf("call QueryOrderByOutTradeNo err: %s", err)
- return "", err
- } else {
- // 处理返回结果
- log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
- }
- fmt.Printf("支付状态 %+v\n", *resp.TradeState)
- return *resp.TradeState, nil
- }
|