参考代码 - APP拉起微信支付
参考 文档:
示例代码:
package com.weixin.sdk.pay.demo;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.webkit.CookieManager;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
public class H5PayDemoActivity extends Activity {
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = null;
try {
extras = getIntent().getExtras();
} catch (Exception e) {
finish();
return;
}
if (extras == null) {
finish();
return;
}
// 获取传入的付费界面URL
String url = null;
try {
url = extras.getString("url");
} catch (Exception e) {
finish();
return;
}
if (TextUtils.isEmpty(url)) {
// 测试H5支付,必须设置要打开的url网站
new AlertDialog.Builder(H5PayDemoActivity.this).setTitle(R.string.error)
.setMessage(R.string.error_missing_h5_pay_url)
.setPositiveButton(R.string.confirm, new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
finish();
}
}).show();
}
super.requestWindowFeature(Window.FEATURE_NO_TITLE);
LinearLayout layout = new LinearLayout(getApplicationContext());
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
layout.setOrientation(LinearLayout.VERTICAL);
setContentView(layout, params);
// 此 处使用 this 才能正常展示 webview 内的下拉选择元素
mWebView = new WebView(this);
params.weight = 1;
mWebView.setVisibility(View.VISIBLE);
layout.addView(mWebView, params);
WebSettings settings = mWebView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
// 启用二方/三方 Cookie 存储和 DOM Storage
// 注意:若要在实际 App 中使用,请先了解相关设置项细节。
CookieManager.getInstance().setAcceptCookie(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
CookieManager.getInstance().setAcceptThirdPartyCookies(mWebView, true);
}
settings.setDomStorageEnabled(true);
mWebView.setVerticalScrollbarOverlay(true);
mWebView.setWebViewClient(new MyWebViewClient());
mWebView.loadUrl(url);
// 启用 WebView 调试模式。
// 注意:请勿在实际 App 中打开!
WebView.setWebContentsDebuggingEnabled(true);
}
@Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
finish();
}
}
@Override
public void finish() {
super.finish();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(final WebView view, WebResourceRequest request) {
String url = request.getUrl().toString();
Log.i("UrlLoading", url);
if (!(url.startsWith("http") || url.startsWith("https"))) {
return true;
}
// 处理 艺爪付费的App支付请求
String EZREVENUE_BRIDGE_WEIXIN = "1001";
String EZREVENUE_URL1 = "https://revenue.ezboti.com";
String EZREVENUE_URL2 = "https://revenue.ezfuns.com"; //备用域名
if (url.startsWith(EZREVENUE_URL1 + "/paywall/bridge/") || url.startsWith(EZREVENUE_URL2 + "/paywall/bridge/")) {
Log.i("PaywallBridge", url);
Uri urlObj = request.getUrl();
// 检查是否是拉起微信支付
String bridgeType = urlObj.getQueryParameter("type");
if (EZREVENUE_BRIDGE_WEIXIN.equals((bridgeType))) {
final String orderString = urlObj.getQueryParameter("order_string");
final String callbackUrl = urlObj.getQueryParameter("callback_url");
Log.i("PaywallBridge", "Weixin orderString=" + orderString + " callbackUrl=" + callbackUrl);
// 拉起微信APP支付
PayReq request = new PayReq();
try {
JSONObject appPay = new JSONObject(orderString);
request.appId = appPay.getString("appId");
request.partnerId = appPay.getString("partnerId");
request.prepayId = appPay.getString("prepayId");
request.packageValue = appPay.getString("packageValue");
request.nonceStr = appPay.getString("nonceStr");
request.timeStamp = appPay.getString("timeStamp");
request.sign = appPay.getString("sign");
} catch (JSONException e) {
e.printStackTrace();
throw new RuntimeException(e.toString());
}
IWXAPI api = WXAPIFactory.createWXAPI(this, Constants.APP_ID, false);
api.sendReq(request);
// TODO: 处理微信支付SDK结果回调,参照微信SDK Sample
// https://pay.weixin.qq.com/doc/v3/merchant/4012073588#3.2.2.-%E3%80%90%E5%AE%A2%E6%88%B7%E7%AB%AF%E3%80%91OpenSDK%E8%B0%83%E8%B5%B7%E6%94%AF%E4%BB%98
// H5PayDemoActivity.this.runOnUiThread(new Runnable() {
// @Override
// public void run() {
// view.loadUrl(callbackUrl);
// }
// });
return true;
}
}
return false;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mWebView != null) {
mWebView.removeAllViews();
try {
mWebView.destroy();
} catch (Throwable t) {
}
mWebView = null;
}
}
}
源码参考 微信Open SDK for Android,可 联系我 获取完整 Android 示例项目代码。