您好,登錄后才能下訂單哦!
Android中如何集成微信支付,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
注釋: 要繼承微信支付和支付寶功能,必須要有以下配置信息,微信支付和支付寶支付申請的秘鑰等提供給開發(fā)者(當然也可以自己去申請),將配置信息放在靜態(tài)類中,已被統(tǒng)一使用(安全考慮,微信支付寶推薦這些數(shù)據(jù)放在服務其中)
public class ParameterConfig { public static final String GANHOST = "http://101.226.197.11"; //服務器地址ip(根據(jù)自己替換) /** * 微信 */ <span > </span> //appid public static final String WX_APP_ID = "";// 自己填寫自己項目的 // 商戶號 public static final String WX_MCH_ID = "";// 自己填寫自己項目的 // API密鑰,在商戶平臺設置 public static final String WX_API_KEY = "";// 自己填寫自己項目的 //服務器回調(diào)接口 public static final String WX_notifyUrl = GANHOST+"/service/orderComplete";// 用于微信支付成功的回調(diào)(按自己需求填寫) /** * 支付寶 */ // 商戶PID public static final String PARTNER = "";//自己填寫自己項目的 // 商戶收款賬號 public static final String SELLER = "";//自己填寫自己項目的 // 商戶私鑰,pkcs8格式 public static final String RSA_PRIVATE = "";//自己填寫自己項目的 public static final String aliPay_notifyURL = GANHOST+"/service/alipay/orderComplete";//支付寶支付成功的回調(diào) }
1.微信支付集成的前提條件 (1)首先要導入微信.jar包,從開發(fā)平臺上可以下載到,然后放置在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中配置的名稱,同樣需要在src建立以自己包 名為路經(jīng)的package,一定確保有這個activity)這個activity是微信支付結(jié)果要回調(diào)的activty。
android:name="com.gan.mypay.wxapi.WXPayEntryActivity" android:exported="true" android:launchMode="singleTop"/>
2.代碼繼承 (1)首先要有一個商品頁MainActivity,用來收集商品信息,這里需要后臺交互生成訂單,我們這里做的一個假的訂單。 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="測試商品"; goods.discountprice=0.01; goods.productdescription="商品描述"; goods.companydesc="測試商戶簡單描述"; goods.comanyadress="商戶地址未知"; goods.companyname="測試商戶"; goods.score=1; goods.status=1; goods.stock=300; count=1; initData(); initView(); } private void initData() { username ="客戶名稱"; mobile = "13800380038"; adress="客戶地址"; } 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;//單價 orderInfo.account=count;//數(shù)量 orderInfo.totalamount=Arith.mul(goods.discountprice, count); //double offsetamount;//抵扣金額 orderInfo.score=count*goods.score; //int assessitem;//評價項 //int assesslevel;//評價級別 //String assesscontent;//評價內(nèi)容 //long payid=;//支付編號 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); } /** * 訂單提交成功,進入付款界面 * @param orderInfo * @return */ private void submitorder(OrderInfo orderInfo) { Intent intent=new Intent(this, SelectPayTypeActivity.class); intent.putExtra("data", orderInfo); startActivity(intent); } }
(2)在mainactivty中點擊確認按鈕調(diào)用支付方式選擇頁面SelectPayTypeActivity,用來發(fā)起支付選擇。 (3)根據(jù)支付方式調(diào)用對應工具類微信(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請到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, "提示", "正在獲取預支付訂單..."); } @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){ //實例化student對象 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; } } } }
關于Android中如何集成微信支付問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業(yè)資訊頻道了解更多相關知識。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。