java配合uniapp的微信支付商户转账等解决方案

Posted arygos

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java配合uniapp的微信支付商户转账等解决方案相关的知识,希望对你有一定的参考价值。

maven:

  <dependency>
      <groupId>com.github.wxpay</groupId>
      <artifactId>wxpay-sdk</artifactId>
      <version>0.0.3</version>
  </dependency>

java:

微信支付信息配置类:

public class MyWxpayConfig  implements WXPayConfig {
    private byte [] certData;

    public MyWxpayConfig() throws  Exception{
        InputStream certStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("cert/apiclient_cert.p12");
        this.certData = IOUtils.toByteArray(certStream);
        certStream.close();
    }

    public static final String APP_ID = 应用ID;
    public static final String APP_KEY = 支付key; 

  public static final String MCH_ID = 商户号;
  
   public static final String BODY = 用户界面展示内容;

  public static final String PARTNER_ID = "*******";

  public static final String GRANT_TYPE = "client_credential";

  public static String GATEURL = "https://api.mch.weixin.qq.com/pay/unifiedorder";
  
  public static String NOTIFY_URL = 回调接口地址;

  public static String ORDER_QUERY = "https://api.mch.weixin.qq.com/pay/orderquery";

   @Override
public String getAppID() { return APP_ID; }

   @Override
public String getMchID() { return MCH_ID; }

   @Override public String getKey() { return APP_KEY; }

   @Override
public InputStream getCertStream() { return null; }

   @Override
public int getHttpConnectTimeoutMs() { return 8000; }

   @Override
public int getHttpReadTimeoutMs() { return 10000; } }

 

微信支付实现类:


   @Service

   public class WxPayServiceImpl implements WxPayService {

  
  @Override
public Map<String, String> dounifiedOrder(订单信息,IP地址) throws Exception {      //本地订单信息操作略过 Map<String, String> fail = new HashMap<>(); MyWxpayConfig config = new MyWxpayConfig(); WXPay wxpay = new WXPay(config); Map<String, String> data = new HashMap<String, String>(); data.put("appid", MyWxpayConfig.APP_ID); data.put("mch_id", MyWxpayConfig.MCH_ID); data.put("nonce_str", WXPayUtil.generateNonceStr()); data.put("body", 用户界面信息); data.put("out_trade_no", UUID.randomUUID().toString().replaceAll("-", "")); data.put("total_fee", 订单金额);//String类型,以分为单位 data.put("spbill_create_ip", IP地址); data.put("notify_url", MyWxpayConfig.NOTIFY_URL); data.put("trade_type", "APP"); data.put("sign", WXPayUtil.generateSignature(data, MyWxpayConfig.APP_KEY)); Map<String,String>dataMap=new HashMap<String,String>(); Map<String,String> map = new HashMap<>(); try { Map<String, String> resp = wxpay.unifiedOrder(data); if("SUCCESS".equals(resp.get("result_code"))){ String nonce_str = (String)resp.get("nonce_str"); String prepay_id = (String)resp.get("prepay_id"); Long time =System.currentTimeMillis()/1000; map.put("appid",config.getAppID()); map.put("partnerid",config.getMchID()); map.put("package","Sign=WXPay"); map.put("noncestr",nonce_str); map.put("timestamp",time+""); map.put("prepayid",prepay_id); map.put("sign",sign); } resp.put("time",System.currentTimeMillis()/1000+""); return map; } catch (Exception e) {
       
fail.put("failMsg",e.toString());
       return fail;
     }
  }

 

微信商户转账到个人实现类

private static final String TRANSFERS_PAY = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers"; 

    private static final String TRANSFERS_PAY_QUERY = "https://api.mch.weixin.qq.com/mmpaymkttransfers/gettransferinfo"; 

    private static final String APP_ID = MyWxpayConfig.APP_ID;

    private static final String MCH_ID = MyWxpayConfig.MCH_ID;
public Map<String,String> wxSendWallet (IP地址) {boolean tansferStatus=false;
            String responseInfo="";
            String xml = wxSendWallet(用户openId,金额,IP地址 );
            try {
                //指定读取证书格式为PKCS12
                KeyStore keyStore = KeyStore.getInstance("PKCS12");
                InputStream instream = Thread.currentThread().getContextClassLoader().getResourceAsStream("cert/apiclient_cert.p12");
                try {
                    keyStore.load(instream, MyWxpayConfig.MCH_ID.toCharArray());
                } finally {
                    instream.close();
                }
                SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, MyWxpayConfig.MCH_ID.toCharArray()).build();
                SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                sslcontext, new String[]{"TLSv1"}, null, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
                CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
                HttpPost httppost = new HttpPost("https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers");
                httppost.addHeader("Content-Type", "text/xml");
                StringEntity se = new StringEntity(xml, "UTF-8");
                httppost.setEntity(se);
                CloseableHttpResponse responseEntry = httpclient.execute(httppost);
                try {
                    HttpEntity entity = responseEntry.getEntity();
                    if (entity != null) {
                        SAXReader saxReader = new SAXReader();
                        Document document = saxReader.read(entity.getContent());
                        Element rootElt = document.getRootElement();
                        String resultCode = rootElt.elementText("result_code");
                        if (resultCode.equals("SUCCESS")) {
                            tansferStatus=true;
                 //成功后操作
} else { //失败后操作 responseInfo=rootElt.elementText("err_code_des"); } } EntityUtils.consume(entity); } catch (Exception e) { } finally { responseEntry.close(); } } catch (Exception e) { } if(tansferStatus==true){ } HashMap<String, Object> responseBody = new HashMap<>(); responseBody.put("status",tansferStatus); responseBody.put("responseInfo",responseInfo); return responseBody; } public static String wxSendWallet( 用户openId,金额,IP地址) { String data = null; try { String nonceStr = WXPayUtil.generateNonceStr(); SortedMap<String,String> parameters = new TreeMap<String,String>(); parameters.put("mch_appid",APP_ID ); parameters.put("mchid", MCH_ID); parameters.put("nonce_str", nonceStr); String outTradeNo = UUID.randomUUID().toString().replaceAll("-", ""); parameters.put("partner_trade_no", outTradeNo); parameters.put("openid", 用户openId); parameters.put("check_name", "NO_CHECK"); parameters.put("amount", 金额); parameters.put("spbill_create_ip", IP地址); parameters.put("desc", 商家名称); //签名 parameters.put("sign", WXPayUtil.generateSignature(parameters, MyWxpayConfig.APP_KEY)); data = WXPayUtil.mapToXml(parameters); } catch (Exception e) { e.printStackTrace(); } return data; } //元转分 public String changeY2F(double price) { int amount= (int) (price * 100); return String.valueOf(amount); } }

uniapp调起微信支付

that.$myRequest({
      url: ‘/Pay/wxOrder‘,
      methods: ‘post‘,
      data: { 订单信息 },
      })
    .then(res
=> { uni.requestPayment({   provider: ‘wxpay‘, orderInfo: 后台返回的微信支付参数, success: (e) => {
          成功后的操作
        }, fail: (e) => {
          失败后的操作    }, complete: ()
=> {
          完成后的操作         }        })     }).
catch(err => { });

 

以上是关于java配合uniapp的微信支付商户转账等解决方案的主要内容,如果未能解决你的问题,请参考以下文章

微信公众号给微信用户转账的Java实现

商家转账到零钱批次已存在

牛逼的uniapp+Java微信小程序商城来了

微信支付

uniapp 公众号支付

微信支付品台商户号未签约什么意思