private static final String appid = PropKit.get("appid"); //應用ID private static final String mch_id = PropKit.get("mch_id"); //商戶號 private static final String paternerKey = PropKit.get("paternerkey"); //支付密鑰 private static final String notify_url = PropKit.get("notify_url"); //通知地址 public RetKit weixinPay(String ip,String amount,Map<String,Object> map){ // 統一下單文檔地址:https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1 //調起微信支付,統一下單 Map<String,String> params = new HashMap<String,String>(); params.put("appid", appid); params.put("mch_id", mch_id); params.put("body", "魅格"); params.put("out_trade_no", new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+(int)(Math.random()*90000+10000)); //訂單編號 params.put("total_fee", amount); //訂單金額 單位為分 if(StrKit.isBlank(ip)){ ip = "127.0.0.1"; } params.put("spbill_create_ip", ip); //終端ip params.put("trade_type", TradeType.APP.name()); //交易類型app:手機app支付,NATIVE:返回支付連接,可轉成二維碼客戶掃描支付 params.put("nonce_str", System.currentTimeMillis() / 1000 + ""); //隨機字符串 params.put("notify_url", notify_url); //支付后通知回調地址 params.put("attach", map.toString()); //附加數據,支付后原數據返回 String sign = PaymentKit.createSign(params, paternerKey); //生成簽名 params.put("sign", sign); //下單 String xmlResult = PaymentApi.pushOrder(params); System.out.println("下單后結果"+xmlResult); Map<String,String> result = PaymentKit.xmlToMap(xmlResult); String return_code = result.get("return_code"); String return_msg = result.get("return_msg"); if(StrKit.isBlank(return_code) || !"SUCCESS".equals(return_code)){ return RetKit.fail(return_msg); } String result_code = result.get("result_code"); if(StrKit.isBlank(result_code) || !"SUCCESS".equals(result_code)){ return RetKit.fail(return_msg); } return RetKit.okData(xmlResult); }
2,支付結果回調,微信不保證會一定返回結果通知,正常會調用多次
獲取支付結果
String xmlMsg = HttpKit.readData(getRequest());
public RetKit weixinPay_notify(String xmlMsg){ // 支付結果通用通知文檔: https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_7 System.out.println("支付通知"+xmlMsg); Map<String,String> result = PaymentKit.xmlToMap(xmlMsg); String result_code = result.get("result_code"); //總金額 String total_fee = result.get("total_fee"); Double amount = Double.valueOf(total_fee)*0.01; //商戶訂單號 String trade_no = result.get("out_trade_no"); //微信支付訂單號 String transaction_id = result.get("transaction_id"); //附近數據 String attach = result.get("attach"); //支付完成時間 //String time_end = result.get("time_end"); // 注意重復通知的情況,同一訂單號可能收到多次通知,請注意一定先判斷訂單狀態 // 避免已經成功、關閉、退款的訂單被再次更新 if(PaymentKit.verifyNotify(result, paternerKey)){ if("SUCCESS".equals(result_code)){ //添加各種流水 addBills(attach,Reward.access_weixin, amount.toString(), trade_no, transaction_id); } } return RetKit.ok(); }
支付結果通用通知文檔