一. H5支付配置
1.在微信商户平台中进行登录并申请相关功能和配置
1.1微信商户平台https://pay.weixin.qq.com/index.php/core/home/loginreturn_url=%2F
登录并配置,在商户平台上 - 产品中心 - 开通相关的产品,比如我这里使用的是 H5支付
1.2 然后配置相关的参数
//APPID//mchid(商户号)//API key(V3的密钥)//privateKey(商户私钥)//mchSerialNo (证书序列号)//以上参数的配置说明可以参考微信文档 https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter2_6_1.shtml
二.开发
2.1导入相关依赖
<dependency><groupId>com.github.wechatpay-apiv3</groupId><artifactId>wechatpay-apache-httpclient</artifactId><version>0.2.2</version></dependency>
2.2 代码编写
//此方法为微信的H5支付//cashNum 微信支付的金额单位是 分,如果使用的元,在这里需要转换。//orderNo 商户的订单号,在同一个商户号下必须是唯一的。//redirect_url 支付成功后的跳转地址。publicReturnJsonweChatPay_H5(int cashNum,String orderNo,String redirect_url)throwsException{//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(newFileInputStream(path));//加载平台证书(mchId:商户号,mchSerialNo:商户证书序列号,apiV3Key:V3密钥)AutoUpdateCertificatesVerifier verifier =newAutoUpdateCertificatesVerifier(newWechatPay2Credentials(WX_MCH_ID,newPrivateKeySigner(WX_Serial_Number, merchantPrivateKey)),V3_key.getBytes("utf-8"));//初始化httpClientCloseableHttpClient builder =WechatPayHttpClientBuilder.create().withMerchant(WX_MCH_ID,WX_Serial_Number, merchantPrivateKey).withValidator(newWechatPay2Validator(verifier)).build();//请求的微信支付地址HttpPost httpPost =newHttpPost("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 appidString 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 =newStringEntity(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();returnReturnUtils.returnVal(CommonConstants.appCode.SUCCESS.get(),payUrl);}elseif(statusCode ==204){returnReturnUtils.returnVal(CommonConstants.appCode.SUCCESS.get(),EntityUtils.toString(response.getEntity()));}else{returnReturnUtils.returnVal(CommonConstants.appCode.DATAERROR.get(),EntityUtils.toString(response.getEntity()));}}finally{
response.close();}}
2.3 编写H5支付回调接口
//用户支付成功后,通知的回调地址//进行两步操作 1.接收微信发送的参数后,验签并解析。//2.根据微信给到的参数,出发后续的操作,比如修改订单状态、发送站内信等等//此接口微信可能会出现多次调用,请注意处理@ResponseBody@RequestMapping(value ="/wechatRefundNotify",produces ={"application/json;charset=utf-8"})publicResultEntitywechatRefundNotify(@RequestBodyWeChatPayEntity weChatPayEntity){AesUtil aesUtils =newAesUtil(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");//获取支付状态 SUCCESSif(refund_status.equals("SUCCESS")){//进行支付成功后的相关操作.....//然后返回给微信指定的参数,否则微信可能会再次通知returnnewResultEntity("SUCCESS","成功");}else{//这里是支付状态错误.....//进行相关的操作returnnewResultEntity("FAIL","失败");}}catch(Exception e){//错误处理}returnnewResultEntity("FAIL","失败");}
2.4 微信回调接收的相关参数实体类
//WeChatPayEntity 类publicclassWeChatPayEntity{//通知创建时间privateString create_time;//回调摘要privateString summary;//通知数据类型privateString resiyrce_type;//通知的资源数据(支付订单的相关信息)privateWeChatRequestEntities resource =newWeChatRequestEntities();}
//WeChatRequestEntities 类publicclassWeChatRequestEntities{//微信返回交易状态//SUCCESS:支付成功//REFUND:转入退款//NOTPAY:未支付//CLOSED:已关闭//REVOKED:已撤销(付款码支付)//USERPAYING:用户支付中(付款码支付)//PAYERROR:支付失败(其他原因,如银行返回失败)privateString trade_state;//商户订单号(我们对接H5支付接口时候传入的 订单号)privateString out_trade_no;//附加数据privateString associated_data;//随机串privateString nonce;//数据密文privateString ciphertext;}
本文转载自: https://blog.csdn.net/qq_42444963/article/details/128805511
版权归原作者 几多荒唐乀 所有, 如有侵权,请联系我们删除。
版权归原作者 几多荒唐乀 所有, 如有侵权,请联系我们删除。