微信扫码(Native)支付

微信扫码支付
官网通道Native支付:https://pay.weixin.qq.com/static/product/product_intro.shtml?name=native
Native支付开发文档:https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=5_1

有点坑, 后台说让客户端接入, 于是硬着头皮就来官网看了一看:微信扫码(Native)支付_第1张图片
demo下载下来就是针对后台接入的,客户端实在是难以下手(但既然要接入,总不能不尝试一下吧,结果试了很长时间各种问题…)

好在万能的博主们总有办法,参考一篇比较旧博客,多次尝试总算完成了demo(原地址忘记保存了,非常抱歉 某位大佬)

依赖:

implementation 'com.github.wxpay:WXPay-SDK-Java:0.0.4'

核心代码:

//此类是配置类
public class WXConfig implements WXPayConfig {
	
	//这个证书并没有用, 在官方文档中会看到此接口不需要证书
    private byte[] certData;  
    private int type;

    public WXConfig(int type)  {
        this.type=type;
    }

    public String getAppID() {
        return "wx12333ed86ce5d123";
    }

    public String getMchID() {
        if (type==1) {
            return "1301235123";
        }
       return "123456";
    }

    public String getKey() {
        return "beijing12312345devmpinctrl12345";
    }

    public InputStream getCertStream() {
        ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }

    public int getHttpConnectTimeoutMs() {
        return 8000;
    }

    public int getHttpReadTimeoutMs() {
        return 10000;
    }

//在main类中使用
WXConfig config = null;
        try {
            config = new WXConfig(type);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mPay = new WXPay(config);

        Map data = new HashMap();
        System.out.println("pay bill ....................................");
        data = new HashMap();
        data.put("body", "zpd_test");
        data.put("out_trade_no", "111");
        data.put("auth_code", "111");
        data.put("total_fee", "1");
        data.put("spbill_create_ip", "123.12.12.123");

        final Map finalData = data;
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (count < mStrings.length) {
                    Log.d("111", "onClick: " + mStrings[count++]);
                }
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Map resp = mPay.microPay(finalData, 3000, 3000);

                            final String return_code = resp.get("return_code");
                            Log.d("1111", "run: " + return_code);

                            if (TextUtils.equals(return_code, "SUCCESS")) {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(MainActivity.this, return_code, Toast.LENGTH_SHORT).show();
                                    }
                                });
                            } else {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(MainActivity.this, return_code, Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();

            }
        });

你可能感兴趣的:(微信扫码(Native)支付)