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:
haiwu
2023-09-07 15:45:54 +08:00
committed by GitHub
parent 7318ee6e3a
commit 16cd09d175
12 changed files with 243 additions and 130 deletions

View File

@ -55,6 +55,7 @@ type Payment struct {
// Order Info
OutOrderId string `xorm:"varchar(100)" json:"outOrderId"`
PayUrl string `xorm:"varchar(2000)" json:"payUrl"`
SuccessUrl string `xorm:"varchar(2000)" json:"successUrl""` // `successUrl` is redirected from `payUrl` after pay success
State pp.PaymentState `xorm:"varchar(100)" json:"state"`
Message string `xorm:"varchar(2000)" json:"message"`
}
@ -152,7 +153,7 @@ func DeletePayment(payment *Payment) (bool, error) {
return affected != 0, nil
}
func notifyPayment(request *http.Request, body []byte, owner string, paymentName string, orderId string) (*Payment, *pp.NotifyResult, error) {
func notifyPayment(request *http.Request, body []byte, owner string, paymentName string) (*Payment, *pp.NotifyResult, error) {
payment, err := getPayment(owner, paymentName)
if err != nil {
return nil, nil, err
@ -180,11 +181,7 @@ func notifyPayment(request *http.Request, body []byte, owner string, paymentName
return nil, nil, err
}
if orderId == "" {
orderId = payment.OutOrderId
}
notifyResult, err := pProvider.Notify(request, body, cert.AuthorityPublicKey, orderId)
notifyResult, err := pProvider.Notify(request, body, cert.AuthorityPublicKey, payment.OutOrderId)
if err != nil {
return payment, nil, err
}
@ -205,8 +202,8 @@ func notifyPayment(request *http.Request, body []byte, owner string, paymentName
return payment, notifyResult, nil
}
func NotifyPayment(request *http.Request, body []byte, owner string, paymentName string, orderId string) (*Payment, error) {
payment, notifyResult, err := notifyPayment(request, body, owner, paymentName, orderId)
func NotifyPayment(request *http.Request, body []byte, owner string, paymentName string) (*Payment, error) {
payment, notifyResult, err := notifyPayment(request, body, owner, paymentName)
if payment != nil {
if err != nil {
payment.State = pp.PaymentStateError

View File

@ -158,24 +158,23 @@ func (product *Product) getProvider(providerName string) (*Provider, error) {
return provider, nil
}
func BuyProduct(id string, user *User, providerName, pricingName, planName, host string) (string, string, error) {
func BuyProduct(id string, user *User, providerName, pricingName, planName, host string) (*Payment, error) {
product, err := GetProduct(id)
if err != nil {
return "", "", err
return nil, err
}
if product == nil {
return "", "", fmt.Errorf("the product: %s does not exist", id)
return nil, fmt.Errorf("the product: %s does not exist", id)
}
provider, err := product.getProvider(providerName)
if err != nil {
return "", "", err
return nil, err
}
pProvider, _, err := provider.getPaymentProvider()
if err != nil {
return "", "", err
return nil, err
}
owner := product.Owner
@ -192,15 +191,15 @@ func BuyProduct(id string, user *User, providerName, pricingName, planName, host
if pricingName != "" && planName != "" {
plan, err := GetPlan(util.GetId(owner, planName))
if err != nil {
return "", "", err
return nil, err
}
if plan == nil {
return "", "", fmt.Errorf("the plan: %s does not exist", planName)
return nil, fmt.Errorf("the plan: %s does not exist", planName)
}
sub := NewSubscription(owner, user.Name, plan.Name, paymentName, plan.Period)
_, err = AddSubscription(sub)
if err != nil {
return "", "", err
return nil, err
}
returnUrl = fmt.Sprintf("%s/buy-plan/%s/%s/result?subscription=%s", originFrontend, owner, pricingName, sub.Name)
}
@ -208,10 +207,10 @@ func BuyProduct(id string, user *User, providerName, pricingName, planName, host
// Create an OrderId and get the payUrl
payUrl, orderId, err := pProvider.Pay(providerName, productName, payerName, paymentName, productDisplayName, product.Price, product.Currency, returnUrl, notifyUrl)
if err != nil {
return "", "", err
return nil, err
}
// Create a Payment linked with Product and Order
payment := Payment{
payment := &Payment{
Owner: product.Owner,
Name: paymentName,
CreatedTime: util.GetCurrentTime(),
@ -230,6 +229,7 @@ func BuyProduct(id string, user *User, providerName, pricingName, planName, host
User: user.Name,
PayUrl: payUrl,
SuccessUrl: returnUrl,
State: pp.PaymentStateCreated,
OutOrderId: orderId,
}
@ -238,15 +238,15 @@ func BuyProduct(id string, user *User, providerName, pricingName, planName, host
payment.State = pp.PaymentStatePaid
}
affected, err := AddPayment(&payment)
affected, err := AddPayment(payment)
if err != nil {
return "", "", err
return nil, err
}
if !affected {
return "", "", fmt.Errorf("failed to add payment: %s", util.StructToJson(payment))
return nil, fmt.Errorf("failed to add payment: %s", util.StructToJson(payment))
}
return payUrl, orderId, err
return payment, err
}
func ExtendProductWithProviders(product *Product) error {