王老狗博客电话15858213085

烟台网站优化_烟台seo_烟台网络推广_烟台网站建设_王老狗博客电话15858213085

上一篇: 支付宝:ali-native原始扫码支付是什么意思 下一篇:代码:请问html文档后面加个问号的是什么技术?

 

原文链接http://ask.dcloud.net.cn/article/458

下面是我Java服务端的代码,给大家分享一下
支付宝(使用了支付宝商户SDK)

public void alipayapi(HttpServletRequest request, HttpServletResponse response) throws Exception {
    response.setContentType("text/plain; charset=UTF-8");
    PrintWriter out = response.getWriter();
    ////////////////////////////////////请求参数//////////////////////////////////////

    //支付类型
    String payment_type = "1";
    //必填,不能修改
    //服务器异步通知页面路径
    String notify_url = ApplicationListener.getBasePath() + "pay/sdk/alipay/notify";
    //需http://格式的完整路径,不能加?id=123这类自定义参数

    //页面跳转同步通知页面路径
//        String return_url = URLEncoder.encode(ApplicationListener.getBasePath() + "pay/wap/alipay/return");
    //需http://格式的完整路径,不能加?id=123这类自定义参数,不能写成http://localhost/

    //商户订单号
    String out_trade_no = request.getParameter("orderId");
    //商户网站订单系统中唯一订单号,必填

    //订单名称
    String subject = "支付预定金";
    //必填

    //付款金额
    String total_fee = request.getParameter("total_fee");
    //必填

    //商品展示地址
    String show_url = ApplicationListener.getBasePath();
    //必填,需以http://开头的完整路径,例如:http://www.商户网址.com/myorder.html

    //订单描述
    String body = "订单支付定金";
    //选填

    //超时时间
    String it_b_pay = "1d";
    //选填

    //把请求参数打包成数组
    Map<String, String> sParaTemp = new HashMap<String, String>();
    sParaTemp.put("service", "mobile.securitypay.pay");
    sParaTemp.put("partner", AlipayConfig.partner);
    sParaTemp.put("seller_id", AlipayConfig.seller_id);
    sParaTemp.put("_input_charset", AlipayConfig.input_charset);
    sParaTemp.put("payment_type", payment_type);
    sParaTemp.put("notify_url", notify_url);
    sParaTemp.put("out_trade_no", out_trade_no);
    sParaTemp.put("subject", subject);
    sParaTemp.put("total_fee", total_fee);
    sParaTemp.put("show_url", show_url);
    sParaTemp.put("body", body);
    sParaTemp.put("it_b_pay", it_b_pay);
    String sHtmlText = "";
    for(Iterator iter = sParaTemp.keySet().iterator(); iter.hasNext();) {
        String name = (String) iter.next();
        String value = sParaTemp.get(name);
        sHtmlText += name + "=\"" + value + "\"&";
    }
    //建立请求
    System.out.println(sHtmlText);
    sHtmlText = sHtmlText.substring(0, sHtmlText.length()-1);
    String sign = RSA.sign(sHtmlText, AlipayConfig.PRIVATE, AlipayConfig.input_charset);
    String outText = sHtmlText + "&sign=\"" + URLEncoder.encode(sign, "UTF-8") + "\"&sign_type=\""+AlipayConfig.sign_type+"\"";
    out.print(outText);
}

微信支付(用到了微信Java版SDK,下载地址)
RequestData.java

public class RequestData {
    private String appid;
    private String body;
    private String mch_id;
    private String nonce_str;
    private String notify_url;
    private String out_trade_no;
    private String sign;
    private String spbill_create_ip;
    private String total_fee;
    private String trade_type;

    //getter,setter
}

