--- /dev/null
+package com.supwisdom.dlpay.agent.domain;
+
+import org.joda.time.DateTime;
+
+import javax.persistence.*;
+import javax.validation.constraints.NotNull;
+import java.io.Serializable;
+import java.sql.Timestamp;
+import java.time.Instant;
+
+@Entity
+@Table(name = "tb_inapp_pay_trans")
+@SequenceGenerator(name = "inapp_pay_trans_seq")
+public class InAppPayTrans implements Serializable {
+ private static final long serialVersionUID = 4414357931991577103L;
+
+ @Id
+ @Column(name = "id")
+ @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "inapp_pay_trans_seq")
+ private Integer id;
+
+ @Column(name = "refno")
+ @NotNull
+ private String refno;
+
+ @Column(name = "sourcetype")
+ @NotNull
+ private String sourceType;
+
+ @Column(name = "agent_refno")
+ private String agentRefno;
+
+ @Column(name = "create_time")
+ private Timestamp createTime;
+
+ @Column(name = "update_time")
+ private Timestamp updateTime;
+
+ @PrePersist
+ public void prePersist() {
+ if (this.createTime == null) {
+ this.createTime = Timestamp.from(Instant.now());
+ }
+ }
+}
package com.supwisdom.dlpay.api.controller
+import com.supwisdom.dlpay.agent.AgentCode
+import com.supwisdom.dlpay.agent.AgentPayServiceContext
+import com.supwisdom.dlpay.agent.InAppPayService
import com.supwisdom.dlpay.api.TransactionBuilder
import com.supwisdom.dlpay.api.bean.InAppPayParam
import com.supwisdom.dlpay.api.bean.InAppPayResponse
import com.supwisdom.dlpay.api.bean.TransactionQueryResponse
import com.supwisdom.dlpay.api.domain.TSourceType
+import com.supwisdom.dlpay.api.domain.TTransactionMain
import com.supwisdom.dlpay.api.service.AccountUtilServcie
import com.supwisdom.dlpay.api.service.SourceTypeService
import com.supwisdom.dlpay.api.service.TransactionServiceProxy
import org.springframework.context.annotation.Lazy
import org.springframework.data.redis.connection.RedisConnectionFactory
import org.springframework.data.redis.core.RedisTemplate
+import org.springframework.http.HttpRequest
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
private val systemUtilService: SystemUtilService,
private val accountUtilServcie: AccountUtilServcie,
private val sourceTypeService: SourceTypeService,
+ private val agentPayServiceContext: AgentPayServiceContext,
redisConnectionFactory: RedisConnectionFactory) {
private val transactionContainer = TransactionContainer("inapp.pay", redisConnectionFactory)
+ fun doQuery(transaction: TTransactionMain, agent: InAppPayService<Any>): TTransactionMain {
+ val agentResponse = agent.query(transaction)
+ return when (agentResponse.agentCode) {
+ AgentCode.REQUIRE_QUERY.value() -> {
+ transaction
+ }
+ AgentCode.SUCCESS.value() -> {
+ transactionServiceProxy.success(transaction.refno)
+
+ }
+ else -> {
+ transactionServiceProxy.fail(transaction.refno, agentResponse.agentMsg)
+ }
+ }
+ }
+
@GetMapping("/inapp/query")
fun queryTransaction(refno: String): ResponseEntity<*> {
- val tranaction = transactionServiceProxy.findTransactionByRefno(refno)
+ val transaction = transactionServiceProxy.findTransactionByRefno(refno)
?: return ResponseBodyBuilder.notFound("未找到流水")
- if (tranaction.tenantid != TenantContextHolder.getContext().tenant.id) {
+ if (transaction.tenantid != TenantContextHolder.getContext().tenant.id) {
return ResponseBodyBuilder.conflict("未找到流水")
}
- if (tranaction.status != TradeDict.DTL_STATUS_SUCCESS) {
- transactionContainer.add(refno)
- // 查询第三方
- transactionContainer.remove(refno)
+ val agent = agentPayServiceContext.findInAppPayService<Any>(transaction.sourceType)
+ ?: return ResponseBodyBuilder.notFound("支付类别不支持 <${transaction.sourceType}>")
+ val resultTrans = when (transaction.status) {
+ TradeDict.DTL_STATUS_WIP -> doQuery(transaction, agent)
+ else -> transaction
}
return ResponseBodyBuilder.successEntity(TransactionQueryResponse().apply {
- this.refno = tranaction.refno
- accdate = tranaction.accdate
- status = tranaction.status
+ this.refno = resultTrans.refno
+ accdate = resultTrans.accdate
+ status = resultTrans.status
hostdate = systemUtilService.sysdatetime.hostdate
hosttime = systemUtilService.sysdatetime.hosttime
}, "成功")
fun inAppPayInit(@Valid param: InAppPayParam): ResponseEntity<*> {
val response = InAppPayResponse()
if (transactionContainer.count() > 100) {
- return ResponseBodyBuilder.serviceUnavailable("第三方请求异常")
+ return ResponseBodyBuilder.internalServerError("第三方请求异常")
}
val sourceType = sourceTypeService.getBySourceType(param.sourceType)
?: return ResponseBodyBuilder.badRequest("source type <${param.sourceType}> 不存在")
+ val agent = agentPayServiceContext.findInAppPayService<Any>(sourceType.sourceType)
+ ?: return ResponseBodyBuilder.badRequest("不支持支付方式<${sourceType.sourceType}>")
+
val subject = accountUtilServcie.readSubject(sourceType.depositeSubjno)
val tradeCode = when (param.inAppType) {
}
val transaction = transactionServiceProxy.init(builder)
// 调用第三方完成交易初始化
-
transactionServiceProxy.wip(transaction.refno)
transactionContainer.add(transaction.refno)
+ val agentResponse = agent.init(transaction)
+ if (agentResponse.code != AgentCode.SUCCESS) {
+ return ResponseBodyBuilder
+ .internalServerError("请求第三方支付平台失败,${agentResponse.agentMsg}")
+ }
+
response.apply {
when (param.recipientType) {
"shop" -> actuallyAmount = (transaction.shopDtl.amount * 100).roundToInt()
@RestController("/api/notify")
class TransactionNotifyController(@Lazy private val tenantDetailsRegistrar: TenantDetailsRegistrar,
- private val transactionServiceProxy: TransactionServiceProxy) {
-
- @PostMapping("/inapp/alipay/{tenant}")
- fun inAppPayCallback(@PathVariable("tenant") tenant: String,
- refno: String): ResponseEntity<*> {
+ private val transactionServiceProxy: TransactionServiceProxy,
+ private val agentPayServiceContext: AgentPayServiceContext) {
+
+ @PostMapping("/inapp/{sourcetype}/{tenant}")
+ fun inAppPayCallback(@PathVariable("sourcetype") sourcetype: String,
+ @PathVariable("tenant") tenant: String,
+ refno: String,
+ request: HttpRequest): ResponseEntity<*> {
val tenantDetails = tenantDetailsRegistrar.getTenantDetailsById(tenant)
?: return ResponseBodyBuilder.notFound("请求租户 <$tenant> 不存在")
+ val agent = agentPayServiceContext.findInAppPayService<Any>(sourcetype)
+ ?: return ResponseBodyBuilder.badRequest("请求支付类型错误 <$sourcetype> 不存在")
TenantContextHolder.getContext().tenant = tenantDetails.get()
- val transaction = transactionServiceProxy.success(refno)
- return ResponseEntity.ok(ResponseBodyBuilder.create()
- .data("refno", transaction.refno))
+ 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("请求参数错误")
}
}
\ No newline at end of file