微信支付

微信支付與微信登陸 依賴同樣java

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

微信支付權限android

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

在支付的Activity裏面的佈局xml寫入一個Buttonapi

<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
app

//建立
    @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
如下是代碼:





ide

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"/>
相關文章
相關標籤/搜索