时间:2021-08-21 09:10:41 | 栏目:Android代码 | 点击:次
准备工作这里就不说了,包括签约和申请APPID,附上微信开放平台APP开发步骤,不懂的同学可以参考这里:
https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_5
上面的步骤很详细,这里主要说下调起支付的注意事项。按照上面文档中说的商户服务器生成支付订单,先调用统一下单API生成预付单,获取到prepay_id后将参数再次签名传输给APP发起支付。
相关代码如下:
/** * 商户服务器生成支付订单,先调用统一下单API(详见第7节)生成预付单,获取到prepay_id后将参数再次签名传输给APP发起支付。 */ //商品描述 String body = "iphone6s"; //随机字符串 String nonce_str = ResourceUtil.createRandomString(32); //通知地址 String notify_url = "http://www.weixin.qq.com/wxpay/pay.php"; //商户订单号 String out_trade_no = ResourceUtil.generateOutTradeNo(32); //总金额(单位分) int total_fee = 1; String url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; String sign = SignUtil.signByMD5("appid=" + Constants.APP_ID + "&body=" + body + "&mch_id=" + Constants.MCH_ID + "&nonce_str=" + nonce_str + "¬ify_url=" + notify_url + "&out_trade_no=" + out_trade_no + "&spbill_create_ip=127.0.0.1" + "&total_fee=" + total_fee + "&trade_type=APP" + "&key=" + Constants.KEY).toUpperCase(Locale.getDefault()); //参数以xml格式传递 String entity = "<xml><appid>" + Constants.APP_ID + "</appid><mch_id>" + Constants.MCH_ID + "</mch_id><nonce_str>" + nonce_str +"</nonce_str><sign>" + sign + "</sign><body>" + body + "</body><out_trade_no>" + out_trade_no + "</out_trade_no><total_fee>" + total_fee + "</total_fee><spbill_create_ip>127.0.0.1</spbill_create_ip><notify_url>http://www.weixin.qq.com/wxpay/pay.php</notify_url><trade_type>APP</trade_type></xml>"; Log.d("entity", entity); payButton.setEnabled(false); Toast.makeText(PayActivity.this, "获取订单中...", Toast.LENGTH_SHORT).show(); byte[] buf = Util.httpPost(url, entity); if (buf != null && buf.length > 0) { String content = new String(buf); Log.d("get server pay params:", content); OrderResult orderResult = ResourceUtil.parseXml(new ByteArrayInputStream(content.getBytes())); if (!TextUtils.equals(orderResult.getReturnCode(), "SUCCESS")) { Toast.makeText(PayActivity.this, orderResult.getReturnMsg(), Toast.LENGTH_SHORT).show(); return; } if (!TextUtils.equals(orderResult.getResultCode(), "SUCCESS")) { Toast.makeText(PayActivity.this, orderResult.getErrorDesc(), Toast.LENGTH_SHORT).show(); return; } //下单成功,调起支付 PayReq request = new PayReq(); request.appId = Constants.APP_ID; request.partnerId = Constants.MCH_ID; request.prepayId = orderResult.getPrepayId(); request.packageValue = "Sign=WXPay"; request.nonceStr = nonce_str; String timeStamp = String.valueOf(System.currentTimeMillis() / 1000); request.timeStamp = timeStamp; request.sign = SignUtil.signByMD5("appid=" + Constants.APP_ID + "&noncestr=" + nonce_str + "&package=Sign=WXPay" + "&partnerid=" + Constants.MCH_ID + "&prepayid=" + orderResult.getPrepayId() + "×tamp=" + timeStamp + "&key=" + Constants.KEY).toUpperCase(Locale.getDefault()); api.sendReq(request); payButton.setEnabled(true); } } });
相关参数说明在文档上都注明了,我这里面nonce_str和out_trade_no都是我随机生成的字符创,附上我的工具类,方便大家参考。
ResourceUtil.java
package com.xylpay.sdk.pay.uikit; import java.io.IOException; import java.io.InputStream; import java.util.Random; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import com.xylpay.sdk.pay.bean.OrderResult; import android.util.Xml; public class ResourceUtil { /** * 随机生成字符串 * @param length 字符串的长度 * @return 随机字符串 */ public static String createRandomString(int length) { String source = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; Random random = new Random(); StringBuilder builder = new StringBuilder(); for (int i = 0; i < length; i++) { int position = random.nextInt(source.length()); builder.append(source.charAt(position)); } return builder.toString(); } public static String generateOutTradeNo(int n) { StringBuilder builder = new StringBuilder(); Random random = new Random(); for (int i = 0; i < n; i++) { builder.append(random.nextInt(10)); } return builder.toString(); } public static OrderResult parseXml(InputStream is) { //PULL解析xml数据 XmlPullParser parser = Xml.newPullParser(); OrderResult orderResult = null; try { parser.setInput(is, "UTF-8"); int type = parser.getEventType(); while(type != XmlPullParser.END_DOCUMENT) { switch (type) { case XmlPullParser.START_DOCUMENT: break; case XmlPullParser.START_TAG: if (parser.getName().equals("xml")) { orderResult = new OrderResult(); } else if (parser.getName().equals("return_code")) { orderResult.setReturnCode(parser.nextText()); } else if (parser.getName().equals("return_msg")) { orderResult.setReturnMsg(parser.nextText()); } else if (parser.getName().equals("result_code")) { orderResult.setResultCode(parser.nextText()); } else if (parser.getName().equals("err_code_des")) { orderResult.setErrorDesc(parser.nextText()); } else if (parser.getName().equals("prepay_id")) { orderResult.setPrepayId(parser.nextText()); } break; case XmlPullParser.END_TAG: break; } type = parser.next(); } } catch(XmlPullParserException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return orderResult; } }
其中关于sign的生成,参数的顺序一定要严格按照上面的顺序加上key进行MD5加密,查看签名规范。
关于key的说明,这里的key是需要自己生成然后配置到微信开放平台的,参考商户支付密钥key的生成与设置进行配置,两边需要保持一致。另外,下单时,参数要以xml的格式来传递。
最后附上自己的签名算法:
SignUtil.java
package com.xylpay.sdk.pay.uikit; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /** * Created by Jackie on 2016/2/15. * * MD5加密 */ public class SignUtil { public static String signByMD5(String source) { byte[] bytes = null; try { MessageDigest digest = MessageDigest.getInstance("MD5"); digest.update(source.getBytes()); //更新摘要 bytes = digest.digest(); //再通过执行诸如填充之类的最终操作完成哈希计算。在调用此方法之后,摘要被重置。 } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } StringBuilder builder = new StringBuilder(bytes.length * 2); for (byte b : bytes) { /** * 0xFF默认是整形,一个byte跟0xFF相与会先将那个byte转化成整形运算 */ if ((b & 0xFF) < 0x10) { //如果为1位 前面补个0 builder.append("0"); } builder.append(Integer.toHexString(b & 0xFF)); } return builder.toString(); } }