一、下载最新微信支付SDK demo 并运行过程
官网 https://pay.weixin.qq.com/wiki/doc/api/index.html
APP支付 https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_1
以下链接是当前可用链接(如若官方更新后请告知,感谢)
微信支付demo gradle版本(官方demo下载后无法运行,小编根据官方demo代码自己重新建的工程 )
https://download.csdn.net/download/w690333243/11008796
https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419319167&token=&lang=zh_CN
eclipse demo
https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=11_1
新建的官方demo运行效果图
二、按照官方文档,在包名下新建 wxapi目录,并新建Activity WXPayEntryActivity(activity名必须是这个WXPayEntryActivity,在此activity中接收充值相关的结果回调)
在 app 下的build.gradle 中添加依赖
//compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+'
compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:5.3.1'
WXPayEntryActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
public class WXPayEntryActivity extends BaseSwipeBackActivity implements IWXAPIEventHandler {
private final String TAG = "WXPayEntryActivity";
private IWXAPI api;
@Override
protected String getTitleText() {
return null;
}
@Override
protected void initViews(Bundle savedInstanceState) {
api = WXAPIFactory.createWXAPI(this, App.appId);
api.handleIntent(getIntent(), this);
}
@Override
public int getContentViewId() {
return R.layout.activity_wxpay_entry;
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
api.handleIntent(intent, this);
}
@Override
public void onReq(BaseReq baseReq) {
Log.i(TAG, "onReq: " + baseReq.toString());
}
/**
* @auther WangQX
* created at 2019/3/11 0011
* https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_12&index=2
* errCode = -1 //签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等
*/
@Override
public void onResp(BaseResp baseResp) {
Log.i(TAG, "onResp: ");
Log.i(TAG, "onPayFinish,errCode=" + baseResp.errCode);
if (baseResp.errCode == BaseResp.ErrCode.ERR_COMM) {
showToast(getResources().getString(R.string.recharge_failed));
} else if (baseResp.errCode == BaseResp.ErrCode.ERR_OK) {
showToast(getResources().getString(R.string.recharge_successful));
}
startActivity(MyWalletActivity.class);
finish();
}
}
请求微信充值方法:
data是服务器返回的支付相关信息的json
//微信支付
private void payWechat(final String data) {
final IWXAPI msgApi = WXAPIFactory.createWXAPI(this, null);
WeChatPayEntity weChatPayEntity = new Gson().fromJson(data, new TypeToken() {
}.getType());
if (weChatPayEntity == null) {
return;
}
String appId = weChatPayEntity.getAppid();
if (!TextUtils.isEmpty(appId)) {
// 将该app注册到微信
msgApi.registerApp(appId);
PayReq req = new PayReq();
App.appId = appId;
req.appId = appId;
req.partnerId = weChatPayEntity.getPartnerid();
req.prepayId = weChatPayEntity.getPrepayid();
req.packageValue = weChatPayEntity.getPackageX();
req.nonceStr = weChatPayEntity.getNoncestr();
req.timeStamp = weChatPayEntity.getTimestamp();
req.sign = weChatPayEntity.getPaySign();
boolean result = msgApi.sendReq(req);
Log.d(TAG, "payWechat: result:"+result);
}
}
在 清单文件AndroidManifest.xml中添加
出现的问题:
android 的App只能在第一次调起微信支付界面,且支付不一定成功(没验证是否能支付成功,只看到第二次无法调起支付界面了),原因:微信开放平台中填写的应用相关的包名、签名不正确
android studio 下 Terminal窗口中输入如下命令
keytool -list -keystore -v -keystore C:\Users\Administrator\Desktop\test.jks 查看签名
https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_5#
https://blog.csdn.net/xuchao_blog/article/details/58135338
https://blog.csdn.net/afanbaby/article/details/60885642
https://blog.csdn.net/Jarry_JR/article/details/80843901
https://blog.csdn.net/m_sicily/article/details/82493171
https://blog.csdn.net/u010083127/article/details/78968761
https://blog.csdn.net/u012552275/article/details/78758571/
https://blog.csdn.net/xinluqishi123/article/details/52234002
https://blog.csdn.net/li438730745/article/details/80494155
https://blog.csdn.net/ws1836300/article/details/53893102
https://www.jianshu.com/p/81c6c7d3390a
https://blog.csdn.net/hongcha36/article/details/78743535
https://blog.csdn.net/catshitone/article/details/73614321
https://blog.csdn.net/CLinuxF/article/details/79456207
https://blog.csdn.net/jay100500/article/details/52956000