支付宝&微信支付的简单封装-EasyPay

支付宝&微信支付的简单封装-EasyPay

微信和支付宝的支付SDK有时候调试真的很让人抓狂,为了能够让支付变得顺利一点就自己写了个很简单的封装(Java版本),取名:EasyPay。

项目地址:https://github.com/liangdodo/easypay-sdk

注:由于本项目创建时间比较久远,可能与支付平台最新版本的接口会有出入,所以使用时请参考旧文档。

微信参考文档:https://pay.weixin.qq.com/wiki/doc/api/index.html

使用方法:

一、在你项目的pom.xml文件中添加依赖

<!--添加私有仓库-->
<repositories>
        <repository>
            <id>easypay-sdk-release</id>
            <name>easypay-sdk-release Repository</name>
            <url>http://nexus.kijk.top:7001/content/repositories/easypay-sdk-release/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
</repositories>

<!--添加easypay-sdk依赖-->
<dependencies>
         <dependency>
            <groupId>com.nossiac.jx</groupId>
            <artifactId>easypay-sdk</artifactId>
            <version>1.0-RELEASES</version>

            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-simple</artifactId>
                </exclusion>
            </exclusions>

        </dependency>
</dependencies>

二、初始配置

        //配置支付宝
        AlipayConfig alipayConfig=new AlipayConfig();
        alipayConfig.setAppId("支付宝APPID");
        alipayConfig.setAppPrivateKey("商户私钥");
        alipayConfig.setAlipayPublicKey("支付宝公钥");
        alipayConfig.setNotifyUrl("异步通知URL");

        //配置微信
        WxpayConfig wxpayConfig=new WxpayConfig();
        wxpayConfig.setAppID("微信APPID");
        wxpayConfig.setMchID("微信MchId");
        wxpayConfig.setCertPath("微信支付证书路径(退款等需要用到)");
        wxpayConfig.setKey("微信支付Key");
        wxpayConfig.setSignType(WXPayConstants.SignType.HMACSHA256);//签名方式
        wxpayConfig.setNotifyUrl("异步通知URL");

        //创建easyPayService对象
        EasyPayService easyPayService = new EasyPayServiceImpl();
        easyPayService.setAlipayConfig(alipayConfig);//设置支付宝配置参数
        easyPayService.setWxpayConfig(wxpayConfig);//设置微信配置参数

三、发起支付

        //填充和设置支付参数
        EasyPayRequest easyPayRequest=new EasyPayRequest();
        easyPayRequest.setTradeNo("123456789");//订单号
        easyPayRequest.setAmount(0.01F);//支付金额
        easyPayRequest.setSubject("测试支付");//支付标题
        easyPayRequest.setBody("附加内容");//附加内容
        easyPayRequest.setOpenid("xxx");//微信JsApi等支付时必传
        easyPayRequest.setProductId("xxx");//微信Native支付时必传
        easyPayRequest.setTimeout(600);//设置超时时间(秒)
        easyPayRequest.setReturnUrl("http://xxxxx.html");//支付宝网页支付付款完后要跳转到的的页面URL
        easyPayService.setEasyPayRequest(easyPayRequest);//设置请求数据对象
        easyPayService.setEasyPayType(EasyPayTypeEnum.WXPAY_NATIVE);//设置支付方式(支付方式附在本文末尾处)

        //发起支付请求
	Object obj= easyPayService.pay();//发起支付
        System.out.println(JSON.toJSONString(obj));//打印返回结果

