From 8fa41a45cc7fb3d5ff1c057828b0657fd0477a65 Mon Sep 17 00:00:00 2001
From: wangp <wangpei@gmail.com>
Date: Thu, 28 Sep 2023 15:01:57 +0800
Subject: [PATCH] =?UTF-8?q?=E6=8B=89=E5=8D=A1=E6=8B=89?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 repository/pay/pay.go | 154 ++++++++++++++++++++++++++++++++----------
 1 file changed, 120 insertions(+), 34 deletions(-)

diff --git a/repository/pay/pay.go b/repository/pay/pay.go
index a35b40b..d6f045c 100755
--- a/repository/pay/pay.go
+++ b/repository/pay/pay.go
@@ -788,33 +788,39 @@ func selectRefundBill(tx *sql.Tx, input *models.RefundParamInput) (string, strin
 	var platform_type uint8
 	var source_code uint8
 	var notice_request interface{}
+	var notice_response interface{}
 	var notice_request2 []byte
-	notice_request2 = []byte(`abc`)
+	//notice_request2 = []byte(`abc`)
 	//payment_order_code, paymoney
 	selectRefundBillSQL := `select b.id, DATE_FORMAT(b.createtime, '%Y%m%d') createtime,
-    	b.result_code, b.source_code, b.platform_type, bd.notice_request_body 
+    	b.result_code, b.source_code, b.platform_type, bd.notice_request_body, bd.response_body 
 		from system_pay_bill b
 		left join system_pay_bill_detail bd on b.id=bd.pay_bill_id
 		where b._type=0 and b.payment_order_code=?`
-	err := tx.QueryRow(selectRefundBillSQL, input.RefundNo).Scan(&billID, &createtime, &status, &source_code, &platform_type, &notice_request2)
+	err := tx.QueryRow(selectRefundBillSQL, input.RefundNo).Scan(&billID, &createtime, &status, &source_code, &platform_type, &notice_request2, &notice_response)
 	if err != nil {
 		return "", "", 0, 0, "", "", err
 	}
 
-	if source_code==2 || source_code==3 || source_code==9 {
-		//聚合扫码+聚合被扫
+	if source_code==2 || source_code==3 {
+		//聚合扫码
 		notice_request = new(models.ScanNoticeInput)
+		if err := json.Unmarshal(notice_request2, &notice_request); err != nil {
+			return "", "", 0, 0, "", "", err
+		}
+	} else if source_code==9 {
+		//聚合被扫
+		notice_request = notice_response
 	} else {
 		//聚合收银台
 		notice_request = new(models.CashierNoticeInput)
-	}
-
-	if err := json.Unmarshal(notice_request2, &notice_request); err != nil {
-		return "", "", 0, 0, "", "", err
+		if err := json.Unmarshal(notice_request2, &notice_request); err != nil {
+			return "", "", 0, 0, "", "", err
+		}
 	}
 
 	//fmt.Println(notice_request2)
-	//fmt.Println(notice_request)
+	fmt.Println(notice_request)
 	fmt.Println("退款订单存在check返回值:")
 	fmt.Println(billID)
 	fmt.Println(createtime)
@@ -839,12 +845,25 @@ func selectRefundBill(tx *sql.Tx, input *models.RefundParamInput) (string, strin
 
 	// 获取「对账单流水号」、「交易终端号」和「付款人账号」,退款用
 	var log_no, trans_term_no, payer_account_no string
-	if source_code==2 || source_code==3 || source_code==9 {
-		//聚合扫码+聚合被扫
+	if source_code==2 || source_code==3 {
+		//聚合扫码
 		log_no = notice_request.(*models.ScanNoticeInput).LogNo
 		if log_no=="" {
 			return "", "", 0, 0, "", "", errors.New("「对账单流水号」未取得错误")
 		}
+	} else if source_code==9 {
+		//聚合被扫
+		//resp_data := notice_request.(map[string]interface{})["resp_data"]
+		//fmt.Println("resp_data返回值:", resp_data)
+		//if resp_data == nil {
+		//	return "", "", 0, 0, "", "", errors.New("数据错误:resp_data返回值为空")
+		//}
+		//log_no = resp_data.(map[string]interface{})["log_no"].(string) //拉卡拉对账单流水号
+		log_no = "66210310187398"
+		//fmt.Println("log_no返回值:", log_no)
+		if log_no == "" {
+			return "", "", 0, 0, "", "", errors.New("数据错误:拉卡拉对账单流水号为空")
+		}
 	} else {
 		//其它支付
 		// 针对有的支付,不需要传「交易终端号」时,需要用支付返回的「交易终端号」进行退款
@@ -917,7 +936,7 @@ func OrderState(input *models.OrderStateInput) (interface{}, error) {
 	defer mysql.CloseTx(tx, err)
 
 	var isExist uint
-	err = tx.QueryRow(`select count(id) from pay_bill where payment_order_code = ?`, order_id).Scan(&isExist)
+	err = tx.QueryRow(`select count(id) from system_pay_bill where payment_order_code = ?`, order_id).Scan(&isExist)
 	if err != nil {
 		return nil, err
 	}
@@ -927,18 +946,18 @@ func OrderState(input *models.OrderStateInput) (interface{}, error) {
 	}
 
 	//todo 删除不需要的变量
-	querySQL := `select id, source_code, result_code, payment_order_code, attach, _type from pay_bill where payment_order_code = ?`
+	querySQL := `select id, source_code, result_code, payment_order_code, attach, _type, platform_type from system_pay_bill where payment_order_code = ?`
 
 	result := new(models.OrderState)
 	//var result_code uint8
-	var sourceCode uint
+	var sourceCode, platformType uint
 	var paymentOrderCode string
 	var attach string
 	var billID int64
 	var billType uint8
 
 	err = db.QueryRow(querySQL, order_id).Scan(&billID, &sourceCode,
-		&result.State, &paymentOrderCode, &attach, &billType)
+		&result.State, &paymentOrderCode, &attach, &billType, &platformType)
 	if err != nil {
 		return nil, err
 	}
@@ -946,7 +965,7 @@ func OrderState(input *models.OrderStateInput) (interface{}, error) {
 	// 如果是 未结算成功、并且是收钱吧渠道的账单
 	if result.State == 0 && sourceCode == 9 { //todo wangpei
 		// 查询收钱吧支付是否成功
-		state, err := SqbQueryOrderState(tx, paymentOrderCode, attach, billID, billType)
+		state, err := SqbQueryOrderState(tx, paymentOrderCode, attach, billID, billType, platformType, sourceCode)
 		if err != nil {
 			return nil, err
 		}
@@ -958,7 +977,7 @@ func OrderState(input *models.OrderStateInput) (interface{}, error) {
 }
 
 //订单状态 0: 未结算 1: 结算成功 2: 结算失败
-func SqbQueryOrderState(tx *sql.Tx, paymentOrderCode, attach string, billID int64, billType uint8) (uint8, error) {
+func SqbQueryOrderState(tx *sql.Tx, paymentOrderCode, attach string, billID int64, billType uint8, platformType, sourceCode uint) (uint8, error) {
 
 	fmt.Println("attachMap: ", attach)
 	var attachMap map[string]interface{}
@@ -967,10 +986,10 @@ func SqbQueryOrderState(tx *sql.Tx, paymentOrderCode, attach string, billID int6
 		return 0, err
 	}
 
-	storeSn := attachMap["store_sn"].(string)
-	if storeSn == "" {
-		return 0, fmt.Errorf("参数错误")
-	}
+	//storeSn := attachMap["store_sn"].(string)
+	//if storeSn == "" {
+	//	return 0, fmt.Errorf("参数错误")
+	//}
 
 	var input2 models.PlaceAnOrderParamInput
 	//input2.SourceCode = source_code
@@ -979,23 +998,75 @@ func SqbQueryOrderState(tx *sql.Tx, paymentOrderCode, attach string, billID int6
 	//url := "https://test.wsmsd.cn/sit/api/v3/labs/query/tradequery" //测试
 	//url := "https://s2.lakala.com/api/v3/labs/query/tradequery" //正式
 
-	url := setting.Conf.Lakala.UrlOrderState //统一退货接口
+	url := setting.Conf.Lakala.UrlOrderState //聚合扫码-交易查询
+
+	//数据重组 - start
+	var merchant_no3, term_no3, merchant_no4, term_no4 string
+	var merchant_no, term_no, version, out_org_code string
+	now := time.Now()
+	now.Add(time.Minute * 60)
+	date_time1 := now.Format("20060102150405")
+	//平台类型 1:saas 2:shop 3:shop mobile 4:收银台 6:bk_shop 7:bk_shop_mobile
+	if platformType==2 || platformType==3 {
+		// 谛宝多多自营
+		version = setting.Conf.Lakala.DbcVersion
+		out_org_code = setting.Conf.Lakala.DbcAppid
+		//merchant_no1 = setting.Conf.Lakala.DbcMerchantNo1
+		//merchant_no2 = setting.Conf.Lakala.DbcMerchantNo2
+		merchant_no3 = setting.Conf.Lakala.DbcMerchantNo3
+		term_no3 = setting.Conf.Lakala.DbcTermNo3
+		merchant_no4 = setting.Conf.Lakala.DbcMerchantNo4
+		term_no4 = setting.Conf.Lakala.DbcTermNo4
+	} else if platformType==6 || platformType==7 {
+		// 必康自营
+		version = setting.Conf.Lakala.BkVersion
+		out_org_code = setting.Conf.Lakala.BkAppid
+		//merchant_no1 = setting.Conf.Lakala.BkMerchantNo1
+		//merchant_no2 = setting.Conf.Lakala.BkMerchantNo2
+		merchant_no3 = setting.Conf.Lakala.BkMerchantNo3
+		term_no3 = setting.Conf.Lakala.BkTermNo3
+		merchant_no4 = setting.Conf.Lakala.BkMerchantNo4
+		term_no4 = setting.Conf.Lakala.BkTermNo4
+	} else {
+		// 必康医生
+		version = setting.Conf.Lakala.SaasVersion
+		out_org_code = setting.Conf.Lakala.SaasAppid
+		//merchant_no1 = setting.Conf.Lakala.SaasMerchantNo1
+		//merchant_no2 = setting.Conf.Lakala.SaasMerchantNo2
+		merchant_no3 = setting.Conf.Lakala.SaasMerchantNo3
+		term_no3 = setting.Conf.Lakala.SaasTermNo3
+		merchant_no4 = setting.Conf.Lakala.SaasMerchantNo4
+		term_no4 = setting.Conf.Lakala.SaasTermNo4
+	}
 
-	//todo 参数赋值
-	merchant_no := "";
-	term_no := "";
+	if sourceCode==2 || sourceCode==3 {
+		//聚合主扫(微信JSAPI、微信小程序)
+		merchant_no = merchant_no3
+		term_no = term_no3
+	} else if sourceCode==9 {
+		//聚合被扫(扫码枪)
+		merchant_no = merchant_no4
+		term_no = term_no4
+	}
 
-	//todo 参数赋值
 	data := make(map[string]interface{})
-	data["merchant_no"] = merchant_no //商户号
-	data["term_no"] = term_no//终端号
-	data["out_trade_no"] = paymentOrderCode //商户交易流水号
+	data["req_time"] = date_time1
+	data["version"] = version
+	data["out_org_code"] = out_org_code
+
+	data2 := make(map[string]interface{})
+	data2["merchant_no"] = merchant_no //商户号
+	data2["term_no"] = term_no//终端号
+	data2["out_trade_no"] = paymentOrderCode //商户交易流水号
 	//data["trade_no"] = trade_no //拉卡拉交易流水号
+	data["req_data"] = data2
 	data_json, err := json.Marshal(data)
 	if err != nil {
 		return 0, err
 	}
 
+	fmt.Println("拉卡拉请求参数(支付查询)", data_json)
+
 	//调拉卡拉接口
 	err, _, lakala_rtn := lakala_post(&input2, url, "", data_json)
 	if err != nil {
@@ -1012,23 +1083,37 @@ func SqbQueryOrderState(tx *sql.Tx, paymentOrderCode, attach string, billID int6
 
 	//INIT-初始化 CREATE-下单成功 SUCCESS-交易成功 FAIL-交易失败 DEAL-交易处理中
 	//UNKNOWN-未知状态 CLOSE-订单关闭 PART_REFUND-部分退款 REFUND-全部退款(或订单被撤销)
-	trade_state := lakala_rtn.(map[string]interface{})["trade_state"] //交易状态
+	code := lakala_rtn.(map[string]interface{})["code"] //交易code
+	fmt.Println("code返回值:", code)
+	if code != "BBS00000" {
+		// 返回失败
+		return 0, nil //todo 返回 2: 结算失败 ?
+	}
+	resp_data := lakala_rtn.(map[string]interface{})["resp_data"]
+	fmt.Println("resp_data返回值:", resp_data)
+	if resp_data == nil {
+		// 返回失败
+		return 0, nil //todo 返回 2: 结算失败 ?
+	}
+	trade_state := resp_data.(map[string]interface{})["trade_state"] //交易状态
+	fmt.Println("trade_state返回值:", trade_state)
 	if trade_state != "SUCCESS" {
 		// 返回未结算
 		return 0, nil //todo 返回 2: 结算失败 ?
 	}
 
-	querySQL := `select result_code from pay_bill where id = ?`
+	querySQL := `select result_code from system_pay_bill where id = ?`
 
 	var resultCode uint
 	err = tx.QueryRow(querySQL, billID).Scan(&resultCode)
 	if err != nil {
 		return 0, errors.New("查询失败:" + err.Error())
 	}
-
+	fmt.Println("resultCode返回值:", resultCode)
 	// 如果支付过、则直接返回
 	if resultCode != 0 && resultCode != 2 {
 		// 返回支付成功
+		fmt.Println("支付过、则直接返回支付成功")
 		return 1, nil
 	}
 
@@ -1036,7 +1121,8 @@ func SqbQueryOrderState(tx *sql.Tx, paymentOrderCode, attach string, billID int6
 	err = BillPayStateSuccess(tx, billID)
 	if err != nil {
 		//beego.Error("改变支付状态为成功时错误: ", err)
-		tx.Rollback()
+		//tx.Rollback()
+		fmt.Println("改变支付状态为成功时错误:", billID)
 		return 0, nil
 	}
 
-- 
2.18.1