一 . 微信支付的接入
1:在微信开发者平台上配置相关的信息(这里我就不多说了)直接上代码了
2:新建一个wxapi的包WXPayEntryActivity(提示:这里的包名和类名必须和你微信官方平台上的是先对应的,一般在主项目下)
package com.example.weixinzhifu.wxapi;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
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;
//https://blog.csdn.net/gf771115/article/details/71104143
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
private IWXAPI api;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
api = WXAPIFactory.createWXAPI(this, "你的APPID");
api.handleIntent(getIntent(), this);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
api.handleIntent(intent, this);
}
@Override
public void onResp(BaseResp resp) {
// if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
// AlertDialog.Builder builder = new AlertDialog.Builder(this);
// builder.setTitle(R.string.app_tip);
// builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode)));
// builder.show();
// }
if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
if (resp.errCode == 0) {
Toast.makeText(this, "支付成功", Toast.LENGTH_LONG).show();
} else {
Log.e("java", "onResp: " + resp.errCode);
Toast.makeText(this, "支付失败", Toast.LENGTH_LONG).show();
}
finish();
}
}
@Override
public void onReq(BaseReq baseReq) {
}
}
WXPayUtils中
import android.content.Context;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import java.security.MessageDigest;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Random;
public class WXPayUtils {
private IWXAPI iwxapi; //微信支付api
private WXPayBuilder builder;
private WXPayUtils(WXPayBuilder builder) {
this.builder = builder;
}
/**
* 调起微信支付的方法,不需要在客户端签名
**/
public void toWXPayNotSign(Context context) {
iwxapi = WXAPIFactory.createWXAPI(context, null); //初始化微信api
iwxapi.registerApp(builder.getAppId()); //注册appid appid可以在开发平台获取
Runnable payRunnable = new Runnable() { //这里注意要放在子线程
@Override
public void run() {
PayReq request = new PayReq(); //调起微信APP的对象
//下面是设置必要的参数,也就是前面说的参数,这几个参数从何而来请看上面说明
request.appId = builder.getAppId();
request.partnerId = builder.getPartnerId();
request.prepayId = builder.getPrepayId();
request.packageValue = builder.getPackageValue();
request.nonceStr = builder.getNonceStr();
request.timeStamp = builder.getTimeStamp();
request.sign = builder.getSign();
iwxapi.sendReq(request);//发送调起微信的请求
}
};
Thread payThread = new Thread(payRunnable);
payThread.start();
}
/**
* 调起微信支付的方法,需要在客户端签名
**/
public void toWXPayAndSign(Context context, String appid, final String key) {
iwxapi = WXAPIFactory.createWXAPI(context, null); //初始化微信api
iwxapi.registerApp(appid); //注册appid appid可以在开发平台获取
Runnable payRunnable = new Runnable() { //这里注意要放在子线程
@Override
public void run() {
PayReq request = new PayReq(); //调起微信APP的对象
//下面是设置必要的参数,也就是前面说的参数,这几个参数从何而来请看上面说明
request.appId = builder.getAppId();
request.partnerId = builder.getPartnerId();
request.prepayId = builder.getPrepayId();
request.packageValue = "Sign=WXPay";
// request.nonceStr = genNonceStr();
// request.timeStamp = String.valueOf(genTimeStamp());
request.nonceStr = builder.getNonceStr();
request.timeStamp = builder.getTimeStamp();
request.sign = builder.getSign();
//签名
LinkedHashMap<String, String> signParams = new LinkedHashMap<>();
signParams.put("appid", request.appId);
signParams.put("noncestr", request.nonceStr);
signParams.put("package", request.packageValue);
signParams.put("partnerid", request.partnerId);
signParams.put("prepayid", request.prepayId);
signParams.put("timestamp", request.timeStamp);
request.sign = genPackageSign(signParams, key);
iwxapi.sendReq(request);//发送调起微信的请求
}
};
Thread payThread = new Thread(payRunnable);
payThread.start();
}
/**
* 调起微信APP支付,签名
* 生成签名
*/
private String genPackageSign(LinkedHashMap<String, String> params, String key) {
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : params.entrySet()) {
sb.append(entry.getKey());
sb.append('=');
sb.append(entry.getValue());
sb.append('&');
}
sb.append("key=");
sb.append(key);
String packageSign = getMessageDigest(sb.toString().getBytes()).toUpperCase();
return packageSign;
}
/**
* md5加密
*
* @param buffer
* @return
*/
private 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;
}
}
/**
* 获取随机数
*
* @return
*/
private String genNonceStr() {
Random random = new Random();
return getMessageDigest(String.valueOf(random.nextInt(10000)).getBytes());
}
/**
* 获取时间戳
*
* @return
*/
private long genTimeStamp() {
return System.currentTimeMillis() / 1000;
}
public static class WXPayBuilder {
public String appId;
public String partnerId;
public String prepayId;
public String packageValue;
public String nonceStr;
public String timeStamp;
public String sign;
public WXPayUtils build() {
return new WXPayUtils(this);
}
public String getAppId() {
return appId;
}
public WXPayBuilder setAppId(String appId) {
this.appId = appId;
return this;
}
public String getPartnerId() {
return partnerId;
}
public WXPayBuilder setPartnerId(String partnerId) {
this.partnerId = partnerId;
return this;
}
public String getPrepayId() {
return prepayId;
}
public WXPayBuilder setPrepayId(String prepayId) {
this.prepayId = prepayId;
return this;
}
public String getPackageValue() {
return packageValue;
}
public WXPayBuilder setPackageValue(String packageValue) {
this.packageValue = packageValue;
return this;
}
public String getNonceStr() {
return nonceStr;
}
public WXPayBuilder setNonceStr(String nonceStr) {
this.nonceStr = nonceStr;
return this;
}
public String getTimeStamp() {
return timeStamp;
}
public WXPayBuilder setTimeStamp(String timeStamp) {
this.timeStamp = timeStamp;
return this;
}
public String getSign() {
return sign;
}
public WXPayBuilder setSign(String sign) {
this.sign = sign;
return this;
}
}
}
Mainactivity 中
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//在服务端签名
findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//假装请求了服务器 获取到了所有的数据,注意参数不能少
WXPayUtils.WXPayBuilder builder = new WXPayUtils.WXPayBuilder();
String partnerId = "";
String prepayId = "";
String nonceStr = "";
String timeStamp = "";
String sign = "";
String appId = "微信开发平台的APPID";
builder.setAppId(appId)
.setPartnerId(partnerId)
.setPrepayId(prepayId)
.setPackageValue("Sign=WXPay")
.setNonceStr(nonceStr)
.setTimeStamp(timeStamp)
.setSign(sign)
.build().toWXPayNotSign(MainActivity.this);
}
});
//在客户端签名
/*findViewById(R.id.btn2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//假装请求了服务端信息,并获取了appid、partnerId、prepayId,注意参数不能少
WXPayUtils.WXPayBuilder builder = new WXPayUtils.WXPayBuilder();
builder.setAppId("123")
.setPartnerId("213")
.setPrepayId("3213")
.setPackageValue("Sign=WXPay")
.build()
.toWXPayAndSign(MainActivity.this,"123","key");
}
});*/
}
}
网友评论