public void alipayapi(HttpServletRequest request, HttpServletResponse response) throws Exception {
    response.setContentType("text/plain; charset=UTF-8");
    PrintWriter out = response.getWriter();

    //微信分配的公众账号ID(企业号corpid即为此appId)
    String appid = Configure.getAppid();
    //必填

    //商品或支付单简要描述
    String body = "预定金支付";
    //必填

    //微信支付分配的商户号
    String mch_id = Configure.getMchid();
    //必填

    //随机字符串,不长于32位。推荐随机数生成算法
    String nonce_str = RandomStringGenerator.getRandomStringByLength(32);
    //必填

    //接收微信支付异步通知回调地址
    String notify_url = ApplicationListener.getBasePath() + "pay/sdk/wxpay/notify";
    //必填

    //商户系统内部的订单号,32个字符内、可包含字母, 其他说明见商户订单号
    String out_trade_no = request.getParameter("orderId");
    //必填

    //签名,详见签名生成算法
    String sign = "";
    //必填

    //APP和网页支付提交用户端ip,Native支付填调用微信支付API的机器IP。
    String spbill_create_ip = IpUtil.getIpAddr(request);
    //必填

    //订单总金额,单位为分,详见支付金额
    Double total_fee_d = Double.parseDouble(request.getParameter("total_fee"));
    Double total_fee_s = total_fee_d * 100;
    String total_fee = total_fee_s.intValue() + "";
    //必填

    //取值如下:JSAPI,NATIVE,APP,详细说明见参数规定
    String trade_type = request.getParameter("trade_type");
    //必填

    //=============================以下参数 非必填 ===============================

    //商品名称明细列表
    String detail = "";
    //非必填

    //附加数据,在查询API和支付通知中原样返回,该字段主要用于商户携带订单的自定义数据
    String attach = "";
    //非必填

    //符合ISO 4217标准的三位字母代码,默认人民币:CNY,其他值列表详见货币类型
    String fee_type = "";
    //非必填

    //终端设备号(门店号或收银设备ID),注意:PC网页或公众号内支付请传"WEB"
    String device_info = "";
    //非必填

    //商品标记,代金券或立减优惠功能的参数,说明详见代金券或立减优惠
    String goods_tag = "";
    //非必填

    //订单生成时间,格式为yyyyMMddHHmmss,如2009年12月25日9点10分10秒表示为20091225091010。其他详见时间规则
    String time_start = "";
    //非必填

    //订单失效时间,格式为yyyyMMddHHmmss,如2009年12月27日9点10分10秒表示为20091227091010。其他详见时间规则
    //注意:最短失效时间间隔必须大于5分钟
    String time_expire = "";
    //非必填

    //trade_type=NATIVE,此参数必传。此id为二维码中包含的商品ID,商户自行定义。
    String product_id = "";
    //非必填

    //no_credit--指定不能使用信用卡支付
    String limit_pay = "";
    //非必填

    //trade_type=JSAPI,此参数必传,用户在商户appid下的唯一标识。openid如何获取,可参考【获取openid】。
    //企业号请使用【企业号OAuth2.0接口】获取企业号内成员userid,再调用【企业号userid转openid接口】进行转换
    String openid = "";
    //非必填

    RequestData reqData = new RequestData();
    reqData.setAppid(appid);
    reqData.setBody(body);
    reqData.setMch_id(mch_id);
    reqData.setNonce_str(nonce_str);
    reqData.setNotify_url(notify_url);
    reqData.setOut_trade_no(out_trade_no);
    reqData.setSpbill_create_ip(spbill_create_ip);
    reqData.setTotal_fee(total_fee);
    reqData.setTrade_type(trade_type);
    sign = Signature.getSign(reqData);
    reqData.setSign(sign);
    String result = new HttpsRequest().sendPost("https://api.mch.weixin.qq.com/pay/unifiedorder", reqData);

    System.out.println(result);

    Map map = XMLParser.getMapFromXML(result);
    String prepay_id = (String) map.get("prepay_id");
    System.out.println(prepay_id);

    String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
    String s="appid="+appid+"&noncestr="+nonce_str+"&package=Sign=WXPay"+"&partnerid="+
            mch_id+"&prepayid="+prepay_id+"&timestamp="+timestamp+"&key=" + Configure.getKey();
    String newSign = MD5.MD5Encode(s).toUpperCase();
    StringBuffer json = new StringBuffer();
    json.append("{\"appid\":\"");
    json.append(appid);
    json.append("\",\"noncestr\":\"");
    json.append(nonce_str);
    json.append("\",\"package\":\"");
    json.append("Sign=WXPay");
    json.append("\",\"partnerid\":\"");
    json.append(mch_id);
    json.append("\",\"prepayid\":\"");
    json.append(prepay_id);
    json.append("\",\"timestamp\":\"");
    json.append(timestamp);
    json.append("\",\"sign\":\"");
    json.append(newSign);
    json.append("\"}");
    System.out.println(json.toString());
    out.print(json.toString());
}


 

点击这里获取该日志的TrackBack引用地址

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

关于本文

您正在阅读的是:接入支付:java版本接入微信支付和支付宝支付实例
kevin482 发表于:2016-8-10 15:35:21
分类:生活点滴
关键词:支付宝  

博客作者

王老狗博客电话15858213085

最近发表

最新评论及回复

最近留言

控制面板

Search

网站分类

文章归档

图标汇集

点击这里给我发消息

鲁ICP备09047409号-3