微信、支付宝支付那点事

微信、支付宝支付那点事

公司要在MUI开发的APP里添加上支付功能,然后爬坑开始了。。

因为公司用的是Java语言开发的服务端,所以就要找Java版本的支付代码了
首先在dcloud的问答里搜索看有没有相关文章,找到了下面两篇有用的

第一篇是配置支付宝支付的,第二篇是我在下面发了一个回复求微信支付的代码(所以代码在回复里)

dcloud官方给的php代码地址:https://github.com/dcloudio/H5P.Server

下面说说我在爬坑的时候碰到的障碍
1. Java语言输出方法有print(),println() 切记一定不能用println() 这个方法输出后会换行,所以一直是失败状态,我开发的时候是支付宝报的错ALI10
2. 微信支付,从APP里请求到服务端接口,在接口里会调用一次微信的接口,此时涉及到一次签名(sign),切记在签名前发送给微信服务器的参数要按照a-z排列好,然后在去签名,完成之后请求微信支付接口,微信给返回一些XML数据,其中只有prepay_id有用,其他需要的参数基本上都是在微信的配置类里配置好了的,此时转换成的json格式数据写出的还有一次签名(sign),这个签名跟上面第一次的签名不一样,要记得在签名参数最后带上key
3. 支付宝支付的参数,地址类的字符串比如:notify_url等,不需要URLEncoder.encode(),参数都要加上""
4. xcode里,如果APP跳转到支付宝打开的是网页版的,而不是支付宝APP,就需要在xcode里配置plist文件添加下面这些代码

<key>LSApplicationQueriesSchemes</key><array><string>weixin</string><string>wechat</string><string>alipay</string><string>sinaweibo</string><string>weibosdk</string><string>tencentweiboSdkv2</string><string>weibosdk2.5</string><string>mqq</string><string>mqqOpensdkSSoLogin</string><string>mqqopensdkapiV2</string><string>mqqwpa</string><string>mqqopensdkapiV3</string><string>wtloginmqq2</string></array>

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

publicvoid alipayapi(HttpServletRequest request,HttpServletResponse response)throwsException{
    response.setContentType("text/plain; charset=UTF-8");PrintWriterout= 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 =newHashMap<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

publicclassRequestData{privateString appid;privateString body;privateString mch_id;privateString nonce_str;privateString notify_url;privateString out_trade_no;privateString sign;privateString spbill_create_ip;privateString total_fee;privateString trade_type;//getter,setter}
publicvoid alipayapi(HttpServletRequest request,HttpServletResponse response)throwsException{
    response.setContentType("text/plain; charset=UTF-8");PrintWriterout= 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 =newRequestData();
    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 =newHttpsRequest().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 =newStringBuffer();
    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());}

 

posted @ 2018-05-08 11:10  ido  阅读(606)  评论(1编辑  收藏  举报