四、回调通知:

    /**
     * 支付宝异步通知
     */
    @PostMapping(value = "/alipay/notify")
    public String alipayNotify(@RequestParam Map<String,String> notifyData) {
        //System.out.println(notifyMap);
        easyPayService.setEasyPayPlatform(EasyPayPlatformEnum.ALIPAY);//设置支付平台(支付平台附在本文末尾处)
        EasyPayNotify easyPayNotify=easyPayService.notify(notifyData);
        System.out.println(easyPayNotify);

        if(easyPayNotify!=null){
            AlipayNotify alipayNotify=easyPayNotify.getAlipayNotify();

            //支付成功
            if(alipayNotify.getTradeStatus().equals("TRADE_SUCCESS")){

                //注:支付宝只退款部分金额时也会触发回调通知,所以需要过滤掉退款触发的回调通知,(支付宝的奇葩逻辑....)
                if(
                        null!=notifyData.get("out_biz_no") &&
                        null!=notifyData.get("refund_fee") &&
                        null!=notifyData.get("gmt_refund")
                ){
                    return "success";
                }
                 //easyPayNotify和alipayNotify对象里有你需要的数据,请自行查看
                //这里写你的业务逻辑代码......
            }

            return "success";
        }

        return "fail";
    }


    /**
     * 微信异步通知
     */
    @PostMapping(value="/wxpay/notify",produces = MediaType.TEXT_XML_VALUE)
    public String wxpayNotify(@RequestBody String notifyData){
        easyPayService.setEasyPayPlatform(EasyPayPlatformEnum.WXPAY);//设置支付平台
        EasyPayNotify easyPayNotify=easyPayService.notify(notifyData);
        System.out.println(easyPayNotify);

        if(easyPayNotify!=null){
            //支付成功
            WxpayNotify wxpayNotify=easyPayNotify.getWxpayNotify();

            if(wxpayNotify.getResultCode().equals("SUCCESS")){
                //easyPayNotify和wxpayNotify对象里有你需要的数据,请自行查看
                //这里写你的业务逻辑代码......
            }

            return "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
        }

        return "<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
    }

五、订单退款

//订单退款
EasyRefundRequest easyRefundRequest=new EasyRefundRequest();
easyRefundRequest.setTradeNo("您的商户单号");//商户订单号
easyRefundRequest.setRefundNo("你的退款单号");//商户退款单号(商户系统自己生成)
easyRefundRequest.setTotalAmount(100.00);//该笔订单的原始总金额
easyRefundRequest.setRefundAmount(25.00);//该笔订单要退款的金额
easyRefundRequest.setRefundCause("退款原因");//退款原因
		
try {
			
    easyPayService.setEasyPayPlatform(EasyPayPlatformEnum.WXPAY);//设置支付平台
    EasyRefundResponse easyRefundResponse = easyPayService.refund(easyRefundRequest);//发起退款请求
			
    if (0 != easyRefundResponse.getCode()) {
        //代码不为0,表示退款失败
    }else{
        System.out.println(easyRefundResponse);
        //退款成功业务逻辑...
    }
			
}catch (EasyPayException e){
  System.out.println(e.getMessage());
}

六、订单查询

         //查询订单
	 EasyQueryRequest easyQueryRequest=new EasyQueryRequest();//创建查询对象
         easyQueryRequest.setTradeNo("您的商户订单号");//要查询的订单号
         //easyQueryRequest.setPayTradeNo("微信或支付宝订单号");//也可用支付平台的订单号进行查询

	 try {
		easyPayService.setEasyPayPlatform(EasyPayPlatformEnum.WXPAY);//设置支付平台
		EasyQueryResponse easyQueryResponse=easyPayService.orderQuery(easyQueryRequest);
		System.out.println(easyQueryResponse);
	 }catch (EasyPayException e){;
		System.out.println(e.getMessage());
	 }

七、退款查询

    //退款查询
    EasyQueryRequest easyQueryRequest=new EasyQueryRequest();
    easyQueryRequest.setTradeNo("您的商户订单号");//商户订单号
    easyQueryRequest.setRefundNo("您的商户退款单号");//商户退款单号
    
    try {
            easyPayService.setEasyPayPlatform(EasyPayPlatformEnum.WXPAY);//设置支付平台
            EasyQueryResponse easyQueryResponse=easyPayService.refundQuery(easyQueryRequest);
            System.out.println(easyQueryResponse);
        }catch (EasyPayException e){
            System.out.println(e.getMessage());
        }

附:

支付平台枚举:

EasyPayPlatformEnum.UNKNOWN //未知的支付平台

EasyPayPlatformEnum.ALIPAY //支付宝

EasyPayPlatformEnum.WXPAY //微信

支付方式枚举

EasyPayTypeEnum.UNKNOWN //未知的支付方式
EasyPayTypeEnum.ALIPAY_APP //支付宝app支付
EasyPayTypeEnum.ALIPAY_PAGE //支付宝page支付
EasyPayTypeEnum.ALIPAY_WAP //支付宝wap支付
EasyPayTypeEnum.ALIPAY_QRCODE //支付宝二维码支付

EasyPayTypeEnum.WXPAY_JSAPI //微信公众账号/小程序支付
EasyPayTypeEnum.WXPAY_MWEB //微信H5支付
EasyPayTypeEnum.WXPAY_NATIVE //微信Native支付(扫码支付)
EasyPayTypeEnum.WXPAY_APP //微信APP支付

发表回复