2023-04-10 18:04:10 +08:00
|
|
|
// Copyright 2022 The Casdoor Authors. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package pp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-05-26 21:26:41 +08:00
|
|
|
"errors"
|
2023-04-10 18:04:10 +08:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/casdoor/casdoor/util"
|
|
|
|
"github.com/go-pay/gopay"
|
|
|
|
"github.com/go-pay/gopay/wechat/v3"
|
|
|
|
)
|
|
|
|
|
2023-05-26 21:26:41 +08:00
|
|
|
type WechatPayNotifyResponse struct {
|
|
|
|
Code string `json:"Code"`
|
|
|
|
Message string `json:"Message"`
|
|
|
|
}
|
|
|
|
|
2023-04-10 18:04:10 +08:00
|
|
|
type WechatPaymentProvider struct {
|
2023-05-30 19:57:30 +08:00
|
|
|
Client *wechat.ClientV3
|
|
|
|
appId string
|
2023-04-10 18:04:10 +08:00
|
|
|
}
|
|
|
|
|
2023-05-27 19:28:24 +08:00
|
|
|
func NewWechatPaymentProvider(mchId string, apiV3Key string, appId string, mchCertSerialNumber string, privateKey string) (*WechatPaymentProvider, error) {
|
|
|
|
if appId == "" && mchId == "" && mchCertSerialNumber == "" && apiV3Key == "" && privateKey == "" {
|
2023-05-26 21:26:41 +08:00
|
|
|
return &WechatPaymentProvider{}, nil
|
|
|
|
}
|
|
|
|
|
2023-04-10 18:04:10 +08:00
|
|
|
pp := &WechatPaymentProvider{appId: appId}
|
|
|
|
|
|
|
|
clientV3, err := wechat.NewClientV3(mchId, mchCertSerialNumber, apiV3Key, privateKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-05-26 21:26:41 +08:00
|
|
|
platformCert, serialNo, err := clientV3.GetAndSelectNewestCert()
|
2023-04-10 18:04:10 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-05-26 21:26:41 +08:00
|
|
|
|
2023-05-30 19:57:30 +08:00
|
|
|
pp.Client = clientV3.SetPlatformCert([]byte(platformCert), serialNo)
|
2023-05-26 21:26:41 +08:00
|
|
|
|
2023-04-10 18:04:10 +08:00
|
|
|
return pp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pp *WechatPaymentProvider) Pay(providerName string, productName string, payerName string, paymentName string, productDisplayName string, price float64, returnUrl string, notifyUrl string) (string, error) {
|
|
|
|
// pp.Client.DebugSwitch = gopay.DebugOn
|
|
|
|
|
|
|
|
bm := gopay.BodyMap{}
|
|
|
|
|
2023-05-26 21:26:41 +08:00
|
|
|
bm.Set("attach", joinAttachString([]string{productDisplayName, productName, providerName}))
|
|
|
|
bm.Set("appid", pp.appId)
|
|
|
|
bm.Set("description", productDisplayName)
|
2023-04-10 18:04:10 +08:00
|
|
|
bm.Set("notify_url", notifyUrl)
|
|
|
|
bm.Set("out_trade_no", paymentName)
|
2023-05-26 21:26:41 +08:00
|
|
|
bm.SetBodyMap("amount", func(bm gopay.BodyMap) {
|
|
|
|
bm.Set("total", int(price*100))
|
|
|
|
bm.Set("currency", "CNY")
|
|
|
|
})
|
2023-04-10 18:04:10 +08:00
|
|
|
|
2023-05-30 19:57:30 +08:00
|
|
|
wxRsp, err := pp.Client.V3TransactionNative(context.Background(), bm)
|
2023-04-10 18:04:10 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-05-26 21:26:41 +08:00
|
|
|
if wxRsp.Code != wechat.Success {
|
|
|
|
return "", errors.New(wxRsp.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return wxRsp.Response.CodeUrl, nil
|
2023-04-10 18:04:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pp *WechatPaymentProvider) Notify(request *http.Request, body []byte, authorityPublicKey string) (string, string, float64, string, string, error) {
|
2023-05-26 21:26:41 +08:00
|
|
|
notifyReq, err := wechat.V3ParseNotify(request)
|
2023-04-10 18:04:10 +08:00
|
|
|
if err != nil {
|
2023-05-26 21:26:41 +08:00
|
|
|
panic(err)
|
2023-04-10 18:04:10 +08:00
|
|
|
}
|
|
|
|
|
2023-05-30 19:57:30 +08:00
|
|
|
cert := pp.Client.WxPublicKey()
|
2023-05-26 21:26:41 +08:00
|
|
|
err = notifyReq.VerifySignByPK(cert)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", 0, "", "", err
|
|
|
|
}
|
2023-04-10 18:04:10 +08:00
|
|
|
|
2023-05-30 19:57:30 +08:00
|
|
|
apiKey := string(pp.Client.ApiV3Key)
|
2023-05-26 21:26:41 +08:00
|
|
|
result, err := notifyReq.DecryptCipherText(apiKey)
|
2023-04-10 18:04:10 +08:00
|
|
|
if err != nil {
|
2023-05-26 21:26:41 +08:00
|
|
|
return "", "", 0, "", "", err
|
2023-04-10 18:04:10 +08:00
|
|
|
}
|
|
|
|
|
2023-05-26 21:26:41 +08:00
|
|
|
paymentName := result.OutTradeNo
|
|
|
|
price := float64(result.Amount.PayerTotal) / 100
|
2023-04-10 18:04:10 +08:00
|
|
|
|
2023-05-26 21:26:41 +08:00
|
|
|
productDisplayName, productName, providerName, err := parseAttachString(result.Attach)
|
2023-04-10 18:04:10 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", "", 0, "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return productDisplayName, paymentName, price, productName, providerName, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pp *WechatPaymentProvider) GetInvoice(paymentName string, personName string, personIdCard string, personEmail string, personPhone string, invoiceType string, invoiceTitle string, invoiceTaxId string) (string, error) {
|
|
|
|
return "", nil
|
|
|
|
}
|
2023-05-26 21:26:41 +08:00
|
|
|
|
|
|
|
func (pp *WechatPaymentProvider) GetResponseError(err error) string {
|
|
|
|
response := &WechatPayNotifyResponse{
|
|
|
|
Code: "SUCCESS",
|
|
|
|
Message: "",
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
response.Code = "FAIL"
|
|
|
|
response.Message = err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
return util.StructToJson(response)
|
|
|
|
}
|