微信官网申请好appid
1.将微信的libs下的jar拷贝到自己项目中
添加为依赖库
2.在Androidmanifest.xml中添加
<activity
android:name=".wxapi.WXPayEntryActivity"
android:launchMode="singleTop"
android:exported="true">
</activity>
3.再包名com.xxx.xxx.wxap下添加一个WXPayEntryActivity类:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.ftaro.adapter.FtPay;
import com.ftaro.adapter.Manager;
import com.ftaro.adapter.PublicArgs;
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;
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
private IWXAPI api;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
api = WXAPIFactory.createWXAPI(this, PublicArgs.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) {
// TODO Auto-generated method stub
}
@Override
public void onResp(BaseResp resp) {
Log.d("coyc", "onPayFinish, errCode = " + resp.errCode);
int pid=((FtPay)(Manager.getInstance().ftPay)).pid;
if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
if (resp.errCode == 0) {
// Toast.makeText(WXPayEntryActivity.this, “支付成功”, Toast.LENGTH_LONG).show();
Manager.notifyPay(1,pid,"");
}
else if (resp.errCode == -2) { // 用户取消
// Toast.makeText(WXPayEntryActivity.this, “用户取消”, Toast.LENGTH_LONG).show();
Manager.notifyPay(-2,pid,"");
}
else{
// Toast.makeText(WXPayEntryActivity.this, “支付失败”, Toast.LENGTH_LONG).show();
Manager.notifyPay(-1,pid,"");
}
}
finish();
}
}
4.调用支付签名都需要服务端做:
//微信支付
public void callWxPay() {
if (api.isWXAppInstalled() && !api.isWXAppSupportAPI()) { //判断微信版本是否支持支付
Toast.makeText(activity, “请您先安装微信客户端!”,Toast.LENGTH_SHORT).show();
return;
}
connectWetChatWeb();
}
private void connectWetChatWeb() {
new Timer().schedule(new TimerTask() {
@Override
public void run() {
params.add(new BasicNameValuePair(“method”, “xxxx”));
params.add(new BasicNameValuePair(“key”, “xxxxx”));
params.add(new BasicNameValuePair(“payType”, payType));//payType:1支付宝 0微信
params.add(new BasicNameValuePair(“orderId”,Manager.getInstance().js_order));//Manager.getInstance().js_order
String result = post(“自己服务端地址”);
Log.d(“gmf weixinresult:”,result);
String orderInfo=result;
try {
JSONObject data = new JSONObject(orderInfo.toString());
Log.d(“data123:”,data.toString());
String appId = data.getString(“appid”);
String partnerId = data.getString(“partnerid”);
String prepayId = data.getString(“prepayid”);
String packageValue = data.getString(“package”);
String nonceStr = data.getString(“noncestr”);
String timeStamp = data.getString(“timestamp”);
String sign = data.getString(“sign”);
Log.d("gmfappId",appId);
Log.d("gmfpartnerId",partnerId);
Log.d("gmfprepayId",prepayId);
Log.d("gmfpackageValue",packageValue);
Log.d("gmfnonceStr",nonceStr);
Log.d("gmftimeStamp",timeStamp);
Log.d("gmfsign",sign);
PayReq req = new PayReq();
req.appId = appId;
req.partnerId = partnerId;
req.prepayId = prepayId;
req.packageValue = packageValue;
req.nonceStr = nonceStr;
req.timeStamp = timeStamp;
req.sign = sign;
api.sendReq(req);
return;
}
catch (JSONException e) {
e.printStackTrace();
}
}
}, 0);
}