Android微信支付SDK


App對接微信調起微信支付需要在微信平台注冊,鑒別的標識就是App的包名,所以將申請的包名單獨打包成一個Apk文件,則在其他的App調起此Apk的時候同樣可以起到調用微信支付的功能。這樣就實現了調起微信支付的SDk的功效。操作實現中要將Apk文件安放在assets文件夾的目錄下。

當安裝好App之后,要將Apk文件保存到本地中

代碼實現如下:

private boolean saveApk() throws Exception {
        /** 首先默認個文件保存路徑 */
        sdcard = Environment.getExternalStorageState().equalsIgnoreCase(
                Environment.MEDIA_MOUNTED) ? Environment
                .getExternalStorageDirectory().getAbsolutePath()
                : "/mnt/sdcard";// 保存到SD卡
        apk_path = sdcard + "/paytend_wx/saveApk";// 保存的確切位置
        return copyApkFromAssets(mContext, "paytendSafe2Pay.apk", apk_path);

    }

private boolean copyApkFromAssets(Context context, String fileName,
            String path) {
        boolean copyIsFinish = false;
        try {
            File foder = new File(apk_path);
            if (!foder.exists()) {
                foder.mkdirs();
            }
            File myCaptureFile = new File(apk_path, fileName);
            if (!myCaptureFile.exists()) {
                myCaptureFile.createNewFile();
            }
            InputStream is = context.getAssets().open(fileName);
            FileOutputStream fos = new FileOutputStream(myCaptureFile);
            byte[] temp = new byte[1024];
            int i = 0;
            while ((i = is.read(temp)) > 0) {
                fos.write(temp, 0, i);
            }
            fos.close();
            is.close();
            copyIsFinish = true;
        } catch (IOException e) {
            return false;
        }
        return copyIsFinish;
    }

如果Apk已經安裝保存到指定的位置,接下來就是調起安裝界面將Apk安裝到手機中

代碼如下:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(
Uri.parse("file://" + apk_path
+ "/paytendSafe2Pay.apk"),
"application/vnd.android.package-archive");
mContext.startActivity(intent);

調起微信需要傳一些參數,考慮到參數的安全性,最終的參數需要從后台請求獲得。

參數的后台請求工作放在了Apk文件中。

在App端調用jar中的方法傳參給Apk文件:

    public Map<String, Object> CreatParameters() {
        //由商戶生成的訂單號
        out_trade_no = System.currentTimeMillis() + "";
        //分配給商戶id
        merchantId = Constants.MC_ID;
        // 需要支付的金額,單位是分
        total_fee = "1";
        //商戶后台支付狀態需要調用的接口
        sub_mch_notify_url = "http://test.paytend.com:7000/paytend_wxpay_demo/notify.jsp";
        // 商品的名字
        body = "青龍偃月刀";
        // 隨機字符串
        nonce_str = getRandomStr(20);
        mMap = new HashMap<String, Object>();
        mMap.put("out_trade_no", out_trade_no);
        mMap.put("merchantId", merchantId);
        mMap.put("total_fee", total_fee);
        mMap.put("sub_mch_notify_url", sub_mch_notify_url);
        mMap.put("body", body);
        mMap.put("nonce_str", nonce_str);
        // 簽名
        sign = getCommonSign(mMap, Constants.API_KEY);
        mMap.put("sign", sign);
        return mMap;
    }

之所以將調起Apk的方法封裝成jar文件,就是保證Apk的包名和Apk支付界面名的不泄露。在Apk中向后台發送請求,獲取參數,調起微信支付。

當微信支付成功后,Apk退出(Apk是沒有界面的)發送廣播通知App交易狀況。

 

總結流程如下:

點擊App上的微信支付按鈕,App傳一些固定參數到jar文件中,jar文件調起支付的Apk,並將這些參數傳遞給支付的Apk,支付的Apk向后台請求獲取支付的參數,將這些參數傳遞給微信的jar文件,通過微信jar文件中的方法調起微信支付。

當微信支付成功后,Apk退出(Apk是沒有界面的)發送廣播通知App交易狀況。

寫的比較亂,權當備忘了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM