Android APP微信支付开发的步骤

1.我们看官方文档的步骤

APP端开发步骤: https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_5

在微信开放平台(https://open.weixin.qq.com)上申请开发应用,管理中心-->创建移动应用,

需要填写资料信息,审核时间大概7个工作日。

审核通过后,微信开放平台会生成APP的唯一标识AppID和AppSecret(要保存好);

在应用详情中有获得微信支付能力选项,我们可以去申请开通,开通后微信会给我们微信支付商户平台的账号和密码

登录地址:https://pay.weixin.qq.com/index.php/core/home/login?return_url=%2F

我们可以看 https://jingyan.baidu.com/article/75ab0bcbbf7034d6864db2c3.html,微信支付商户平台-配置密钥。

由此我们在开发过程中需要的三个东西:AppID、商户平台账号MCH_ID、商户密钥API_KEY都有了;


开发Android的时候,设置应用签名和应用包名;

签名工具下载地址https://open.weixin.qq.com/zh_CN/htmledition/res/dev/download/sdk/Gen_Signature_Android.apk




2.在开发过程中请求接口的参数需要用到签名校验

微信支付接口签名的规则
https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=4_3


微信支付接口签名校验工具
官方文档地址   https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=20_1


具体编写代码 java 请看这篇文章
http://blog.csdn.net/xb12369/article/details/45716665




3.向微信支付后台请求统一下单接口,生成微信支付预支付订单prepayid,这个本来该后台来做的
Android 微信支付实现统一下单接口,官方文档 https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1
看文档,主要流程就是把20个左右的参数封装为XML格式发送到微信给的接口地址,这其中就用到第2步中的签名校验。
然后就可以获取到返回的内容了,如果成功里面就有支付所需要的预支付prepayid。



创建请求微信统一下单接口:https://api.mch.weixin.qq.com/pay/unifiedorder
返回结果model

public class WXPrepareModel {

    private String return_code;
    private String return_msg;
    private String appid;
    private String mch_id;
    private String nonce_str;
    private String sign;
    private String result_code;
    private String prepay_id;
    private String trade_type;

    public String getReturn_code() {
        return return_code;
    }

    public void setReturn_code(String return_code) {
        this.return_code = return_code;
    }

    public String getReturn_msg() {
        return return_msg;
    }

    public void setReturn_msg(String return_msg) {
        this.return_msg = return_msg;
    }

    public String getAppid() {
        return appid;
    }

    public void setAppid(String appid) {
        this.appid = appid;
    }

    public String getMch_id() {
        return mch_id;
    }

    public void setMch_id(String mch_id) {
        this.mch_id = mch_id;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public String getResult_code() {
        return result_code;
    }

    public void setResult_code(String result_code) {
        this.result_code = result_code;
    }

    public String getPrepay_id() {
        return prepay_id;
    }

    public void setPrepay_id(String prepay_id) {
        this.prepay_id = prepay_id;
    }

    public String getTrade_type() {
        return trade_type;
    }

    public void setTrade_type(String trade_type) {
        this.trade_type = trade_type;
    }

    public String getNonce_str() {
        return nonce_str;
    }

    public void setNonce_str(String nonce_str) {
        this.nonce_str = nonce_str;
    }


    @Override
    public String toString() {
        return "WXPrepareModel{" +
                "return_code='" + return_code + '\'' +
                ", return_msg='" + return_msg + '\'' +
                ", appid='" + appid + '\'' +
                ", mch_id='" + mch_id + '\'' +
                ", nonce_str='" + nonce_str + '\'' +
                ", sign='" + sign + '\'' +
                ", result_code='" + result_code + '\'' +
                ", prepay_id='" + prepay_id + '\'' +
                ", trade_type='" + trade_type + '\'' +
                '}';
    }
}


创建一个工具类 
public class WXPayManager {

    // MARK: - 微信参数配置
    public static String API_KEY = ""; // 商户秘钥
    public static String MCH_ID = "";  // 商户id

    public static String APPID = "";

    // 微信支付成功后向咱们后台的,回调地址
    public static String WXNotify_url = "";




    // MARK: - 随机字符串生成
    public static String getRandomString(int length) {
        //length表示生成字符串的长度
        String base = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }


    // MARK: - 请求xml组装
    public static String getRequestXml(SortedMap<String,Object> parameters){

        StringBuffer sb = new StringBuffer();
        sb.append("<xml>");
        Set es = parameters.entrySet();
        Iterator it = es.iterator();
        while(it.hasNext()) {
            Map.Entry entry = (Map.Entry)it.next();
            String key = (String)entry.getKey();
            String value = (String)entry.getValue();
            if ("attach".equalsIgnoreCase(key)||"body".equalsIgnoreCase(key)||"sign".equalsIgnoreCase(key)) {
                sb.append("<"+key+">"
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值