From c2eb2e29d0a50aefb61d9ee77e27bad113a6fb8d Mon Sep 17 00:00:00 2001 From: "yunpeng.ma" Date: Mon, 2 Mar 2020 17:04:02 +0800 Subject: [PATCH] =?utf8?q?=E5=BE=AE=E4=BF=A1=E5=85=85=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../com/supwisdom/dlpay/agent/AgentCode.java | 2 + .../dlpay/agent/InAppPayService.java | 4 +- .../agent/service/impl/wechat_service_impl.kt | 101 ++++++++++++++++++ .../dlpay/agent/service/inapp_alipay.kt | 3 +- .../dlpay/agent/service/inapp_wechatpay.kt | 57 ++++++++-- .../dlpay/agent/service/wechat_service.kt | 3 + .../api/controller/transaction_controller.kt | 34 +++--- 7 files changed, 175 insertions(+), 29 deletions(-) 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 0d56cc14..a8058868 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 47f51d9f..88aec260 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 @@ package com.supwisdom.dlpay.agent; import com.supwisdom.dlpay.api.domain.TTransactionMain; import org.springframework.http.HttpRequest; +import javax.servlet.http.HttpServletRequest; + /** * 微信、支付宝、银联等第三方支付平台 H5 , JSAPI , Native , 小程序等手机端支付接口 * @@ -25,7 +27,7 @@ public interface InAppPayService { * @param request - 第三方平台通知请求的 HttpRequest * @return */ - AgentResponse notify(TTransactionMain transaction, HttpRequest request); + AgentResponse 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 61fa7347..25a10ba0 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.agent.service.WechatService 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.http.converter.StringHttpMessageConverter 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 @@ class WechatServiceImpl(val sourceTypeService: SourceTypeService, return agentResponse } + override fun dopayInit(transaction: TTransactionMain): AgentResponse { + val agentResponse = AgentResponse() + //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 { + val agentResponse = AgentResponse() + //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 b0bcc319..b1166751 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.agent.InAppPayService 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 @@ class AlipayInAppServcie : InAppPayService { TODO("not implemented") //To change body of created functions use File | Settings | File Templates. } - override fun notify(transaction: TTransactionMain?, request: HttpRequest?): AgentResponse { + override fun notify(transaction: TTransactionMain?, request: HttpServletRequest?): AgentResponse { 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 494e1344..d531da1b 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 @@ class WechatInAppService(private val sourceTypeService: SourceTypeService, override fun init(transation: TTransactionMain): AgentResponse { val wechatPayResponse = AgentResponse(); - 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 { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + override fun notify(transaction: TTransactionMain?, request: HttpServletRequest): AgentResponse { + val wechatPayResponse = AgentResponse(); + // 解析结果存储在HashMap + var map: MutableMap = HashMap() + val inputStream = request.inputStream + // 读取输入流 + val reader = SAXReader() + val document = reader.read(inputStream) + // 得到xml根元素 + val root = document.rootElement + // 得到根元素的所有子节点 + val elementList = root.elements() as MutableList + // 遍历所有子节点 + for (e in elementList) { + map[e.name] = e.text + } + // 释放资源 + inputStream.close() + val resp = wechatService.doNotify(map) + if (resp.code == AgentCode.SUCCESS) { + wechatPayResponse.agentBody = "" + } else { + wechatPayResponse.agentBody = "" + } + return wechatPayResponse; } - override fun query(transation: TTransactionMain?): AgentResponse { - TODO("not implemented") //To change body of created functions use File | Settings | File Templates. + override fun query(transation: TTransactionMain): AgentResponse { + val wechatPayResponse = AgentResponse(); + 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 683d1860..30c88504 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 @@ interface WechatService { fun doNotify(param: Map): AgentResponse + fun dopayInit(transaction: TTransactionMain): AgentResponse + + fun doPayQuery(transaction: TTransactionMain): AgentResponse } @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 b8952849..30e5a91b 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.HttpStatus 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 @@ class TransactionController(private val transactionServiceProxy: TransactionServ val agent = agentPayServiceContext.findInAppPayService(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 @@ class TransactionController(private val transactionServiceProxy: TransactionServ .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 @@ class TransactionController(private val transactionServiceProxy: TransactionServ 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 @@ class TransactionController(private val transactionServiceProxy: TransactionServ 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 @@ class TransactionNotifyController(@Lazy private val tenantDetailsRegistrar: Tena @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 "不存在]]>" val agent = agentPayServiceContext.findInAppPayService(sourcetype) - ?: return ResponseBodyBuilder.badRequest("请求支付类型错误 <$sourcetype> 不存在") + ?: return " 不存在]]>" 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 -- 2.17.1