微信充值
diff --git a/payapi/src/main/java/com/supwisdom/dlpay/agent/AgentCode.java b/payapi/src/main/java/com/supwisdom/dlpay/agent/AgentCode.java
index 0d56cc1..a805886 100644
--- a/payapi/src/main/java/com/supwisdom/dlpay/agent/AgentCode.java
+++ b/payapi/src/main/java/com/supwisdom/dlpay/agent/AgentCode.java
@@ -1,6 +1,8 @@
 package com.supwisdom.dlpay.agent;
 
 public enum AgentCode {
+  PAYINITSUCCESS("SUCCESS", "成功"),
+  PAYINITFAIL("FAIL", "成功"),
   SUCCESS("success", "成功"),
   FAIL("fail", "交易失败"),
   REFNO_NOT_EXISTS("refno_not_exists", "指定流水不存在"),
diff --git a/payapi/src/main/java/com/supwisdom/dlpay/agent/InAppPayService.java b/payapi/src/main/java/com/supwisdom/dlpay/agent/InAppPayService.java
index 47f51d9..88aec26 100644
--- a/payapi/src/main/java/com/supwisdom/dlpay/agent/InAppPayService.java
+++ b/payapi/src/main/java/com/supwisdom/dlpay/agent/InAppPayService.java
@@ -3,6 +3,8 @@
 import com.supwisdom.dlpay.api.domain.TTransactionMain;
 import org.springframework.http.HttpRequest;
 
+import javax.servlet.http.HttpServletRequest;
+
 /**
  * 微信、支付宝、银联等第三方支付平台 H5 , JSAPI , Native , 小程序等手机端支付接口
  *
@@ -25,7 +27,7 @@
    * @param request     - 第三方平台通知请求的 HttpRequest
    * @return
    */
-  AgentResponse<T> notify(TTransactionMain transaction, HttpRequest request);
+  AgentResponse<T> notify(TTransactionMain transaction, HttpServletRequest request);
 
   /**
    * 由服务端主动发起的查询第三方支付平交易情况
diff --git a/payapi/src/main/kotlin/com/supwisdom/dlpay/agent/service/impl/wechat_service_impl.kt b/payapi/src/main/kotlin/com/supwisdom/dlpay/agent/service/impl/wechat_service_impl.kt
index 61fa734..25a10ba 100644
--- a/payapi/src/main/kotlin/com/supwisdom/dlpay/agent/service/impl/wechat_service_impl.kt
+++ b/payapi/src/main/kotlin/com/supwisdom/dlpay/agent/service/impl/wechat_service_impl.kt
@@ -9,6 +9,8 @@
 import com.supwisdom.dlpay.api.bean.BaseResp
 import com.supwisdom.dlpay.api.bean.WechatReqResp
 import com.supwisdom.dlpay.api.dao.PersonIdentityDao
+import com.supwisdom.dlpay.api.dao.ShopSourceTypeConfigDao
+import com.supwisdom.dlpay.api.dao.SourceTypeConfigDao
 import com.supwisdom.dlpay.api.domain.TTransactionMain
 import com.supwisdom.dlpay.api.service.ConsumePayService
 import com.supwisdom.dlpay.api.service.SourceTypeService
@@ -22,12 +24,15 @@
 import org.springframework.stereotype.Service
 import org.springframework.web.client.RestTemplate
 import java.nio.charset.StandardCharsets
+import java.util.HashMap
 
 
 @Service
 class WechatServiceImpl(val sourceTypeService: SourceTypeService,
                         val agentServiceProxy: AgentServiceProxy,
                         val personIdentityDao: PersonIdentityDao,
+                        val sourceTypeConfigDao: SourceTypeConfigDao,
+                        var shopSourceTypeConfigDao: ShopSourceTypeConfigDao,
                         val consumePayService: ConsumePayService,
                         val transactionService: TransactionServiceProxy,
                         val restTemplate: RestTemplate) : WechatService {
@@ -354,4 +359,100 @@
         return agentResponse
     }
 
+    override fun dopayInit(transaction: TTransactionMain): AgentResponse<QrcodePayTrans> {
+        val agentResponse = AgentResponse<QrcodePayTrans>()
+            //H5 支付
+         //   val trans = agentServiceProxy.qrcodePayTransFindByRefno(transaction.refno)
+           // agentResponse.payload = trans
+            val bean = WechatReqResp().apply {
+                this.appid = "wx03d12be843cdadd9"
+                this.mch_id = "1513444041"
+                this.key = "3VVhvjD2F0kHzayrjgLM81qcz39FgAv1"
+                this.out_trade_no = transaction.refno
+                this.total_fee = 1
+                this.spbill_create_ip = "14.23.150.211"
+                this.body = "充值测试"
+                this.notify_url = "http://ykt.supwisdom.com:10201/payapi/api/notify/inapp/wechat/100010"
+                this.trade_type = "MWEB"
+                this.scene_info = "{\"h5_info\": {\"type\":\"Wap\",\"wap_url\": \" https://pay.qq.com\",\"wap_name\": \"微信支付\"}}"
+            }
+            bean.generaSign()
+            val xml = bean.generaXML()
+            restTemplate.messageConverters[1] = StringHttpMessageConverter(StandardCharsets.UTF_8)
+            val res = restTemplate.postForEntity(PaytypeUtil.CFG_WECHAT_UNIONPAY, xml, String::class.java)
+            val eleMap = XmlUtils.parseXml(res.body)
+            val retcode = eleMap["return_code"]
+            val resultCode = eleMap["result_code"]
+
+            if (!retcode.isNullOrEmpty() && "SUCCESS" == retcode
+                    && !resultCode.isNullOrEmpty() && "SUCCESS" == resultCode) {
+                agentResponse.code = AgentCode.REQUIRE_QUERY
+                agentResponse.agentCode = resultCode
+                agentResponse.agentMsg = eleMap["return_msg"]
+                when {
+                    transaction.transCode == TradeCode.TRANSCODE_H5PAY -> agentResponse.agentBody = eleMap["mweb_url"]
+                    transaction.transCode == TradeCode.TRANSCODE_APPPAY -> {
+                        bean.prepay_id = eleMap["prepay_id"]
+                        bean.timestamp = DateUtil.getNowSecond().toString()
+                        bean.nonce_str = getRandomString(30)
+                        bean.wpackage = "Sign=WXPay"
+                        bean.generaAPPSign()
+                        agentResponse.agentBody = bean.generaAPPParamters()
+                    }
+                    transaction.transCode == TradeCode.TRANSCODE_JSPAY || transaction.transCode == TradeCode.TRANSCODE_SMPAY -> {
+                        bean.prepay_id = eleMap["prepay_id"]
+                        bean.timestamp = DateUtil.getNowSecond().toString()
+                        bean.nonce_str = getRandomString(30)
+                        bean.generaJSAPISign()
+                        agentResponse.agentBody = bean.generaJSAPIParamters()
+                    }
+                }
+                agentResponse.agentRefno = eleMap["prepay_id"]
+            } else {
+                logger.error { "code=${eleMap["err_code"]},des=${eleMap["err_code_des"]}" }
+                agentResponse.code = AgentCode.FAIL
+                agentResponse.agentCode = eleMap["err_code"]
+                agentResponse.agentMsg = eleMap["err_code_des"]
+            }
+
+        return agentResponse
+    }
+
+    override fun doPayQuery(transaction: TTransactionMain): AgentResponse<QrcodePayTrans> {
+        val agentResponse = AgentResponse<QrcodePayTrans>()
+            //H5 支付
+            val bean = WechatReqResp().apply {
+                this.appid = "wx03d12be843cdadd9"
+                this.mch_id = "1513444041"
+                this.key = "3VVhvjD2F0kHzayrjgLM81qcz39FgAv1"
+                this.out_trade_no = transaction.refno
+            }
+            bean.generalQuerySign()
+            val xml = bean.generalQueryXML()
+            restTemplate.messageConverters[1] = StringHttpMessageConverter(StandardCharsets.UTF_8)
+            val res = restTemplate.postForEntity(PaytypeUtil.CFG_WECHAT_QUERY, xml, String::class.java)
+            val eleMap = XmlUtils.parseXml(res.body)
+            val retcode = eleMap["return_code"]
+            val resultCode = eleMap["result_code"]
+            val tradeState = eleMap["trade_state"]
+            if (!retcode.isNullOrEmpty() && "SUCCESS" == retcode
+                    && !resultCode.isNullOrEmpty() && "SUCCESS" == resultCode) {
+                agentResponse.code = AgentCode.REQUIRE_QUERY
+                agentResponse.agentCode = resultCode
+                agentResponse.agentMsg = eleMap["return_msg"]
+                when (tradeState) {
+                    "SUCCESS" -> agentResponse.dtlStatus = DtlStatus.SUCCESS
+                    "REFUND" -> agentResponse.dtlStatus = DtlStatus.REFUND
+                    "NOTPAY", "CLOSED", "REVOKED", "PAYERROR" -> agentResponse.dtlStatus = DtlStatus.FAIL
+                    "USERPAYING" -> agentResponse.dtlStatus = DtlStatus.WAIT
+                }
+            } else {
+                logger.error { "code=${eleMap["err_code"]}},des=${eleMap["err_code_des"]}" }
+                agentResponse.code = AgentCode.FAIL
+                agentResponse.agentCode = eleMap["err_code"]
+                agentResponse.agentMsg = eleMap["err_code_des"]
+            }
+        return agentResponse
+    }
+
 }
\ No newline at end of file
diff --git a/payapi/src/main/kotlin/com/supwisdom/dlpay/agent/service/inapp_alipay.kt b/payapi/src/main/kotlin/com/supwisdom/dlpay/agent/service/inapp_alipay.kt
index b0bcc31..b116675 100644
--- a/payapi/src/main/kotlin/com/supwisdom/dlpay/agent/service/inapp_alipay.kt
+++ b/payapi/src/main/kotlin/com/supwisdom/dlpay/agent/service/inapp_alipay.kt
@@ -5,6 +5,7 @@
 import com.supwisdom.dlpay.api.domain.TTransactionMain
 import org.springframework.http.HttpRequest
 import org.springframework.stereotype.Component
+import javax.servlet.http.HttpServletRequest
 
 class AlipayInAppResponse {
 
@@ -16,7 +17,7 @@
         TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
     }
 
-    override fun notify(transaction: TTransactionMain?, request: HttpRequest?): AgentResponse<AlipayInAppResponse> {
+    override fun notify(transaction: TTransactionMain?, request: HttpServletRequest?): AgentResponse<AlipayInAppResponse> {
         TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
     }
 
diff --git a/payapi/src/main/kotlin/com/supwisdom/dlpay/agent/service/inapp_wechatpay.kt b/payapi/src/main/kotlin/com/supwisdom/dlpay/agent/service/inapp_wechatpay.kt
index 494e134..d531da1 100644
--- a/payapi/src/main/kotlin/com/supwisdom/dlpay/agent/service/inapp_wechatpay.kt
+++ b/payapi/src/main/kotlin/com/supwisdom/dlpay/agent/service/inapp_wechatpay.kt
@@ -1,12 +1,18 @@
 package com.supwisdom.dlpay.agent.service
 
+import com.supwisdom.dlpay.agent.AgentCode
 import com.supwisdom.dlpay.agent.AgentResponse
 import com.supwisdom.dlpay.agent.InAppPayService
 import com.supwisdom.dlpay.agent.domain.QrcodePayTrans
+import com.supwisdom.dlpay.api.domain.TShopdtl
 import com.supwisdom.dlpay.api.domain.TTransactionMain
 import com.supwisdom.dlpay.api.service.SourceTypeService
+import org.dom4j.Element
+import org.dom4j.io.SAXReader
 import org.springframework.http.HttpRequest
 import org.springframework.stereotype.Component
+import java.util.HashMap
+import javax.servlet.http.HttpServletRequest
 
 class WechatPayResponse {
     var preId: String = "";
@@ -20,17 +26,56 @@
 
     override fun init(transation: TTransactionMain): AgentResponse<WechatPayResponse> {
         val wechatPayResponse =  AgentResponse<WechatPayResponse>();
-        val agentResponse =  wechatService.doPrepay(transation);
-        System.err.println("++++++++++++++++");
+        System.err.println("#########");
+        val shopDtlTemp =  TShopdtl();
+        transation.shopDtl = shopDtlTemp;               //transaction.shopDtl must not be null
+        val agentResponse =  wechatService.dopayInit(transation);
         System.err.println(agentResponse);
+        wechatPayResponse.agentBody = agentResponse.agentBody;
+        wechatPayResponse.agentCode = agentResponse.agentCode;
+        wechatPayResponse.agentMsg = agentResponse.agentMsg;
+        wechatPayResponse.agentRefno = agentResponse.agentRefno;
+        wechatPayResponse.code = agentResponse.code;
+        wechatPayResponse.dtlStatus = agentResponse.dtlStatus;
         return wechatPayResponse;
     }
 
-    override fun notify(transaction: TTransactionMain?, request: HttpRequest?): AgentResponse<WechatPayResponse> {
-        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
+    override fun notify(transaction: TTransactionMain?, request: HttpServletRequest): AgentResponse<WechatPayResponse> {
+        val wechatPayResponse =  AgentResponse<WechatPayResponse>();
+        // 解析结果存储在HashMap
+        var map: MutableMap<String, String> = HashMap()
+        val inputStream = request.inputStream
+        // 读取输入流
+        val reader = SAXReader()
+        val document = reader.read(inputStream)
+        // 得到xml根元素
+        val root = document.rootElement
+        // 得到根元素的所有子节点
+        val elementList = root.elements() as MutableList<Element>
+        // 遍历所有子节点
+        for (e in elementList) {
+            map[e.name] = e.text
+        }
+        // 释放资源
+        inputStream.close()
+        val resp = wechatService.doNotify(map)
+         if (resp.code == AgentCode.SUCCESS) {
+             wechatPayResponse.agentBody = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>"
+        } else {
+             wechatPayResponse.agentBody = "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[" + resp.agentMsg + "]]></return_msg></xml>"
+        }
+        return wechatPayResponse;
     }
 
-    override fun query(transation: TTransactionMain?): AgentResponse<WechatPayResponse> {
-        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
+    override fun query(transation: TTransactionMain): AgentResponse<WechatPayResponse> {
+        val wechatPayResponse =  AgentResponse<WechatPayResponse>();
+        val agentResponse = wechatService.doPayQuery(transation);
+        wechatPayResponse.agentMsg = agentResponse.agentMsg;
+        wechatPayResponse.agentCode = agentResponse.agentCode;
+        wechatPayResponse.agentBody = agentResponse.agentBody;
+        wechatPayResponse.agentRefno = agentResponse.agentRefno;
+        wechatPayResponse.code = agentResponse.code;
+        wechatPayResponse.dtlStatus = agentResponse.dtlStatus;
+        return wechatPayResponse;
     }
 }
\ No newline at end of file
diff --git a/payapi/src/main/kotlin/com/supwisdom/dlpay/agent/service/wechat_service.kt b/payapi/src/main/kotlin/com/supwisdom/dlpay/agent/service/wechat_service.kt
index 683d186..30c8850 100644
--- a/payapi/src/main/kotlin/com/supwisdom/dlpay/agent/service/wechat_service.kt
+++ b/payapi/src/main/kotlin/com/supwisdom/dlpay/agent/service/wechat_service.kt
@@ -23,6 +23,9 @@
 
     fun doNotify(param: Map<String, String>): AgentResponse<QrcodePayTrans>
 
+    fun dopayInit(transaction: TTransactionMain): AgentResponse<QrcodePayTrans>
+
+    fun doPayQuery(transaction: TTransactionMain): AgentResponse<QrcodePayTrans>
 }
 
 @Component("wechatAgent")
diff --git a/payapi/src/main/kotlin/com/supwisdom/dlpay/api/controller/transaction_controller.kt b/payapi/src/main/kotlin/com/supwisdom/dlpay/api/controller/transaction_controller.kt
index b895284..30e5a91 100644
--- a/payapi/src/main/kotlin/com/supwisdom/dlpay/api/controller/transaction_controller.kt
+++ b/payapi/src/main/kotlin/com/supwisdom/dlpay/api/controller/transaction_controller.kt
@@ -28,6 +28,7 @@
 import org.springframework.http.ResponseEntity
 import org.springframework.validation.annotation.Validated
 import org.springframework.web.bind.annotation.*
+import javax.servlet.http.HttpServletRequest
 import javax.validation.ConstraintViolationException
 import javax.validation.Valid
 import kotlin.math.roundToInt
@@ -103,7 +104,7 @@
 
         val agent = agentPayServiceContext.findInAppPayService<Any>(sourceType.sourceType)
                 ?: return ResponseBodyBuilder.badRequest("不支持支付方式<${sourceType.sourceType}>")
-
+        sourceType.depositeSubjno = "112211";
         val subject = accountUtilServcie.readSubject(sourceType.depositeSubjno)
 
         val tradeCode = when (param.inAppType) {
@@ -116,7 +117,9 @@
                 .setTransInfo(param.transDate, param.transTime, tradeCode, sourceType.sourceType)
                 .setOutTransInfo(TenantContextHolder.getContext().tenant.id, param.billno)
                 .apply {
+                    param.description = "描述不能为空";
                     description = param.description
+                    dtltype = "recharge"   //必须的参数
                 }
         val amount = param.totalAmount / 100.0
         when (param.recipientType) {
@@ -164,7 +167,7 @@
         transactionServiceProxy.wip(transaction.refno)
         transactionContainer.add(transaction.refno)
         val agentResponse = agent.init(transaction)
-        if (agentResponse.code != AgentCode.SUCCESS) {
+        if (agentResponse.agentCode != AgentCode.PAYINITSUCCESS.value()) {
             return ResponseBodyBuilder
                     .internalServerError("请求第三方支付平台失败,${agentResponse.agentMsg}")
         }
@@ -177,6 +180,8 @@
             this.refno = transaction.refno
             this.status = transaction.status
             this.message = "初始化成功"
+            this.callbackUrl =  agentResponse.agentBody
+            this.externalRefno = agentResponse.agentRefno
         }
         return ResponseBodyBuilder.successEntity(response)
     }
@@ -221,27 +226,14 @@
     @PostMapping("/inapp/{sourcetype}/{tenant}")
     fun inAppPayCallback(@PathVariable("sourcetype") sourcetype: String,
                          @PathVariable("tenant") tenant: String,
-                         refno: String,
-                         request: HttpRequest): ResponseEntity<*> {
+                         request: HttpServletRequest): String {
         val tenantDetails = tenantDetailsRegistrar.getTenantDetailsById(tenant)
-                ?: return ResponseBodyBuilder.notFound("请求租户 <$tenant> 不存在")
+                ?: return "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[请求租户<$tenant>不存在]]></return_msg></xml>"
         val agent = agentPayServiceContext.findInAppPayService<Any>(sourcetype)
-                ?: return ResponseBodyBuilder.badRequest("请求支付类型错误 <$sourcetype> 不存在")
+                ?: return "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[请求支付类型错误 <$sourcetype> 不存在]]></return_msg></xml>"
         TenantContextHolder.getContext().tenant = tenantDetails.get()
-        val transaction = transactionServiceProxy.findTransactionByRefno(refno)
-        val agentResponse = agent.notify(transaction, request)
-        if (agentResponse.code == AgentCode.SUCCESS) {
-            transactionServiceProxy.success(refno)
-        } else if (agentResponse.code == AgentCode.REQUIRE_QUERY) {
-            transaction
-        } else {
-            transactionServiceProxy.fail(refno, agentResponse.agentMsg)
-        }?.let {
-            return ResponseBodyBuilder.successEntity(InAppPayResponse().apply {
-                this.refno = it.refno
-                this.status = it.status
-            })
-        }
-        return ResponseBodyBuilder.internalServerError("请求参数错误")
+        val agentResponse = agent.notify(null,request)
+
+        return agentResponse.agentBody;
     }
 }
\ No newline at end of file