Add InvoicePayment() API.

This commit is contained in:
Gucheng Wang
2022-04-26 22:17:45 +08:00
parent cab51fae9c
commit cf3b46130b
15 changed files with 248 additions and 10 deletions

View File

@ -53,6 +53,7 @@ type Payment struct {
InvoiceTitle string `xorm:"varchar(100)" json:"invoiceTitle"`
InvoiceTaxId string `xorm:"varchar(100)" json:"invoiceTaxId"`
InvoiceRemark string `xorm:"varchar(100)" json:"invoiceRemark"`
InvoiceUrl string `xorm:"varchar(100)" json:"invoiceUrl"`
}
func GetPaymentCount(owner, field, value string) int {
@ -206,6 +207,46 @@ func NotifyPayment(request *http.Request, body []byte, owner string, providerNam
return ok
}
func invoicePayment(payment *Payment) (string, error) {
provider := getProvider(payment.Owner, payment.Provider)
if provider == nil {
return "", fmt.Errorf("the payment provider: %s does not exist", payment.Provider)
}
pProvider, _, err := provider.getPaymentProvider()
if err != nil {
return "", err
}
invoiceUrl, err := pProvider.GetInvoice(payment.Name, payment.PersonName, payment.PersonIdCard, payment.PersonEmail, payment.PersonPhone, payment.InvoiceType, payment.InvoiceTitle, payment.InvoiceTaxId)
if err != nil {
return "", err
}
return invoiceUrl, nil
}
func InvoicePayment(payment *Payment) bool {
if payment.State != "Paid" {
return false
}
invoiceUrl, err := invoicePayment(payment)
if err != nil {
payment.State = "Error"
payment.Message = err.Error()
} else {
payment.State = "Invoiced"
payment.InvoiceUrl = invoiceUrl
}
UpdatePayment(payment.GetId(), payment)
ok := err == nil
return ok
}
func (payment *Payment) GetId() string {
return fmt.Sprintf("%s/%s", payment.Owner, payment.Name)
}