赞
踩
登录并配置,在商户平台上 - 产品中心 - 开通相关的产品,比如我这里使用的是 H5支付
//APPID
//mchid(商户号)
//API key(V3的密钥)
//privateKey(商户私钥)
//mchSerialNo (证书序列号)
//以上参数的配置说明可以参考微信文档 https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter2_6_1.shtml
<dependency>
<groupId>com.github.wechatpay-apiv3</groupId>
<artifactId>wechatpay-apache-httpclient</artifactId>
<version>0.2.2</version>
</dependency>
//此方法为微信的H5支付 //cashNum 微信支付的金额单位是 分,如果使用的元,在这里需要转换。 //orderNo 商户的订单号,在同一个商户号下必须是唯一的。 //redirect_url 支付成功后的跳转地址。 public ReturnJson weChatPay_H5(int cashNum, String orderNo,String redirect_url) throws Exception { //1.私钥为String字符串的方式来加载 //PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(new ByteArrayInputStream(WX_KEY.getBytes("utf-8"))); //2.通过加载文件的方式来读取私钥 String path = this.getClass().getClassLoader().getResources("./apiclient_key.pem").nextElement().getPath(); //加载商户私钥(私钥存储在文件) PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(new FileInputStream(path)); //加载平台证书(mchId:商户号,mchSerialNo:商户证书序列号,apiV3Key:V3密钥) AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(new WechatPay2Credentials(WX_MCH_ID, new PrivateKeySigner(WX_Serial_Number, merchantPrivateKey)),V3_key.getBytes("utf-8")); //初始化httpClient CloseableHttpClient builder = WechatPayHttpClientBuilder.create().withMerchant(WX_MCH_ID, WX_Serial_Number, merchantPrivateKey).withValidator(new WechatPay2Validator(verifier)).build(); //请求的微信支付地址 HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/h5"); //发送给微信的部分json参数 //amount 为订单的金额信息 //total 为订单总金额(单位:分) //currency 货币类型 //scene_info 支付的场景相关信息 //payer_client_ip 用户的终端ip,支持IPV4和IPV6 //h5_info h5支付的场景信息 //type 支付场景类型 //mch_id 商户号 //description 商品描述 //notify_url 回调通知地址(此地址是用户支付成功后,微信方要发送支付成功的通知) //out_trade_no 商户订单号 //goods_tag 商品标签 //appid appid String reqData = "{" + "\"amount\": {" + "\"total\": "+cashNum+"," + "\"currency\": \"CNY\"" + "}," + "\"scene_info\": {" + "\"payer_client_ip\":\"14.23.150.200\"," + "\"h5_info\": {" + "\"type\": \"Wap\"" + "}}," + "\"mchid\": \""+WX_MCH_ID+"\"," + "\"description\": \"优选商城\"," + "\"notify_url\": \""+AliPay_H5_NotifyUrl+"\"," + "\"out_trade_no\": \""+orderNo+"\"," + "\"goods_tag\": \"WXG\"," + "\"appid\": \""+WX_APP_ID+"\"" + "}"; StringEntity entity = new StringEntity(reqData,"utf-8"); entity.setContentType("application/json"); httpPost.setEntity(entity); httpPost.setHeader("Accept","application/json"); CloseableHttpResponse response = builder.execute(httpPost); try { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == 200) { String url = URLEncoder.encode(redirect_url, "GBK"); JSONObject js = JSONObject.parseObject(EntityUtils.toString(response.getEntity())); String aa = js.get("h5_url").toString(); js.put("h5_url",aa+"&redirect_url="+url); String payUrl = js.toJSONString(); return ReturnUtils.returnVal(CommonConstants.appCode.SUCCESS.get(),payUrl); } else if (statusCode == 204) { return ReturnUtils.returnVal(CommonConstants.appCode.SUCCESS.get(), EntityUtils.toString(response.getEntity())); } else { return ReturnUtils.returnVal(CommonConstants.appCode.DATAERROR.get(), EntityUtils.toString(response.getEntity())); } } finally { response.close(); } }
//用户支付成功后,通知的回调地址 //进行两步操作 1.接收微信发送的参数后,验签并解析。 //2.根据微信给到的参数,出发后续的操作,比如修改订单状态、发送站内信等等 //此接口微信可能会出现多次调用,请注意处理 @ResponseBody @RequestMapping(value = "/wechatRefundNotify",produces = {"application/json;charset=utf-8"}) public ResultEntity wechatRefundNotify(@RequestBody WeChatPayEntity weChatPayEntity) { AesUtil aesUtils = new AesUtil(this.V3_key.getBytes()); try { String string = aesUtils.decryptToString(weChatPayEntity.getResource().getAssociated_data().getBytes(), weChatPayEntity.getResource().getNonce().getBytes(), weChatPayEntity.getResource().getCiphertext()); JSONObject jsonObject = JSONObject.parseObject(string); //获取订单号 String out_trade_no = jsonObject.getString("out_trade_no"); //获取支付的订单状态, SUCCESS(支付成功) String trade_state = jsonObject.getString("trade_state"); //获取支付状态 SUCCESS if(refund_status.equals("SUCCESS")){ //进行支付成功后的相关操作..... //然后返回给微信指定的参数,否则微信可能会再次通知 return new ResultEntity("SUCCESS", "成功"); }else { //这里是支付状态错误..... //进行相关的操作 return new ResultEntity("FAIL", "失败"); } } catch (Exception e) { //错误处理 } return new ResultEntity("FAIL", "失败"); }
//WeChatPayEntity 类
public class WeChatPayEntity {
//通知创建时间
private String create_time;
//回调摘要
private String summary;
//通知数据类型
private String resiyrce_type;
//通知的资源数据(支付订单的相关信息)
private WeChatRequestEntities resource = new WeChatRequestEntities();
}
//WeChatRequestEntities 类 public class WeChatRequestEntities { //微信返回交易状态 //SUCCESS:支付成功 //REFUND:转入退款 //NOTPAY:未支付 //CLOSED:已关闭 //REVOKED:已撤销(付款码支付) //USERPAYING:用户支付中(付款码支付) //PAYERROR:支付失败(其他原因,如银行返回失败) private String trade_state; //商户订单号(我们对接H5支付接口时候传入的 订单号) private String out_trade_no; //附加数据 private String associated_data; //随机串 private String nonce; //数据密文 private String ciphertext; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。