Skip to content

Commit

Permalink
Add InvoicePayment() API.
Browse files Browse the repository at this point in the history
  • Loading branch information
nomeguy committed Apr 26, 2022
1 parent cab51fa commit cf3b461
Show file tree
Hide file tree
Showing 15 changed files with 248 additions and 10 deletions.
14 changes: 14 additions & 0 deletions controllers/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
41 changes: 41 additions & 0 deletions object/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions pp/alipay.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
88 changes: 88 additions & 0 deletions pp/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand Down Expand Up @@ -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
}
1 change: 1 addition & 0 deletions pp/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions routers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
58 changes: 48 additions & 10 deletions web/src/PaymentEditPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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={
Expand Down Expand Up @@ -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);
Expand All @@ -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>
Expand All @@ -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>
Expand All @@ -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>
Expand All @@ -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);
Expand All @@ -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>
Expand All @@ -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>
Expand All @@ -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>
)
}
Expand All @@ -293,15 +331,15 @@ 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!");
}

if (this.state.payment.invoiceTaxId !== "") {
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!");
}

Expand Down
9 changes: 9 additions & 0 deletions web/src/Setting.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 6 additions & 0 deletions web/src/locales/de/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions web/src/locales/en/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading

0 comments on commit cf3b461

Please sign in to comment.