mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-02 03:00:18 +08:00
feat: support wechat pay (#2312)
* feat: support wechat pay * feat: support wechat pay * feat: update wechatpay.go * feat: add router /qrcode
This commit is contained in:
@ -17,6 +17,7 @@ package pp
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/casdoor/casdoor/util"
|
||||
@ -31,17 +32,22 @@ type WechatPayNotifyResponse struct {
|
||||
|
||||
type WechatPaymentProvider struct {
|
||||
Client *wechat.ClientV3
|
||||
appId string
|
||||
AppId string
|
||||
}
|
||||
|
||||
func NewWechatPaymentProvider(mchId string, apiV3Key string, appId string, mchCertSerialNumber string, privateKey string) (*WechatPaymentProvider, error) {
|
||||
if appId == "" && mchId == "" && mchCertSerialNumber == "" && apiV3Key == "" && privateKey == "" {
|
||||
func NewWechatPaymentProvider(mchId string, apiV3Key string, appId string, serialNo string, privateKey string) (*WechatPaymentProvider, error) {
|
||||
// https://pay.weixin.qq.com/docs/merchant/products/native-payment/preparation.html
|
||||
// clientId => mchId
|
||||
// clientSecret => apiV3Key
|
||||
// clientId2 => appId
|
||||
|
||||
// appCertificate => serialNo
|
||||
// appPrivateKey => privateKey
|
||||
if appId == "" || mchId == "" || serialNo == "" || apiV3Key == "" || privateKey == "" {
|
||||
return &WechatPaymentProvider{}, nil
|
||||
}
|
||||
|
||||
pp := &WechatPaymentProvider{appId: appId}
|
||||
|
||||
clientV3, err := wechat.NewClientV3(mchId, mchCertSerialNumber, apiV3Key, privateKey)
|
||||
clientV3, err := wechat.NewClientV3(mchId, serialNo, apiV3Key, privateKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -50,73 +56,70 @@ func NewWechatPaymentProvider(mchId string, apiV3Key string, appId string, mchCe
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pp.Client = clientV3.SetPlatformCert([]byte(platformCert), serialNo)
|
||||
pp := &WechatPaymentProvider{
|
||||
Client: clientV3.SetPlatformCert([]byte(platformCert), serialNo),
|
||||
AppId: appId,
|
||||
}
|
||||
|
||||
return pp, nil
|
||||
}
|
||||
|
||||
func (pp *WechatPaymentProvider) Pay(providerName string, productName string, payerName string, paymentName string, productDisplayName string, price float64, currency string, returnUrl string, notifyUrl string) (string, string, error) {
|
||||
// pp.Client.DebugSwitch = gopay.DebugOn
|
||||
|
||||
bm := gopay.BodyMap{}
|
||||
|
||||
bm.Set("attach", joinAttachString([]string{productDisplayName, productName, providerName}))
|
||||
bm.Set("appid", pp.appId)
|
||||
bm.Set("appid", pp.AppId)
|
||||
bm.Set("description", productDisplayName)
|
||||
bm.Set("notify_url", notifyUrl)
|
||||
bm.Set("out_trade_no", paymentName)
|
||||
bm.SetBodyMap("amount", func(bm gopay.BodyMap) {
|
||||
bm.Set("total", int(price*100))
|
||||
bm.Set("currency", "CNY")
|
||||
bm.Set("total", priceFloat64ToInt64(price))
|
||||
bm.Set("currency", currency)
|
||||
})
|
||||
|
||||
wxRsp, err := pp.Client.V3TransactionNative(context.Background(), bm)
|
||||
nativeRsp, err := pp.Client.V3TransactionNative(context.Background(), bm)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
if wxRsp.Code != wechat.Success {
|
||||
return "", "", errors.New(wxRsp.Error)
|
||||
if nativeRsp.Code != wechat.Success {
|
||||
return "", "", errors.New(nativeRsp.Error)
|
||||
}
|
||||
|
||||
return wxRsp.Response.CodeUrl, "", nil
|
||||
return nativeRsp.Response.CodeUrl, paymentName, nil // Wechat can use paymentName as the OutTradeNo to query order status
|
||||
}
|
||||
|
||||
func (pp *WechatPaymentProvider) Notify(request *http.Request, body []byte, authorityPublicKey string, orderId string) (*NotifyResult, error) {
|
||||
notifyReq, err := wechat.V3ParseNotify(request)
|
||||
notifyResult := &NotifyResult{}
|
||||
queryRsp, err := pp.Client.V3TransactionQueryOrder(context.Background(), wechat.OutTradeNo, orderId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cert := pp.Client.WxPublicKey()
|
||||
err = notifyReq.VerifySignByPK(cert)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if queryRsp.Code != wechat.Success {
|
||||
return nil, errors.New(queryRsp.Error)
|
||||
}
|
||||
|
||||
apiKey := string(pp.Client.ApiV3Key)
|
||||
result, err := notifyReq.DecryptCipherText(apiKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
switch queryRsp.Response.TradeState {
|
||||
case "SUCCESS":
|
||||
// skip
|
||||
case "CLOSED":
|
||||
notifyResult.PaymentStatus = PaymentStateCanceled
|
||||
return notifyResult, nil
|
||||
case "NOTPAY", "USERPAYING": // not-pad: waiting for user to pay; user-paying: user is paying
|
||||
notifyResult.PaymentStatus = PaymentStateCreated
|
||||
return notifyResult, nil
|
||||
default:
|
||||
notifyResult.PaymentStatus = PaymentStateError
|
||||
notifyResult.NotifyMessage = fmt.Sprintf("unexpected wechat trade state: %v", queryRsp.Response.TradeState)
|
||||
return notifyResult, nil
|
||||
}
|
||||
|
||||
paymentName := result.OutTradeNo
|
||||
price := float64(result.Amount.PayerTotal) / 100
|
||||
|
||||
productDisplayName, productName, providerName, err := parseAttachString(result.Attach)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
notifyResult := &NotifyResult{
|
||||
productDisplayName, productName, providerName, _ := parseAttachString(queryRsp.Response.Attach)
|
||||
notifyResult = &NotifyResult{
|
||||
ProductName: productName,
|
||||
ProductDisplayName: productDisplayName,
|
||||
ProviderName: providerName,
|
||||
OrderId: orderId,
|
||||
Price: price,
|
||||
Price: priceInt64ToFloat64(int64(queryRsp.Response.Amount.Total)),
|
||||
PaymentStatus: PaymentStatePaid,
|
||||
PaymentName: paymentName,
|
||||
PaymentName: queryRsp.Response.OutTradeNo,
|
||||
}
|
||||
return notifyResult, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user