Fix NotifyPayment().

This commit is contained in:
Yang Luo
2022-03-13 19:57:23 +08:00
parent 55a84644e1
commit fde8c4b5f6
5 changed files with 50 additions and 25 deletions

View File

@ -44,6 +44,7 @@ type Payment struct {
PayUrl string `xorm:"varchar(2000)" json:"payUrl"` PayUrl string `xorm:"varchar(2000)" json:"payUrl"`
ReturnUrl string `xorm:"varchar(1000)" json:"returnUrl"` ReturnUrl string `xorm:"varchar(1000)" json:"returnUrl"`
State string `xorm:"varchar(100)" json:"state"` State string `xorm:"varchar(100)" json:"state"`
Message string `xorm:"varchar(1000)" json:"message"`
} }
func GetPaymentCount(owner, field, value string) int { func GetPaymentCount(owner, field, value string) int {
@ -142,7 +143,7 @@ func DeletePayment(payment *Payment) bool {
return affected != 0 return affected != 0
} }
func NotifyPayment(bm gopay.BodyMap) bool { func notifyPayment(bm gopay.BodyMap) (*Payment, error) {
owner := "admin" owner := "admin"
productName := bm.Get("subject") productName := bm.Get("subject")
paymentId := bm.Get("out_trade_no") paymentId := bm.Get("out_trade_no")
@ -153,52 +154,60 @@ func NotifyPayment(bm gopay.BodyMap) bool {
product := getProduct(owner, productId) product := getProduct(owner, productId)
if product == nil { if product == nil {
panic(fmt.Errorf("the product: %s does not exist", productId)) return nil, fmt.Errorf("the product: %s does not exist", productId)
} }
if productName != product.DisplayName { if productName != product.DisplayName {
panic(fmt.Errorf("the payment's product name: %s doesn't equal to the expected product name: %s", productName, product.DisplayName)) return nil, fmt.Errorf("the payment's product name: %s doesn't equal to the expected product name: %s", productName, product.DisplayName)
} }
if price != product.Price { if price != product.Price {
panic(fmt.Errorf("the payment's price: %f doesn't equal to the expected price: %f", price, product.Price)) return nil, fmt.Errorf("the payment's price: %f doesn't equal to the expected price: %f", price, product.Price)
} }
payment := getPayment(owner, paymentId) payment := getPayment(owner, paymentId)
if payment == nil { if payment == nil {
panic(fmt.Errorf("the payment: %s does not exist", paymentId)) return nil, fmt.Errorf("the payment: %s does not exist", paymentId)
} }
provider, err := product.getProvider(providerId) provider, err := product.getProvider(providerId)
if err != nil { if err != nil {
panic(err) return payment, err
} }
cert := getCert(owner, provider.Cert) cert := getCert(owner, provider.Cert)
if cert == nil { if cert == nil {
panic(fmt.Errorf("the cert: %s does not exist", provider.Cert)) return payment, fmt.Errorf("the cert: %s does not exist", provider.Cert)
} }
ok, err := alipay.VerifySignWithCert(cert.AuthorityPublicKey, bm) ok, err := alipay.VerifySignWithCert(cert.AuthorityPublicKey, bm)
if err != nil { if err != nil {
panic(err) return payment, err
} }
if ok { if !ok {
payment.State = "Paid" return payment, fmt.Errorf("VerifySignWithCert() failed: %v", ok)
} else { }
if cert == nil {
panic(fmt.Errorf("VerifySignWithCert() failed: %v", ok)) return payment, nil
}
func NotifyPayment(bm gopay.BodyMap) bool {
payment, err := notifyPayment(bm)
if payment != nil {
if err != nil {
payment.State = "Error"
payment.Message = err.Error()
} else {
payment.State = "Paid"
} }
//payment.State = "Failed"
UpdatePayment(payment.GetId(), payment)
} }
affected, err := adapter.Engine.ID(core.PK{owner, paymentId}).AllCols().Update(payment) ok := err == nil
if err != nil { return ok
panic(err)
}
return affected != 0
} }
func (payment *Payment) GetId() string { func (payment *Payment) GetId() string {

View File

@ -179,7 +179,7 @@ func BuyProduct(id string, providerId string, user *User, host string) (string,
productId := product.Name productId := product.Name
originFrontend, originBackend := getOriginFromHost(host) originFrontend, originBackend := getOriginFromHost(host)
returnUrl := fmt.Sprintf("%s/payments/%s", originFrontend, paymentId) returnUrl := fmt.Sprintf("%s/payments/%s/result", originFrontend, paymentId)
notifyUrl := fmt.Sprintf("%s/api/notify-payment", originBackend) notifyUrl := fmt.Sprintf("%s/api/notify-payment", originBackend)
payUrl, err := pProvider.Pay(productName, productId, providerId, paymentId, product.Price, returnUrl, notifyUrl) payUrl, err := pProvider.Pay(productName, productId, providerId, paymentId, product.Price, returnUrl, notifyUrl)

View File

@ -50,12 +50,13 @@ func (pp *AlipayPaymentProvider) Pay(productName string, productId string, provi
priceString := strings.TrimRight(strings.TrimRight(fmt.Sprintf("%.2f", price), "0"), ".") priceString := strings.TrimRight(strings.TrimRight(fmt.Sprintf("%.2f", price), "0"), ".")
bm := gopay.BodyMap{} bm := gopay.BodyMap{}
bm.Set("subject", productName)
bm.Set("out_trade_no", paymentId)
bm.Set("total_amount", priceString)
bm.Set("return_url", returnUrl) bm.Set("return_url", returnUrl)
bm.Set("notify_url", notifyUrl) bm.Set("notify_url", notifyUrl)
bm.Set("subject", productName)
bm.Set("out_trade_no", paymentId)
bm.Set("total_amount", priceString)
bm.Set("productId", productId) bm.Set("productId", productId)
bm.Set("providerId", productId) bm.Set("providerId", productId)

View File

@ -160,6 +160,16 @@ class PaymentEditPage extends React.Component {
}} /> }} />
</Col> </Col>
</Row> </Row>
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={(Setting.isMobile()) ? 22 : 2}>
{Setting.getLabel(i18next.t("payment:Message"), i18next.t("payment:Message - Tooltip"))} :
</Col>
<Col span={22} >
<Input value={this.state.payment.message} onChange={e => {
// this.updatePaymentField('message', e.target.value);
}} />
</Col>
</Row>
</Card> </Card>
) )
} }

View File

@ -36,9 +36,14 @@ class PaymentListPage extends BaseListPage {
user: "admin", user: "admin",
productId: "computer-1", productId: "computer-1",
productName: "A notebook computer", productName: "A notebook computer",
price: 300.00, detail: "This is a computer with excellent CPU, memory and disk",
tag: "Promotion-1",
currency: "USD", currency: "USD",
price: 300.00,
payUrl: "https://pay.com/pay.php",
returnUrl: "https://door.casdoor.com/payments",
state: "Paid", state: "Paid",
message: "",
} }
} }