Android中如何集成微信支付,針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿(mǎn)足客戶(hù)于互聯(lián)網(wǎng)時(shí)代的鳳泉網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
注釋?zhuān)?要繼承微信支付和支付寶功能,必須要有以下配置信息,微信支付和支付寶支付申請(qǐng)的秘鑰等提供給開(kāi)發(fā)者(當(dāng)然也可以自己去申請(qǐng)),將配置信息放在靜態(tài)類(lèi)中,已被統(tǒng)一使用(安全考慮,微信支付寶推薦這些數(shù)據(jù)放在服務(wù)其中)
public class ParameterConfig { public static final String GANHOST = "http://101.226.197.11"; //服務(wù)器地址ip(根據(jù)自己替換) /** * 微信 */ <span > </span> //appid public static final String WX_APP_ID = "";// 自己填寫(xiě)自己項(xiàng)目的 // 商戶(hù)號(hào) public static final String WX_MCH_ID = "";// 自己填寫(xiě)自己項(xiàng)目的 // API密鑰,在商戶(hù)平臺(tái)設(shè)置 public static final String WX_API_KEY = "";// 自己填寫(xiě)自己項(xiàng)目的 //服務(wù)器回調(diào)接口 public static final String WX_notifyUrl = GANHOST+"/service/orderComplete";// 用于微信支付成功的回調(diào)(按自己需求填寫(xiě)) /** * 支付寶 */ // 商戶(hù)PID public static final String PARTNER = "";//自己填寫(xiě)自己項(xiàng)目的 // 商戶(hù)收款賬號(hào) public static final String SELLER = "";//自己填寫(xiě)自己項(xiàng)目的 // 商戶(hù)私鑰,pkcs8格式 public static final String RSA_PRIVATE = "";//自己填寫(xiě)自己項(xiàng)目的 public static final String aliPay_notifyURL = GANHOST+"/service/alipay/orderComplete";//支付寶支付成功的回調(diào) }
1.微信支付集成的前提條件 (1)首先要導(dǎo)入微信.jar包,從開(kāi)發(fā)平臺(tái)上可以下載到,然后放置在libs文件夾上。 (2)需要配置mainfest
<uses-permission android:name="android.permission.INTERNET"> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"> </uses-permission></uses-permission></uses-permission>
b.activity配置,這里com.gan.mypay改成自己的包名(如果自己包名與src下的package 名不一樣,這里要的是在manifest中配置的名稱(chēng),同樣需要在src建立以自己包 名為路經(jīng)的package,一定確保有這個(gè)activity)這個(gè)activity是微信支付結(jié)果要回調(diào)的activty。
android:name="com.gan.mypay.wxapi.WXPayEntryActivity" android:exported="true" android:launchMode="singleTop"/>
2.代碼繼承 (1)首先要有一個(gè)商品頁(yè)MainActivity,用來(lái)收集商品信息,這里需要后臺(tái)交互生成訂單,我們這里做的一個(gè)假的訂單。 MainActivity.java(這里用了xutils的注入)
@ContentView(R.layout.activity_main) public class MainActivity extends Activity { private Goods goods; private String username; private String mobile; private String adress; private int count; @ViewInject(R.id.product_ordsubmit_username) private TextView usernameTV; @ViewInject(R.id.product_ordsubmit_phone) private TextView phoneTV; @ViewInject(R.id.product_ordsubmit_adress) private TextView adressTV; @ViewInject(R.id.product_ordsubmit_desc) private TextView descTV; @ViewInject(R.id.product_ordsubmit_price) private TextView priceTV; @ViewInject(R.id.product_ordsubmit_intg) private TextView intgTV; @ViewInject(R.id.product_ordsubmit_count1) private TextView countTV1; @ViewInject(R.id.product_ordsubmit_count) private TextView countTV; @ViewInject(R.id.product_ordsubmit_intgtotal1) private TextView intgtotal1TV; @ViewInject(R.id.product_ordsubmit_intgtotal2) private TextView intgtotal2TV; @ViewInject(R.id.product_ordsubmit_pricetotal1) private TextView pricetotal1TV; @ViewInject(R.id.product_ordsubmit_pricetotal2) private TextView pricetotal2TV; @ViewInject(R.id.product_ordsubmit_counttotal) private TextView counttotalTV; @ViewInject(R.id.product_ordsubmit_ok) private Button okBtn; @ViewInject(R.id.product_ordsubmit_say_et) private TextView sayEt; @ViewInject(R.id.product_ordsubmit_img) private ImageView img; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ViewUtils.inject(this); goods = new Goods(); goods.costprice=100; goods.productid=692356222; goods.producttypeid=11; goods.productname="測(cè)試商品"; goods.discountprice=0.01; goods.productdescription="商品描述"; goods.companydesc="測(cè)試商戶(hù)簡(jiǎn)單描述"; goods.comanyadress="商戶(hù)地址未知"; goods.companyname="測(cè)試商戶(hù)"; goods.score=1; goods.status=1; goods.stock=300; count=1; initData(); initView(); } private void initData() { username ="客戶(hù)名稱(chēng)"; mobile = "13800380038"; adress="客戶(hù)地址"; } private void initView() { usernameTV.setText("收貨人:"+username); phoneTV.setText(mobile+""); adressTV.setText(adress); descTV.setText(goods.productdescription); priceTV.setText("¥"+goods.discountprice); intgTV.setText("積分:"+goods.score); countTV1.setText("X"+count); countTV.setText(count+""); intgtotal1TV.setText("共得到"+count*goods.score+"積分"); intgtotal2TV.setText("積分:"+count*goods.score); counttotalTV.setText("共"+count+"件"); pricetotal1TV.setText("¥"+Arith.mul(goods.discountprice, count)); pricetotal2TV.setText("¥"+Arith.mul(goods.discountprice, count)); //ImageLoader.getInstance().displayImage(goods.pic1, img); } /** * 增加數(shù)量 * @param v */ @OnClick(R.id.product_ordsubmit_count_add) public void add(View v) { count++; countTV1.setText("X"+count); countTV.setText(count+""); intgtotal1TV.setText("共得到"+count*goods.score+"積分"); intgtotal2TV.setText("積分:"+count*goods.score); counttotalTV.setText("共"+count+"件"); pricetotal1TV.setText("¥"+Arith.mul(goods.discountprice, count)); pricetotal2TV.setText("¥"+Arith.mul(goods.discountprice, count)); } /** * 減少數(shù)量 * @param v */ @OnClick(R.id.product_ordsubmit_count_sub) public void sub(View v) { if (count>1) { count--; countTV1.setText("X"+count); countTV.setText(count+""); intgtotal1TV.setText("共得到"+count*goods.score+"積分"); intgtotal2TV.setText("積分:"+count*goods.score); counttotalTV.setText("共"+count+"件"); pricetotal1TV.setText("¥"+Arith.mul(goods.discountprice, count)); pricetotal2TV.setText("¥"+Arith.mul(goods.discountprice, count)); } } /** * 提交訂單 * @param v */ @OnClick(R.id.product_ordsubmit_ok) public void submit(View v) { final OrderInfo orderInfo=new OrderInfo(); orderInfo.userid=13752; orderInfo.areacode=23; orderInfo.buildno="10"; orderInfo.roomno="1001"; orderInfo.producttypeid=goods.producttypeid; orderInfo.productid=goods.productid; orderInfo.amount=goods.discountprice;//單價(jià) orderInfo.account=count;//數(shù)量 orderInfo.totalamount=Arith.mul(goods.discountprice, count); //double offsetamount;//抵扣金額 orderInfo.score=count*goods.score; //int assessitem;//評(píng)價(jià)項(xiàng) //int assesslevel;//評(píng)價(jià)級(jí)別 //String assesscontent;//評(píng)價(jià)內(nèi)容 //long payid=;//支付編號(hào) orderInfo.status=2;//支付狀態(tài)待付款 orderInfo.type=11;//日用品 orderInfo.usermemo =sayEt.getText().toString();//業(yè)主備注 orderInfo.address =adress; orderInfo.productname =goods.productname;// orderInfo.desccontext =goods.productdescription;// orderInfo.outtradeno=System.currentTimeMillis()+""+orderInfo.userid; orderInfo.merchantid=goods.companyid; submitorder(orderInfo); } /** * 訂單提交成功,進(jìn)入付款界面 * @param orderInfo * @return */ private void submitorder(OrderInfo orderInfo) { Intent intent=new Intent(this, SelectPayTypeActivity.class); intent.putExtra("data", orderInfo); startActivity(intent); } }
(2)在mainactivty中點(diǎn)擊確認(rèn)按鈕調(diào)用支付方式選擇頁(yè)面SelectPayTypeActivity,用來(lái)發(fā)起支付選擇。 (3)根據(jù)支付方式調(diào)用對(duì)應(yīng)工具類(lèi)微信(WXpayUtil)
public class WXpayUtil { private IWXAPI api; private OrderInfo order; private Context context; private PayReq req; private Map<string,string> resultunifiedorder; private static final String TAG = "ewuye.online.SelectPayTypeActivity"; public WXpayUtil(Context mcontext,OrderInfo order){ //初始化微信支付 this.order=order; this.context=mcontext; if (TextUtils.isEmpty(ParameterConfig.WX_APP_ID) || TextUtils.isEmpty(ParameterConfig.WX_MCH_ID) || TextUtils.isEmpty(ParameterConfig.WX_API_KEY)) { new AlertDialog.Builder(context).setTitle("警告").setMessage("需要配置WX_APP_ID | WX_MCH_ID| WX_API_KEY\n請(qǐng)到ParameterConfig.java里配置") .setPositiveButton("確定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialoginterface, int i) { // ((Activity)context).finish(); } }).show(); return; } api = WXAPIFactory.createWXAPI(context, null); req = new PayReq(); //生成prepay_id GetPrepayIdTask getPrepayId = new GetPrepayIdTask(); getPrepayId.execute(); } /** * 用于獲取 * @author 95 * */ private class GetPrepayIdTask extends AsyncTask<void, string="">> { private ProgressDialog dialog; @Override protected void onPreExecute() { dialog = ProgressDialog.show(context, "提示", "正在獲取預(yù)支付訂單..."); } @Override protected void onPostExecute(Map<string,string> result) { if (dialog != null) { dialog.dismiss(); } resultunifiedorder=result; genPayReq(); } @Override protected void onCancelled() { super.onCancelled(); } @Override protected Map<string,string> doInBackground(Void... params) { String url = String.format("https://api.mch.weixin.qq.com/pay/unifiedorder"); String entity = genProductArgs(); Log.e("orion",entity); byte[] buf = httpPost(url, entity); String content = new String(buf); Log.e("orion", content); Map<string,string> xml=decodeXml(content); return xml; } } private void genPayReq() { req.appId = ParameterConfig.WX_APP_ID; req.partnerId = ParameterConfig.WX_MCH_ID; req.prepayId = resultunifiedorder.get("prepay_id"); req.packageValue = "prepay_id="+resultunifiedorder.get("prepay_id"); req.nonceStr = genNonceStr(); req.timeStamp = String.valueOf(genTimeStamp()); List<namevaluepair> signParams = new LinkedList<namevaluepair>(); signParams.add(new BasicNameValuePair("appid", req.appId)); signParams.add(new BasicNameValuePair("noncestr", req.nonceStr)); signParams.add(new BasicNameValuePair("package", req.packageValue)); signParams.add(new BasicNameValuePair("partnerid", req.partnerId)); signParams.add(new BasicNameValuePair("prepayid", req.prepayId)); signParams.add(new BasicNameValuePair("timestamp", req.timeStamp)); req.sign = genAppSign(signParams); Log.e("orion", signParams.toString()); sendPayReq(); } private void sendPayReq() { api.registerApp(ParameterConfig.WX_APP_ID); api.sendReq(req); } private String genProductArgs() { StringBuffer xml = new StringBuffer(); try { String nonceStr = genNonceStr(); xml.append(""); List<namevaluepair> packageParams = new LinkedList<namevaluepair>(); packageParams.add(new BasicNameValuePair("appid", ParameterConfig.WX_APP_ID)); packageParams.add(new BasicNameValuePair("body", order.productname)); packageParams.add(new BasicNameValuePair("mch_id", ParameterConfig.WX_MCH_ID)); packageParams.add(new BasicNameValuePair("nonce_str", nonceStr)); packageParams.add(new BasicNameValuePair("notify_url", ParameterConfig.WX_notifyUrl)); packageParams.add(new BasicNameValuePair("out_trade_no",genOutTradNo())); packageParams.add(new BasicNameValuePair("spbill_create_ip","127.0.0.1")); packageParams.add(new BasicNameValuePair("total_fee", (int)(order.totalamount*100)+"")); packageParams.add(new BasicNameValuePair("trade_type", "APP")); String sign = genPackageSign(packageParams); packageParams.add(new BasicNameValuePair("sign", sign)); String xmlstring =toXml(packageParams); return new String(xmlstring.toString().getBytes(), "ISO8859-1"); //return xmlstring; } catch (Exception e) { Log.e(TAG, "genProductArgs fail, ex = " + e.getMessage()); return null; } } private String genAppSign(List<namevaluepair> params) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < params.size(); i++) { sb.append(params.get(i).getName()); sb.append('='); sb.append(params.get(i).getValue()); sb.append('&'); } sb.append("key="); sb.append(ParameterConfig.WX_API_KEY); String appSign = getMessageDigest(sb.toString().getBytes()); Log.e("orion",appSign); return appSign; } private HttpClient getNewHttpClient() { try { KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); trustStore.load(null, null); SSLSocketFactory sf = new SSLSocketFactoryEx(trustStore); sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); registry.register(new Scheme("https", sf, 443)); ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); return new DefaultHttpClient(ccm, params); } catch (Exception e) { return new DefaultHttpClient(); } } private class SSLSocketFactoryEx extends SSLSocketFactory { SSLContext sslContext = SSLContext.getInstance("TLS"); public SSLSocketFactoryEx(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException { super(truststore); TrustManager tm = new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return null; } @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws java.security.cert.CertificateException { } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws java.security.cert.CertificateException { } }; sslContext.init(null, new TrustManager[] { tm }, null); } @Override public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException { return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose); } @Override public Socket createSocket() throws IOException { return sslContext.getSocketFactory().createSocket(); } } public byte[] httpPost(String url, String entity) { if (url == null || url.length() == 0) { Log.e(TAG, "httpPost, url is null"); return null; } HttpClient httpClient = getNewHttpClient(); HttpPost httpPost = new HttpPost(url); try { httpPost.setEntity(new StringEntity(entity)); httpPost.setHeader("Accept", "application/json"); httpPost.setHeader("Content-type", "application/json"); HttpResponse resp = httpClient.execute(httpPost); if (resp.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { Log.e(TAG, "httpGet fail, status code = " + resp.getStatusLine().getStatusCode()); return null; } return EntityUtils.toByteArray(resp.getEntity()); } catch (Exception e) { Log.e(TAG, "httpPost exception, e = " + e.getMessage()); e.printStackTrace(); return null; } } private String genOutTradNo() { Random random = new Random(); return getMessageDigest(String.valueOf(random.nextInt(10000)).getBytes()); } public Map<string,string> decodeXml(String content) { try { Map<string, string=""> xml = new HashMap<string, string="">(); XmlPullParser parser = Xml.newPullParser(); parser.setInput(new StringReader(content)); int event = parser.getEventType(); while (event != XmlPullParser.END_DOCUMENT) { String nodeName=parser.getName(); switch (event) { case XmlPullParser.START_DOCUMENT: break; case XmlPullParser.START_TAG: if("xml".equals(nodeName)==false){ //實(shí)例化student對(duì)象 xml.put(nodeName,parser.nextText()); } break; case XmlPullParser.END_TAG: break; } event = parser.next(); } return xml; } catch (Exception e) { Log.e("orion",e.toString()); } return null; } private String genNonceStr() { Random random = new Random(); return getMessageDigest(String.valueOf(random.nextInt(10000)).getBytes()); } private long genTimeStamp() { return System.currentTimeMillis() / 1000; } public String getMessageDigest(byte[] buffer) { char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; try { MessageDigest mdTemp = MessageDigest.getInstance("MD5"); mdTemp.update(buffer); byte[] md = mdTemp.digest(); int j = md.length; char str[] = new char[j * 2]; int k = 0; for (int i = 0; i < j; i++) { byte byte0 = md[i]; str[k++] = hexDigits[byte0 >>> 4 & 0xf]; str[k++] = hexDigits[byte0 & 0xf]; } return new String(str); } catch (Exception e) { return null; } } /** 生成簽名 */ private String genPackageSign(List<namevaluepair> params) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < params.size(); i++) { sb.append(params.get(i).getName()); sb.append('='); sb.append(params.get(i).getValue()); sb.append('&'); } sb.append("key="); sb.append(ParameterConfig.WX_API_KEY); String packageSign = getMessageDigest(sb.toString().getBytes()).toUpperCase(); Log.e("orion",packageSign); return packageSign; } private String toXml(List<namevaluepair> params) { StringBuilder sb = new StringBuilder(); sb.append("<xml>"); for (int i = 0; i < params.size(); i++) { sb.append("<"+params.get(i).getName()+">"); sb.append(params.get(i).getValue()); sb.append("<!--"+params.get(i).getName()+"-->"); } sb.append("</xml>"); Log.e("orion",sb.toString()); return sb.toString(); } }
微信回調(diào)的Activity
package com.gan.mypay.wxapi; import com.gan.mypay.ParameterConfig; import com.gan.mypay.R; import com.gan.mypay.SelectPayTypeActivity; import com.tencent.mm.sdk.constants.ConstantsAPI; import com.tencent.mm.sdk.modelbase.BaseReq; import com.tencent.mm.sdk.modelbase.BaseResp; import com.tencent.mm.sdk.openapi.IWXAPI; import com.tencent.mm.sdk.openapi.IWXAPIEventHandler; import com.tencent.mm.sdk.openapi.WXAPIFactory; import android.app.Activity; import android.app.AlertDialog; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.widget.Toast; public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler{ private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity"; private IWXAPI api; // private TextView reulttv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.wx_pay_result); api = WXAPIFactory.createWXAPI(this, ParameterConfig.WX_APP_ID); api.handleIntent(getIntent(), this); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); api.handleIntent(intent, this); } @Override public void onReq(BaseReq req) { } @Override public void onResp(BaseResp resp) { Log.d(TAG, "onPayFinish, errCode = " + resp.errCode); if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("提示"); //builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode))); builder.show(); Intent intent; int code = resp.errCode; switch (code) { case 0: Toast.makeText(this, "支付成功",0).show(); intent=new Intent(this,SelectPayTypeActivity.class); intent.putExtra("result", 0); startActivity(intent); finish(); break; case -1: Toast.makeText(this, "支付失敗",0).show(); intent=new Intent(this,SelectPayTypeActivity.class); intent.putExtra("result", -1); startActivity(intent); finish(); break; case -2: Toast.makeText(this, "支付取消",0).show(); intent=new Intent(this,SelectPayTypeActivity.class); intent.putExtra("result", -2); startActivity(intent); finish(); break; default: break; } } } }
關(guān)于Android中如何集成微信支付問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
網(wǎng)頁(yè)名稱(chēng):Android中如何集成微信支付
URL地址:http://www.rwnh.cn/article36/jdcipg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、網(wǎng)站建設(shè)、、企業(yè)網(wǎng)站制作、商城網(wǎng)站、定制開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)