Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
jinli gu
Jeepay
Commits
20a57324
Commit
20a57324
authored
Oct 24, 2017
by
jmdhappy
Browse files
提交xxpay4spring-boot版本
parent
c454edfd
Changes
39
Hide whitespace changes
Inline
Side-by-side
xxpay4spring-boot/src/main/java/org/xxpay/boot/service/Notify4BasePay.java
0 → 100644
View file @
20a57324
package
org.xxpay.boot.service
;
import
com.alibaba.fastjson.JSONObject
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
import
org.xxpay.common.constant.PayConstant
;
import
org.xxpay.common.util.MyLog
;
import
org.xxpay.common.util.PayDigestUtil
;
import
org.xxpay.common.util.XXPayUtil
;
import
org.xxpay.dal.dao.model.MchInfo
;
import
org.xxpay.dal.dao.model.PayOrder
;
import
org.xxpay.boot.service.mq.Mq4PayNotify
;
import
java.io.UnsupportedEncodingException
;
import
java.net.URLEncoder
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* @Description: 支付通知处理基类
* @author dingzhiwei jmdhappy@126.com
* @date 2017-07-05
* @version V1.0
* @Copyright: www.xxpay.org
*/
@Component
public
class
Notify4BasePay
extends
BaseService
{
private
static
final
MyLog
_log
=
MyLog
.
getLog
(
Notify4BasePay
.
class
);
@Autowired
private
Mq4PayNotify
mq4PayNotify
;
/**
* 创建响应URL
* @param payOrder
* @param backType 1:前台页面;2:后台接口
* @return
*/
public
String
createNotifyUrl
(
PayOrder
payOrder
,
String
backType
)
{
String
mchId
=
payOrder
.
getMchId
();
MchInfo
mchInfo
=
super
.
baseSelectMchInfo
(
mchId
);
String
resKey
=
mchInfo
.
getResKey
();
Map
<
String
,
Object
>
paramMap
=
new
HashMap
<>();
paramMap
.
put
(
"payOrderId"
,
payOrder
.
getPayOrderId
()
==
null
?
""
:
payOrder
.
getPayOrderId
());
// 支付订单号
paramMap
.
put
(
"mchId"
,
payOrder
.
getMchId
()
==
null
?
""
:
payOrder
.
getMchId
());
// 商户ID
paramMap
.
put
(
"mchOrderNo"
,
payOrder
.
getMchOrderNo
()
==
null
?
""
:
payOrder
.
getMchOrderNo
());
// 商户订单号
paramMap
.
put
(
"channelId"
,
payOrder
.
getChannelId
()
==
null
?
""
:
payOrder
.
getChannelId
());
// 渠道ID
paramMap
.
put
(
"amount"
,
payOrder
.
getAmount
()
==
null
?
""
:
payOrder
.
getAmount
());
// 支付金额
paramMap
.
put
(
"currency"
,
payOrder
.
getCurrency
()
==
null
?
""
:
payOrder
.
getCurrency
());
// 货币类型
paramMap
.
put
(
"status"
,
payOrder
.
getStatus
()
==
null
?
""
:
payOrder
.
getStatus
());
// 支付状态
paramMap
.
put
(
"clientIp"
,
payOrder
.
getClientIp
()==
null
?
""
:
payOrder
.
getClientIp
());
// 客户端IP
paramMap
.
put
(
"device"
,
payOrder
.
getDevice
()==
null
?
""
:
payOrder
.
getDevice
());
// 设备
paramMap
.
put
(
"subject"
,
payOrder
.
getSubject
()==
null
?
""
:
payOrder
.
getSubject
());
// 商品标题
paramMap
.
put
(
"channelOrderNo"
,
payOrder
.
getChannelOrderNo
()==
null
?
""
:
payOrder
.
getChannelOrderNo
());
// 渠道订单号
paramMap
.
put
(
"param1"
,
payOrder
.
getParam1
()==
null
?
""
:
payOrder
.
getParam1
());
// 扩展参数1
paramMap
.
put
(
"param2"
,
payOrder
.
getParam2
()==
null
?
""
:
payOrder
.
getParam2
());
// 扩展参数2
paramMap
.
put
(
"paySuccTime"
,
payOrder
.
getPaySuccTime
()==
null
?
""
:
payOrder
.
getPaySuccTime
());
// 支付成功时间
paramMap
.
put
(
"backType"
,
backType
==
null
?
""
:
backType
);
// 先对原文签名
String
reqSign
=
PayDigestUtil
.
getSign
(
paramMap
,
resKey
);
paramMap
.
put
(
"sign"
,
reqSign
);
// 签名
// 签名后再对有中文参数编码
try
{
paramMap
.
put
(
"device"
,
URLEncoder
.
encode
(
payOrder
.
getDevice
()==
null
?
""
:
payOrder
.
getDevice
(),
PayConstant
.
RESP_UTF8
));
paramMap
.
put
(
"subject"
,
URLEncoder
.
encode
(
payOrder
.
getSubject
()==
null
?
""
:
payOrder
.
getSubject
(),
PayConstant
.
RESP_UTF8
));
paramMap
.
put
(
"param1"
,
URLEncoder
.
encode
(
payOrder
.
getParam1
()==
null
?
""
:
payOrder
.
getParam1
(),
PayConstant
.
RESP_UTF8
));
paramMap
.
put
(
"param2"
,
URLEncoder
.
encode
(
payOrder
.
getParam2
()==
null
?
""
:
payOrder
.
getParam2
(),
PayConstant
.
RESP_UTF8
));
}
catch
(
UnsupportedEncodingException
e
)
{
_log
.
error
(
"URL Encode exception."
,
e
);
return
null
;
}
String
param
=
XXPayUtil
.
genUrlParams
(
paramMap
);
StringBuffer
sb
=
new
StringBuffer
();
sb
.
append
(
payOrder
.
getNotifyUrl
()).
append
(
"?"
).
append
(
param
);
return
sb
.
toString
();
}
/**
* 处理支付结果前台页面跳转
*/
public
boolean
doPage
(
PayOrder
payOrder
)
{
String
redirectUrl
=
createNotifyUrl
(
payOrder
,
"1"
);
_log
.
info
(
"redirect to respUrl:"
+
redirectUrl
);
// 前台跳转业务系统
/*try {
response.sendRedirect(redirectUrl);
} catch (IOException e) {
_log.error("XxPay sendRedirect exception. respUrl="+redirectUrl, e);
return false;
}*/
return
true
;
}
/**
* 处理支付结果后台服务器通知
*/
public
void
doNotify
(
PayOrder
payOrder
)
{
_log
.
info
(
">>>>>> PAY开始回调通知业务系统 <<<<<<"
);
// 发起后台通知业务系统
JSONObject
object
=
createNotifyInfo
(
payOrder
);
try
{
mq4PayNotify
.
send
(
object
.
toJSONString
());
}
catch
(
Exception
e
)
{
_log
.
error
(
"payOrderId={},sendMessage error."
,
payOrder
!=
null
?
payOrder
.
getPayOrderId
()
:
""
,
e
);
}
_log
.
info
(
">>>>>> PAY回调通知业务系统完成 <<<<<<"
);
}
public
JSONObject
createNotifyInfo
(
PayOrder
payOrder
)
{
JSONObject
object
=
new
JSONObject
();
object
.
put
(
"method"
,
"GET"
);
object
.
put
(
"url"
,
createNotifyUrl
(
payOrder
,
"2"
));
object
.
put
(
"orderId"
,
payOrder
.
getPayOrderId
());
object
.
put
(
"count"
,
payOrder
.
getNotifyCount
());
object
.
put
(
"createTime"
,
System
.
currentTimeMillis
());
return
object
;
}
}
xxpay4spring-boot/src/main/java/org/xxpay/boot/service/channel/alipay/AlipayConfig.java
0 → 100644
View file @
20a57324
package
org.xxpay.boot.service.channel.alipay
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONObject
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.stereotype.Component
;
import
org.springframework.util.Assert
;
/**
* @author: dingzhiwei
* @date: 17/8/21
* @description:
*/
@Component
@ConfigurationProperties
(
prefix
=
"config.ali"
)
public
class
AlipayConfig
{
// 商户appid
private
String
app_id
;
// 私钥 pkcs8格式的
private
String
rsa_private_key
;
// 服务器异步通知页面路径 需http://或者https://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
private
String
notify_url
;
// 页面跳转同步通知页面路径 需http://或者https://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问 商户可以自定义同步跳转地址
private
String
return_url
;
// 请求网关地址
private
String
url
=
"https://openapi.alipay.com/gateway.do"
;
// 编码
public
static
String
CHARSET
=
"UTF-8"
;
// 返回格式
public
static
String
FORMAT
=
"json"
;
// 支付宝公钥
public
String
alipay_public_key
;
// RSA2
public
static
String
SIGNTYPE
=
"RSA2"
;
// 是否沙箱环境,1:沙箱,0:正式环境
private
Short
isSandbox
=
0
;
/**
* 初始化支付宝配置
* @param configParam
* @return
*/
public
AlipayConfig
init
(
String
configParam
)
{
Assert
.
notNull
(
configParam
,
"init alipay config error"
);
JSONObject
paramObj
=
JSON
.
parseObject
(
configParam
);
this
.
setApp_id
(
paramObj
.
getString
(
"appid"
));
this
.
setRsa_private_key
(
paramObj
.
getString
(
"private_key"
));
this
.
setAlipay_public_key
(
paramObj
.
getString
(
"alipay_public_key"
));
this
.
setIsSandbox
(
paramObj
.
getShortValue
(
"isSandbox"
));
if
(
this
.
getIsSandbox
()
==
1
)
this
.
setUrl
(
"https://openapi.alipaydev.com/gateway.do"
);
return
this
;
}
public
String
getApp_id
()
{
return
app_id
;
}
public
void
setApp_id
(
String
app_id
)
{
this
.
app_id
=
app_id
;
}
public
String
getRsa_private_key
()
{
return
rsa_private_key
;
}
public
void
setRsa_private_key
(
String
rsa_private_key
)
{
this
.
rsa_private_key
=
rsa_private_key
;
}
public
String
getNotify_url
()
{
return
notify_url
;
}
public
void
setNotify_url
(
String
notify_url
)
{
this
.
notify_url
=
notify_url
;
}
public
String
getReturn_url
()
{
return
return_url
;
}
public
void
setReturn_url
(
String
return_url
)
{
this
.
return_url
=
return_url
;
}
public
String
getUrl
()
{
return
url
;
}
public
void
setUrl
(
String
url
)
{
this
.
url
=
url
;
}
public
Short
getIsSandbox
()
{
return
isSandbox
;
}
public
void
setIsSandbox
(
Short
isSandbox
)
{
this
.
isSandbox
=
isSandbox
;
}
public
String
getAlipay_public_key
()
{
return
alipay_public_key
;
}
public
void
setAlipay_public_key
(
String
alipay_public_key
)
{
this
.
alipay_public_key
=
alipay_public_key
;
}
}
xxpay4spring-boot/src/main/java/org/xxpay/boot/service/channel/wechat/WxPayProperties.java
0 → 100755
View file @
20a57324
package
org.xxpay.boot.service.channel.wechat
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.stereotype.Component
;
/**
* User: rizenguo
* Date: 2014/10/29
* Time: 14:40
* 这里放置各种配置数据
*/
@Component
@ConfigurationProperties
(
prefix
=
"config.wx"
)
public
class
WxPayProperties
{
private
String
certRootPath
;
private
String
notifyUrl
;
public
String
getCertRootPath
()
{
return
certRootPath
;
}
public
void
setCertRootPath
(
String
certRootPath
)
{
this
.
certRootPath
=
certRootPath
;
}
public
String
getNotifyUrl
()
{
return
notifyUrl
;
}
public
void
setNotifyUrl
(
String
notifyUrl
)
{
this
.
notifyUrl
=
notifyUrl
;
}
}
xxpay4spring-boot/src/main/java/org/xxpay/boot/service/channel/wechat/WxPayUtil.java
0 → 100644
View file @
20a57324
package
org.xxpay.boot.service.channel.wechat
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONObject
;
import
com.github.binarywang.wxpay.config.WxPayConfig
;
import
java.io.File
;
/**
* @author: dingzhiwei
* @date: 17/8/25
* @description:
*/
public
class
WxPayUtil
{
/**
* 获取微信支付配置
* @param configParam
* @param tradeType
* @param certRootPath
* @param notifyUrl
* @return
*/
public
static
WxPayConfig
getWxPayConfig
(
String
configParam
,
String
tradeType
,
String
certRootPath
,
String
notifyUrl
)
{
WxPayConfig
wxPayConfig
=
new
WxPayConfig
();
JSONObject
paramObj
=
JSON
.
parseObject
(
configParam
);
wxPayConfig
.
setMchId
(
paramObj
.
getString
(
"mchId"
));
wxPayConfig
.
setAppId
(
paramObj
.
getString
(
"appId"
));
wxPayConfig
.
setKeyPath
(
certRootPath
+
File
.
separator
+
paramObj
.
getString
(
"certLocalPath"
));
wxPayConfig
.
setMchKey
(
paramObj
.
getString
(
"key"
));
wxPayConfig
.
setNotifyUrl
(
notifyUrl
);
wxPayConfig
.
setTradeType
(
tradeType
);
return
wxPayConfig
;
}
/**
* 获取微信支付配置
* @param configParam
* @return
*/
public
static
WxPayConfig
getWxPayConfig
(
String
configParam
)
{
WxPayConfig
wxPayConfig
=
new
WxPayConfig
();
JSONObject
paramObj
=
JSON
.
parseObject
(
configParam
);
wxPayConfig
.
setMchId
(
paramObj
.
getString
(
"mchId"
));
wxPayConfig
.
setAppId
(
paramObj
.
getString
(
"appId"
));
wxPayConfig
.
setMchKey
(
paramObj
.
getString
(
"key"
));
return
wxPayConfig
;
}
}
xxpay4spring-boot/src/main/java/org/xxpay/boot/service/impl/MchInfoServiceImpl.java
0 → 100644
View file @
20a57324
package
org.xxpay.boot.service.impl
;
import
org.springframework.stereotype.Service
;
import
org.xxpay.boot.service.BaseService
;
import
org.xxpay.boot.service.IMchInfoService
;
import
org.xxpay.common.domain.BaseParam
;
import
org.xxpay.common.enumm.RetEnum
;
import
org.xxpay.common.util.JsonUtil
;
import
org.xxpay.common.util.MyLog
;
import
org.xxpay.common.util.ObjectValidUtil
;
import
org.xxpay.common.util.RpcUtil
;
import
org.xxpay.dal.dao.model.MchInfo
;
import
java.util.Map
;
/**
* @author: dingzhiwei
* @date: 17/9/8
* @description:
*/
@Service
public
class
MchInfoServiceImpl
extends
BaseService
implements
IMchInfoService
{
private
static
final
MyLog
_log
=
MyLog
.
getLog
(
MchInfoServiceImpl
.
class
);
@Override
public
Map
selectMchInfo
(
String
jsonParam
)
{
BaseParam
baseParam
=
JsonUtil
.
getObjectFromJson
(
jsonParam
,
BaseParam
.
class
);
Map
<
String
,
Object
>
bizParamMap
=
baseParam
.
getBizParamMap
();
if
(
ObjectValidUtil
.
isInvalid
(
bizParamMap
))
{
_log
.
warn
(
"查询商户信息失败, {}. jsonParam={}"
,
RetEnum
.
RET_PARAM_NOT_FOUND
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_NOT_FOUND
);
}
String
mchId
=
baseParam
.
isNullValue
(
"mchId"
)
?
null
:
bizParamMap
.
get
(
"mchId"
).
toString
();
if
(
ObjectValidUtil
.
isInvalid
(
mchId
))
{
_log
.
warn
(
"查询商户信息失败, {}. jsonParam={}"
,
RetEnum
.
RET_PARAM_INVALID
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_INVALID
);
}
MchInfo
mchInfo
=
super
.
baseSelectMchInfo
(
mchId
);
if
(
mchInfo
==
null
)
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_BIZ_DATA_NOT_EXISTS
);
String
jsonResult
=
JsonUtil
.
object2Json
(
mchInfo
);
return
RpcUtil
.
createBizResult
(
baseParam
,
jsonResult
);
}
}
xxpay4spring-boot/src/main/java/org/xxpay/boot/service/impl/NotifyPayServiceImpl.java
0 → 100644
View file @
20a57324
package
org.xxpay.boot.service.impl
;
import
com.alipay.api.AlipayApiException
;
import
com.alipay.api.internal.util.AlipaySignature
;
import
com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse
;
import
com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult
;
import
com.github.binarywang.wxpay.config.WxPayConfig
;
import
com.github.binarywang.wxpay.exception.WxPayException
;
import
com.github.binarywang.wxpay.service.WxPayService
;
import
com.github.binarywang.wxpay.service.impl.WxPayServiceImpl
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.util.StringUtils
;
import
org.xxpay.boot.service.INotifyPayService
;
import
org.xxpay.boot.service.Notify4BasePay
;
import
org.xxpay.boot.service.channel.alipay.AlipayConfig
;
import
org.xxpay.boot.service.channel.wechat.WxPayUtil
;
import
org.xxpay.common.constant.PayConstant
;
import
org.xxpay.common.domain.BaseParam
;
import
org.xxpay.common.enumm.RetEnum
;
import
org.xxpay.common.util.JsonUtil
;
import
org.xxpay.common.util.MyLog
;
import
org.xxpay.common.util.ObjectValidUtil
;
import
org.xxpay.common.util.RpcUtil
;
import
org.xxpay.dal.dao.model.PayChannel
;
import
org.xxpay.dal.dao.model.PayOrder
;
import
java.math.BigDecimal
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* @author: dingzhiwei
* @date: 17/9/10
* @description:
*/
@Service
public
class
NotifyPayServiceImpl
extends
Notify4BasePay
implements
INotifyPayService
{
private
static
final
MyLog
_log
=
MyLog
.
getLog
(
NotifyPayServiceImpl
.
class
);
@Autowired
private
AlipayConfig
alipayConfig
;
@Override
public
Map
doAliPayNotify
(
String
jsonParam
)
{
String
logPrefix
=
"【处理支付宝支付回调】"
;
_log
.
info
(
"====== 开始处理支付宝支付回调通知 ======"
);
BaseParam
baseParam
=
JsonUtil
.
getObjectFromJson
(
jsonParam
,
BaseParam
.
class
);
Map
<
String
,
Object
>
bizParamMap
=
baseParam
.
getBizParamMap
();
if
(
ObjectValidUtil
.
isInvalid
(
bizParamMap
))
{
_log
.
warn
(
"处理支付宝支付回调失败, {}. jsonParam={}"
,
RetEnum
.
RET_PARAM_NOT_FOUND
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_NOT_FOUND
);
}
Map
params
=
baseParam
.
isNullValue
(
"params"
)
?
null
:
(
Map
)
bizParamMap
.
get
(
"params"
);
if
(
ObjectValidUtil
.
isInvalid
(
params
))
{
_log
.
warn
(
"处理支付宝支付回调失败, {}. jsonParam={}"
,
RetEnum
.
RET_PARAM_INVALID
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_INVALID
);
}
Map
<
String
,
Object
>
payContext
=
new
HashMap
();
PayOrder
payOrder
;
payContext
.
put
(
"parameters"
,
params
);
if
(!
verifyAliPayParams
(
payContext
))
{
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_BIZ_PAY_NOTIFY_VERIFY_FAIL
);
}
_log
.
info
(
"{}验证支付通知数据及签名通过"
,
logPrefix
);
String
trade_status
=
params
.
get
(
"trade_status"
).
toString
();
// 交易状态
// 支付状态成功或者完成
if
(
trade_status
.
equals
(
PayConstant
.
AlipayConstant
.
TRADE_STATUS_SUCCESS
)
||
trade_status
.
equals
(
PayConstant
.
AlipayConstant
.
TRADE_STATUS_FINISHED
))
{
int
updatePayOrderRows
;
payOrder
=
(
PayOrder
)
payContext
.
get
(
"payOrder"
);
byte
payStatus
=
payOrder
.
getStatus
();
// 0:订单生成,1:支付中,-1:支付失败,2:支付成功,3:业务处理完成,-2:订单过期
if
(
payStatus
!=
PayConstant
.
PAY_STATUS_SUCCESS
&&
payStatus
!=
PayConstant
.
PAY_STATUS_COMPLETE
)
{
updatePayOrderRows
=
super
.
baseUpdateStatus4Success
(
payOrder
.
getPayOrderId
());
if
(
updatePayOrderRows
!=
1
)
{
_log
.
error
(
"{}更新支付状态失败,将payOrderId={},更新payStatus={}失败"
,
logPrefix
,
payOrder
.
getPayOrderId
(),
PayConstant
.
PAY_STATUS_SUCCESS
);
_log
.
info
(
"{}响应给支付宝结果:{}"
,
logPrefix
,
PayConstant
.
RETURN_ALIPAY_VALUE_FAIL
);
return
RpcUtil
.
createBizResult
(
baseParam
,
PayConstant
.
RETURN_ALIPAY_VALUE_FAIL
);
}
_log
.
info
(
"{}更新支付状态成功,将payOrderId={},更新payStatus={}成功"
,
logPrefix
,
payOrder
.
getPayOrderId
(),
PayConstant
.
PAY_STATUS_SUCCESS
);
payOrder
.
setStatus
(
PayConstant
.
PAY_STATUS_SUCCESS
);
}
}
else
{
// 其他状态
_log
.
info
(
"{}支付状态trade_status={},不做业务处理"
,
logPrefix
,
trade_status
);
_log
.
info
(
"{}响应给支付宝结果:{}"
,
logPrefix
,
PayConstant
.
RETURN_ALIPAY_VALUE_SUCCESS
);
return
RpcUtil
.
createBizResult
(
baseParam
,
PayConstant
.
RETURN_ALIPAY_VALUE_SUCCESS
);
}
doNotify
(
payOrder
);
_log
.
info
(
"====== 完成处理支付宝支付回调通知 ======"
);
return
RpcUtil
.
createBizResult
(
baseParam
,
PayConstant
.
RETURN_ALIPAY_VALUE_SUCCESS
);
}
@Override
public
Map
doWxPayNotify
(
String
jsonParam
)
{
String
logPrefix
=
"【处理微信支付回调】"
;
_log
.
info
(
"====== 开始处理微信支付回调通知 ======"
);
BaseParam
baseParam
=
JsonUtil
.
getObjectFromJson
(
jsonParam
,
BaseParam
.
class
);
try
{
Map
<
String
,
Object
>
bizParamMap
=
baseParam
.
getBizParamMap
();
if
(
ObjectValidUtil
.
isInvalid
(
bizParamMap
))
{
_log
.
warn
(
"处理微信支付回调失败, {}. jsonParam={}"
,
RetEnum
.
RET_PARAM_NOT_FOUND
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createBizResult
(
baseParam
,
WxPayNotifyResponse
.
fail
(
RetEnum
.
RET_PARAM_NOT_FOUND
.
getMessage
()));
//return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_NOT_FOUND);
}
String
xmlResult
=
baseParam
.
isNullValue
(
"xmlResult"
)
?
null
:
bizParamMap
.
get
(
"xmlResult"
).
toString
();
if
(
ObjectValidUtil
.
isInvalid
(
xmlResult
))
{
_log
.
warn
(
"处理微信支付回调失败, {}. jsonParam={}"
,
RetEnum
.
RET_PARAM_INVALID
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createBizResult
(
baseParam
,
WxPayNotifyResponse
.
fail
(
RetEnum
.
RET_PARAM_INVALID
.
getMessage
()));
//return RpcUtil.createFailResult(baseParam, RetEnum.RET_PARAM_INVALID);
}
WxPayService
wxPayService
=
new
WxPayServiceImpl
();
WxPayOrderNotifyResult
result
=
WxPayOrderNotifyResult
.
fromXML
(
xmlResult
);
Map
<
String
,
Object
>
payContext
=
new
HashMap
();
payContext
.
put
(
"parameters"
,
result
);
// 验证业务数据是否正确,验证通过后返回PayOrder和WxPayConfig对象
if
(!
verifyWxPayParams
(
payContext
))
{
return
RpcUtil
.
createBizResult
(
baseParam
,
WxPayNotifyResponse
.
fail
((
String
)
payContext
.
get
(
"retMsg"
)));
}
PayOrder
payOrder
=
(
PayOrder
)
payContext
.
get
(
"payOrder"
);
WxPayConfig
wxPayConfig
=
(
WxPayConfig
)
payContext
.
get
(
"wxPayConfig"
);
wxPayService
.
setConfig
(
wxPayConfig
);
// 这里做了签名校验(这里又做了一次xml转换对象,可以考虑优化)
wxPayService
.
parseOrderNotifyResult
(
xmlResult
);
// 处理订单
byte
payStatus
=
payOrder
.
getStatus
();
// 0:订单生成,1:支付中,-1:支付失败,2:支付成功,3:业务处理完成,-2:订单过期
if
(
payStatus
!=
PayConstant
.
PAY_STATUS_SUCCESS
&&
payStatus
!=
PayConstant
.
PAY_STATUS_COMPLETE
)
{
int
updatePayOrderRows
=
super
.
baseUpdateStatus4Success
(
payOrder
.
getPayOrderId
());
if
(
updatePayOrderRows
!=
1
)
{
_log
.
error
(
"{}更新支付状态失败,将payOrderId={},更新payStatus={}失败"
,
logPrefix
,
payOrder
.
getPayOrderId
(),
PayConstant
.
PAY_STATUS_SUCCESS
);
return
RpcUtil
.
createBizResult
(
baseParam
,
WxPayNotifyResponse
.
fail
(
"处理订单失败"
));
}
_log
.
error
(
"{}更新支付状态成功,将payOrderId={},更新payStatus={}成功"
,
logPrefix
,
payOrder
.
getPayOrderId
(),
PayConstant
.
PAY_STATUS_SUCCESS
);
payOrder
.
setStatus
(
PayConstant
.
PAY_STATUS_SUCCESS
);
}
// 业务系统后端通知
doNotify
(
payOrder
);
_log
.
info
(
"====== 完成处理微信支付回调通知 ======"
);
return
RpcUtil
.
createBizResult
(
baseParam
,
WxPayNotifyResponse
.
success
(
"OK"
));
}
catch
(
WxPayException
e
)
{
//出现业务错误
_log
.
error
(
e
,
"微信回调结果异常,异常原因"
);
_log
.
info
(
"{}请求数据result_code=FAIL"
,
logPrefix
);
_log
.
info
(
"err_code:"
,
e
.
getErrCode
());
_log
.
info
(
"err_code_des:"
,
e
.
getErrCodeDes
());
return
RpcUtil
.
createBizResult
(
baseParam
,
WxPayNotifyResponse
.
fail
(
e
.
getMessage
()));
}
catch
(
Exception
e
)
{
_log
.
error
(
e
,
"微信回调结果异常,异常原因"
);
return
RpcUtil
.
createBizResult
(
baseParam
,
WxPayNotifyResponse
.
fail
(
e
.
getMessage
()));
}
}
@Override
public
Map
sendBizPayNotify
(
String
jsonParam
)
{
BaseParam
baseParam
=
JsonUtil
.
getObjectFromJson
(
jsonParam
,
BaseParam
.
class
);
Map
<
String
,
Object
>
bizParamMap
=
baseParam
.
getBizParamMap
();
if
(
ObjectValidUtil
.
isInvalid
(
bizParamMap
))
{
_log
.
warn
(
"发送业务支付通知失败, {}. jsonParam={}"
,
RetEnum
.
RET_PARAM_NOT_FOUND
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_NOT_FOUND
);
}
String
payOrderId
=
baseParam
.
isNullValue
(
"payOrderId"
)
?
null
:
bizParamMap
.
get
(
"payOrderId"
).
toString
();
if
(
ObjectValidUtil
.
isInvalid
(
payOrderId
))
{
_log
.
warn
(
"发送业务支付通知失败, {}. jsonParam={}"
,
RetEnum
.
RET_PARAM_INVALID
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_INVALID
);
}
PayOrder
payOrder
=
super
.
baseSelectPayOrder
(
payOrderId
);
if
(
payOrder
==
null
)
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_BIZ_DATA_NOT_EXISTS
);
try
{
// 发送业务支付通知
super
.
doNotify
(
payOrder
);
}
catch
(
Exception
e
)
{
return
RpcUtil
.
createBizResult
(
baseParam
,
0
);
}
return
RpcUtil
.
createBizResult
(
baseParam
,
1
);
}
/**
* 验证支付宝支付通知参数
* @return
*/
public
boolean
verifyAliPayParams
(
Map
<
String
,
Object
>
payContext
)
{
Map
<
String
,
String
>
params
=
(
Map
<
String
,
String
>)
payContext
.
get
(
"parameters"
);
String
out_trade_no
=
params
.
get
(
"out_trade_no"
);
// 商户订单号
String
total_amount
=
params
.
get
(
"total_amount"
);
// 支付金额
if
(
StringUtils
.
isEmpty
(
out_trade_no
))
{
_log
.
error
(
"AliPay Notify parameter out_trade_no is empty. out_trade_no={}"
,
out_trade_no
);
payContext
.
put
(
"retMsg"
,
"out_trade_no is empty"
);
return
false
;
}
if
(
StringUtils
.
isEmpty
(
total_amount
))
{
_log
.
error
(
"AliPay Notify parameter total_amount is empty. total_fee={}"
,
total_amount
);
payContext
.
put
(
"retMsg"
,
"total_amount is empty"
);
return
false
;
}
String
errorMessage
;
// 查询payOrder记录
String
payOrderId
=
out_trade_no
;
PayOrder
payOrder
=
super
.
baseSelectPayOrder
(
payOrderId
);
if
(
payOrder
==
null
)
{
_log
.
error
(
"Can't found payOrder form db. payOrderId={}, "
,
payOrderId
);
payContext
.
put
(
"retMsg"
,
"Can't found payOrder"
);
return
false
;
}
// 查询payChannel记录
String
mchId
=
payOrder
.
getMchId
();
String
channelId
=
payOrder
.
getChannelId
();
PayChannel
payChannel
=
super
.
baseSelectPayChannel
(
mchId
,
channelId
);
if
(
payChannel
==
null
)
{
_log
.
error
(
"Can't found payChannel form db. mchId={} channelId={}, "
,
payOrderId
,
mchId
,
channelId
);
payContext
.
put
(
"retMsg"
,
"Can't found payChannel"
);
return
false
;
}
boolean
verify_result
=
false
;
try
{
verify_result
=
AlipaySignature
.
rsaCheckV1
(
params
,
alipayConfig
.
init
(
payChannel
.
getParam
()).
getAlipay_public_key
(),
AlipayConfig
.
CHARSET
,
"RSA2"
);
}
catch
(
AlipayApiException
e
)
{
_log
.
error
(
e
,
"AlipaySignature.rsaCheckV1 error"
);
}
// 验证签名
if
(!
verify_result
)
{
errorMessage
=
"rsaCheckV1 failed."
;
_log
.
error
(
"AliPay Notify parameter {}"
,
errorMessage
);
payContext
.
put
(
"retMsg"
,
errorMessage
);
return
false
;
}
// 核对金额
long
aliPayAmt
=
new
BigDecimal
(
total_amount
).
movePointRight
(
2
).
longValue
();
long
dbPayAmt
=
payOrder
.
getAmount
().
longValue
();
if
(
dbPayAmt
!=
aliPayAmt
)
{
_log
.
error
(
"db payOrder record payPrice not equals total_amount. total_amount={},payOrderId={}"
,
total_amount
,
payOrderId
);
payContext
.
put
(
"retMsg"
,
""
);
return
false
;
}
payContext
.
put
(
"payOrder"
,
payOrder
);
return
true
;
}
/**
* 验证微信支付通知参数
* @return
*/
public
boolean
verifyWxPayParams
(
Map
<
String
,
Object
>
payContext
)
{
WxPayOrderNotifyResult
params
=
(
WxPayOrderNotifyResult
)
payContext
.
get
(
"parameters"
);
//校验结果是否成功
if
(!
PayConstant
.
RETURN_VALUE_SUCCESS
.
equalsIgnoreCase
(
params
.
getResultCode
())
||
!
PayConstant
.
RETURN_VALUE_SUCCESS
.
equalsIgnoreCase
(
params
.
getResultCode
()))
{
_log
.
error
(
"returnCode={},resultCode={},errCode={},errCodeDes={}"
,
params
.
getReturnCode
(),
params
.
getResultCode
(),
params
.
getErrCode
(),
params
.
getErrCodeDes
());
payContext
.
put
(
"retMsg"
,
"notify data failed"
);
return
false
;
}
Integer
total_fee
=
params
.
getTotalFee
();
// 总金额
String
out_trade_no
=
params
.
getOutTradeNo
();
// 商户系统订单号
// 查询payOrder记录
String
payOrderId
=
out_trade_no
;
PayOrder
payOrder
=
super
.
baseSelectPayOrder
(
payOrderId
);
if
(
payOrder
==
null
)
{
_log
.
error
(
"Can't found payOrder form db. payOrderId={}, "
,
payOrderId
);
payContext
.
put
(
"retMsg"
,
"Can't found payOrder"
);
return
false
;
}
// 查询payChannel记录
String
mchId
=
payOrder
.
getMchId
();
String
channelId
=
payOrder
.
getChannelId
();
PayChannel
payChannel
=
super
.
baseSelectPayChannel
(
mchId
,
channelId
);
if
(
payChannel
==
null
)
{
_log
.
error
(
"Can't found payChannel form db. mchId={} channelId={}, "
,
payOrderId
,
mchId
,
channelId
);
payContext
.
put
(
"retMsg"
,
"Can't found payChannel"
);
return
false
;
}
payContext
.
put
(
"wxPayConfig"
,
WxPayUtil
.
getWxPayConfig
(
payChannel
.
getParam
()));
// 核对金额
long
wxPayAmt
=
new
BigDecimal
(
total_fee
).
longValue
();
long
dbPayAmt
=
payOrder
.
getAmount
().
longValue
();
if
(
dbPayAmt
!=
wxPayAmt
)
{
_log
.
error
(
"db payOrder record payPrice not equals total_fee. total_fee={},payOrderId={}"
,
total_fee
,
payOrderId
);
payContext
.
put
(
"retMsg"
,
"total_fee is not the same"
);
return
false
;
}
payContext
.
put
(
"payOrder"
,
payOrder
);
return
true
;
}
}
xxpay4spring-boot/src/main/java/org/xxpay/boot/service/impl/PayChannel4AliServiceImpl.java
0 → 100644
View file @
20a57324
package
org.xxpay.boot.service.impl
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONObject
;
import
com.alipay.api.AlipayApiException
;
import
com.alipay.api.AlipayClient
;
import
com.alipay.api.DefaultAlipayClient
;
import
com.alipay.api.domain.AlipayTradeAppPayModel
;
import
com.alipay.api.domain.AlipayTradePagePayModel
;
import
com.alipay.api.domain.AlipayTradePrecreateModel
;
import
com.alipay.api.domain.AlipayTradeWapPayModel
;
import
com.alipay.api.request.AlipayTradeAppPayRequest
;
import
com.alipay.api.request.AlipayTradePagePayRequest
;
import
com.alipay.api.request.AlipayTradePrecreateRequest
;
import
com.alipay.api.request.AlipayTradeWapPayRequest
;
import
org.apache.commons.lang3.ObjectUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.xxpay.boot.service.BaseService
;
import
org.xxpay.boot.service.IPayChannel4AliService
;
import
org.xxpay.boot.service.channel.alipay.AlipayConfig
;
import
org.xxpay.common.constant.PayConstant
;
import
org.xxpay.common.domain.BaseParam
;
import
org.xxpay.common.enumm.RetEnum
;
import
org.xxpay.common.util.*
;
import
org.xxpay.dal.dao.model.PayChannel
;
import
org.xxpay.dal.dao.model.PayOrder
;
import
java.util.Map
;
/**
* @author: dingzhiwei
* @date: 17/9/10
* @description:
*/
@Service
public
class
PayChannel4AliServiceImpl
extends
BaseService
implements
IPayChannel4AliService
{
private
static
final
MyLog
_log
=
MyLog
.
getLog
(
PayChannel4AliServiceImpl
.
class
);
@Autowired
private
AlipayConfig
alipayConfig
;
@Override
public
Map
doAliPayWapReq
(
String
jsonParam
)
{
String
logPrefix
=
"【支付宝WAP支付下单】"
;
BaseParam
baseParam
=
JsonUtil
.
getObjectFromJson
(
jsonParam
,
BaseParam
.
class
);
Map
<
String
,
Object
>
bizParamMap
=
baseParam
.
getBizParamMap
();
if
(
ObjectValidUtil
.
isInvalid
(
bizParamMap
))
{
_log
.
warn
(
"{}失败, {}. jsonParam={}"
,
logPrefix
,
RetEnum
.
RET_PARAM_NOT_FOUND
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_NOT_FOUND
);
}
JSONObject
payOrderObj
=
baseParam
.
isNullValue
(
"payOrder"
)
?
null
:
JSONObject
.
parseObject
(
bizParamMap
.
get
(
"payOrder"
).
toString
());
PayOrder
payOrder
=
BeanConvertUtils
.
map2Bean
(
payOrderObj
,
PayOrder
.
class
);
if
(
ObjectValidUtil
.
isInvalid
(
payOrder
))
{
_log
.
warn
(
"{}失败, {}. jsonParam={}"
,
logPrefix
,
RetEnum
.
RET_PARAM_INVALID
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_INVALID
);
}
String
payOrderId
=
payOrder
.
getPayOrderId
();
String
mchId
=
payOrder
.
getMchId
();
String
channelId
=
payOrder
.
getChannelId
();
PayChannel
payChannel
=
super
.
baseSelectPayChannel
(
mchId
,
channelId
);
alipayConfig
.
init
(
payChannel
.
getParam
());
AlipayClient
client
=
new
DefaultAlipayClient
(
alipayConfig
.
getUrl
(),
alipayConfig
.
getApp_id
(),
alipayConfig
.
getRsa_private_key
(),
AlipayConfig
.
FORMAT
,
AlipayConfig
.
CHARSET
,
alipayConfig
.
getAlipay_public_key
(),
AlipayConfig
.
SIGNTYPE
);
AlipayTradeWapPayRequest
alipay_request
=
new
AlipayTradeWapPayRequest
();
// 封装请求支付信息
AlipayTradeWapPayModel
model
=
new
AlipayTradeWapPayModel
();
model
.
setOutTradeNo
(
payOrderId
);
model
.
setSubject
(
payOrder
.
getSubject
());
model
.
setTotalAmount
(
AmountUtil
.
convertCent2Dollar
(
payOrder
.
getAmount
().
toString
()));
model
.
setBody
(
payOrder
.
getBody
());
model
.
setProductCode
(
"QUICK_WAP_PAY"
);
// 获取objParams参数
String
objParams
=
payOrder
.
getExtra
();
if
(
StringUtils
.
isNotEmpty
(
objParams
))
{
try
{
JSONObject
objParamsJson
=
JSON
.
parseObject
(
objParams
);
if
(
StringUtils
.
isNotBlank
(
objParamsJson
.
getString
(
"quit_url"
)))
{
model
.
setQuitUrl
(
objParamsJson
.
getString
(
"quit_url"
));
}
}
catch
(
Exception
e
)
{
_log
.
error
(
"{}objParams参数格式错误!"
,
logPrefix
);
}
}
alipay_request
.
setBizModel
(
model
);
// 设置异步通知地址
alipay_request
.
setNotifyUrl
(
alipayConfig
.
getNotify_url
());
// 设置同步地址
alipay_request
.
setReturnUrl
(
alipayConfig
.
getReturn_url
());
String
payUrl
=
null
;
try
{
payUrl
=
client
.
pageExecute
(
alipay_request
).
getBody
();
}
catch
(
AlipayApiException
e
)
{
e
.
printStackTrace
();
}
_log
.
info
(
"{}生成跳转路径:payUrl={}"
,
logPrefix
,
payUrl
);
super
.
baseUpdateStatus4Ing
(
payOrderId
,
null
);
_log
.
info
(
"{}生成请求支付宝数据,req={}"
,
logPrefix
,
alipay_request
.
getBizModel
());
_log
.
info
(
"###### 商户统一下单处理完成 ######"
);
Map
<
String
,
Object
>
map
=
XXPayUtil
.
makeRetMap
(
PayConstant
.
RETURN_VALUE_SUCCESS
,
""
,
PayConstant
.
RETURN_VALUE_SUCCESS
,
null
);
map
.
put
(
"payOrderId"
,
payOrderId
);
map
.
put
(
"payUrl"
,
payUrl
);
return
RpcUtil
.
createBizResult
(
baseParam
,
map
);
}
@Override
public
Map
doAliPayPcReq
(
String
jsonParam
)
{
String
logPrefix
=
"【支付宝PC支付下单】"
;
BaseParam
baseParam
=
JsonUtil
.
getObjectFromJson
(
jsonParam
,
BaseParam
.
class
);
Map
<
String
,
Object
>
bizParamMap
=
baseParam
.
getBizParamMap
();
if
(
ObjectValidUtil
.
isInvalid
(
bizParamMap
))
{
_log
.
warn
(
"{}失败, {}. jsonParam={}"
,
logPrefix
,
RetEnum
.
RET_PARAM_NOT_FOUND
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_NOT_FOUND
);
}
JSONObject
payOrderObj
=
baseParam
.
isNullValue
(
"payOrder"
)
?
null
:
JSONObject
.
parseObject
(
bizParamMap
.
get
(
"payOrder"
).
toString
());
PayOrder
payOrder
=
BeanConvertUtils
.
map2Bean
(
payOrderObj
,
PayOrder
.
class
);
if
(
ObjectValidUtil
.
isInvalid
(
payOrder
))
{
_log
.
warn
(
"{}失败, {}. jsonParam={}"
,
logPrefix
,
RetEnum
.
RET_PARAM_INVALID
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_INVALID
);
}
String
payOrderId
=
payOrder
.
getPayOrderId
();
String
mchId
=
payOrder
.
getMchId
();
String
channelId
=
payOrder
.
getChannelId
();
PayChannel
payChannel
=
super
.
baseSelectPayChannel
(
mchId
,
channelId
);
alipayConfig
.
init
(
payChannel
.
getParam
());
AlipayClient
client
=
new
DefaultAlipayClient
(
alipayConfig
.
getUrl
(),
alipayConfig
.
getApp_id
(),
alipayConfig
.
getRsa_private_key
(),
AlipayConfig
.
FORMAT
,
AlipayConfig
.
CHARSET
,
alipayConfig
.
getAlipay_public_key
(),
AlipayConfig
.
SIGNTYPE
);
AlipayTradePagePayRequest
alipay_request
=
new
AlipayTradePagePayRequest
();
// 封装请求支付信息
AlipayTradePagePayModel
model
=
new
AlipayTradePagePayModel
();
model
.
setOutTradeNo
(
payOrderId
);
model
.
setSubject
(
payOrder
.
getSubject
());
model
.
setTotalAmount
(
AmountUtil
.
convertCent2Dollar
(
payOrder
.
getAmount
().
toString
()));
model
.
setBody
(
payOrder
.
getBody
());
model
.
setProductCode
(
"FAST_INSTANT_TRADE_PAY"
);
// 获取objParams参数
String
objParams
=
payOrder
.
getExtra
();
String
qr_pay_mode
=
"2"
;
String
qrcode_width
=
"200"
;
if
(
StringUtils
.
isNotEmpty
(
objParams
))
{
try
{
JSONObject
objParamsJson
=
JSON
.
parseObject
(
objParams
);
qr_pay_mode
=
ObjectUtils
.
toString
(
objParamsJson
.
getString
(
"qr_pay_mode"
),
"2"
);
qrcode_width
=
ObjectUtils
.
toString
(
objParamsJson
.
getString
(
"qrcode_width"
),
"200"
);
}
catch
(
Exception
e
)
{
_log
.
error
(
"{}objParams参数格式错误!"
,
logPrefix
);
}
}
model
.
setQrPayMode
(
qr_pay_mode
);
model
.
setQrcodeWidth
(
Long
.
parseLong
(
qrcode_width
));
alipay_request
.
setBizModel
(
model
);
// 设置异步通知地址
alipay_request
.
setNotifyUrl
(
alipayConfig
.
getNotify_url
());
// 设置同步地址
alipay_request
.
setReturnUrl
(
alipayConfig
.
getReturn_url
());
String
payUrl
=
null
;
try
{
payUrl
=
client
.
pageExecute
(
alipay_request
).
getBody
();
}
catch
(
AlipayApiException
e
)
{
e
.
printStackTrace
();
}
_log
.
info
(
"{}生成跳转路径:payUrl={}"
,
logPrefix
,
payUrl
);
super
.
baseUpdateStatus4Ing
(
payOrderId
,
null
);
_log
.
info
(
"{}生成请求支付宝数据,req={}"
,
logPrefix
,
alipay_request
.
getBizModel
());
_log
.
info
(
"###### 商户统一下单处理完成 ######"
);
Map
<
String
,
Object
>
map
=
XXPayUtil
.
makeRetMap
(
PayConstant
.
RETURN_VALUE_SUCCESS
,
""
,
PayConstant
.
RETURN_VALUE_SUCCESS
,
null
);
map
.
put
(
"payOrderId"
,
payOrderId
);
map
.
put
(
"payUrl"
,
payUrl
);
return
RpcUtil
.
createBizResult
(
baseParam
,
map
);
}
@Override
public
Map
doAliPayMobileReq
(
String
jsonParam
)
{
String
logPrefix
=
"【支付宝APP支付下单】"
;
BaseParam
baseParam
=
JsonUtil
.
getObjectFromJson
(
jsonParam
,
BaseParam
.
class
);
Map
<
String
,
Object
>
bizParamMap
=
baseParam
.
getBizParamMap
();
if
(
ObjectValidUtil
.
isInvalid
(
bizParamMap
))
{
_log
.
warn
(
"{}失败, {}. jsonParam={}"
,
logPrefix
,
RetEnum
.
RET_PARAM_NOT_FOUND
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_NOT_FOUND
);
}
JSONObject
payOrderObj
=
baseParam
.
isNullValue
(
"payOrder"
)
?
null
:
JSONObject
.
parseObject
(
bizParamMap
.
get
(
"payOrder"
).
toString
());
PayOrder
payOrder
=
BeanConvertUtils
.
map2Bean
(
payOrderObj
,
PayOrder
.
class
);
if
(
ObjectValidUtil
.
isInvalid
(
payOrder
))
{
_log
.
warn
(
"{}失败, {}. jsonParam={}"
,
logPrefix
,
RetEnum
.
RET_PARAM_INVALID
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_INVALID
);
}
String
payOrderId
=
payOrder
.
getPayOrderId
();
String
mchId
=
payOrder
.
getMchId
();
String
channelId
=
payOrder
.
getChannelId
();
PayChannel
payChannel
=
super
.
baseSelectPayChannel
(
mchId
,
channelId
);
alipayConfig
.
init
(
payChannel
.
getParam
());
AlipayClient
client
=
new
DefaultAlipayClient
(
alipayConfig
.
getUrl
(),
alipayConfig
.
getApp_id
(),
alipayConfig
.
getRsa_private_key
(),
AlipayConfig
.
FORMAT
,
AlipayConfig
.
CHARSET
,
alipayConfig
.
getAlipay_public_key
(),
AlipayConfig
.
SIGNTYPE
);
AlipayTradeAppPayRequest
alipay_request
=
new
AlipayTradeAppPayRequest
();
// 封装请求支付信息
AlipayTradeAppPayModel
model
=
new
AlipayTradeAppPayModel
();
model
.
setOutTradeNo
(
payOrderId
);
model
.
setSubject
(
payOrder
.
getSubject
());
model
.
setTotalAmount
(
AmountUtil
.
convertCent2Dollar
(
payOrder
.
getAmount
().
toString
()));
model
.
setBody
(
payOrder
.
getBody
());
model
.
setProductCode
(
"QUICK_MSECURITY_PAY"
);
alipay_request
.
setBizModel
(
model
);
// 设置异步通知地址
alipay_request
.
setNotifyUrl
(
alipayConfig
.
getNotify_url
());
// 设置同步地址
alipay_request
.
setReturnUrl
(
alipayConfig
.
getReturn_url
());
String
payParams
=
null
;
try
{
payParams
=
client
.
sdkExecute
(
alipay_request
).
getBody
();
}
catch
(
AlipayApiException
e
)
{
e
.
printStackTrace
();
}
super
.
baseUpdateStatus4Ing
(
payOrderId
,
null
);
_log
.
info
(
"{}生成请求支付宝数据,payParams={}"
,
logPrefix
,
payParams
);
_log
.
info
(
"###### 商户统一下单处理完成 ######"
);
Map
<
String
,
Object
>
map
=
XXPayUtil
.
makeRetMap
(
PayConstant
.
RETURN_VALUE_SUCCESS
,
""
,
PayConstant
.
RETURN_VALUE_SUCCESS
,
null
);
map
.
put
(
"payOrderId"
,
payOrderId
);
map
.
put
(
"payParams"
,
payParams
);
return
RpcUtil
.
createBizResult
(
baseParam
,
map
);
}
@Override
public
Map
doAliPayQrReq
(
String
jsonParam
)
{
String
logPrefix
=
"【支付宝当面付之扫码支付下单】"
;
BaseParam
baseParam
=
JsonUtil
.
getObjectFromJson
(
jsonParam
,
BaseParam
.
class
);
Map
<
String
,
Object
>
bizParamMap
=
baseParam
.
getBizParamMap
();
if
(
ObjectValidUtil
.
isInvalid
(
bizParamMap
))
{
_log
.
warn
(
"{}失败, {}. jsonParam={}"
,
logPrefix
,
RetEnum
.
RET_PARAM_NOT_FOUND
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_NOT_FOUND
);
}
JSONObject
payOrderObj
=
baseParam
.
isNullValue
(
"payOrder"
)
?
null
:
JSONObject
.
parseObject
(
bizParamMap
.
get
(
"payOrder"
).
toString
());
PayOrder
payOrder
=
BeanConvertUtils
.
map2Bean
(
payOrderObj
,
PayOrder
.
class
);
if
(
ObjectValidUtil
.
isInvalid
(
payOrder
))
{
_log
.
warn
(
"{}失败, {}. jsonParam={}"
,
logPrefix
,
RetEnum
.
RET_PARAM_INVALID
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_INVALID
);
}
String
payOrderId
=
payOrder
.
getPayOrderId
();
String
mchId
=
payOrder
.
getMchId
();
String
channelId
=
payOrder
.
getChannelId
();
PayChannel
payChannel
=
super
.
baseSelectPayChannel
(
mchId
,
channelId
);
alipayConfig
.
init
(
payChannel
.
getParam
());
AlipayClient
client
=
new
DefaultAlipayClient
(
alipayConfig
.
getUrl
(),
alipayConfig
.
getApp_id
(),
alipayConfig
.
getRsa_private_key
(),
AlipayConfig
.
FORMAT
,
AlipayConfig
.
CHARSET
,
alipayConfig
.
getAlipay_public_key
(),
AlipayConfig
.
SIGNTYPE
);
AlipayTradePrecreateRequest
alipay_request
=
new
AlipayTradePrecreateRequest
();
// 封装请求支付信息
AlipayTradePrecreateModel
model
=
new
AlipayTradePrecreateModel
();
model
.
setOutTradeNo
(
payOrderId
);
model
.
setSubject
(
payOrder
.
getSubject
());
model
.
setTotalAmount
(
AmountUtil
.
convertCent2Dollar
(
payOrder
.
getAmount
().
toString
()));
model
.
setBody
(
payOrder
.
getBody
());
// 获取objParams参数
String
objParams
=
payOrder
.
getExtra
();
if
(
StringUtils
.
isNotEmpty
(
objParams
))
{
try
{
JSONObject
objParamsJson
=
JSON
.
parseObject
(
objParams
);
if
(
StringUtils
.
isNotBlank
(
objParamsJson
.
getString
(
"discountable_amount"
)))
{
//可打折金额
model
.
setDiscountableAmount
(
objParamsJson
.
getString
(
"discountable_amount"
));
}
if
(
StringUtils
.
isNotBlank
(
objParamsJson
.
getString
(
"undiscountable_amount"
)))
{
//不可打折金额
model
.
setUndiscountableAmount
(
objParamsJson
.
getString
(
"undiscountable_amount"
));
}
}
catch
(
Exception
e
)
{
_log
.
error
(
"{}objParams参数格式错误!"
,
logPrefix
);
}
}
alipay_request
.
setBizModel
(
model
);
// 设置异步通知地址
alipay_request
.
setNotifyUrl
(
alipayConfig
.
getNotify_url
());
// 设置同步地址
alipay_request
.
setReturnUrl
(
alipayConfig
.
getReturn_url
());
String
payUrl
=
null
;
try
{
payUrl
=
client
.
execute
(
alipay_request
).
getBody
();
}
catch
(
AlipayApiException
e
)
{
e
.
printStackTrace
();
}
_log
.
info
(
"{}生成跳转路径:payUrl={}"
,
logPrefix
,
payUrl
);
super
.
baseUpdateStatus4Ing
(
payOrderId
,
null
);
_log
.
info
(
"{}生成请求支付宝数据,req={}"
,
logPrefix
,
alipay_request
.
getBizModel
());
_log
.
info
(
"###### 商户统一下单处理完成 ######"
);
Map
<
String
,
Object
>
map
=
XXPayUtil
.
makeRetMap
(
PayConstant
.
RETURN_VALUE_SUCCESS
,
""
,
PayConstant
.
RETURN_VALUE_SUCCESS
,
null
);
map
.
put
(
"payOrderId"
,
payOrderId
);
map
.
put
(
"payUrl"
,
payUrl
);
return
RpcUtil
.
createBizResult
(
baseParam
,
map
);
}
}
xxpay4spring-boot/src/main/java/org/xxpay/boot/service/impl/PayChannel4WxServiceImpl.java
0 → 100644
View file @
20a57324
package
org.xxpay.boot.service.impl
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONObject
;
import
com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest
;
import
com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult
;
import
com.github.binarywang.wxpay.config.WxPayConfig
;
import
com.github.binarywang.wxpay.constant.WxPayConstants
;
import
com.github.binarywang.wxpay.exception.WxPayException
;
import
com.github.binarywang.wxpay.service.WxPayService
;
import
com.github.binarywang.wxpay.service.impl.WxPayServiceImpl
;
import
com.github.binarywang.wxpay.util.SignUtils
;
import
org.springframework.stereotype.Service
;
import
org.xxpay.boot.service.BaseService
;
import
org.xxpay.boot.service.IPayChannel4WxService
;
import
org.xxpay.boot.service.channel.wechat.WxPayProperties
;
import
org.xxpay.boot.service.channel.wechat.WxPayUtil
;
import
org.xxpay.common.constant.PayConstant
;
import
org.xxpay.common.domain.BaseParam
;
import
org.xxpay.common.enumm.RetEnum
;
import
org.xxpay.common.util.*
;
import
org.xxpay.dal.dao.model.PayChannel
;
import
org.xxpay.dal.dao.model.PayOrder
;
import
javax.annotation.Resource
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* @Description: 支付渠道接口:微信
* @author dingzhiwei jmdhappy@126.com
* @date 2017-09-05
* @version V1.0
* @Copyright: www.xxpay.org
*/
@Service
public
class
PayChannel4WxServiceImpl
extends
BaseService
implements
IPayChannel4WxService
{
private
final
MyLog
_log
=
MyLog
.
getLog
(
PayChannel4WxServiceImpl
.
class
);
@Resource
private
WxPayProperties
wxPayProperties
;
public
Map
doWxPayReq
(
String
jsonParam
)
{
String
logPrefix
=
"【微信支付统一下单】"
;
BaseParam
baseParam
=
JsonUtil
.
getObjectFromJson
(
jsonParam
,
BaseParam
.
class
);
Map
<
String
,
Object
>
bizParamMap
=
baseParam
.
getBizParamMap
();
try
{
if
(
ObjectValidUtil
.
isInvalid
(
bizParamMap
))
{
_log
.
warn
(
"{}失败, {}. jsonParam={}"
,
logPrefix
,
RetEnum
.
RET_PARAM_NOT_FOUND
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_NOT_FOUND
);
}
JSONObject
payOrderObj
=
baseParam
.
isNullValue
(
"payOrder"
)
?
null
:
JSONObject
.
parseObject
(
bizParamMap
.
get
(
"payOrder"
).
toString
());
String
tradeType
=
baseParam
.
isNullValue
(
"tradeType"
)
?
null
:
bizParamMap
.
get
(
"tradeType"
).
toString
();
PayOrder
payOrder
=
BeanConvertUtils
.
map2Bean
(
payOrderObj
,
PayOrder
.
class
);
if
(
ObjectValidUtil
.
isInvalid
(
payOrder
,
tradeType
))
{
_log
.
warn
(
"{}失败, {}. jsonParam={}"
,
logPrefix
,
RetEnum
.
RET_PARAM_INVALID
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_INVALID
);
}
String
mchId
=
payOrder
.
getMchId
();
String
channelId
=
payOrder
.
getChannelId
();
PayChannel
payChannel
=
super
.
baseSelectPayChannel
(
mchId
,
channelId
);
WxPayConfig
wxPayConfig
=
WxPayUtil
.
getWxPayConfig
(
payChannel
.
getParam
(),
tradeType
,
wxPayProperties
.
getCertRootPath
(),
wxPayProperties
.
getNotifyUrl
());
WxPayService
wxPayService
=
new
WxPayServiceImpl
();
wxPayService
.
setConfig
(
wxPayConfig
);
WxPayUnifiedOrderRequest
wxPayUnifiedOrderRequest
=
buildUnifiedOrderRequest
(
payOrder
,
wxPayConfig
);
String
payOrderId
=
payOrder
.
getPayOrderId
();
WxPayUnifiedOrderResult
wxPayUnifiedOrderResult
;
try
{
wxPayUnifiedOrderResult
=
wxPayService
.
unifiedOrder
(
wxPayUnifiedOrderRequest
);
_log
.
info
(
"{} >>> 下单成功"
,
logPrefix
);
Map
<
String
,
Object
>
map
=
new
HashMap
<>();
map
.
put
(
"payOrderId"
,
payOrderId
);
map
.
put
(
"prepayId"
,
wxPayUnifiedOrderResult
.
getPrepayId
());
int
result
=
super
.
baseUpdateStatus4Ing
(
payOrderId
,
wxPayUnifiedOrderResult
.
getPrepayId
());
_log
.
info
(
"更新第三方支付订单号:payOrderId={},prepayId={},result={}"
,
payOrderId
,
wxPayUnifiedOrderResult
.
getPrepayId
(),
result
);
switch
(
tradeType
)
{
case
PayConstant
.
WxConstant
.
TRADE_TYPE_NATIVE
:
{
map
.
put
(
"codeUrl"
,
wxPayUnifiedOrderResult
.
getCodeURL
());
// 二维码支付链接
break
;
}
case
PayConstant
.
WxConstant
.
TRADE_TYPE_APP
:
{
Map
<
String
,
String
>
payInfo
=
new
HashMap
<>();
String
timestamp
=
String
.
valueOf
(
System
.
currentTimeMillis
()
/
1000
);
String
nonceStr
=
String
.
valueOf
(
System
.
currentTimeMillis
());
// APP支付绑定的是微信开放平台上的账号,APPID为开放平台上绑定APP后发放的参数
String
appId
=
wxPayConfig
.
getAppId
();
Map
<
String
,
String
>
configMap
=
new
HashMap
<>();
// 此map用于参与调起sdk支付的二次签名,格式全小写,timestamp只能是10位,格式固定,切勿修改
String
partnerId
=
wxPayConfig
.
getMchId
();
configMap
.
put
(
"prepayid"
,
wxPayUnifiedOrderResult
.
getPrepayId
());
configMap
.
put
(
"partnerid"
,
partnerId
);
String
packageValue
=
"Sign=WXPay"
;
configMap
.
put
(
"package"
,
packageValue
);
configMap
.
put
(
"timestamp"
,
timestamp
);
configMap
.
put
(
"noncestr"
,
nonceStr
);
configMap
.
put
(
"appid"
,
appId
);
// 此map用于客户端与微信服务器交互
payInfo
.
put
(
"sign"
,
SignUtils
.
createSign
(
configMap
,
wxPayConfig
.
getMchKey
(),
null
));
payInfo
.
put
(
"prepayId"
,
wxPayUnifiedOrderResult
.
getPrepayId
());
payInfo
.
put
(
"partnerId"
,
partnerId
);
payInfo
.
put
(
"appId"
,
appId
);
payInfo
.
put
(
"packageValue"
,
packageValue
);
payInfo
.
put
(
"timeStamp"
,
timestamp
);
payInfo
.
put
(
"nonceStr"
,
nonceStr
);
map
.
put
(
"payParams"
,
payInfo
);
break
;
}
case
PayConstant
.
WxConstant
.
TRADE_TYPE_JSPAI
:
{
Map
<
String
,
String
>
payInfo
=
new
HashMap
<>();
String
timestamp
=
String
.
valueOf
(
System
.
currentTimeMillis
()
/
1000
);
String
nonceStr
=
String
.
valueOf
(
System
.
currentTimeMillis
());
payInfo
.
put
(
"appId"
,
wxPayUnifiedOrderResult
.
getAppid
());
// 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
payInfo
.
put
(
"timeStamp"
,
timestamp
);
payInfo
.
put
(
"nonceStr"
,
nonceStr
);
payInfo
.
put
(
"package"
,
"prepay_id="
+
wxPayUnifiedOrderResult
.
getPrepayId
());
payInfo
.
put
(
"signType"
,
WxPayConstants
.
SignType
.
MD5
);
payInfo
.
put
(
"paySign"
,
SignUtils
.
createSign
(
payInfo
,
wxPayConfig
.
getMchKey
(),
null
));
map
.
put
(
"payParams"
,
payInfo
);
break
;
}
case
PayConstant
.
WxConstant
.
TRADE_TYPE_MWEB
:
{
map
.
put
(
"payUrl"
,
wxPayUnifiedOrderResult
.
getMwebUrl
());
// h5支付链接地址
break
;
}
}
return
RpcUtil
.
createBizResult
(
baseParam
,
map
);
}
catch
(
WxPayException
e
)
{
_log
.
error
(
e
,
"下单失败"
);
//出现业务错误
_log
.
info
(
"{}下单返回失败"
,
logPrefix
);
_log
.
info
(
"err_code:{}"
,
e
.
getErrCode
());
_log
.
info
(
"err_code_des:{}"
,
e
.
getErrCodeDes
());
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_BIZ_WX_PAY_CREATE_FAIL
);
// return XXPayUtil.makeRetData(XXPayUtil.makeRetMap(PayConstant.RETURN_VALUE_SUCCESS, "", PayConstant.RETURN_VALUE_FAIL, "0111", "调用微信支付失败," + e.getErrCode() + ":" + e.getErrCodeDes()), resKey);
}
}
catch
(
Exception
e
)
{
_log
.
error
(
e
,
"微信支付统一下单异常"
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_BIZ_WX_PAY_CREATE_FAIL
);
//return XXPayUtil.makeRetFail(XXPayUtil.makeRetMap(PayConstant.RETURN_VALUE_FAIL, "", PayConstant.RETURN_VALUE_FAIL, PayEnum.ERR_0001));
}
}
/**
* 构建微信统一下单请求数据
* @param payOrder
* @param wxPayConfig
* @return
*/
WxPayUnifiedOrderRequest
buildUnifiedOrderRequest
(
PayOrder
payOrder
,
WxPayConfig
wxPayConfig
)
{
String
tradeType
=
wxPayConfig
.
getTradeType
();
String
payOrderId
=
payOrder
.
getPayOrderId
();
Integer
totalFee
=
payOrder
.
getAmount
().
intValue
();
// 支付金额,单位分
String
deviceInfo
=
payOrder
.
getDevice
();
String
body
=
payOrder
.
getBody
();
String
detail
=
null
;
String
attach
=
null
;
String
outTradeNo
=
payOrderId
;
String
feeType
=
"CNY"
;
String
spBillCreateIP
=
payOrder
.
getClientIp
();
String
timeStart
=
null
;
String
timeExpire
=
null
;
String
goodsTag
=
null
;
String
notifyUrl
=
wxPayConfig
.
getNotifyUrl
();
String
productId
=
null
;
if
(
tradeType
.
equals
(
PayConstant
.
WxConstant
.
TRADE_TYPE_NATIVE
))
productId
=
JSON
.
parseObject
(
payOrder
.
getExtra
()).
getString
(
"productId"
);
String
limitPay
=
null
;
String
openId
=
null
;
if
(
tradeType
.
equals
(
PayConstant
.
WxConstant
.
TRADE_TYPE_JSPAI
))
openId
=
JSON
.
parseObject
(
payOrder
.
getExtra
()).
getString
(
"openId"
);
String
sceneInfo
=
null
;
if
(
tradeType
.
equals
(
PayConstant
.
WxConstant
.
TRADE_TYPE_MWEB
))
sceneInfo
=
JSON
.
parseObject
(
payOrder
.
getExtra
()).
getString
(
"sceneInfo"
);
// 微信统一下单请求对象
WxPayUnifiedOrderRequest
request
=
new
WxPayUnifiedOrderRequest
();
request
.
setDeviceInfo
(
deviceInfo
);
request
.
setBody
(
body
);
request
.
setDetail
(
detail
);
request
.
setAttach
(
attach
);
request
.
setOutTradeNo
(
outTradeNo
);
request
.
setFeeType
(
feeType
);
request
.
setTotalFee
(
totalFee
);
request
.
setSpbillCreateIp
(
spBillCreateIP
);
request
.
setTimeStart
(
timeStart
);
request
.
setTimeExpire
(
timeExpire
);
request
.
setGoodsTag
(
goodsTag
);
request
.
setNotifyURL
(
notifyUrl
);
request
.
setTradeType
(
tradeType
);
request
.
setProductId
(
productId
);
request
.
setLimitPay
(
limitPay
);
request
.
setOpenid
(
openId
);
request
.
setSceneInfo
(
sceneInfo
);
return
request
;
}
}
xxpay4spring-boot/src/main/java/org/xxpay/boot/service/impl/PayChannelServiceImpl.java
0 → 100644
View file @
20a57324
package
org.xxpay.boot.service.impl
;
import
org.springframework.stereotype.Service
;
import
org.xxpay.boot.service.BaseService
;
import
org.xxpay.boot.service.IPayChannelService
;
import
org.xxpay.common.domain.BaseParam
;
import
org.xxpay.common.enumm.RetEnum
;
import
org.xxpay.common.util.JsonUtil
;
import
org.xxpay.common.util.MyLog
;
import
org.xxpay.common.util.ObjectValidUtil
;
import
org.xxpay.common.util.RpcUtil
;
import
org.xxpay.dal.dao.model.PayChannel
;
import
java.util.Map
;
/**
* @author: dingzhiwei
* @date: 17/9/8
* @description:
*/
@Service
public
class
PayChannelServiceImpl
extends
BaseService
implements
IPayChannelService
{
private
static
final
MyLog
_log
=
MyLog
.
getLog
(
PayChannelServiceImpl
.
class
);
@Override
public
Map
selectPayChannel
(
String
jsonParam
)
{
BaseParam
baseParam
=
JsonUtil
.
getObjectFromJson
(
jsonParam
,
BaseParam
.
class
);
Map
<
String
,
Object
>
bizParamMap
=
baseParam
.
getBizParamMap
();
if
(
ObjectValidUtil
.
isInvalid
(
bizParamMap
))
{
_log
.
warn
(
"查询支付渠道信息失败, {}. jsonParam={}"
,
RetEnum
.
RET_PARAM_NOT_FOUND
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_NOT_FOUND
);
}
String
mchId
=
baseParam
.
isNullValue
(
"mchId"
)
?
null
:
bizParamMap
.
get
(
"mchId"
).
toString
();
String
channelId
=
baseParam
.
isNullValue
(
"channelId"
)
?
null
:
bizParamMap
.
get
(
"channelId"
).
toString
();
if
(
ObjectValidUtil
.
isInvalid
(
mchId
,
channelId
))
{
_log
.
warn
(
"查询支付渠道信息失败, {}. jsonParam={}"
,
RetEnum
.
RET_PARAM_INVALID
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_INVALID
);
}
PayChannel
payChannel
=
super
.
baseSelectPayChannel
(
mchId
,
channelId
);
if
(
payChannel
==
null
)
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_BIZ_DATA_NOT_EXISTS
);
String
jsonResult
=
JsonUtil
.
object2Json
(
payChannel
);
return
RpcUtil
.
createBizResult
(
baseParam
,
jsonResult
);
}
}
xxpay4spring-boot/src/main/java/org/xxpay/boot/service/impl/PayOrderServiceImpl.java
0 → 100644
View file @
20a57324
package
org.xxpay.boot.service.impl
;
import
com.alibaba.fastjson.JSONObject
;
import
org.springframework.stereotype.Service
;
import
org.xxpay.boot.service.BaseService
;
import
org.xxpay.boot.service.IPayOrderService
;
import
org.xxpay.common.domain.BaseParam
;
import
org.xxpay.common.enumm.RetEnum
;
import
org.xxpay.common.util.*
;
import
org.xxpay.dal.dao.model.PayOrder
;
import
java.util.Map
;
/**
* @author: dingzhiwei
* @date: 17/9/8
* @description:
*/
@Service
public
class
PayOrderServiceImpl
extends
BaseService
implements
IPayOrderService
{
private
static
final
MyLog
_log
=
MyLog
.
getLog
(
PayOrderServiceImpl
.
class
);
@Override
public
Map
createPayOrder
(
String
jsonParam
)
{
BaseParam
baseParam
=
JsonUtil
.
getObjectFromJson
(
jsonParam
,
BaseParam
.
class
);
Map
<
String
,
Object
>
bizParamMap
=
baseParam
.
getBizParamMap
();
if
(
ObjectValidUtil
.
isInvalid
(
bizParamMap
))
{
_log
.
warn
(
"新增支付订单失败, {}. jsonParam={}"
,
RetEnum
.
RET_PARAM_NOT_FOUND
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_NOT_FOUND
);
}
JSONObject
payOrderObj
=
baseParam
.
isNullValue
(
"payOrder"
)
?
null
:
JSONObject
.
parseObject
(
bizParamMap
.
get
(
"payOrder"
).
toString
());
if
(
payOrderObj
==
null
)
{
_log
.
warn
(
"新增支付订单失败, {}. jsonParam={}"
,
RetEnum
.
RET_PARAM_INVALID
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_INVALID
);
}
PayOrder
payOrder
=
BeanConvertUtils
.
map2Bean
(
payOrderObj
,
PayOrder
.
class
);
if
(
payOrder
==
null
)
{
_log
.
warn
(
"新增支付订单失败, {}. jsonParam={}"
,
RetEnum
.
RET_PARAM_INVALID
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_INVALID
);
}
int
result
=
super
.
baseCreatePayOrder
(
payOrder
);
return
RpcUtil
.
createBizResult
(
baseParam
,
result
);
}
@Override
public
Map
selectPayOrder
(
String
jsonParam
)
{
BaseParam
baseParam
=
JsonUtil
.
getObjectFromJson
(
jsonParam
,
BaseParam
.
class
);
Map
<
String
,
Object
>
bizParamMap
=
baseParam
.
getBizParamMap
();
if
(
ObjectValidUtil
.
isInvalid
(
bizParamMap
))
{
_log
.
warn
(
"根据支付订单号查询支付订单失败, {}. jsonParam={}"
,
RetEnum
.
RET_PARAM_NOT_FOUND
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_NOT_FOUND
);
}
String
payOrderId
=
baseParam
.
isNullValue
(
"payOrderId"
)
?
null
:
bizParamMap
.
get
(
"payOrderId"
).
toString
();
if
(
ObjectValidUtil
.
isInvalid
(
payOrderId
))
{
_log
.
warn
(
"根据支付订单号查询支付订单失败, {}. jsonParam={}"
,
RetEnum
.
RET_PARAM_INVALID
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_INVALID
);
}
PayOrder
payOrder
=
super
.
baseSelectPayOrder
(
payOrderId
);
if
(
payOrder
==
null
)
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_BIZ_DATA_NOT_EXISTS
);
String
jsonResult
=
JsonUtil
.
object2Json
(
payOrder
);
return
RpcUtil
.
createBizResult
(
baseParam
,
jsonResult
);
}
@Override
public
Map
selectPayOrderByMchIdAndPayOrderId
(
String
jsonParam
)
{
BaseParam
baseParam
=
JsonUtil
.
getObjectFromJson
(
jsonParam
,
BaseParam
.
class
);
Map
<
String
,
Object
>
bizParamMap
=
baseParam
.
getBizParamMap
();
if
(
ObjectValidUtil
.
isInvalid
(
bizParamMap
))
{
_log
.
warn
(
"根据商户号和支付订单号查询支付订单失败, {}. jsonParam={}"
,
RetEnum
.
RET_PARAM_NOT_FOUND
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_NOT_FOUND
);
}
String
mchId
=
baseParam
.
isNullValue
(
"mchId"
)
?
null
:
bizParamMap
.
get
(
"mchId"
).
toString
();
String
payOrderId
=
baseParam
.
isNullValue
(
"payOrderId"
)
?
null
:
bizParamMap
.
get
(
"payOrderId"
).
toString
();
if
(
ObjectValidUtil
.
isInvalid
(
mchId
,
payOrderId
))
{
_log
.
warn
(
"根据商户号和支付订单号查询支付订单失败, {}. jsonParam={}"
,
RetEnum
.
RET_PARAM_INVALID
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_INVALID
);
}
PayOrder
payOrder
=
super
.
baseSelectPayOrderByMchIdAndPayOrderId
(
mchId
,
payOrderId
);
if
(
payOrder
==
null
)
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_BIZ_DATA_NOT_EXISTS
);
String
jsonResult
=
JsonUtil
.
object2Json
(
payOrder
);
return
RpcUtil
.
createBizResult
(
baseParam
,
jsonResult
);
}
@Override
public
Map
selectPayOrderByMchIdAndMchOrderNo
(
String
jsonParam
)
{
BaseParam
baseParam
=
JsonUtil
.
getObjectFromJson
(
jsonParam
,
BaseParam
.
class
);
Map
<
String
,
Object
>
bizParamMap
=
baseParam
.
getBizParamMap
();
if
(
ObjectValidUtil
.
isInvalid
(
bizParamMap
))
{
_log
.
warn
(
"根据商户号和商户订单号查询支付订单失败, {}. jsonParam={}"
,
RetEnum
.
RET_PARAM_NOT_FOUND
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_NOT_FOUND
);
}
String
mchId
=
baseParam
.
isNullValue
(
"mchId"
)
?
null
:
bizParamMap
.
get
(
"mchId"
).
toString
();
String
mchOrderNo
=
baseParam
.
isNullValue
(
"mchOrderNo"
)
?
null
:
bizParamMap
.
get
(
"mchOrderNo"
).
toString
();
if
(
ObjectValidUtil
.
isInvalid
(
mchId
,
mchOrderNo
))
{
_log
.
warn
(
"根据商户号和商户订单号查询支付订单失败, {}. jsonParam={}"
,
RetEnum
.
RET_PARAM_INVALID
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_INVALID
);
}
PayOrder
payOrder
=
super
.
baseSelectPayOrderByMchIdAndMchOrderNo
(
mchId
,
mchOrderNo
);
if
(
payOrder
==
null
)
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_BIZ_DATA_NOT_EXISTS
);
String
jsonResult
=
JsonUtil
.
object2Json
(
payOrder
);
return
RpcUtil
.
createBizResult
(
baseParam
,
jsonResult
);
}
@Override
public
Map
updateStatus4Ing
(
String
jsonParam
)
{
BaseParam
baseParam
=
JsonUtil
.
getObjectFromJson
(
jsonParam
,
BaseParam
.
class
);
Map
<
String
,
Object
>
bizParamMap
=
baseParam
.
getBizParamMap
();
if
(
ObjectValidUtil
.
isInvalid
(
bizParamMap
))
{
_log
.
warn
(
"修改支付订单状态为支付中失败, {}. jsonParam={}"
,
RetEnum
.
RET_PARAM_NOT_FOUND
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_NOT_FOUND
);
}
String
payOrderId
=
baseParam
.
isNullValue
(
"payOrderId"
)
?
null
:
bizParamMap
.
get
(
"payOrderId"
).
toString
();
String
channelOrderNo
=
baseParam
.
isNullValue
(
"channelOrderNo"
)
?
null
:
bizParamMap
.
get
(
"channelOrderNo"
).
toString
();
if
(
ObjectValidUtil
.
isInvalid
(
payOrderId
,
channelOrderNo
))
{
_log
.
warn
(
"修改支付订单状态为支付中失败, {}. jsonParam={}"
,
RetEnum
.
RET_PARAM_INVALID
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_INVALID
);
}
int
result
=
super
.
baseUpdateStatus4Ing
(
payOrderId
,
channelOrderNo
);
return
RpcUtil
.
createBizResult
(
baseParam
,
result
);
}
@Override
public
Map
updateStatus4Success
(
String
jsonParam
)
{
BaseParam
baseParam
=
JsonUtil
.
getObjectFromJson
(
jsonParam
,
BaseParam
.
class
);
Map
<
String
,
Object
>
bizParamMap
=
baseParam
.
getBizParamMap
();
if
(
ObjectValidUtil
.
isInvalid
(
bizParamMap
))
{
_log
.
warn
(
"修改支付订单状态为支付成功失败, {}. jsonParam={}"
,
RetEnum
.
RET_PARAM_NOT_FOUND
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_NOT_FOUND
);
}
String
payOrderId
=
baseParam
.
isNullValue
(
"payOrderId"
)
?
null
:
bizParamMap
.
get
(
"payOrderId"
).
toString
();
if
(
ObjectValidUtil
.
isInvalid
(
payOrderId
))
{
_log
.
warn
(
"修改支付订单状态为支付成功失败, {}. jsonParam={}"
,
RetEnum
.
RET_PARAM_INVALID
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_INVALID
);
}
int
result
=
super
.
baseUpdateStatus4Success
(
payOrderId
);
return
RpcUtil
.
createBizResult
(
baseParam
,
result
);
}
@Override
public
Map
updateStatus4Complete
(
String
jsonParam
)
{
BaseParam
baseParam
=
JsonUtil
.
getObjectFromJson
(
jsonParam
,
BaseParam
.
class
);
Map
<
String
,
Object
>
bizParamMap
=
baseParam
.
getBizParamMap
();
if
(
ObjectValidUtil
.
isInvalid
(
bizParamMap
))
{
_log
.
warn
(
"修改支付订单状态为支付完成失败, {}. jsonParam={}"
,
RetEnum
.
RET_PARAM_NOT_FOUND
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_NOT_FOUND
);
}
String
payOrderId
=
baseParam
.
isNullValue
(
"payOrderId"
)
?
null
:
bizParamMap
.
get
(
"payOrderId"
).
toString
();
if
(
ObjectValidUtil
.
isInvalid
(
payOrderId
))
{
_log
.
warn
(
"修改支付订单状态为支付完成失败, {}. jsonParam={}"
,
RetEnum
.
RET_PARAM_INVALID
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_INVALID
);
}
int
result
=
super
.
baseUpdateStatus4Complete
(
payOrderId
);
return
RpcUtil
.
createBizResult
(
baseParam
,
result
);
}
@Override
public
Map
updateNotify
(
String
jsonParam
)
{
BaseParam
baseParam
=
JsonUtil
.
getObjectFromJson
(
jsonParam
,
BaseParam
.
class
);
Map
<
String
,
Object
>
bizParamMap
=
baseParam
.
getBizParamMap
();
if
(
ObjectValidUtil
.
isInvalid
(
bizParamMap
))
{
_log
.
warn
(
"修改支付订单通知次数失败, {}. jsonParam={}"
,
RetEnum
.
RET_PARAM_NOT_FOUND
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_NOT_FOUND
);
}
String
payOrderId
=
baseParam
.
isNullValue
(
"payOrderId"
)
?
null
:
bizParamMap
.
get
(
"payOrderId"
).
toString
();
Byte
count
=
baseParam
.
isNullValue
(
"count"
)
?
null
:
Byte
.
parseByte
(
bizParamMap
.
get
(
"count"
).
toString
());
if
(
ObjectValidUtil
.
isInvalid
(
payOrderId
,
count
))
{
_log
.
warn
(
"修改支付订单通知次数失败, {}. jsonParam={}"
,
RetEnum
.
RET_PARAM_INVALID
.
getMessage
(),
jsonParam
);
return
RpcUtil
.
createFailResult
(
baseParam
,
RetEnum
.
RET_PARAM_INVALID
);
}
int
result
=
super
.
baseUpdateNotify
(
payOrderId
,
count
);
return
RpcUtil
.
createBizResult
(
baseParam
,
result
);
}
}
xxpay4spring-boot/src/main/java/org/xxpay/boot/service/mq/Mq4PayNotify.java
0 → 100644
View file @
20a57324
package
org.xxpay.boot.service.mq
;
import
com.alibaba.fastjson.JSON
;
import
com.alibaba.fastjson.JSONObject
;
import
org.apache.activemq.ScheduledMessage
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.jms.annotation.JmsListener
;
import
org.springframework.jms.core.JmsTemplate
;
import
org.springframework.jms.core.MessageCreator
;
import
org.springframework.stereotype.Component
;
import
org.springframework.util.StringUtils
;
import
org.xxpay.common.util.MyLog
;
import
org.xxpay.boot.service.BaseService
;
import
javax.jms.*
;
import
javax.net.ssl.HttpsURLConnection
;
import
javax.net.ssl.SSLContext
;
import
javax.net.ssl.TrustManager
;
import
javax.net.ssl.X509TrustManager
;
import
java.io.BufferedReader
;
import
java.io.InputStreamReader
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
import
java.security.cert.CertificateException
;
import
java.security.cert.X509Certificate
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
/**
* @Description: 业务通知MQ实现
* @author dingzhiwei jmdhappy@126.com
* @date 2017-07-05
* @version V1.0
* @Copyright: www.xxpay.org
*/
@Component
public
class
Mq4PayNotify
extends
BaseService
{
@Autowired
private
Queue
payNotifyQueue
;
@Autowired
private
JmsTemplate
jmsTemplate
;
private
static
final
MyLog
_log
=
MyLog
.
getLog
(
Mq4PayNotify
.
class
);
public
void
send
(
String
msg
)
{
_log
.
info
(
"发送MQ消息:msg={}"
,
msg
);
this
.
jmsTemplate
.
convertAndSend
(
this
.
payNotifyQueue
,
msg
);
}
/**
* 发送延迟消息
* @param msg
* @param delay
*/
public
void
send
(
String
msg
,
long
delay
)
{
_log
.
info
(
"发送MQ延时消息:msg={},delay={}"
,
msg
,
delay
);
jmsTemplate
.
send
(
this
.
payNotifyQueue
,
new
MessageCreator
()
{
public
Message
createMessage
(
Session
session
)
throws
JMSException
{
TextMessage
tm
=
session
.
createTextMessage
(
msg
);
tm
.
setLongProperty
(
ScheduledMessage
.
AMQ_SCHEDULED_DELAY
,
delay
);
tm
.
setLongProperty
(
ScheduledMessage
.
AMQ_SCHEDULED_PERIOD
,
1
*
1000
);
tm
.
setLongProperty
(
ScheduledMessage
.
AMQ_SCHEDULED_REPEAT
,
1
);
return
tm
;
}
});
}
private
static
class
TrustAnyTrustManager
implements
X509TrustManager
{
public
void
checkClientTrusted
(
X509Certificate
[]
chain
,
String
authType
)
throws
CertificateException
{
}
public
void
checkServerTrusted
(
X509Certificate
[]
chain
,
String
authType
)
throws
CertificateException
{
}
public
X509Certificate
[]
getAcceptedIssuers
()
{
return
new
X509Certificate
[]
{};
}
}
@JmsListener
(
destination
=
MqConfig
.
PAY_NOTIFY_QUEUE_NAME
)
public
void
receive
(
String
msg
)
{
_log
.
info
(
"do notify task, msg={}"
,
msg
);
JSONObject
msgObj
=
JSON
.
parseObject
(
msg
);
String
respUrl
=
msgObj
.
getString
(
"url"
);
String
orderId
=
msgObj
.
getString
(
"orderId"
);
int
count
=
msgObj
.
getInteger
(
"count"
);
if
(
StringUtils
.
isEmpty
(
respUrl
))
{
_log
.
warn
(
"notify url is empty. respUrl={}"
,
respUrl
);
return
;
}
try
{
StringBuffer
sb
=
new
StringBuffer
();
URL
console
=
new
URL
(
respUrl
);
_log
.
info
(
"==>MQ通知业务系统开始[orderId:{}][count:{}][time:{}]"
,
orderId
,
count
,
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
).
format
(
new
Date
()));
if
(
"https"
.
equals
(
console
.
getProtocol
()))
{
SSLContext
sc
=
SSLContext
.
getInstance
(
"SSL"
);
sc
.
init
(
null
,
new
TrustManager
[]
{
new
TrustAnyTrustManager
()
},
new
java
.
security
.
SecureRandom
());
HttpsURLConnection
con
=
(
HttpsURLConnection
)
console
.
openConnection
();
con
.
setSSLSocketFactory
(
sc
.
getSocketFactory
());
con
.
setRequestMethod
(
"POST"
);
con
.
setDoInput
(
true
);
con
.
setDoOutput
(
true
);
con
.
setUseCaches
(
false
);
con
.
setConnectTimeout
(
10
*
1000
);
con
.
setReadTimeout
(
5
*
1000
);
con
.
setRequestProperty
(
"Content-Type"
,
"application/x-www-form-urlencoded"
);
BufferedReader
in
=
new
BufferedReader
(
new
InputStreamReader
(
con
.
getInputStream
()),
1024
*
1024
);
while
(
true
)
{
String
line
=
in
.
readLine
();
if
(
line
==
null
)
{
break
;
}
sb
.
append
(
line
);
}
in
.
close
();
}
else
if
(
"http"
.
equals
(
console
.
getProtocol
()))
{
HttpURLConnection
con
=
(
HttpURLConnection
)
console
.
openConnection
();
con
.
setRequestMethod
(
"POST"
);
con
.
setDoInput
(
true
);
con
.
setDoOutput
(
true
);
con
.
setUseCaches
(
false
);
con
.
setConnectTimeout
(
10
*
1000
);
con
.
setReadTimeout
(
5
*
1000
);
con
.
setRequestProperty
(
"Content-Type"
,
"application/x-www-form-urlencoded"
);
BufferedReader
in
=
new
BufferedReader
(
new
InputStreamReader
(
con
.
getInputStream
()),
1024
*
1024
);
while
(
true
)
{
String
line
=
in
.
readLine
();
if
(
line
==
null
)
{
break
;
}
sb
.
append
(
line
);
}
in
.
close
();
}
else
{
_log
.
error
(
"not do protocol. protocol=%s"
,
console
.
getProtocol
());
return
;
}
_log
.
info
(
"<==MQ通知业务系统结束[orderId:{}][count:{}][time:{}]"
,
orderId
,
count
,
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
).
format
(
new
Date
()));
// 验证结果
_log
.
info
(
"notify response , OrderID={}"
,
orderId
);
if
(
sb
.
toString
().
trim
().
equalsIgnoreCase
(
"success"
)){
//_log.info("{} notify success, url:{}", _notifyInfo.getBusiId(), respUrl);
//修改订单表
try
{
int
result
=
super
.
baseUpdateStatus4Complete
(
orderId
);
_log
.
info
(
"修改payOrderId={},订单状态为处理完成->{}"
,
orderId
,
result
==
1
?
"成功"
:
"失败"
);
}
catch
(
Exception
e
)
{
_log
.
error
(
e
,
"修改订单状态为处理完成异常"
);
}
// 修改通知次数
try
{
int
result
=
super
.
baseUpdateNotify
(
orderId
,
(
byte
)
1
);
_log
.
info
(
"修改payOrderId={},通知业务系统次数->{}"
,
orderId
,
result
==
1
?
"成功"
:
"失败"
);
}
catch
(
Exception
e
)
{
_log
.
error
(
e
,
"修改通知次数异常"
);
}
return
;
// 通知成功结束
}
else
{
// 通知失败,延时再通知
int
cnt
=
count
+
1
;
_log
.
info
(
"notify count={}"
,
cnt
);
// 修改通知次数
try
{
int
result
=
super
.
baseUpdateNotify
(
orderId
,
(
byte
)
cnt
);
_log
.
info
(
"修改payOrderId={},通知业务系统次数->{}"
,
orderId
,
result
==
1
?
"成功"
:
"失败"
);
}
catch
(
Exception
e
)
{
_log
.
error
(
e
,
"修改通知次数异常"
);
}
if
(
cnt
>
5
)
{
_log
.
info
(
"notify count>5 stop. url={}"
,
respUrl
);
return
;
}
msgObj
.
put
(
"count"
,
cnt
);
this
.
send
(
msgObj
.
toJSONString
(),
cnt
*
60
*
1000
);
}
_log
.
warn
(
"notify failed. url:{}, response body:{}"
,
respUrl
,
sb
.
toString
());
}
catch
(
Exception
e
)
{
_log
.
info
(
"<==MQ通知业务系统结束[orderId:{}][count:{}][time:{}]"
,
orderId
,
count
,
new
SimpleDateFormat
(
"yyyy-MM-dd HH:mm:ss"
).
format
(
new
Date
()));
_log
.
error
(
e
,
"notify exception. url:%s"
,
respUrl
);
}
}
}
xxpay4spring-boot/src/main/java/org/xxpay/boot/service/mq/MqConfig.java
0 → 100644
View file @
20a57324
package
org.xxpay.boot.service.mq
;
import
org.apache.activemq.command.ActiveMQQueue
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
javax.jms.Queue
;
/**
* @Description:
* @author dingzhiwei jmdhappy@126.com
* @date 2017-07-05
* @version V1.0
* @Copyright: www.xxpay.org
*/
@Configuration
public
class
MqConfig
{
public
static
final
String
PAY_NOTIFY_QUEUE_NAME
=
"pay.notify.queue"
;
@Bean
public
Queue
payNotifyQueue
()
{
return
new
ActiveMQQueue
(
PAY_NOTIFY_QUEUE_NAME
);
}
}
xxpay4spring-boot/src/main/java/org/xxpay/boot/web/MchInfoService.java
0 → 100644
View file @
20a57324
package
org.xxpay.boot.web
;
import
com.alibaba.fastjson.JSONObject
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.xxpay.common.util.RpcUtil
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* @author: dingzhiwei
* @date: 17/9/9
* @description:
*/
@Service
public
class
MchInfoService
{
@Autowired
private
RpcCommonService
rpcCommonService
;
public
JSONObject
getByMchId
(
String
mchId
)
{
Map
<
String
,
Object
>
paramMap
=
new
HashMap
<>();
paramMap
.
put
(
"mchId"
,
mchId
);
String
jsonParam
=
RpcUtil
.
createBaseParam
(
paramMap
);
Map
<
String
,
Object
>
result
=
rpcCommonService
.
rpcMchInfoService
.
selectMchInfo
(
jsonParam
);
String
s
=
RpcUtil
.
mkRet
(
result
);
if
(
s
==
null
)
return
null
;
return
JSONObject
.
parseObject
(
s
);
}
}
xxpay4spring-boot/src/main/java/org/xxpay/boot/web/NotifyPayService.java
0 → 100644
View file @
20a57324
package
org.xxpay.boot.web
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.xxpay.common.constant.PayConstant
;
import
org.xxpay.common.util.RpcUtil
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* @author: dingzhiwei
* @date: 17/9/10
* @description:
*/
@Service
public
class
NotifyPayService
{
@Autowired
private
RpcCommonService
rpcCommonService
;
public
String
doAliPayNotify
(
Map
params
)
{
Map
<
String
,
Object
>
paramMap
=
new
HashMap
<>();
paramMap
.
put
(
"params"
,
params
);
String
jsonParam
=
RpcUtil
.
createBaseParam
(
paramMap
);
Map
<
String
,
Object
>
result
=
rpcCommonService
.
rpcNotifyPayService
.
doAliPayNotify
(
jsonParam
);
String
s
=
RpcUtil
.
mkRet
(
result
);
if
(
s
==
null
)
{
return
PayConstant
.
RETURN_ALIPAY_VALUE_FAIL
;
}
return
s
;
}
public
String
doWxPayNotify
(
String
xmlResult
)
{
Map
<
String
,
Object
>
paramMap
=
new
HashMap
<>();
paramMap
.
put
(
"xmlResult"
,
xmlResult
);
String
jsonParam
=
RpcUtil
.
createBaseParam
(
paramMap
);
// 返回给微信的数据格式已经有service处理(包括正确与错误),肯定会返回result
Map
<
String
,
Object
>
result
=
rpcCommonService
.
rpcNotifyPayService
.
doWxPayNotify
(
jsonParam
);
return
RpcUtil
.
mkRet
(
result
);
}
}
xxpay4spring-boot/src/main/java/org/xxpay/boot/web/PayChannelService.java
0 → 100644
View file @
20a57324
package
org.xxpay.boot.web
;
import
com.alibaba.fastjson.JSONObject
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.xxpay.common.util.RpcUtil
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* @author: dingzhiwei
* @date: 17/9/9
* @description:
*/
@Service
public
class
PayChannelService
{
@Autowired
private
RpcCommonService
rpcCommonService
;
public
JSONObject
getByMchIdAndChannelId
(
String
mchId
,
String
channelId
)
{
Map
<
String
,
Object
>
paramMap
=
new
HashMap
<>();
paramMap
.
put
(
"mchId"
,
mchId
);
paramMap
.
put
(
"channelId"
,
channelId
);
String
jsonParam
=
RpcUtil
.
createBaseParam
(
paramMap
);
Map
<
String
,
Object
>
result
=
rpcCommonService
.
rpcPayChannelService
.
selectPayChannel
(
jsonParam
);
String
s
=
RpcUtil
.
mkRet
(
result
);
if
(
s
==
null
)
return
null
;
return
JSONObject
.
parseObject
(
s
);
}
}
xxpay4spring-boot/src/main/java/org/xxpay/boot/web/PayOrderService.java
0 → 100644
View file @
20a57324
package
org.xxpay.boot.web
;
import
com.alibaba.fastjson.JSONObject
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.xxpay.common.constant.PayConstant
;
import
org.xxpay.common.util.MyLog
;
import
org.xxpay.common.util.RpcUtil
;
import
org.xxpay.common.util.XXPayUtil
;
import
java.util.HashMap
;
import
java.util.Map
;
/**
* @author: dingzhiwei
* @date: 17/9/9
* @description:
*/
@Service
public
class
PayOrderService
{
private
static
final
MyLog
_log
=
MyLog
.
getLog
(
PayOrderService
.
class
);
@Autowired
private
RpcCommonService
rpcCommonService
;
public
int
createPayOrder
(
JSONObject
payOrder
)
{
Map
<
String
,
Object
>
paramMap
=
new
HashMap
<>();
paramMap
.
put
(
"payOrder"
,
payOrder
);
String
jsonParam
=
RpcUtil
.
createBaseParam
(
paramMap
);
Map
<
String
,
Object
>
result
=
rpcCommonService
.
rpcPayOrderService
.
createPayOrder
(
jsonParam
);
String
s
=
RpcUtil
.
mkRet
(
result
);
if
(
s
==
null
)
return
0
;
return
Integer
.
parseInt
(
s
);
}
public
JSONObject
queryPayOrder
(
String
mchId
,
String
payOrderId
,
String
mchOrderNo
,
String
executeNotify
)
{
Map
<
String
,
Object
>
paramMap
=
new
HashMap
<>();
Map
<
String
,
Object
>
result
;
if
(
StringUtils
.
isNotBlank
(
payOrderId
))
{
paramMap
.
put
(
"mchId"
,
mchId
);
paramMap
.
put
(
"payOrderId"
,
payOrderId
);
String
jsonParam
=
RpcUtil
.
createBaseParam
(
paramMap
);
result
=
rpcCommonService
.
rpcPayOrderService
.
selectPayOrderByMchIdAndPayOrderId
(
jsonParam
);
}
else
{
paramMap
.
put
(
"mchId"
,
mchId
);
paramMap
.
put
(
"mchOrderNo"
,
mchOrderNo
);
String
jsonParam
=
RpcUtil
.
createBaseParam
(
paramMap
);
result
=
rpcCommonService
.
rpcPayOrderService
.
selectPayOrderByMchIdAndMchOrderNo
(
jsonParam
);
}
String
s
=
RpcUtil
.
mkRet
(
result
);
if
(
s
==
null
)
return
null
;
boolean
isNotify
=
Boolean
.
parseBoolean
(
executeNotify
);
JSONObject
payOrder
=
JSONObject
.
parseObject
(
s
);
if
(
isNotify
)
{
paramMap
=
new
HashMap
<>();
paramMap
.
put
(
"payOrderId"
,
payOrderId
);
String
jsonParam
=
RpcUtil
.
createBaseParam
(
paramMap
);
result
=
rpcCommonService
.
rpcNotifyPayService
.
sendBizPayNotify
(
jsonParam
);
s
=
RpcUtil
.
mkRet
(
result
);
_log
.
info
(
"业务查单完成,并再次发送业务支付通知.发送结果:{}"
,
s
);
}
return
payOrder
;
}
public
String
doWxPayReq
(
String
tradeType
,
JSONObject
payOrder
,
String
resKey
)
{
Map
<
String
,
Object
>
paramMap
=
new
HashMap
<>();
paramMap
.
put
(
"tradeType"
,
tradeType
);
paramMap
.
put
(
"payOrder"
,
payOrder
);
String
jsonParam
=
RpcUtil
.
createBaseParam
(
paramMap
);
Map
<
String
,
Object
>
result
=
rpcCommonService
.
rpcPayChannel4WxService
.
doWxPayReq
(
jsonParam
);
String
s
=
RpcUtil
.
mkRet
(
result
);
if
(
s
==
null
)
{
return
XXPayUtil
.
makeRetData
(
XXPayUtil
.
makeRetMap
(
PayConstant
.
RETURN_VALUE_SUCCESS
,
""
,
PayConstant
.
RETURN_VALUE_FAIL
,
"0111"
,
"调用微信支付失败"
),
resKey
);
}
Map
<
String
,
Object
>
map
=
XXPayUtil
.
makeRetMap
(
PayConstant
.
RETURN_VALUE_SUCCESS
,
""
,
PayConstant
.
RETURN_VALUE_SUCCESS
,
null
);
map
.
putAll
((
Map
)
result
.
get
(
"bizResult"
));
return
XXPayUtil
.
makeRetData
(
map
,
resKey
);
}
public
String
doAliPayReq
(
String
channelId
,
JSONObject
payOrder
,
String
resKey
)
{
Map
<
String
,
Object
>
paramMap
=
new
HashMap
<>();
paramMap
.
put
(
"payOrder"
,
payOrder
);
String
jsonParam
=
RpcUtil
.
createBaseParam
(
paramMap
);
Map
<
String
,
Object
>
result
;
switch
(
channelId
)
{
case
PayConstant
.
PAY_CHANNEL_ALIPAY_MOBILE
:
result
=
rpcCommonService
.
rpcPayChannel4AliService
.
doAliPayMobileReq
(
jsonParam
);
break
;
case
PayConstant
.
PAY_CHANNEL_ALIPAY_PC
:
result
=
rpcCommonService
.
rpcPayChannel4AliService
.
doAliPayPcReq
(
jsonParam
);
break
;
case
PayConstant
.
PAY_CHANNEL_ALIPAY_WAP
:
result
=
rpcCommonService
.
rpcPayChannel4AliService
.
doAliPayWapReq
(
jsonParam
);
break
;
case
PayConstant
.
PAY_CHANNEL_ALIPAY_QR
:
result
=
rpcCommonService
.
rpcPayChannel4AliService
.
doAliPayQrReq
(
jsonParam
);
break
;
default
:
result
=
null
;
break
;
}
String
s
=
RpcUtil
.
mkRet
(
result
);
if
(
s
==
null
)
{
return
XXPayUtil
.
makeRetData
(
XXPayUtil
.
makeRetMap
(
PayConstant
.
RETURN_VALUE_SUCCESS
,
""
,
PayConstant
.
RETURN_VALUE_FAIL
,
"0111"
,
"调用支付宝支付失败"
),
resKey
);
}
Map
<
String
,
Object
>
map
=
XXPayUtil
.
makeRetMap
(
PayConstant
.
RETURN_VALUE_SUCCESS
,
""
,
PayConstant
.
RETURN_VALUE_SUCCESS
,
null
);
map
.
putAll
((
Map
)
result
.
get
(
"bizResult"
));
return
XXPayUtil
.
makeRetData
(
map
,
resKey
);
}
}
xxpay4spring-boot/src/main/java/org/xxpay/boot/web/RpcCommonService.java
0 → 100644
View file @
20a57324
package
org.xxpay.boot.web
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.xxpay.boot.service.*
;
/**
* @author: dingzhiwei
* @date: 17/9/10
* @description:
*/
@Service
public
class
RpcCommonService
{
@Autowired
public
IMchInfoService
rpcMchInfoService
;
@Autowired
public
IPayChannelService
rpcPayChannelService
;
@Autowired
public
IPayOrderService
rpcPayOrderService
;
@Autowired
public
IPayChannel4WxService
rpcPayChannel4WxService
;
@Autowired
public
IPayChannel4AliService
rpcPayChannel4AliService
;
@Autowired
public
INotifyPayService
rpcNotifyPayService
;
}
xxpay4spring-boot/src/main/webapp/WEB-INF/lib/alipay-sdk-java20170818173712.jar
0 → 100755
View file @
20a57324
File added
xxpay4spring-cloud/xxpay-service/pom.xml
View file @
20a57324
...
...
@@ -27,6 +27,10 @@
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-activemq
</artifactId>
</dependency>
<dependency>
<groupId>
org.apache.activemq
</groupId>
<artifactId>
activemq-pool
</artifactId>
</dependency>
<dependency>
<groupId>
org.mybatis.spring.boot
</groupId>
<artifactId>
mybatis-spring-boot-starter
</artifactId>
...
...
Prev
1
2
Next
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment