微信支付
- 微信支付是什么
- 微信支付的流程
微信支付正的很坑
1) 首先我们上套话微信支付是什么:微信支付是集成在微信客户端的支付功能,用户可以通过手机完成快速的支付流程。微信支付以绑定银行卡的快捷支付为基础,向用户提供安全、快捷、高效的支付服务。
2)微信支付的流程
微信的支付的流程大致可分为 两步走原则- (1)第一步呢就是调用微信支付提供de商户同意支付接口,获取第二步需要的预支付id,(prepred_id)
- **(2)调用成功之后会返回一个xml文件,把xml解析出来,获取预支付id,来调用微信支付的界面。
废话不多说我们上代码。**
终极代码
private class GetPrepayIdTask extends AsyncTask
调用同意支付网络请求的工具类
public class Util { private static final String TAG = "SDK_Sample.Util"; public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) { ByteArrayOutputStream output = new ByteArrayOutputStream(); bmp.compress(CompressFormat.PNG, 100, output); if (needRecycle) { bmp.recycle(); } byte[] result = output.toByteArray(); try { output.close(); } catch (Exception e) { e.printStackTrace(); } return result; } public static byte[] getHtmlByteArray(final String url) { URL htmlUrl = null; InputStream inStream = null; try { htmlUrl = new URL(url); URLConnection connection = htmlUrl.openConnection(); HttpURLConnection httpConnection = (HttpURLConnection)connection; int responseCode = httpConnection.getResponseCode(); if(responseCode == HttpURLConnection.HTTP_OK){ inStream = httpConnection.getInputStream(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } byte[] data = inputStreamToByte(inStream); return data; } public static byte[] inputStreamToByte(InputStream is) { try{ ByteArrayOutputStream bytestream = new ByteArrayOutputStream(); int ch; while ((ch = is.read()) != -1) { bytestream.write(ch); } byte imgdata[] = bytestream.toByteArray(); bytestream.close(); return imgdata; }catch(Exception e){ e.printStackTrace(); } return null; } private static final int MAX_DECODE_PICTURE_SIZE = 1920 * 1440; public static Bitmap extractThumbNail(final String path, final int height, final int width, final boolean crop) { Assert.assertTrue(path != null && !path.equals("") && height > 0 && width > 0); BitmapFactory.Options options = new BitmapFactory.Options(); try { options.inJustDecodeBounds = true; Bitmap tmp = BitmapFactory.decodeFile(path, options); if (tmp != null) { tmp.recycle(); tmp = null; } Log.d(TAG, "extractThumbNail: round=" + width + "x" + height + ", crop=" + crop); final double beY = options.outHeight * 1.0 / height; final double beX = options.outWidth * 1.0 / width; Log.d(TAG, "extractThumbNail: extract beX = " + beX + ", beY = " + beY); options.inSampleSize = (int) (crop ? (beY > beX ? beX : beY) : (beY
注意:
本人当时写微信支付遇到好多的坑,一一例举如下:
- 从导入的jar包开始,网上大概有两个版本的jar包,建议你从微信支付的官网demo里面考入
- 网络请求的工具类必须是post请求,来携带xml参数,可以用我上面写的工具类,已经验证过
- sign也就是签名,需要经过两次,第一次网络请求的签名,携带的参数,都是需要卸载xml里面一起签名的。xml里面携带的参数可参见微信开发文档(https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1)
- 如果请求成功,当返回成功的xml文件,用工具类里面的decoml解析成Map集合类,获取prepred_id。
<pre>private void sendPayRequest(Map<String,String> result) {
PayReq req = new PayReq();
req.appId = Constants.APP_ID;
req.partnerId = Constants.MUH_ID;//分配的商户号
req.prepayId = result.get("prepay_id");//核心参数:预支付订单号
req.packageValue = "Sign=WXPay";
String secre32 = WeiXinPayUtils.getRandomStringByLength(32);
req.nonceStr = secre32;//随机字符串,不长于32位。推荐随机数生成算法
String timeStap = WeiXinPayUtils.getTimeCuttent()+"";
req.timeStamp = timeStap;
Map<String,Object>map = new HashMap<String,Object>();
map.put("appid",Constants.APP_ID);
map.put("partnerid",Constants.MUH_ID);
map.put("prepayid",result.get("prepay_id"));
map.put("package","Sign=WXPay");
map.put("noncestr",secre32);
map.put("timestamp",timeStap);
req.sign = WeiXinPayUtils.getSign(map);
// 在支付之前,如果应用没有注册到微信,应该先调用IWXMsg.registerApp将应用注册到微信
//3.调用微信支付sdk支付方法
// 调微信支付
if(mApi.isWXAppInstalled()&&mApi.isWXAppSupportAPI()){
mApi.registerApp(Constants.APP_ID);
mApi.sendReq(req);
}else{
Toast.makeText(BuyActivity.this,"请先安装微信客户端",Toast.LENGTH_SHORT).show();
}
}</pre>
当第二次请求成功之后的结构即刻返回到WXPayEntryActivity这个类里面,注意这个类一定是在你当前报名的wxapi文件夹下
<pre>
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
///
// 此页面用来显示微信的支付回调结果
///
private static final String TAG = “MicroMsg.SDKSample.WXPayEntryActivity”;
private IWXAPI api;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wxentry);
api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
api.registerApp(Constants.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) {
}
@Override
public void onResp(BaseResp resp) {
if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("支付结果");
builder.setMessage("支付返回状态码:" + String.valueOf(resp.errCode));
builder.show();
}
}
}