-
Notifications
You must be signed in to change notification settings - Fork 0
/
payment_report.go
45 lines (42 loc) · 1.58 KB
/
payment_report.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package WeixinPayV3
import (
"context"
"errors"
"fmt"
FinancePayMod "github.com/fotomxq/weeekj_core/v5/finance/pay/mod"
"github.com/wechatpay-apiv3/wechatpay-go/core/auth/verifiers"
"github.com/wechatpay-apiv3/wechatpay-go/core/downloader"
"github.com/wechatpay-apiv3/wechatpay-go/core/notify"
"github.com/wechatpay-apiv3/wechatpay-go/services/payments"
"net/http"
)
// ArgsPaymentReport 支付回调处理参数
type ArgsPaymentReport struct {
//组织ID
OrgID int64 `json:"orgID"`
}
// PaymentReport 支付回调处理
func PaymentReport(args *ArgsPaymentReport, request *http.Request) (*payments.Transaction, error) {
//隶属关系
args.OrgID = FinancePayMod.FixOrgID(args.OrgID)
//获取证书
_, clientConfig, err := getClient(args.OrgID)
if err != nil {
return &payments.Transaction{}, err
}
//获取平台证书访问器
certVisitor := downloader.MgrInstance().GetCertificateVisitor(clientConfig.MerchantID)
handler := notify.NewNotifyHandler(clientConfig.KeyV3, verifiers.NewSHA256WithRSAVerifier(certVisitor))
//解密数据包
transaction := new(payments.Transaction)
notifyReq, err := handler.ParseNotifyRequest(context.Background(), request, transaction)
// 如果验签未通过,或者解密失败
if err != nil {
err = errors.New(fmt.Sprint("parse notify request, org id: ", args.OrgID, ", merchant id: ", clientConfig.MerchantID, ", cert sn: ", clientConfig.CertSN, ", err: ", err))
return &payments.Transaction{}, err
}
if notifyReq.EventType == "TRANSACTION.SUCCESS" {
return transaction, nil
}
return nil, errors.New(fmt.Sprint("failed, ", notifyReq))
}