### 快速入门
#####1.支付整合配置
```java
/**
* 支付类型
* @author egan
* @email egzosn@gmail.com
* @date 2016/11/20 0:30
*/
public enum PayType implements BasePayType {
aliPay{
/**
* @see com.egzosn.pay.ali.api.AliPayService 17年更新的版本,旧版本请自行切换{@link com.egzosn.pay.ali.before.api.AliPayService }
* @param apyAccount
* @return
*/
@Override
public PayService getPayService(ApyAccount apyAccount) {
AliPayConfigStorage aliPayConfigStorage = new AliPayConfigStorage();
aliPayConfigStorage.setPid(apyAccount.getPartner());
aliPayConfigStorage.setAppId(apyAccount.getAppid());
aliPayConfigStorage.setAliPublicKey(apyAccount.getPublicKey());
aliPayConfigStorage.setKeyPrivate(apyAccount.getPrivateKey());
aliPayConfigStorage.setNotifyUrl(apyAccount.getNotifyUrl());
aliPayConfigStorage.setReturnUrl(apyAccount.getReturnUrl());
aliPayConfigStorage.setSignType(apyAccount.getSignType());
aliPayConfigStorage.setSeller(apyAccount.getSeller());
aliPayConfigStorage.setPayType(apyAccount.getPayType().toString());
aliPayConfigStorage.setMsgType(apyAccount.getMsgType());
aliPayConfigStorage.setInputCharset(apyAccount.getInputCharset());
return new AliPayService(aliPayConfigStorage);
}
@Override
public TransactionType getTransactionType(String transactionType) {
// com.egzosn.pay.ali.bean.AliTransactionType 17年更新的版本,旧版本请自行切换{@link com.egzosn.pay.ali.before.bean.AliTransactionType}
return AliTransactionType.valueOf(transactionType);
}
},wxPay {
@Override
public PayService getPayService(ApyAccount apyAccount) {
WxPayConfigStorage wxPayConfigStorage = new WxPayConfigStorage();
wxPayConfigStorage.setMchId(apyAccount.getPartner());
wxPayConfigStorage.setAppSecret(apyAccount.getPublicKey());
wxPayConfigStorage.setKeyPublic(apyAccount.getPublicKey());
wxPayConfigStorage.setAppid(apyAccount.getAppid());
wxPayConfigStorage.setKeyPrivate(apyAccount.getPrivateKey());
wxPayConfigStorage.setNotifyUrl(apyAccount.getNotifyUrl());
wxPayConfigStorage.setSignType(apyAccount.getSignType());
wxPayConfigStorage.setPayType(apyAccount.getPayType().toString());
wxPayConfigStorage.setMsgType(apyAccount.getMsgType());
wxPayConfigStorage.setInputCharset(apyAccount.getInputCharset());
return new WxPayService(wxPayConfigStorage);
}
/**
* 根据支付类型获取交易类型
* @param transactionType 类型值
* @see com.egzosn.pay.wx.bean.WxTransactionType
* @return
*/
@Override
public TransactionType getTransactionType(String transactionType) {
return WxTransactionType.valueOf(transactionType);
}
},youdianPay {
@Override
public PayService getPayService(ApyAccount apyAccount) {
// TODO 2017/1/23 14:12 author: egan 集群的话,友店可能会有bug。暂未测试集群环境
WxYouDianPayConfigStorage wxPayConfigStorage = new WxYouDianPayConfigStorage();
wxPayConfigStorage.setKeyPrivate(apyAccount.getPrivateKey());
wxPayConfigStorage.setKeyPublic(apyAccount.getPublicKey());
// wxPayConfigStorage.setNotifyUrl(apyAccount.getNotifyUrl());
// wxPayConfigStorage.setReturnUrl(apyAccount.getReturnUrl());
wxPayConfigStorage.setSignType(apyAccount.getSignType());
wxPayConfigStorage.setPayType(apyAccount.getPayType().toString());
wxPayConfigStorage.setMsgType(apyAccount.getMsgType());
wxPayConfigStorage.setSeller(apyAccount.getSeller());
wxPayConfigStorage.setInputCharset(apyAccount.getInputCharset());
return new WxYouDianPayService(wxPayConfigStorage);
}
/**
* 根据支付类型获取交易类型
* @param transactionType 类型值
* @see com.egzosn.pay.wx.youdian.bean.YoudianTransactionType
* @return
*/
@Override
public TransactionType getTransactionType(String transactionType) {
return YoudianTransactionType.valueOf(transactionType);
}
};
public abstract PayService getPayService(ApyAccount apyAccount);
}
/**
* 支付响应对象
* @author: egan
* @email egzosn@gmail.com
* @date 2016/11/18 0:34
*/
public class PayResponse {
@Resource
private AutowireCapableBeanFactory spring;
private PayConfigStorage storage;
private PayService service;
private PayMessageRouter router;
public PayResponse() {
}
/**
* 初始化支付配置
* @param apyAccount 账户信息
* @see com.egzosn.pay.demo.entity.ApyAccount 对应表结构详情--》 pay-java-demo/resources/apy_account.sql
*/
public void init(ApyAccount apyAccount) {
//根据不同的账户类型 初始化支付配置
this.service = apyAccount.getPayType().getPayService(apyAccount);
this.storage = service.getPayConfigStorage();
buildRouter(apyAccount.getPayId());
}
/**
* 配置路由
* @param payId 指定账户id,用户多微信支付多支付宝支付
*/
private void buildRouter(Integer payId) {
router = new PayMessageRouter(this.service);
router
.rule()
.async(false)
.msgType(MsgType.text.name()) //消息类型
.payType(PayType.aliPay.name()) //支付账户事件类型
.transactionType(AliTransactionType.UNAWARE.name())//交易类型,有关回调的可在这处理
.interceptor(new AliPayMessageInterceptor(payId)) //拦截器
.handler(autowire(new AliPayMessageHandler(payId))) //处理器
.end()
.rule()
.async(false)
.msgType(MsgType.xml.name())
.payType(PayType.wxPay.name())
.handler(autowire(new WxPayMessageHandler(payId)))
.end()
.rule()
.async(false)
.msgType(MsgType.json.name())
.payType(PayType.youdianPay.name())
.handler(autowire(new YouDianPayMessageHandler(payId)))
.end()
;
}
private PayMessageHandler autowire(PayMessageHandler handler) {
spring.autowireBean(handler);
return handler;
}
public PayConfigStorage getStorage() {
return storage;
}
public PayService getService() {
return service;
}
public PayMessageRouter getRouter() {
return router;
}
}
```
#####2.支付处理器与拦截器简单实现
```java
/**
* 微信支付回调处理器
* Created by ZaoSheng on 2016/6/1.
*/
public class WxPayMessageHandler extends BasePayMessageHandler {
public WxPayMessageHandler(Integer payId) {
super(payId);
}
@Override
public PayOutMessage handle(PayMessage payMessage, Map<String, Object> context, PayService payService) throws PayErrorException {
//交易状态
if ("SUCCESS".equals(payMessage.getPayMessage().get("result_code"))){
/////这里进行成功的处理
return payService.getPayOutMessage("SUCCESS", "OK");
}
return payService.getPayOutMessage("FAIL", "失败");
}
评论1
最新资源