Android学习系列(28)--App集成支付宝(6)
发布时间:2021-06-07
发布时间:2021-06-07
6.提取核心调用代码
在AlixDemo.java同目录下新建AlixPay.java,来提取AlixDemo.java的核心代码:
1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 7 2 2
// the handler use to receive the pay result. private Handler mHandler =new Handler() { public void handleMessage(Message msg) { try { String strRet = (String) msg.obj; switch (msg.what) { case AlixId.RQF_PAY: { closeProgress(); BaseHelper.log(TAG, st
rRet); try { String memo ="memo="; int imemoStart =
2 strRet.indexOf("memo="); imemoStart += memo.length(); int memo = imemoEnd =
8 strRet.indexOf(";result="); strRet.substring(imemoStart, 9 imemoEnd); 3 0 1 3 2 3 3 4 int retVal = resultChecker.checkSign(); if (retVal BaseHelper.showDialog( mActivity, "提示", == ResultChecker resultChecker =new 3 ResultChecker(strRet);
3 ResultChecker.RESULT_CHECK_SIGN_FAILED) {
3 5 mActivity.getResources().getString( 3 6 R.string.check_sign_failed), 3 7 android.R.drawable.ic_dialog_alert); 3 8 3 提示", memo, 9 4 0 4 1 4 2 4 3 ", strRet, 4 4 4 5 4 6 4 7 4 8 4 9 5 0 // close the progress bar }; } } super.handleMessage(msg); }catch (Exception e) { e.printStackTrace(); } } break; } icon); } }catch (Exception e) { e.printStackTrace(); icon); }else { BaseHelper.showDialog(mActivity,"
BaseHelper.showDialog(mActivity," 提 示
5 1 5 2 5 3 5 4 5 5 5 6 5 5 5 9 6 0 6 1 6 6 3 6 4 6 5 6 6
void closeProgress() { try { if (mProgress !=null) { mProgress.dismiss(); mProgress =null; } }catch (Exception e) { e.printStackTrace(); } } public void pay() { MobileSecurePayHelper boolean mspHelper =new =
7 MobileSecurePayHelper(mActivity); isMobile_spExist 8 mspHelper.detectMobile_sp(); if (!isMobile_spExist) return; if (!checkInfo()) { BaseHelper.showDialog(mActivity,"提示", " 缺 少 partner 或 者 seller , ",
2 icon); return; } try { // prepare the order info. String orderInfo = getOrderInfo(); String signType = getSignType(); String strsign = sign(signType, orderInfo);
6 7 6 +"\"" +"&" 8 6 9 7 0 1 7 2 3 7 7 5 7 6 7 7 7 8 7 9 8 0 8 1 8 2 ==null }
strsign = URLEncoder.encode(strsign); String info = orderInfo +"&sign=" +"\"" + strsign + getSignType(); // start the pay. MobileSecurePayer msp =new MobileSecurePayer(); boolean bRet = msp.pay(info, mHandler,
7 AlixId.RQF_PAY, mActivity); if (bRet) { // show the progress bar to indicate that we have closeProgress(); mProgress =
7 started paying.
4 BaseHelper.showProgress(mActivity,null,"正在支付",false, true); }else ; }catch (Exception ex) { Toast.makeText(mActivity, R.string.remote_call_failed, Toast.LENGTH_SHORT).show(); }
private boolean checkInfo() { String partner = PartnerConfig.PARTNER; String seller = PartnerConfig.SELLER; if (partner ==null || partner.length() <=0 || seller
8 3 8 4 8 5 8 6 8 7 8 8 9 +"\""; 9 0 1 9 2 9 3 9 4 9 9 6 9 7 9 8 }
|| seller.length() <=0) return false; return true;
// get the selected order info for pay. String getOrderInfo() { String strOrderInfo ="partner=" +"\"" +
8 PartnerConfig.PARTNER +"\""; strOrderInfo +="&"; strOrderInfo +="seller=" +"\"" + PartnerConfig.SELLER strOrderInfo +="&"; strOrderInfo +="out_trade_no=" +"\"" +
9 getOutTradeNo() +"\""; strOrderInfo +="&"; //这笔交易价钱 strOrderInfo strOrderInfo +="&"; //这笔交易内容 strOrderInfo strOrderInfo +="&"; //这笔交易价钱 strOrderInfo +="total_fee=" +"\"" +"10.00" +"\""; strOrderInfo +="&"; strOrderI
nfo +="notify_url=" +"\"" +"/index.jsp" +="body=" +"\"" + +="subject=" +"\"" +
mActivity.getString(R.string.donate_subject) +"\"";
5 mActivity.getString(R.string.donate_body) +"\"";
9 +"\""; 9 1 0 0 1 0 1 0 2 1 0 3 1 0 4 1 0 5 1 0 6 1 0 7 1 0 8 1 0 9 // the OnCancelListener for lephone platform. static class AlixOnCancelListenerimplements DialogInterface.OnCancelListener { } // sign the order info. String sign(String signType, String content) { return Rsa.sign(content, PartnerConfig.RSA_PRIVATE); } // get the sign type we use. String getSignType() { String getSignType ="sign_type=" +"\"" +"RSA" +"\""; return getSignType; } java.util.Random r =new java.util.Random(); strKey = strKey + r.nextInt(); strKey = strKey.substring(0,15); return strKey; // get the out_trade_no for an order. String getOutTradeNo() { SimpleDateFormat Date date =new Date(); String strKey = format.format(date); format =new } return strOrderInfo;
1 SimpleDateFormat("MMddHHmmss");
1 1 0 1 1 1 1 1 2 1 1} 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 2 }
Activity mcontext; AlixOnCancelListener(Activity context) { mcontext = context; } public void onCancel(DialogInterface dialog) { mcontext.onKeyDown(KeyEvent.KEYCODE_BACK,null); }
0 1 2 1 1 2 2 1 2 3 1 2 4 1 2 5 1 2 6 1 2 7 1 2 8 1 2 9 1 3 0 1
3 1 1 3 2 1 3 3 1 3 4 1 3 5 1 3 6 1 3 7 1 3 8 1 3 9 1 4 0 1 4 1
1 4 2 1 4 3 1 4 4 1 4 5 1 4 6 1 4 7 1 4 8 1 4 9 1 5 0 1 5 1 1 5
2 1 5 3 1 5 4 1 5 5 1 5 6 1 5 7 1 5 8 1 5 9 1 6 0 1 6 1 1 6 2 1
6 3 1 6 4 1 6 5 1 6 6 1 6 7 1 6 8 1 6 9 1 7 0 1 7 1 1 7 2 1 7 3
1 7 4 1 7 5 1 7 6 1 7 7 1 7 8 1 7 9 1 8 0 1 8 1 1 8 2 1 8 3 1 8
4 1 8 5 1 8 6 1 8 7 1 8 8 1 8 9 1 9 0 1 9 1 1 9 2 1 9 3 1 9 4 1
这个类的pay方法就是支付的方法,最简单的不设置的话,调用方法如下: 如果没有安装支付宝,它会提示你安装,如果已经安装,它直接让你选择付款: