微信支付

2020/12/03 10:27
阅读数 34

微信支付与微信登录 依赖一样

implementation 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+'

微信支付权限

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>

在支付的Activity里面的布局xml写入一个Button

<Button
        android:id="@+id/zf_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="支付"/>

在支付的Activity里面写一个加密的方法

 /**
     * MD5加密
     *
     * @param sourceStr
     * @return
     */
    public static String MD5(String sourceStr) {
   
   
        String result = "";
        try {
   
   
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(sourceStr.getBytes());
            byte b[] = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
   
   
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            result = buf.toString();
        } catch (NoSuchAlgorithmException e) {
   
   
            System.out.println(e);
        }
        return result;
    }

在Button点击事件里面请求数据

 StringBuilder sb = new StringBuilder();

                HashMap<String, Object> map = new HashMap<>();
                map.put("commodityId", 1001);
                result = "";

                sb.append("1719");
                sb.append(1001);
                sb.append("tech");
                result = sb.toString();
                String str = MD5(result);
                map.put("sign", str);
               //网络请求
                RetrofitMananger.getInstance().create().getCj(MyUrl.CJ,map)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<ResponseBody>() {
   
   
                            @Override
                            public void onSubscribe(Disposable d) {
   
   

                            }

                            @Override
                            public void onNext(ResponseBody responseBody) {
   
   
                                try {
   
   
                                    String string = responseBody.string();
                                    CJBean cjBean = new Gson().fromJson(string, CJBean.class);
                                    if (cjBean.getStatus().equals("0000")){
   
   
                                     //网络请求购买
                                        HashMap<String, Object> map1 = new HashMap<>();
                                        map1.put("orderId", "20201201103009829");
                                        map1.put("payType",1);
                                        RetrofitMananger.getInstance().create().getPay(MyUrl.ZF,map1)
                                                .subscribeOn(Schedulers.io())
                                                .observeOn(AndroidSchedulers.mainThread())
                                                .subscribe(new Observer<ResponseBody>() {
   
   
                                                    @Override
                                                    public void onSubscribe(Disposable d) {
   
   

                                                    }

                                                    @Override
                                                    public void onNext(ResponseBody responseBody) {
   
   
                                                        try {
   
   
                                                            String string1 = responseBody.string();
                                                            ZFBean zfBean = new Gson().fromJson(string1, ZFBean.class);
                                                            Log.i("TAG", string1);
                                                            if (zfBean.getStatus().equals("0000")){
   
   
                                                                PayReq req = new PayReq();
                                                                req.appId = zfBean.getAppId();
                                                                req.nonceStr = zfBean.getNonceStr();
                                                                req.partnerId = zfBean.getPartnerId();
                                                                req.prepayId = zfBean.getPrepayId();
                                                                req.sign = zfBean.getSign();
                                                                req.timeStamp = zfBean.getTimeStamp();
                                                                req.packageValue = zfBean.getPackageValue();
                                                                //去调微信
                                                                WXUtils.reg(ZFActivity.this).sendReq(req);
                                                            }
                                                        } catch (IOException e) {
   
   
                                                            e.printStackTrace();
                                                        }
                                                    }

                                                    @Override
                                                    public void onError(Throwable e) {
   
   

                                                    }

                                                    @Override
                                                    public void onComplete() {
   
   

                                                    }
                                                });

                                    }
                                } catch (IOException e) {
   
   
                                    e.printStackTrace();
                                }
                            }

                            @Override
                            public void onError(Throwable e) {
   
   

                            }

                            @Override
                            public void onComplete() {
   
   

                            }
                        });
              

map.put(“commodityId”, 1001);这个里面的1001是查询所有会员商品里面的commodityId
sb.append(“1719”);1719是userId

 //创建
    @POST
    Observable<ResponseBody> getCj(@Url String url, @QueryMap HashMap<String,Object> map);
    ///支付
    @POST
    Observable<ResponseBody> getPay(@Url String url, @QueryMap HashMap<String,Object> map);

第一层:
用用购买VIP里面的 map1.put(“orderId”, “20201201103009829”);
是下面打印出来的
第二层是支付
将工具类放到model里面
老样子在wxapi下新建WXPayEntryActivity
以下是代码:





public class WXPayEntryActivity extends AppCompatActivity implements IWXAPIEventHandler {
   
   
    private IWXAPI iwxapi;
    private static String APP_ID = "wxb3852e6a6b7d9516";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
   
   
        super.onCreate(savedInstanceState);
//        // 隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //接收到分享以及登录的intent传递handleIntent方法,处理结果
        iwxapi = WXAPIFactory.createWXAPI(this, APP_ID, false);
        iwxapi.handleIntent(getIntent(), this);
    }
    @Override
    public void onReq(BaseReq baseReq) {
   
   
    }
    @Override
    public void onResp(BaseResp baseResp) {
   
   
        if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
   
   
            switch (baseResp.errCode) {
   
   
                case BaseResp.ErrCode.ERR_OK:
                    runOnUiThread(new Runnable() {
   
   
                        @Override
                        public void run() {
   
   
                            Toast.makeText(WXPayEntryActivity.this, "成功", Toast.LENGTH_SHORT).show();
                            Log.e("dj", "支付成功");
                            WXPayEntryActivity.this.finish();
                        }
                    });
                    break;
                case BaseResp.ErrCode.ERR_USER_CANCEL:
                    Toast.makeText(WXPayEntryActivity.this, "取消支付", Toast.LENGTH_SHORT).show();
                    Log.e("dj", "用户取消支付");
                    WXPayEntryActivity.this.finish();
                    break;

                default:
                    break;
            }
        }
    }
}

清单注册

<activity android:name=".wxapi.WXPayEntryActivity"/>

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部