打包后才能起调支付
微信支付成功起调
微信skd下载:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=11_1
签名APK下载:https://open.weixin.qq.com/zh_CN/htmledition/res/dev/download/sdk/Gen_Signature_Android.apk
1、首先到微信.开放平台创建账号,登录
微信开放平台:https://open.weixin.qq.com/
2、创建应用,申请微信支付权限
申请通过,填写 资料审核——审核通过如下:
3、微信支付 入驻申请通过会受到邮件如下:两个支付要用到的参数,商户号和APPID
这是点击前往操作,进入微信支付。商户平台
点击设置密钥,完成设置密钥并保持密钥,这个密钥是微信支付的第三个参数,也是最后一个参数。
只需APPID 、商户号、密钥;三个参数即可完成微信支付
4、微信支付六个工具类:
public class Constants {// appidpublic static final String APP_ID = "wx1201a018f113ce4";// 商户号public static final String MCH_ID = "1516571378";/*** 微信开放平台和商户约定的支付密钥** 注意:不能hardcode在客户端,建议genSign这个过程由服务器端完成*/public static final String APP_KEY = "01b2e2bd222e0db0c3765609ebc9b59f";}
、、、、、、、、、、、、、、、、、、、、、、、、、
public class MD5 {private MD5() {}public final static 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;}}
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {private IWXAPI api;// public static final String APP_ID = "wxf660cc9ea105ccae";public static final String APP_ID = "wx1201a018f11dcec6";@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);api = WXAPIFactory.createWXAPI(this, APP_ID);api.handleIntent(getIntent(), this);}@Overrideprotected void onNewIntent(Intent intent) {super.onNewIntent(intent);setIntent(intent);api.handleIntent(intent, this);}@Overridepublic void onReq(BaseReq req) {}@Overridepublic void onResp(BaseResp resp) {if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {//回调结果if (resp.errCode == 0) { Toast.makeText(WXPayEntryActivity.this,"支付成功", Toast.LENGTH_LONG).show();
// BaseApplication.isPay = true;//设置标志为支付完成
// if (BaseApplication.pay_Flag == 1) {
// RechargeActivity.updateData();
// } else if (BaseApplication.pay_Flag == 2) {
// UpgradePayActivity.updateData();
// } else if (BaseApplication.pay_Flag == 3) {
// BuyPeachActivity.updateData();
// }} else if (resp.errCode == -2) {Toast.makeText(WXPayEntryActivity.this,"支付取消", Toast.LENGTH_LONG).show();}finish();}}}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
public class PayWechatManager {private final String TAG = "PayWechatManager";private Context context;// APPIDpublic String APP_ID;// 商户号private String MCH_ID;// API密钥,在商户平台设置private String API_KEY;private String orderOn;private String productfeeName;private String price;private String orderType;// 支付结果回调(通知后台服务器)private String callBackUrl;public PayWechatManager(Context context) {this.context = context;}/*** @param APP_ID* @param MCH_ID* @param API_KEY* @param orderOn* @param productfeeName* @param price* @param callBackUrl* @param orderType //附加参数,不传为null。不能为""*/public void toWeChatPay(String APP_ID, String MCH_ID, String API_KEY, String orderOn, String productfeeName, String price, String callBackUrl, String orderType) {this.APP_ID = APP_ID;this.MCH_ID = MCH_ID;this.API_KEY = API_KEY;this.orderOn = orderOn;this.productfeeName = productfeeName;this.price = price;this.callBackUrl = callBackUrl;this.orderType = orderType;// 附加数据GetPrepayIdTask getPrepayId = new GetPrepayIdTask();getPrepayId.execute();}/*** 异步任务,提交订单(发起支付)*/class GetPrepayIdTask extends AsyncTask<Void, Void, Map<String, String>> {@Overrideprotected void onPreExecute() {// ui.showLoadingDialog();}@Overrideprotected Map<String, String> doInBackground(Void... params) {String url = String.format("https://api.mch.weixin.qq.com/pay/unifiedorder");// 创建微信订单信息String entity = createWeChatOrder();Log.i("lgq","微信得到==="+entity);// 生成微信订单byte[] buf = HttpWxUtile.httpPost(url, entity);// 这里容易出错误,错误信息请见:https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=9_3&index=5String content = new String(buf);Log.i("lgq","微信返回=----==="+content);Map<String, String> xml = decodeXml(content);return xml;}@Overrideprotected void onPostExecute(Map<String, String> result) {/** 用于存储支付信息的StringBuffer(用于打印订单信息) */StringBuffer sbInfo = new StringBuffer();sbInfo.append("prepay_id\n" + result.get("prepay_id") + "\n\n");createPayReq(result, sbInfo);}}/*** 根据您的订单信息 生成 微信产品支付订单信息*/private String createWeChatOrder() {StringBuffer xml = new StringBuffer();try {String nonceStr = genNonceStr();xml.append("</xml>");List<KeyValue> packageParams = new LinkedList<KeyValue>();packageParams.add(new KeyValue("appid", APP_ID));packageParams.add(new KeyValue("attach", orderType));packageParams.add(new KeyValue("body", "天鑫计费-网吧充值"));packageParams.add(new KeyValue("mch_id", MCH_ID));packageParams.add(new KeyValue("nonce_str", nonceStr));packageParams.add(new KeyValue("notify_url", callBackUrl));packageParams.add(new KeyValue("out_trade_no", orderOn));packageParams.add(new KeyValue("spbill_create_ip", "127.0.0.1"));packageParams.add(new KeyValue("total_fee", getTotalFee(price)));// packageParams.add(new KeyValue("total_fee", price));packageParams.add(new KeyValue("trade_type", "APP"));String sign = genPackageSign(packageParams);packageParams.add(new KeyValue("sign", sign));String xmlstring = toXml(packageParams);return xmlstring;// return new String(xmlstring.toString().getBytes(), "ISO8859-1");// return xmlstring;} catch (Exception e) {return null;}}/*** doingBackground*/private Map<String, String> decodeXml(final 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) {}return null;}// 将totalfee从单位(元)装换成整数的(分)private String getTotalFee(String total_fee) {total_fee = Double.valueOf(total_fee) * 100 / 1 + "";total_fee = total_fee.substring(0, total_fee.indexOf("."));return total_fee;}private String toXml(List<KeyValue> params) {StringBuilder sb = new StringBuilder();sb.append("<xml>");for (int i = 0; i < params.size(); i++) {sb.append("<" + params.get(i).getName() + ">");sb.append(String.valueOf(params.get(i).getValue()));sb.append("</" + params.get(i).getName() + ">");}sb.append("</xml>");return sb.toString();}private String genNonceStr() {Random random = new Random();return MD5.getMessageDigest(String.valueOf(random.nextInt(10000)).getBytes());}private long genTimeStamp() {return System.currentTimeMillis() / 1000;}/*** 创建订单支付请求*/private void createPayReq(final Map<String, String> orderInfo, StringBuffer sbInfo) {PayReq req = new PayReq();req.appId = APP_ID;req.partnerId = MCH_ID;req.prepayId = orderInfo.get("prepay_id");req.packageValue = "Sign=WXPay";req.nonceStr = genNonceStr();req.timeStamp = String.valueOf(genTimeStamp());List<KeyValue> signParams = new LinkedList<KeyValue>();signParams.add(new KeyValue("appid", req.appId));signParams.add(new KeyValue("noncestr", req.nonceStr));signParams.add(new KeyValue("package", req.packageValue));signParams.add(new KeyValue("partnerid", req.partnerId));signParams.add(new KeyValue("prepayid", req.prepayId));signParams.add(new KeyValue("timestamp", req.timeStamp));req.sign = genAppSign(sbInfo, signParams);sbInfo.append("sign\n" + req.sign + "\n\n");// 发起支付sendPayReq(req);}// 发起支付,当前页面结束private void sendPayReq(final PayReq req) {IWXAPI wxApi = WXAPIFactory.createWXAPI(context, null);wxApi.registerApp(req.appId);wxApi.sendReq(req);}/*** 生成签名*/@SuppressLint("DefaultLocale")private String genPackageSign(List<KeyValue> 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(API_KEY);String packageSign = MD5.getMessageDigest(sb.toString().getBytes()).toUpperCase();return packageSign;}@SuppressLint("DefaultLocale")private String genAppSign(StringBuffer sbInfo, List<KeyValue> 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(API_KEY);sbInfo.append("sign str\n" + sb.toString() + "\n\n");String appSign = MD5.getMessageDigest(sb.toString().getBytes()).toUpperCase();return appSign;}class KeyValue {private String name;private String value;public KeyValue(String name, String value) {this.name = name;this.value = value;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getValue() {return value;}public void setValue(String value) {this.value = value;}}}
5、配置微信activity
<activityandroid:name=".wxapi.WXEntryActivity"android:configChanges="keyboardHidden|orientation|screenSize"android:exported="true"android:screenOrientation="portrait"android:theme="@android:style/Theme.Translucent.NoTitleBar" /><activityandroid:name=".wxapi.WXPayEntryActivity"android:exported="true"android:launchMode="singleTop" />
6、起调微信支付:
demo链接:https://download.csdn.net/download/meixi_android/10726688
在线回复解决bug:qq1085220040
//微信支付
private void wechat() {new PayWechatManager(LiteActivity.this).toWeChatPay(Constants.APP_ID, Constants.MCH_ID,Constants.APP_KEY, getTime(), "天鑫计费-网吧充值", "0.01","http://yohoweb.91yohoo.com", null);
}