notice.go 5.22 KB
Newer Older
1 2 3
package pay

import (
wangp's avatar
wangp committed
4
	"database/sql"
5 6 7 8 9 10 11 12
	"encoding/json"
	"fmt"
	"system_pay/controller/base"
	"system_pay/models"
	"system_pay/mysql"
)

// 拉卡拉微信支付回调 - AliPayNotice
wangp's avatar
wangp committed
13
func WxNotice(input *models.WxNoticeInput) (*base.ResponseDataWxNotice, error) {
14

wangp's avatar
wangp committed
15
	fmt.Println("拉卡拉微信回调输入参数")
16 17 18 19
	fmt.Println(input)

	response := new(base.ResponseDataWxNotice)

wangp's avatar
wangp committed
20 21 22 23 24 25 26 27
	//db, err := mysql.NewShopConn()
	//if err != nil {
	//	response.Code = "FAIL"
	//	response.Message = "参数格式校验错误"
	//	return response, err
	//}

	// 插入数据库
wangp's avatar
wangp committed
28
	db, err := mysql.NewPayConn()
wangp's avatar
wangp committed
29 30 31 32 33
	if err != nil {
		return response, err
	}

	tx, err := db.Begin()
34 35 36 37
	if err != nil {
		return response, err
	}

wangp's avatar
wangp committed
38
	defer mysql.CloseTx(tx, err)
wangp's avatar
wangp committed
39

40 41
	fmt.Println("111")

wangp's avatar
wangp committed
42
	//1.订单存在check
43 44
	var billID int64
	var attach, url string
wangp's avatar
wangp committed
45
	err = db.QueryRow("select ifnull(id, 0), attach, notify_pay_url from system_pay_bill where payment_order_code = ?",
46 47 48
		input.OutOrderNo).Scan(&billID, &attach, &url)
	if err != nil || billID == 0 || billID == 2 {
		response.Code = "FAIL"
wangp's avatar
wangp committed
49
		response.Message = "db operation fail1"
50 51 52 53 54
		return response, err
	}

	fmt.Println("222")

wangp's avatar
wangp committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
	//if noticeRequest.ReturnCode == "SUCCESS" {
		// 把订单状态置为 结算成功
		err = BillPayStateSuccess(tx, billID)
		if err != nil {
			//beego.Error("微信回调, 根据订单id 把订单置为结算成功 失败: ", err)
			//response.ReturnCode = "FAIL"
			//response.ReturnMsg = "db operation fail"
			//return response, nil
			response.Code = "FAIL"
			response.Message = "db operation fail2"
			InsertPayBillDetailNoticeResponseBody(tx, billID, response)
			return response, err
		}
	//} else {
	//
	//	// 把订单状态置为 结算失败
	//	err = repository.BillPayStateFail(tx, billID)
	//	if err != nil {
	//		beego.Error("微信回调, 根据订单id 把订单置为结算成功 失败: ", err)
	//		response.ReturnCode = "FAIL"
	//		response.ReturnMsg = "db operation fail"
	//		return response, nil
	//	}
	//}

wangp's avatar
wangp committed
80
	//2.「拉卡拉返回数据」存入 notice_request_body
wangp's avatar
wangp committed
81
	err = InsertPayBillDetailNoticeRequestBody(tx, billID, input)
82 83 84
	if err != nil {
		//beego.Error("微信回调, 根据订单id 插入回调Request参数 失败: ", err)
		response.Code = "FAIL"
wangp's avatar
wangp committed
85
		response.Message = "db operation fail3"
wangp's avatar
wangp committed
86
		InsertPayBillDetailNoticeResponseBody(tx, billID, response)
87 88 89 90 91
		return response, err
	}

	fmt.Println("333")

wangp's avatar
wangp committed
92 93
	//3.拉卡拉订收银台订单查询 - check todo

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
	//3.存入 notice_response_body
	//err = InsertPayBillDetailNoticeResponseBody(billID, response)
	//if err != nil {
	//	beego.Error("微信回调, 根据订单id 插入回调Response参数 失败: ", err)
	//	response.ReturnCode = "FAIL"
	//	response.ReturnMsg = "db operation fail"
	//	return err
	//}

	//beego.Info("回调成功 订单成功")

	//type CallbackResponse struct {
	//	OutTradeNo    string `json:"out_trade_no"`   // 订单号
	//	ReturnMsg     string `json:"return_msg"`     // 是否成功
	//	AttachInfo    string `json:"attach_info"`    // 附加信息
	//	TransactionID string `json:"transaction_id"` // 微信支付订单号
	//	TradeNo       string `json:"trade_no"`       // 支付宝交易流水号
	//}

	if url != "" {
		//4.回调业务方「回调函数」
		callbackResponse := new(models.CallbackResponse)
		callbackResponse.ReturnMsg = "SUCCESS"
		//callbackResponse.OutTradeNo = input.OutTradeNo
		//callbackResponse.TransactionID = input.TransactionId
wangp's avatar
wangp committed
119 120
		callbackResponse.OutTradeNo = input.OutOrderNo //交易凭据单号 todo ?
		callbackResponse.TransactionID = input.OutOrderNo //交易凭据单号 todo ? strconv.Itoa(
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

		attachMap := make(map[string]interface{}, 0)
		_ = json.Unmarshal([]byte(attach), &attachMap)
		if attachMap["store_sn"].(string) == "" {
			callbackResponse.AttachInfo = attachMap["old_attach"].(string)
		} else {
			callbackResponse.AttachInfo = attach  //商户订单号(谛宝多多)
		}

		go callBackBusinessService(input.OutOrderNo, url, callbackResponse)
		fmt.Println("444")
	}

	response.Code = "SUCCESS"
	response.Message = "执行成功"
wangp's avatar
wangp committed
136
	InsertPayBillDetailNoticeResponseBody(tx, billID, response)
137 138 139 140
	return response, nil
}

// InsertPayBillDetailNoticeRequestBody is 插入支付订单详情表中的 下单参数字段 notice_request_body
wangp's avatar
wangp committed
141
func InsertPayBillDetailNoticeRequestBody(tx *sql.Tx, billID int64, noticeRequestBody interface{}) error {
142 143 144 145 146

	body, err := json.Marshal(noticeRequestBody)
	if err != nil {
		return err
	}
wangp's avatar
wangp committed
147 148
	insertPayBillDetailSQL := `update system_pay_bill_detail set notice_request_body = ? where pay_bill_id = ?`
	result, err := tx.Exec(insertPayBillDetailSQL, string(body), billID)
149 150 151 152 153 154 155 156 157 158 159 160
	if err != nil {
		return err
	}

	_, err = result.RowsAffected()
	if err != nil {
		return err
	}
	return nil
}

// InsertPayBillDetailNoticeResponseBody is 插入支付订单详情表中的 下单参数字段 notice_response_body
wangp's avatar
wangp committed
161
func InsertPayBillDetailNoticeResponseBody(tx *sql.Tx, billID int64, noticeResponseBody interface{}) error {
162 163 164 165 166

	body, err := json.Marshal(noticeResponseBody)
	if err != nil {
		return err
	}
wangp's avatar
wangp committed
167 168
	insertPayBillDetailSQL := `update system_pay_bill_detail set notice_response_body = ? where pay_bill_id = ?`
	result, err := tx.Exec(insertPayBillDetailSQL, string(body), billID)
169 170 171 172 173 174 175 176 177 178 179
	if err != nil {
		return err
	}

	_, err = result.RowsAffected()
	if err != nil {
		return err
	}
	return nil
}