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

@ -158,3 +158,17 @@ func (c *ApiController) NotifyPayment() {
panic(fmt.Errorf("NotifyPayment() failed: %v", ok))
}
}
// @Title InvoicePayment
// @Tag Payment API
// @Description invoice payment
// @Param id query string true "The id of the payment"
// @Success 200 {object} controllers.Response The Response object
// @router /invoice-payment [post]
func (c *ApiController) InvoicePayment() {
id := c.Input().Get("id")
payment := object.GetPayment(id)
c.Data["json"] = wrapActionResponse(object.InvoicePayment(payment))
c.ServeJSON()
}

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)
}

View File

@ -90,3 +90,7 @@ func (pp *AlipayPaymentProvider) Notify(request *http.Request, body []byte, auth
return productDisplayName, paymentName, price, productName, providerName, nil
}
func (pp *AlipayPaymentProvider) GetInvoice(paymentName string, personName string, personIdCard string, personEmail string, personPhone string, invoiceType string, invoiceTitle string, invoiceTaxId string) (string, error) {
return "", nil
}

View File

@ -87,6 +87,27 @@ type GcResponseBody struct {
Sign string `json:"sign"`
}
type GcInvoiceReqInfo struct {
BusNo string `json:"busno"`
PayerName string `json:"payername"`
IdNum string `json:"idnum"`
PayerType string `json:"payertype"`
InvoiceTitle string `json:"invoicetitle"`
Tin string `json:"tin"`
Phone string `json:"phone"`
Email string `json:"email"`
}
type GcInvoiceRespInfo struct {
BusNo string `json:"busno"`
State string `json:"state"`
EbillCode string `json:"ebillcode"`
EbillNo string `json:"ebillno"`
CheckCode string `json:"checkcode"`
Url string `json:"url"`
Content string `json:"content"`
}
func NewGcPaymentProvider(clientId string, clientSecret string, host string) *GcPaymentProvider {
pp := &GcPaymentProvider{}
@ -230,3 +251,70 @@ func (pp *GcPaymentProvider) Notify(request *http.Request, body []byte, authorit
return productDisplayName, paymentName, price, productName, providerName, nil
}
func (pp *GcPaymentProvider) GetInvoice(paymentName string, personName string, personIdCard string, personEmail string, personPhone string, invoiceType string, invoiceTitle string, invoiceTaxId string) (string, error) {
payerType := "0"
if invoiceType == "Organization" {
payerType = "1"
}
invoiceReqInfo := GcInvoiceReqInfo{
BusNo: paymentName,
PayerName: personName,
IdNum: personIdCard,
PayerType: payerType,
InvoiceTitle: invoiceTitle,
Tin: invoiceTaxId,
Phone: personPhone,
Email: personEmail,
}
b, err := json.Marshal(invoiceReqInfo)
if err != nil {
return "", err
}
body := GcRequestBody{
Op: "InvoiceEBillByOrder",
Xmpch: pp.Xmpch,
Version: "1.4",
Data: base64.StdEncoding.EncodeToString(b),
RequestTime: util.GenerateSimpleTimeId(),
}
params := fmt.Sprintf("data=%s&op=%s&requesttime=%s&version=%s&xmpch=%s%s", body.Data, body.Op, body.RequestTime, body.Version, body.Xmpch, pp.SecretKey)
body.Sign = strings.ToUpper(util.GetMd5Hash(params))
bodyBytes, err := json.Marshal(body)
if err != nil {
return "", err
}
respBytes, err := pp.doPost(bodyBytes)
if err != nil {
return "", err
}
var respBody GcResponseBody
err = json.Unmarshal(respBytes, &respBody)
if err != nil {
return "", err
}
if respBody.ReturnCode != "SUCCESS" {
return "", fmt.Errorf("%s: %s", respBody.ReturnCode, respBody.ReturnMsg)
}
invoiceRespInfoBytes, err := base64.StdEncoding.DecodeString(respBody.Data)
if err != nil {
return "", err
}
var invoiceRespInfo GcInvoiceRespInfo
err = json.Unmarshal(invoiceRespInfoBytes, &invoiceRespInfo)
if err != nil {
return "", err
}
return invoiceRespInfo.Url, nil
}

View File

@ -19,6 +19,7 @@ import "net/http"
type PaymentProvider interface {
Pay(providerName string, productName string, paymentName string, productDisplayName string, price float64, returnUrl string, notifyUrl string) (string, error)
Notify(request *http.Request, body []byte, authorityPublicKey string) (string, string, float64, string, string, error)
GetInvoice(paymentName string, personName string, personIdCard string, personEmail string, personPhone string, invoiceType string, invoiceTitle string, invoiceTaxId string) (string, error)
}
func GetPaymentProvider(typ string, appId string, clientSecret string, host string, appPublicKey string, appPrivateKey string, authorityPublicKey string, authorityRootPublicKey string) PaymentProvider {

View File

@ -167,6 +167,7 @@ func initAPI() {
beego.Router("/api/add-payment", &controllers.ApiController{}, "POST:AddPayment")
beego.Router("/api/delete-payment", &controllers.ApiController{}, "POST:DeletePayment")
beego.Router("/api/notify-payment/?:owner/?:provider/?:product/?:payment", &controllers.ApiController{}, "POST:NotifyPayment")
beego.Router("/api/invoice-payment", &controllers.ApiController{}, "POST:InvoicePayment")
beego.Router("/api/send-email", &controllers.ApiController{}, "POST:SendEmail")
beego.Router("/api/send-sms", &controllers.ApiController{}, "POST:SendSms")

View File

@ -62,6 +62,20 @@ class PaymentEditPage extends React.Component {
});
}
issueInvoice() {
const errorText = this.checkError();
if (errorText !== "") {
Setting.showMessage("error", errorText);
return;
}
alert("111")
}
downloadInvoice() {
Setting.openLinkSafe(this.state.payment.invoiceUrl);
}
renderPayment() {
return (
<Card size="small" title={
@ -177,7 +191,7 @@ class PaymentEditPage extends React.Component {
{Setting.getLabel(i18next.t("payment:Person name"), i18next.t("payment:Person name - Tooltip"))} :
</Col>
<Col span={22} >
<Input value={this.state.payment.personName} onChange={e => {
<Input disabled={this.state.payment.invoiceUrl !== ""} value={this.state.payment.personName} onChange={e => {
this.updatePaymentField('personName', e.target.value);
if (this.state.payment.invoiceType === "Individual") {
this.updatePaymentField('invoiceTitle', e.target.value);
@ -191,7 +205,7 @@ class PaymentEditPage extends React.Component {
{Setting.getLabel(i18next.t("payment:Person ID card"), i18next.t("payment:Person ID card - Tooltip"))} :
</Col>
<Col span={22} >
<Input value={this.state.payment.personIdCard} onChange={e => {
<Input disabled={this.state.payment.invoiceUrl !== ""} value={this.state.payment.personIdCard} onChange={e => {
this.updatePaymentField('personIdCard', e.target.value);
}} />
</Col>
@ -201,7 +215,7 @@ class PaymentEditPage extends React.Component {
{Setting.getLabel(i18next.t("payment:Person Email"), i18next.t("payment:Person Email - Tooltip"))} :
</Col>
<Col span={22} >
<Input value={this.state.payment.personEmail} onChange={e => {
<Input disabled={this.state.payment.invoiceUrl !== ""} value={this.state.payment.personEmail} onChange={e => {
this.updatePaymentField('personEmail', e.target.value);
}} />
</Col>
@ -211,7 +225,7 @@ class PaymentEditPage extends React.Component {
{Setting.getLabel(i18next.t("payment:Person phone"), i18next.t("payment:Person phone - Tooltip"))} :
</Col>
<Col span={22} >
<Input value={this.state.payment.personPhone} onChange={e => {
<Input disabled={this.state.payment.invoiceUrl !== ""} value={this.state.payment.personPhone} onChange={e => {
this.updatePaymentField('personPhone', e.target.value);
}} />
</Col>
@ -221,7 +235,7 @@ class PaymentEditPage extends React.Component {
{Setting.getLabel(i18next.t("payment:Invoice type"), i18next.t("payment:Invoice type - Tooltip"))} :
</Col>
<Col span={22} >
<Select virtual={false} style={{width: '100%'}} value={this.state.payment.invoiceType} onChange={(value => {
<Select disabled={this.state.payment.invoiceUrl !== ""} virtual={false} style={{width: '100%'}} value={this.state.payment.invoiceType} onChange={(value => {
this.updatePaymentField('invoiceType', value);
if (value === "Individual") {
this.updatePaymentField('invoiceTitle', this.state.payment.personName);
@ -242,7 +256,7 @@ class PaymentEditPage extends React.Component {
{Setting.getLabel(i18next.t("payment:Invoice title"), i18next.t("payment:Invoice title - Tooltip"))} :
</Col>
<Col span={22} >
<Input disabled={this.state.payment.invoiceType === "Individual"} value={this.state.payment.invoiceTitle} onChange={e => {
<Input disabled={this.state.payment.invoiceUrl !== "" || this.state.payment.invoiceType === "Individual"} value={this.state.payment.invoiceTitle} onChange={e => {
this.updatePaymentField('invoiceTitle', e.target.value);
}} />
</Col>
@ -252,7 +266,7 @@ class PaymentEditPage extends React.Component {
{Setting.getLabel(i18next.t("payment:Invoice Tax ID"), i18next.t("payment:Invoice Tax ID - Tooltip"))} :
</Col>
<Col span={22} >
<Input disabled={this.state.payment.invoiceType === "Individual"} value={this.state.payment.invoiceTaxId} onChange={e => {
<Input disabled={this.state.payment.invoiceUrl !== "" || this.state.payment.invoiceType === "Individual"} value={this.state.payment.invoiceTaxId} onChange={e => {
this.updatePaymentField('invoiceTaxId', e.target.value);
}} />
</Col>
@ -262,11 +276,35 @@ class PaymentEditPage extends React.Component {
{Setting.getLabel(i18next.t("payment:Invoice remark"), i18next.t("payment:Invoice remark - Tooltip"))} :
</Col>
<Col span={22} >
<Input value={this.state.payment.invoiceRemark} onChange={e => {
<Input disabled={this.state.payment.invoiceUrl !== ""} value={this.state.payment.invoiceRemark} onChange={e => {
this.updatePaymentField('invoiceRemark', e.target.value);
}} />
</Col>
</Row>
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={(Setting.isMobile()) ? 22 : 2}>
{Setting.getLabel(i18next.t("payment:Invoice URL"), i18next.t("payment:Invoice URL - Tooltip"))} :
</Col>
<Col span={22} >
<Input disabled={true} value={this.state.payment.invoiceUrl} onChange={e => {
this.updatePaymentField('invoiceUrl', e.target.value);
}} />
</Col>
</Row>
<Row style={{marginTop: '20px'}} >
<Col style={{marginTop: '5px'}} span={(Setting.isMobile()) ? 22 : 2}>
{Setting.getLabel(i18next.t("payment:Invoice actions"), i18next.t("payment:Invoice actions - Tooltip"))} :
</Col>
<Col span={22} >
{
this.state.payment.invoiceUrl === "" ? (
<Button type={"primary"} onClick={() => this.issueInvoice()}>{i18next.t("payment:Issue Invoice")}</Button>
) : (
<Button type={"primary"} onClick={() => this.downloadInvoice(false)}>{i18next.t("payment:Download Invoice")}</Button>
)
}
</Col>
</Row>
</Card>
)
}
@ -293,7 +331,7 @@ class PaymentEditPage extends React.Component {
}
if (this.state.payment.invoiceType === "Individual") {
if (this.state.payment.invoiceTitle !== "") {
if (this.state.payment.invoiceTitle !== this.state.payment.personName) {
return i18next.t("signup:The input is not invoice title!");
}
@ -301,7 +339,7 @@ class PaymentEditPage extends React.Component {
return i18next.t("signup:The input is not invoice Tax ID!");
}
} else {
if (!Setting.isValidInvoiceTitle(this.state.payment.invoiceTitle)) {
if (this.state.payment.invoiceTitle === "" || !Setting.isValidInvoiceTitle(this.state.payment.invoiceTitle)) {
return i18next.t("signup:The input is not invoice title!");
}

View File

@ -300,6 +300,15 @@ export function openLink(link) {
w.location.href = link;
}
export function openLinkSafe(link) {
// Javascript window.open issue in safari
// https://stackoverflow.com/questions/45569893/javascript-window-open-issue-in-safari
let a = document.createElement('a');
a.href = link;
a.setAttribute('target', '_blank');
a.click();
}
export function goToLink(link) {
window.location.href = link;
}

View File

@ -260,16 +260,22 @@
"payment": {
"Currency": "Currency",
"Currency - Tooltip": "Currency - Tooltip",
"Download Invoice": "Download Invoice",
"Edit Payment": "Edit Payment",
"Individual": "Individual",
"Invoice Tax ID": "Invoice Tax ID",
"Invoice Tax ID - Tooltip": "Invoice Tax ID - Tooltip",
"Invoice URL": "Invoice URL",
"Invoice URL - Tooltip": "Invoice URL - Tooltip",
"Invoice actions": "Invoice actions",
"Invoice actions - Tooltip": "Invoice actions - Tooltip",
"Invoice remark": "Invoice remark",
"Invoice remark - Tooltip": "Invoice remark - Tooltip",
"Invoice title": "Invoice title",
"Invoice title - Tooltip": "Invoice title - Tooltip",
"Invoice type": "Invoice type",
"Invoice type - Tooltip": "Invoice type - Tooltip",
"Issue Invoice": "Issue Invoice",
"Message": "Message",
"Message - Tooltip": "Message - Tooltip",
"New Payment": "New Payment",

View File

@ -260,16 +260,22 @@
"payment": {
"Currency": "Currency",
"Currency - Tooltip": "Currency - Tooltip",
"Download Invoice": "Download Invoice",
"Edit Payment": "Edit Payment",
"Individual": "Individual",
"Invoice Tax ID": "Invoice Tax ID",
"Invoice Tax ID - Tooltip": "Invoice Tax ID - Tooltip",
"Invoice URL": "Invoice URL",
"Invoice URL - Tooltip": "Invoice URL - Tooltip",
"Invoice actions": "Invoice actions",
"Invoice actions - Tooltip": "Invoice actions - Tooltip",
"Invoice remark": "Invoice remark",
"Invoice remark - Tooltip": "Invoice remark - Tooltip",
"Invoice title": "Invoice title",
"Invoice title - Tooltip": "Invoice title - Tooltip",
"Invoice type": "Invoice type",
"Invoice type - Tooltip": "Invoice type - Tooltip",
"Issue Invoice": "Issue Invoice",
"Message": "Message",
"Message - Tooltip": "Message - Tooltip",
"New Payment": "New Payment",

View File

@ -260,16 +260,22 @@
"payment": {
"Currency": "Currency",
"Currency - Tooltip": "Currency - Tooltip",
"Download Invoice": "Download Invoice",
"Edit Payment": "Edit Payment",
"Individual": "Individual",
"Invoice Tax ID": "Invoice Tax ID",
"Invoice Tax ID - Tooltip": "Invoice Tax ID - Tooltip",
"Invoice URL": "Invoice URL",
"Invoice URL - Tooltip": "Invoice URL - Tooltip",
"Invoice actions": "Invoice actions",
"Invoice actions - Tooltip": "Invoice actions - Tooltip",
"Invoice remark": "Invoice remark",
"Invoice remark - Tooltip": "Invoice remark - Tooltip",
"Invoice title": "Invoice title",
"Invoice title - Tooltip": "Invoice title - Tooltip",
"Invoice type": "Invoice type",
"Invoice type - Tooltip": "Invoice type - Tooltip",
"Issue Invoice": "Issue Invoice",
"Message": "Message",
"Message - Tooltip": "Message - Tooltip",
"New Payment": "New Payment",

View File

@ -260,16 +260,22 @@
"payment": {
"Currency": "Currency",
"Currency - Tooltip": "Currency - Tooltip",
"Download Invoice": "Download Invoice",
"Edit Payment": "Edit Payment",
"Individual": "Individual",
"Invoice Tax ID": "Invoice Tax ID",
"Invoice Tax ID - Tooltip": "Invoice Tax ID - Tooltip",
"Invoice URL": "Invoice URL",
"Invoice URL - Tooltip": "Invoice URL - Tooltip",
"Invoice actions": "Invoice actions",
"Invoice actions - Tooltip": "Invoice actions - Tooltip",
"Invoice remark": "Invoice remark",
"Invoice remark - Tooltip": "Invoice remark - Tooltip",
"Invoice title": "Invoice title",
"Invoice title - Tooltip": "Invoice title - Tooltip",
"Invoice type": "Invoice type",
"Invoice type - Tooltip": "Invoice type - Tooltip",
"Issue Invoice": "Issue Invoice",
"Message": "Message",
"Message - Tooltip": "Message - Tooltip",
"New Payment": "New Payment",

View File

@ -260,16 +260,22 @@
"payment": {
"Currency": "Currency",
"Currency - Tooltip": "Currency - Tooltip",
"Download Invoice": "Download Invoice",
"Edit Payment": "Edit Payment",
"Individual": "Individual",
"Invoice Tax ID": "Invoice Tax ID",
"Invoice Tax ID - Tooltip": "Invoice Tax ID - Tooltip",
"Invoice URL": "Invoice URL",
"Invoice URL - Tooltip": "Invoice URL - Tooltip",
"Invoice actions": "Invoice actions",
"Invoice actions - Tooltip": "Invoice actions - Tooltip",
"Invoice remark": "Invoice remark",
"Invoice remark - Tooltip": "Invoice remark - Tooltip",
"Invoice title": "Invoice title",
"Invoice title - Tooltip": "Invoice title - Tooltip",
"Invoice type": "Invoice type",
"Invoice type - Tooltip": "Invoice type - Tooltip",
"Issue Invoice": "Issue Invoice",
"Message": "Message",
"Message - Tooltip": "Message - Tooltip",
"New Payment": "New Payment",

View File

@ -260,16 +260,22 @@
"payment": {
"Currency": "Currency",
"Currency - Tooltip": "Currency - Tooltip",
"Download Invoice": "Download Invoice",
"Edit Payment": "Edit Payment",
"Individual": "Individual",
"Invoice Tax ID": "Invoice Tax ID",
"Invoice Tax ID - Tooltip": "Invoice Tax ID - Tooltip",
"Invoice URL": "Invoice URL",
"Invoice URL - Tooltip": "Invoice URL - Tooltip",
"Invoice actions": "Invoice actions",
"Invoice actions - Tooltip": "Invoice actions - Tooltip",
"Invoice remark": "Invoice remark",
"Invoice remark - Tooltip": "Invoice remark - Tooltip",
"Invoice title": "Invoice title",
"Invoice title - Tooltip": "Invoice title - Tooltip",
"Invoice type": "Invoice type",
"Invoice type - Tooltip": "Invoice type - Tooltip",
"Issue Invoice": "Issue Invoice",
"Message": "Message",
"Message - Tooltip": "Message - Tooltip",
"New Payment": "New Payment",

View File

@ -260,16 +260,22 @@
"payment": {
"Currency": "币种",
"Currency - Tooltip": "如USD美元CNY人民币等",
"Download Invoice": "下载发票",
"Edit Payment": "编辑付款",
"Individual": "个人",
"Invoice Tax ID": "纳税人识别号",
"Invoice Tax ID - Tooltip": "开票类型为单位时,必须输入单位纳税人识别号;开票类型为个人时,不需要填写",
"Invoice URL": "发票URL",
"Invoice URL - Tooltip": "发票的下载地址URL",
"Invoice actions": "发票操作",
"Invoice actions - Tooltip": "操作包含开具发票和下载发票两种",
"Invoice remark": "发票备注",
"Invoice remark - Tooltip": "备注不超过50个字",
"Invoice title": "发票抬头",
"Invoice title - Tooltip": "开票类型为单位时,发票抬头可输入单位名称;开票类型为个人时,系统自动填写为缴费人姓名",
"Invoice type": "开票类型",
"Invoice type - Tooltip": "开票类型可以为个人或者单位",
"Issue Invoice": "开具发票",
"Message": "消息",
"Message - Tooltip": "付款处理结果消息",
"New Payment": "添加付款",