From 1026aa1f9d9088fffbde0d399adf2bf7e3ce1c1e Mon Sep 17 00:00:00 2001 From: qiaowei Date: Tue, 9 Apr 2019 16:32:03 +0800 Subject: [PATCH] =?utf8?q?=E4=B8=80=E5=8D=A1=E9=80=9A=E6=94=AF=E4=BB=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- build.gradle | 3 + .../dlpay/consume/bean/BaseResp.java | 49 +++-- .../dlpay/consume/bean/ReqParam.java | 147 ++++++++++++++ .../dlpay/consume/bean/SupStatusRevResp.java | 25 +++ .../dlpay/consume/bean/SupYktResp.java | 92 +++++++++ .../consume/controller/PayapiAction.java | 98 +++++++++ .../dlpay/consume/dao/PaytypeConfigDao.java | 14 ++ .../dlpay/consume/dao/PaytypeDao.java | 10 + .../dlpay/consume/dao/TransdtlDao.java | 8 + .../dlpay/consume/domain/TPaytype.java | 55 ++++++ .../dlpay/consume/domain/TPaytypeConfig.java | 56 ++++++ .../consume/domain/TPaytypeConfigPK.java | 53 +++++ .../dlpay/consume/domain/TTransdtlOrder.java | 38 ++++ .../dlpay/consume/service/PayapiService.java | 14 ++ .../dlpay/consume/service/PaytypeService.java | 16 ++ .../service/impl/PayapiServiceImpl.java | 186 ++++++++++++++++++ .../service/impl/PaytypeServiceImpl.java | 39 ++++ .../dlpay/framework/util/TradeCode.java | 4 + .../java/com/supwisdom/dlpay/util/Code.java | 39 ++++ .../com/supwisdom/dlpay/util/ConstUtil.java | 15 ++ .../com/supwisdom/dlpay/util/HmacUtil.java | 130 ++++++++++++ .../com/supwisdom/dlpay/util/MoneyUtil.java | 28 +++ .../com/supwisdom/dlpay/util/PaytypeUtil.java | 10 + 23 files changed, 1108 insertions(+), 21 deletions(-) create mode 100644 src/main/java/com/supwisdom/dlpay/consume/bean/ReqParam.java create mode 100644 src/main/java/com/supwisdom/dlpay/consume/bean/SupStatusRevResp.java create mode 100644 src/main/java/com/supwisdom/dlpay/consume/bean/SupYktResp.java create mode 100644 src/main/java/com/supwisdom/dlpay/consume/controller/PayapiAction.java create mode 100644 src/main/java/com/supwisdom/dlpay/consume/dao/PaytypeConfigDao.java create mode 100644 src/main/java/com/supwisdom/dlpay/consume/dao/PaytypeDao.java create mode 100644 src/main/java/com/supwisdom/dlpay/consume/domain/TPaytype.java create mode 100644 src/main/java/com/supwisdom/dlpay/consume/domain/TPaytypeConfig.java create mode 100644 src/main/java/com/supwisdom/dlpay/consume/domain/TPaytypeConfigPK.java create mode 100644 src/main/java/com/supwisdom/dlpay/consume/domain/TTransdtlOrder.java create mode 100644 src/main/java/com/supwisdom/dlpay/consume/service/PayapiService.java create mode 100644 src/main/java/com/supwisdom/dlpay/consume/service/PaytypeService.java create mode 100644 src/main/java/com/supwisdom/dlpay/consume/service/impl/PayapiServiceImpl.java create mode 100644 src/main/java/com/supwisdom/dlpay/consume/service/impl/PaytypeServiceImpl.java create mode 100644 src/main/java/com/supwisdom/dlpay/util/Code.java create mode 100644 src/main/java/com/supwisdom/dlpay/util/ConstUtil.java create mode 100644 src/main/java/com/supwisdom/dlpay/util/HmacUtil.java create mode 100644 src/main/java/com/supwisdom/dlpay/util/MoneyUtil.java create mode 100644 src/main/java/com/supwisdom/dlpay/util/PaytypeUtil.java diff --git a/build.gradle b/build.gradle index ea080fbb..381e2614 100644 --- a/build.gradle +++ b/build.gradle @@ -25,11 +25,14 @@ dependencies { providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat' testImplementation 'org.springframework.boot:spring-boot-starter-test' + compile group: 'com.sun.jersey', name: 'jersey-client', version:'1.19' compile group: 'javax.servlet', name: 'jstl', version: '1.2' compile group: 'taglibs', name: 'standard', version: '1.1.2' compile group: 'javax.servlet.jsp', name: 'jsp-api', version: '2.1' compile group: 'log4j', name: 'log4j', version: '1.2.16' compile files ('libs/ojdbc6.jar') + + compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5' } compileKotlin { diff --git a/src/main/java/com/supwisdom/dlpay/consume/bean/BaseResp.java b/src/main/java/com/supwisdom/dlpay/consume/bean/BaseResp.java index 8c4c8a1d..1df37a71 100644 --- a/src/main/java/com/supwisdom/dlpay/consume/bean/BaseResp.java +++ b/src/main/java/com/supwisdom/dlpay/consume/bean/BaseResp.java @@ -1,31 +1,38 @@ package com.supwisdom.dlpay.consume.bean; +import com.supwisdom.dlpay.util.Code; + public class BaseResp { - private String retcode; - private String retmsg; - private Object data; + private String retcode; + private String retmsg; + private Object data; + + public String getRetcode() { + return retcode; + } - public String getRetcode() { - return retcode; - } + public void setRetcode(String retcode) { + this.retcode = retcode; + } - public void setRetcode(String retcode) { - this.retcode = retcode; - } + public String getRetmsg() { + return retmsg; + } - public String getRetmsg() { - return retmsg; - } + public void setRetmsg(String retmsg) { + this.retmsg = retmsg; + } - public void setRetmsg(String retmsg) { - this.retmsg = retmsg; - } + public Object getData() { + return data; + } - public Object getData() { - return data; - } + public void setData(Object data) { + this.data = data; + } - public void setData(Object data) { - this.data = data; - } + public void setCode(Code code) { + this.retcode = code.getCode(); + this.retmsg = code.getRetmsg(); + } } diff --git a/src/main/java/com/supwisdom/dlpay/consume/bean/ReqParam.java b/src/main/java/com/supwisdom/dlpay/consume/bean/ReqParam.java new file mode 100644 index 00000000..31fac50b --- /dev/null +++ b/src/main/java/com/supwisdom/dlpay/consume/bean/ReqParam.java @@ -0,0 +1,147 @@ +package com.supwisdom.dlpay.consume.bean; + +/** + * Created by shuwei on 2019/4/9. + */ +public class ReqParam { + /** + * 交易流水号 + * */ + private String refno; + /** + * 交易金额(分) + * */ + private Integer amount; + /** + * 支付渠道 + * */ + private String paytype; + /** + * 订单信息 + * */ + private String payinfo; + /** + * 订单详细 + * */ + private String transdesc; + /** + * 交易日期 + * */ + private String transdate; + /** + * 交易时间 + * */ + private String transtime; + /** + * 账号 + * */ + private String accno; + /** + * 商户号 + * */ + private String shopaccno; + /** + * 签名 + * */ + private String sign; + + + private String yktshopid; + private String devphyid; + + public String getYktshopid() { + return yktshopid; + } + + public void setYktshopid(String yktshopid) { + this.yktshopid = yktshopid; + } + + public String getDevphyid() { + return devphyid; + } + + public void setDevphyid(String devphyid) { + this.devphyid = devphyid; + } + + public String getRefno() { + return refno; + } + + public void setRefno(String refno) { + this.refno = refno; + } + + public Integer getAmount() { + return amount; + } + + public void setAmount(Integer amount) { + this.amount = amount; + } + + public String getPaytype() { + return paytype; + } + + public void setPaytype(String paytype) { + this.paytype = paytype; + } + + public String getPayinfo() { + return payinfo; + } + + public void setPayinfo(String payinfo) { + this.payinfo = payinfo; + } + + public String getTransdesc() { + return transdesc; + } + + public void setTransdesc(String transdesc) { + this.transdesc = transdesc; + } + + public String getTransdate() { + return transdate; + } + + public void setTransdate(String transdate) { + this.transdate = transdate; + } + + public String getTranstime() { + return transtime; + } + + public void setTranstime(String transtime) { + this.transtime = transtime; + } + + public String getAccno() { + return accno; + } + + public void setAccno(String accno) { + this.accno = accno; + } + + public String getShopaccno() { + return shopaccno; + } + + public void setShopaccno(String shopaccno) { + this.shopaccno = shopaccno; + } + + public String getSign() { + return sign; + } + + public void setSign(String sign) { + this.sign = sign; + } +} diff --git a/src/main/java/com/supwisdom/dlpay/consume/bean/SupStatusRevResp.java b/src/main/java/com/supwisdom/dlpay/consume/bean/SupStatusRevResp.java new file mode 100644 index 00000000..355fd3d9 --- /dev/null +++ b/src/main/java/com/supwisdom/dlpay/consume/bean/SupStatusRevResp.java @@ -0,0 +1,25 @@ +package com.supwisdom.dlpay.consume.bean; + +/** + * Created by shuwei on 2018/10/18. + */ +public class SupStatusRevResp { + private Integer status; + private Boolean revflag; + + public Integer getStatus() { + return status; + } + + public void setStatus(Integer status) { + this.status = status; + } + + public Boolean getRevflag() { + return revflag; + } + + public void setRevflag(Boolean revflag) { + this.revflag = revflag; + } +} diff --git a/src/main/java/com/supwisdom/dlpay/consume/bean/SupYktResp.java b/src/main/java/com/supwisdom/dlpay/consume/bean/SupYktResp.java new file mode 100644 index 00000000..32caa3e1 --- /dev/null +++ b/src/main/java/com/supwisdom/dlpay/consume/bean/SupYktResp.java @@ -0,0 +1,92 @@ +package com.supwisdom.dlpay.consume.bean; + +/** + * Created by shuwei on 2018/8/28. + */ +public class SupYktResp { + private String retcode; + private String retmsg; + private String tradeno; + private String refno; + private String sign; + private String timestamp; + private String sign_method; + private Integer balance; + private String other; + + public String getOther() { + return other; + } + + public void setOther(String other) { + this.other = other; + } + + public String getRetcode() { + return retcode; + } + + public void setRetcode(String retcode) { + this.retcode = retcode; + } + + public String getRetmsg() { + return retmsg; + } + + public void setRetmsg(String retmsg) { + this.retmsg = retmsg; + } + + public String getTradeno() { + return tradeno; + } + + public void setTradeno(String tradeno) { + this.tradeno = tradeno; + } + + public String getRefno() { + return refno; + } + + public void setRefno(String refno) { + this.refno = refno; + } + + public String getSign() { + return sign; + } + + public void setSign(String sign) { + this.sign = sign; + } + + public String getTimestamp() { + return timestamp; + } + + public void setTimestamp(String timestamp) { + this.timestamp = timestamp; + } + + public String getSign_method() { + return sign_method; + } + + public void setSign_method(String sign_method) { + this.sign_method = sign_method; + } + + public Integer getBalance() { + return balance; + } + + public void setBalance(Integer balance) { + this.balance = balance; + } + + public boolean checkSign(String appid,String key){ + return true; + } +} diff --git a/src/main/java/com/supwisdom/dlpay/consume/controller/PayapiAction.java b/src/main/java/com/supwisdom/dlpay/consume/controller/PayapiAction.java new file mode 100644 index 00000000..ac7d7feb --- /dev/null +++ b/src/main/java/com/supwisdom/dlpay/consume/controller/PayapiAction.java @@ -0,0 +1,98 @@ +package com.supwisdom.dlpay.consume.controller; + +import com.supwisdom.dlpay.consume.bean.BaseResp; +import com.supwisdom.dlpay.consume.bean.ReqParam; +import com.supwisdom.dlpay.consume.service.PayapiService; +import com.supwisdom.dlpay.consume.service.PaytypeService; +import com.supwisdom.dlpay.framework.data.SystemDateTime; +import com.supwisdom.dlpay.util.Code; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.util.StringUtils; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * 统一支付入口 + * 1.支付 + * 2.冲正 + * 3.充值 + * + * 校验 + * -参数 + * -签名 + */ +@RestController +@RequestMapping("/unified") +public class PayapiAction { + @Autowired + private PayapiService payapiService; + + /** + * 公共参数校验 + * */ + + private boolean checkCommonParam(BaseResp resp,ReqParam param){ + if(param.getAmount()==null){ + resp.setRetcode("1"); + resp.setRetmsg("参数错误[amount]"); + return false; + } + if(StringUtils.isEmpty(param.getPayinfo())){ + resp.setRetcode("1"); + resp.setRetmsg("参数错误[payinfo]"); + return false; + } + if(StringUtils.isEmpty(param.getPaytype())){ + resp.setRetcode("1"); + resp.setRetmsg("参数错误[paytype]"); + return false; + } + if(StringUtils.isEmpty(param.getRefno())){ + resp.setRetcode("1"); + resp.setRetmsg("参数错误[refno]"); + return false; + } + return true; + } + /** + * + * 统一消费接口 + * + * + * */ + @RequestMapping("/consume") + public BaseResp consume(@RequestBody ReqParam param) { + BaseResp resp = new BaseResp(); + if(!checkCommonParam(resp,param)){ + return resp; + } + payapiService.doConsume(param,resp); + return resp; + } + + /** + * + * 统一冲正接口 + * + * + * */ + @RequestMapping("/reverse") + public BaseResp reverse() { + BaseResp resp = new BaseResp(); + resp.setCode(Code.SUCCESS); + return resp; + } + + /** + * + * 统一充值接口 + * + * */ + @RequestMapping("/recharge") + public BaseResp recharge() { + BaseResp resp = new BaseResp(); + resp.setCode(Code.SUCCESS); + return resp; + } +} diff --git a/src/main/java/com/supwisdom/dlpay/consume/dao/PaytypeConfigDao.java b/src/main/java/com/supwisdom/dlpay/consume/dao/PaytypeConfigDao.java new file mode 100644 index 00000000..62afeccb --- /dev/null +++ b/src/main/java/com/supwisdom/dlpay/consume/dao/PaytypeConfigDao.java @@ -0,0 +1,14 @@ +package com.supwisdom.dlpay.consume.dao; + +import com.supwisdom.dlpay.consume.domain.TPaytypeConfig; +import com.supwisdom.dlpay.consume.domain.TPaytypeConfigPK; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; + +/** + * Created by shuwei on 2019/4/9. + */ +public interface PaytypeConfigDao extends JpaRepository { + List getByPaytype(String paytype); +} diff --git a/src/main/java/com/supwisdom/dlpay/consume/dao/PaytypeDao.java b/src/main/java/com/supwisdom/dlpay/consume/dao/PaytypeDao.java new file mode 100644 index 00000000..79e97c1c --- /dev/null +++ b/src/main/java/com/supwisdom/dlpay/consume/dao/PaytypeDao.java @@ -0,0 +1,10 @@ +package com.supwisdom.dlpay.consume.dao; + +import com.supwisdom.dlpay.consume.domain.TPaytype; +import org.springframework.data.jpa.repository.JpaRepository; + +/** + * Created by shuwei on 2019/4/9. + */ +public interface PaytypeDao extends JpaRepository { +} diff --git a/src/main/java/com/supwisdom/dlpay/consume/dao/TransdtlDao.java b/src/main/java/com/supwisdom/dlpay/consume/dao/TransdtlDao.java index a7c2c4f0..ff74e332 100644 --- a/src/main/java/com/supwisdom/dlpay/consume/dao/TransdtlDao.java +++ b/src/main/java/com/supwisdom/dlpay/consume/dao/TransdtlDao.java @@ -22,4 +22,12 @@ public interface TransdtlDao extends JpaRepository { "where t.status='success' and t.feetype!='none' and t.accdate=:settledate " + "group by a.drsubjno,a.crsubjno,a.summary,t.shopaccno ", nativeQuery = true) List getFeeVoucherData(@RequestParam("settledate") String settledate); + + TTransdtl getByOuttradenoAndStatus(String outtradeno,String status); + + @Query(value = " select to_char(sysdate,'yyyyMMddhh24missSSS')||to_char(SEQ_REFNO.nextval,'FM0000000') as billno from dual ",nativeQuery = true) + String getReno(); + + @Query(value = " select to_char(CURRENT_TIMESTAMP,'yyyyMMddhh24missSS')||to_char(nextval('SEQ_REFNO'),'FM0000000') as billno ",nativeQuery = true) + String getRenoPG(); } diff --git a/src/main/java/com/supwisdom/dlpay/consume/domain/TPaytype.java b/src/main/java/com/supwisdom/dlpay/consume/domain/TPaytype.java new file mode 100644 index 00000000..07d4679a --- /dev/null +++ b/src/main/java/com/supwisdom/dlpay/consume/domain/TPaytype.java @@ -0,0 +1,55 @@ +package com.supwisdom.dlpay.consume.domain; + +import javax.persistence.*; + +/** + * Created by shuwei on 2019/4/9. + */ +@Entity +@Table(name = "TB_PAYTYPE") +public class TPaytype { + @Id + @Column(name = "PAYTYPE", nullable = false, length = 20) + private String paytype; + + @Column(name = "ENABLE", nullable = false, length = 20) + private String enable; + + @Column(name = "DPS_ENABLE", length = 20) + private String dpsEnable; + + @Column(name = "REVERSE_ENABLE", length = 20) + private String reverseEnable; + + public String getPaytype() { + return paytype; + } + + public void setPaytype(String paytype) { + this.paytype = paytype; + } + + public String getEnable() { + return enable; + } + + public void setEnable(String enable) { + this.enable = enable; + } + + public String getDpsEnable() { + return dpsEnable; + } + + public void setDpsEnable(String dpsEnable) { + this.dpsEnable = dpsEnable; + } + + public String getReverseEnable() { + return reverseEnable; + } + + public void setReverseEnable(String reverseEnable) { + this.reverseEnable = reverseEnable; + } +} diff --git a/src/main/java/com/supwisdom/dlpay/consume/domain/TPaytypeConfig.java b/src/main/java/com/supwisdom/dlpay/consume/domain/TPaytypeConfig.java new file mode 100644 index 00000000..65cf8968 --- /dev/null +++ b/src/main/java/com/supwisdom/dlpay/consume/domain/TPaytypeConfig.java @@ -0,0 +1,56 @@ +package com.supwisdom.dlpay.consume.domain; + +import javax.persistence.*; + +/** + * Created by shuwei on 2019/4/9. + */ +@Entity +@Table(name = "TB_PAYTYPE_CONFIG") +@IdClass(TPaytypeConfigPK.class) +public class TPaytypeConfig { + @Id + @Column(name = "PAYTYPE", nullable = false, length = 20) + private String paytype; + @Id + @Column(name = "CONFIGID", nullable = false, length = 40) + private String configid; + + @Column(name = "CONFIG_VALUE", length = 2000) + private String configValue; + + @Column(name = "CONFIG_NAME", length = 200) + private String configName; + + public String getPaytype() { + return paytype; + } + + public void setPaytype(String paytype) { + this.paytype = paytype; + } + + public String getConfigid() { + return configid; + } + + public void setConfigid(String configid) { + this.configid = configid; + } + + public String getConfigValue() { + return configValue; + } + + public void setConfigValue(String configValue) { + this.configValue = configValue; + } + + public String getConfigName() { + return configName; + } + + public void setConfigName(String configName) { + this.configName = configName; + } +} diff --git a/src/main/java/com/supwisdom/dlpay/consume/domain/TPaytypeConfigPK.java b/src/main/java/com/supwisdom/dlpay/consume/domain/TPaytypeConfigPK.java new file mode 100644 index 00000000..5046c0bb --- /dev/null +++ b/src/main/java/com/supwisdom/dlpay/consume/domain/TPaytypeConfigPK.java @@ -0,0 +1,53 @@ +package com.supwisdom.dlpay.consume.domain; + +import javax.persistence.Column; +import javax.persistence.Embeddable; +import javax.persistence.Id; +import java.io.Serializable; + +/** + * Created by shuwei on 2019/4/9. + */ +@Embeddable +public class TPaytypeConfigPK implements Serializable { + @Id + @Column(name = "PAYTYPE", nullable = false, length = 20) + private String paytype; + @Id + @Column(name = "CONFIGID", nullable = false, length = 40) + private String configid; + + public String getPaytype() { + return paytype; + } + + public void setPaytype(String paytype) { + this.paytype = paytype; + } + + public String getConfigid() { + return configid; + } + + public void setConfigid(String configid) { + this.configid = configid; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + TPaytypeConfigPK that = (TPaytypeConfigPK) o; + + if (!paytype.equals(that.paytype)) return false; + return configid.equals(that.configid); + } + + @Override + public int hashCode() { + int result = paytype.hashCode(); + result = 31 * result + configid.hashCode(); + return result; + } +} diff --git a/src/main/java/com/supwisdom/dlpay/consume/domain/TTransdtlOrder.java b/src/main/java/com/supwisdom/dlpay/consume/domain/TTransdtlOrder.java new file mode 100644 index 00000000..edc005e9 --- /dev/null +++ b/src/main/java/com/supwisdom/dlpay/consume/domain/TTransdtlOrder.java @@ -0,0 +1,38 @@ +package com.supwisdom.dlpay.consume.domain; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +/** +* +* 流水明细表 +* */ + +@Entity +@Table(name = "TB_TRANSDTL_ORDER") +public class TTransdtlOrder { + @Id + @Column(name = "REFNO", nullable = false, length = 32) + private String refno; + + @Column(name = "ERRMSG", length = 100) + private String errmsg; + + public String getRefno() { + return refno; + } + + public void setRefno(String refno) { + this.refno = refno; + } + + public String getErrmsg() { + return errmsg; + } + + public void setErrmsg(String errmsg) { + this.errmsg = errmsg; + } +} diff --git a/src/main/java/com/supwisdom/dlpay/consume/service/PayapiService.java b/src/main/java/com/supwisdom/dlpay/consume/service/PayapiService.java new file mode 100644 index 00000000..0c8bec0c --- /dev/null +++ b/src/main/java/com/supwisdom/dlpay/consume/service/PayapiService.java @@ -0,0 +1,14 @@ +package com.supwisdom.dlpay.consume.service; + +import com.supwisdom.dlpay.consume.bean.BaseResp; +import com.supwisdom.dlpay.consume.bean.ReqParam; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; + +/** + * Created by shuwei on 2019/4/9. + */ +public interface PayapiService { + @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class) + boolean doConsume(ReqParam param, BaseResp resp); +} diff --git a/src/main/java/com/supwisdom/dlpay/consume/service/PaytypeService.java b/src/main/java/com/supwisdom/dlpay/consume/service/PaytypeService.java new file mode 100644 index 00000000..7c6821df --- /dev/null +++ b/src/main/java/com/supwisdom/dlpay/consume/service/PaytypeService.java @@ -0,0 +1,16 @@ +package com.supwisdom.dlpay.consume.service; + +import com.supwisdom.dlpay.consume.domain.TPaytype; + +import java.util.Map; + +/** + * Created by shuwei on 2019/4/9. + */ +public interface PaytypeService { + + TPaytype getByPaytype(String paytype); + + Map getPaytypeConfigByPaytype(String pattype); + +} diff --git a/src/main/java/com/supwisdom/dlpay/consume/service/impl/PayapiServiceImpl.java b/src/main/java/com/supwisdom/dlpay/consume/service/impl/PayapiServiceImpl.java new file mode 100644 index 00000000..382d8212 --- /dev/null +++ b/src/main/java/com/supwisdom/dlpay/consume/service/impl/PayapiServiceImpl.java @@ -0,0 +1,186 @@ +package com.supwisdom.dlpay.consume.service.impl; + +import com.google.gson.Gson; +import com.sun.jersey.api.client.Client; +import com.sun.jersey.api.client.ClientResponse; +import com.sun.jersey.api.client.WebResource; +import com.supwisdom.dlpay.consume.bean.BaseResp; +import com.supwisdom.dlpay.consume.bean.ReqParam; +import com.supwisdom.dlpay.consume.bean.SupStatusRevResp; +import com.supwisdom.dlpay.consume.bean.SupYktResp; +import com.supwisdom.dlpay.consume.dao.TransdtlDao; +import com.supwisdom.dlpay.consume.domain.TPaytype; +import com.supwisdom.dlpay.consume.domain.TTransdtl; +import com.supwisdom.dlpay.consume.service.PayapiService; +import com.supwisdom.dlpay.consume.service.PaytypeService; +import com.supwisdom.dlpay.framework.data.SystemDateTime; +import com.supwisdom.dlpay.framework.service.SystemUtilService; +import com.supwisdom.dlpay.framework.util.DateUtil; +import com.supwisdom.dlpay.framework.util.TradeCode; +import com.supwisdom.dlpay.util.*; +import org.apache.log4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.util.StringUtils; + +import java.util.HashMap; +import java.util.Map; + +/** + * Created by shuwei on 2019/4/9. + */ +@Service +public class PayapiServiceImpl implements PayapiService { + @Autowired + private TransdtlDao transdtlDao; + @Autowired + private SystemUtilService systemUtilService; + @Autowired + private PaytypeService paytypeService; + private static final Logger logger = Logger.getLogger(PayapiServiceImpl.class); + private static final Gson gson = new Gson(); + + @Override + public boolean doConsume(ReqParam param, BaseResp resp) { + TTransdtl tTransdtl = transdtlDao.getByOuttradenoAndStatus(param.getRefno(), ConstUtil.STATUS_SUCCESS); + if (tTransdtl != null) { + resp.setRetcode("1"); + resp.setRetmsg("流水号重复"); + return false; + } + String refno = transdtlDao.getReno(); + if (StringUtils.isEmpty(refno)) { + resp.setCode(Code.SYS_ERROR); + return false; + } + tTransdtl = new TTransdtl(); + tTransdtl.setAccno(param.getAccno()); + tTransdtl.setAmount(MoneyUtil.FenToYuan(param.getAmount())); + tTransdtl.setPaidamt(MoneyUtil.FenToYuan(param.getAmount())); + tTransdtl.setShopaccno(param.getShopaccno()); + tTransdtl.setOuttradeno(param.getRefno()); + tTransdtl.setPaytype(param.getPaytype()); + tTransdtl.setTransdate(DateUtil.getNow("yyyyMMdd")); + tTransdtl.setTranstime(DateUtil.getNow("hhmmss")); + tTransdtl.setCreatetime(DateUtil.getNow()); + tTransdtl.setPayinfo(param.getPayinfo()); + tTransdtl.setStatus(ConstUtil.STATUS_INIT); + tTransdtl.setRefno(refno); + transdtlDao.save(tTransdtl); + switch (param.getPaytype()) { + case PaytypeUtil.YKTPAY: + return doYKTPay(param, tTransdtl, resp); + } + resp.setCode(Code.PAYTYPE_NOT_SUPPORT); + return false; + } + + /** + * 一卡通支付 + */ + + private boolean doYKTPay(ReqParam param, TTransdtl dtl, BaseResp resp) { + //TODO + TPaytype paytype = paytypeService.getByPaytype(PaytypeUtil.YKTPAY); + if (paytype == null || !ConstUtil.ENABLE_YES.equals(paytype.getEnable())) { + resp.setCode(Code.PAYTYPE_NOT_SUPPORT); + return false; + } + Map config = paytypeService.getPaytypeConfigByPaytype(PaytypeUtil.YKTPAY); + String appid = config.get("appid"); + String appkey = config.get("appkey"); + String orderurl = config.get("orderurl"); + if (config.size() == 0 + || StringUtils.isEmpty(appid) + || StringUtils.isEmpty(appkey) + || StringUtils.isEmpty(orderurl)) { + resp.setCode(Code.PAYTYPE_CONFIG_ERROR); + logger.error("支付方式未配置,appid=" + appid + ",appkey=" + appkey + ",orderurl=" + orderurl); + return false; + } + SystemDateTime systemDateTime = systemUtilService.getSysdatetime(); + dtl.setAccdate(systemDateTime.getHostdate()); + Map params = new HashMap(); + params.put("partner_id", appid); + params.put("stuempno", param.getAccno()); + params.put("tradeno", dtl.getRefno()); + params.put("tradename", param.getPayinfo()); + params.put("amount", param.getAmount() + ""); + params.put("shopid", param.getYktshopid()); + params.put("devphyid", param.getDevphyid()); + params.put("calcmanagefee", "T");//是否计算费率 + params.put("timestamp", systemDateTime.getHostdatetime()); + + params.put("sign_method", "HMAC"); + params.put("limitflag", "off"); //是否判断消费限额,on-判断;off-不判断。默认on(判断限额)。为空或不为off都是on; + String signstr = HmacUtil.createLinkString(HmacUtil.paraFilter(params)); + String sign = HmacUtil.HMACSHA1(signstr, appkey); + signstr += "&sign=" + sign + "&sourcetype=food"; + Client c = Client.create(); + c.setConnectTimeout(20000); + dtl.setTranstype(TradeCode.TRANSTYPE_YKTPAY); + dtl.setTranscode(TradeCode.TRANSCODE_YKTPAY); + + WebResource r = c.resource(orderurl); + ClientResponse respClient = r.post(ClientResponse.class, signstr); + if (200 == respClient.getStatus()) { + String ret = respClient.getEntity(String.class); + logger.error("***************yktpay.ret=" + ret + "*************************"); + if (ret != null) { + try { + SupYktResp result = gson.fromJson(ret, SupYktResp.class); + if (result == null) { + resp.setCode(Code.RESPNOSE_CONTENT_ERROR); + logger.error("支付内容转换失败:null"); + return false; + } + if (!"0".equals(result.getRetcode())) { + if ("30".equals(result.getRetcode())) { + if (result.getOther() != null) { + try { + SupStatusRevResp supStatusRevResp = gson.fromJson(result.getOther(), SupStatusRevResp.class); + if (supStatusRevResp != null) { + //同流水号,同一笔已经支付完成的,不重复支付。 + if (3 == supStatusRevResp.getStatus() && supStatusRevResp.getRevflag() != null && !supStatusRevResp.getRevflag()) { + dtl.setStatus(ConstUtil.STATUS_SUCCESS); + dtl.setAccdate(systemDateTime.getHostdate()); + transdtlDao.save(dtl); + resp.setCode(Code.SUCCESS); + return true; + } + } + } catch (Exception e) { + e.printStackTrace(); + logger.error("返回内容转换失败:" + e.getMessage()); + } + } + } + resp.setRetcode("1"); + resp.setRetmsg("交易失败:" + result.getRetmsg()); + logger.error(result.getRetmsg()); + //dtl.setErrmsg(result.getRetmsg()); + //payApiDTLDao.save(dtl); + return false; + } + dtl.setStatus(ConstUtil.STATUS_SUCCESS); + dtl.setAccdate(systemDateTime.getHostdate()); + transdtlDao.save(dtl); + resp.setCode(Code.SUCCESS); + return true; + } catch (Exception e) { + e.printStackTrace(); + resp.setCode(Code.RESPNOSE_CONTENT_ERROR); + logger.error("支付内容转换失败:" + e.getMessage()); + return false; + } + } else { + resp.setCode(Code.REQUEST_ERROR); + return false; + } + } else { + resp.setCode(Code.REQUEST_ERROR); + logger.error("请求返回失败:" + respClient.getEntity(String.class) + ",code=" + respClient.getStatus()); + return false; + } + } +} diff --git a/src/main/java/com/supwisdom/dlpay/consume/service/impl/PaytypeServiceImpl.java b/src/main/java/com/supwisdom/dlpay/consume/service/impl/PaytypeServiceImpl.java new file mode 100644 index 00000000..5aad2232 --- /dev/null +++ b/src/main/java/com/supwisdom/dlpay/consume/service/impl/PaytypeServiceImpl.java @@ -0,0 +1,39 @@ +package com.supwisdom.dlpay.consume.service.impl; + +import com.supwisdom.dlpay.consume.dao.PaytypeConfigDao; +import com.supwisdom.dlpay.consume.dao.PaytypeDao; +import com.supwisdom.dlpay.consume.domain.TPaytype; +import com.supwisdom.dlpay.consume.domain.TPaytypeConfig; +import com.supwisdom.dlpay.consume.service.PaytypeService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Created by shuwei on 2019/4/9. + */ +@Service +public class PaytypeServiceImpl implements PaytypeService { + @Autowired + private PaytypeDao paytypeDao; + @Autowired + private PaytypeConfigDao paytypeConfigDao; + + @Override + public TPaytype getByPaytype(String paytype) { + return paytypeDao.getOne(paytype); + } + + @Override + public Map getPaytypeConfigByPaytype(String pattype) { + List list = paytypeConfigDao.getByPaytype(pattype); + Map map = new HashMap<>(list.size()); + for (TPaytypeConfig paytypeConfig : list) { + map.put(paytypeConfig.getConfigid(), paytypeConfig.getConfigValue()); + } + return map; + } +} diff --git a/src/main/java/com/supwisdom/dlpay/framework/util/TradeCode.java b/src/main/java/com/supwisdom/dlpay/framework/util/TradeCode.java index b2067044..2c9d7df4 100644 --- a/src/main/java/com/supwisdom/dlpay/framework/util/TradeCode.java +++ b/src/main/java/com/supwisdom/dlpay/framework/util/TradeCode.java @@ -5,5 +5,9 @@ package com.supwisdom.dlpay.framework.util; * */ public class TradeCode { public static final int TRANSCODE_PAY = 6630; + public static final int TRANSCODE_YKTPAY=1000; + + public static final int TRANSTYPE_PAY = 311; + public static final int TRANSTYPE_YKTPAY=1000; } diff --git a/src/main/java/com/supwisdom/dlpay/util/Code.java b/src/main/java/com/supwisdom/dlpay/util/Code.java new file mode 100644 index 00000000..8f05ecd5 --- /dev/null +++ b/src/main/java/com/supwisdom/dlpay/util/Code.java @@ -0,0 +1,39 @@ +package com.supwisdom.dlpay.util; + +/** + * Created by shuwei on 2019/4/9. + */ +public enum Code { + SUCCESS("0","OK"), + PARAM_ERROR("1","参数错误"), + SYS_ERROR("2","系统异常"), + PAYTYPE_NOT_SUPPORT("10","支付方式不支持"), + PAYTYPE_CONFIG_ERROR("11","支付方式未配置或配置错误"), + REQUEST_ERROR("20","响应内容错误"), + RESPNOSE_CONTENT_ERROR("21","响应内容错误") + ; + + private String code; + private String retmsg; + + private Code(String code,String retmsg){ + this.code = code; + this.retmsg = retmsg; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getRetmsg() { + return retmsg; + } + + public void setRetmsg(String retmsg) { + this.retmsg = retmsg; + } +} diff --git a/src/main/java/com/supwisdom/dlpay/util/ConstUtil.java b/src/main/java/com/supwisdom/dlpay/util/ConstUtil.java new file mode 100644 index 00000000..4a54efa0 --- /dev/null +++ b/src/main/java/com/supwisdom/dlpay/util/ConstUtil.java @@ -0,0 +1,15 @@ +package com.supwisdom.dlpay.util; + +/** + * Created by shuwei on 2019/4/9. + */ +public class ConstUtil { + public static final String STATUS_INIT = "init"; + public static final String STATUS_SUCCESS = "success"; + public static final String STATUS_FAIL = "fail"; + + public static final String ENABLE_YES = "yes"; + + public static final String ENABLE_NO = "no"; + +} diff --git a/src/main/java/com/supwisdom/dlpay/util/HmacUtil.java b/src/main/java/com/supwisdom/dlpay/util/HmacUtil.java new file mode 100644 index 00000000..832c6998 --- /dev/null +++ b/src/main/java/com/supwisdom/dlpay/util/HmacUtil.java @@ -0,0 +1,130 @@ +package com.supwisdom.dlpay.util; + +import com.supwisdom.dlpay.framework.util.DateUtil; + +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; +import java.io.UnsupportedEncodingException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.util.*; + +public class HmacUtil { + /** + * 除去数组中的空值和签名参数 + * + * @param sArray 签名参数组 + * @return 去掉空值与签名参数后的新签名参数组 + */ + public static Map paraFilter(Map sArray) { + + Map result = new HashMap(); + + if (sArray == null || sArray.size() <= 0) { + return result; + } + + for (String key : sArray.keySet()) { + String value = sArray.get(key); + if (value == null || value.equals("") || "null".equals(value) || key.equalsIgnoreCase("sign")) { + continue; + } + result.put(key, value); + } + + return result; + } + + /** + * 把数组所有元素排序,并按照“参数=参数值”的模式用“&”字符拼接成字符串 + * + * @param params 需要排序并参与字符拼接的参数组 + * @return 拼接后字符串 + */ + public static String createLinkString(Map params) { + + List keys = new ArrayList(params.keySet()); + Collections.sort(keys); + + String prestr = ""; + + for (int i = 0; i < keys.size(); i++) { + String key = keys.get(i); + String value = params.get(key); + + if (i == keys.size() - 1) {//拼接时,不包括最后一个&字符 + prestr = prestr + key + "=" + value; + } else { + prestr = prestr + key + "=" + value + "&"; + } + } + + return prestr; + } + + public static String HMACSHA1(String data, String key) { + byte[] byteHMAC = null; + try { + Mac mac = Mac.getInstance("HmacSHA1"); + SecretKeySpec spec = new SecretKeySpec(key.getBytes(), "HmacSHA1"); + mac.init(spec); + byteHMAC = mac.doFinal(data.getBytes("UTF-8")); + } catch (InvalidKeyException e) { + e.printStackTrace(); + } catch (NoSuchAlgorithmException ignore) { + ignore.printStackTrace(); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + + if (byteHMAC != null) { + try { + String hexMac = getHexString(byteHMAC); + return hexMac; + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return null; + } + return null; + + } + + public static String getHexString(byte[] b) throws Exception { + String result = ""; + for (int i = 0; i < b.length; i++) { + result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1); + } + return result; + } + + public static boolean checkHmacSign(String data, String key, String sign) { + String asign = HMACSHA1(data, key); + if (asign == null) { + return false; + } + if (asign.equalsIgnoreCase(sign)) { + return true; + } + + return false; + } + + public static void main(String[] args) { + String refno = DateUtil.getNow(); + Map mapValue = new HashMap(); + mapValue.put("tradeaccount", "10000097"); + mapValue.put("tradename", "在线点餐"); + mapValue.put("tradeamount", "1"); + mapValue.put("paidamount", "1"); + mapValue.put("outtradeno", refno); + mapValue.put("yktshopid", "2"); + mapValue.put("shopid", "68512084"); + String signstr = createLinkString(HmacUtil.paraFilter(mapValue)); + String sign = HmacUtil.HMACSHA1(signstr, "adc4ac6822fd462780f878b86cb94688"); + System.out.println("{\"tradeaccount\":\"10000097\",\"tradename\":\"在线点餐\",\"tradeamount\":\"1\"," + + "\"paidamount\": \"1\",\"outtradeno\":\""+refno+"\",\"yktshopid\": \"2\",\"shopid\":\"68512084\",\"sign\": \""+sign+"\"}"); + } + +} diff --git a/src/main/java/com/supwisdom/dlpay/util/MoneyUtil.java b/src/main/java/com/supwisdom/dlpay/util/MoneyUtil.java new file mode 100644 index 00000000..2fb8b73a --- /dev/null +++ b/src/main/java/com/supwisdom/dlpay/util/MoneyUtil.java @@ -0,0 +1,28 @@ +package com.supwisdom.dlpay.util; + + +import java.text.DecimalFormat; + +public class MoneyUtil { + + public static int YuanToFen(double yuan) { + return (int) (Math.round(yuan * 100)); + } + + public static boolean moneyCompare(double x1, double x2) { + return YuanToFen(x1) == YuanToFen(x2); + } + public static double formatYuan(double yuan) { + DecimalFormat df = new DecimalFormat("##0.00"); + double money = ((double) YuanToFen(yuan)) / 100; + return Double.valueOf(df.format(money)); + } + + public static double FenToYuan(int fen) { + return formatYuan(fen / 100.0); + } + public static String format(double num,String format) { + DecimalFormat df = new DecimalFormat(format); + return df.format(num); + } +} diff --git a/src/main/java/com/supwisdom/dlpay/util/PaytypeUtil.java b/src/main/java/com/supwisdom/dlpay/util/PaytypeUtil.java new file mode 100644 index 00000000..8222e90a --- /dev/null +++ b/src/main/java/com/supwisdom/dlpay/util/PaytypeUtil.java @@ -0,0 +1,10 @@ +package com.supwisdom.dlpay.util; + +/** + * Created by shuwei on 2019/4/9. + */ +public class PaytypeUtil { + public static final String YKTPAY = "yktpay"; + + public static final String WECHAT = "wechat"; +} -- 2.17.1