微信支付的那些坑

当然,我的是可以正常支付的了,先贴代码,然后再说有哪些坑

首先这个类是调起微信支付界面的:

public class PayActivity extends Activity {

   private IWXAPI api;
    public String process=null;
    int  userId;
    public   String  url;
    Map<String,String> map;
    int category;
    int mold;
    int oId;
    int level;
    int processint;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      //setContentView(R.layout.pay);
        //Toast.makeText(PayActivity.this, "category.....", Toast.LENGTH_SHORT).show();
      api = WXAPIFactory.createWXAPI(this, HfConstant.APP_ID);
        api.registerApp(HfConstant.APP_ID);

        //请求URL        //http://123.56.157.124/wxpay/prepay?uid={user_id}&oid={订单号}&total_fee=费用&category=费用类型
        //* 必传参数:uid=用户id; total_fee=缴费金额(); category=1年费 2押金 3服务费* 可选参数:oid=订单id, category=23时必传, category=1时可不传
        //category=1年费 2押金 3服务费    可选参数:oid=订单id, category=23时必传, category=1时可不传
        //例:http://123.56.157.124/wxpay/prepay?uid=8&oid=12&total_fee=3&category=1
                 url = "http://123.56.157.124/wxpay/prepay";


            try{
                    map=new HashMap<>();
                      category=getIntent().getIntExtra("category",0);
                      mold= ProductInfo.mold;//产品类别
                      // Toast.makeText(PayActivity.this, "category....."+category, Toast.LENGTH_SHORT).show();
                      userId= HfApplication.getInstance().getUserInfo().id;//用户id
                      level= HfApplication.getInstance().getUserInfo().level;//用户等级

                    Intent intent = getIntent();
                    if (intent != null) {
                         oId= (int) SpUtils.get(this,"myOid",0);//订单id
                    }

                    if (category==1){
                       // Toast.makeText(PayActivity.this, "category....."+category, Toast.LENGTH_SHORT).show();
                        map.put("total_fee",200000+"");
                        map.put("uid", userId+"");
                        map.put("category", category+"");
                        map.put("oid", oId+"");
                        //请求微信支付
                        reqyestWXpay(url, map);

                    }else {
                        getProcess(userId,oId);
                    }



                }catch(Exception e){
               Toast.makeText(PayActivity.this, "异常:"+e.getMessage(), Toast.LENGTH_SHORT).show();
                    finish();
            }

   }




    private void reqyestWXpay(String url, Map<String, String> map) {
        HfApplication.getInstance().httpRequestDirect(url, map, new StringCallback() {

            @Override
            public void onError(Call call, Exception e) {
                Toast.makeText(PayActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                finish();
            }

            @Override
            public void onResponse(String response) {
                if (response != null && response.length() > 0) {
                    try {
                        JSONObject json = new JSONObject(response);
                       // Toast.makeText(PayActivity.this, json+"", Toast.LENGTH_LONG).show();
                        Log.e("get server pay params:", response);
                        PayReq req = new PayReq();
                        req.appId = "wxda37f8587cd6023f";  // 测试用appId
                        //req.appId          = json.getString("appid");
                        req.partnerId = json.getString("partnerid");
                        req.prepayId = json.getString("prepayid");
                        req.nonceStr = json.getString("noncestr");
                        req.timeStamp = json.getString("timestamp");
                        req.packageValue = json.getString("package");
                        req.sign = json.getString("sign");
                        req.extData = "app data"; // optional
                        // 在支付之前,如果应用没有注册到微信,应该先调用IWXMsg.registerApp将应用注册到微信
                        //下面这句代码很重要,一定要加上
                        api.registerApp(HfConstant.APP_ID);
                        api.sendReq(req);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    Toast.makeText(PayActivity.this, "微信服务器请求错误", Toast.LENGTH_SHORT).show();
                    finish();
                }
            }
        });
    }



    private void getProcess(final int  userId,final int mOId) {
        String url2= "http://123.56.157.124/loan/api/order/show_order_all_details";
        Map<String,String> map1=new HashMap<>();
        map1.put("userId", userId+"");
        map1.put("oid", mOId+"");
        HfApplication.getInstance().httpRequestDirect(url2, map1, new StringCallback() {


            @Override
            public void onError(Call call, Exception e) {
                Toast.makeText(PayActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                finish();
            }

            @Override
            public void onResponse(String response) {
                if (response != null && response.length() > 0) {
                    try {
                        JSONObject jsonObject= new JSONObject(response);
                        process= jsonObject.getString("process");
                        processint= Integer.parseInt(process);



                        if (level == 0 || level == 2) {//用户
                            if (mold== 3){
                                if (processint==3) {
                                    map.put("total_fee", 150000 + "");
                                    map.put("uid", userId + "");
                                    map.put("category", category + "");
                                    map.put("oid", oId + "");
                                }else if (processint==5){
                                    String money = (String) SpUtils.get(PayActivity.this,"value","");
                                    float aa = Float.parseFloat(money);
                                    int fee= (int)aa*100;
                                    map.put("total_fee",fee+"");
                                    map.put("uid", userId+"");
                                    map.put("category", category+"");
                                    map.put("oid", oId+"");
                                }

                            }
                            if (mold== 1){
                                if (processint==3){
                                    map.put("total_fee",300000+"");
                                    map.put("uid", userId+"");
                                    map.put("category", category+"");
                                    map.put("oid", oId+"");
                                }
                                if (processint==5){
                                    String money = (String) SpUtils.get(PayActivity.this,"value","");
                                    float aa = Float.parseFloat(money);
                                    int fee= (int)aa*100;
                                    map.put("total_fee",fee+"");
                                    map.put("uid", userId+"");
                                    map.put("category", category+"");
                                    map.put("oid", oId+"");
                                }

                            }


                        } else if (level == 1) {//客户经理

                        }  else if (level == 3) {//渠道经理

                        } else if (level == 22) {//特殊服务商

                        }
                        //请求微信支付
                        reqyestWXpay(url, map);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    Toast.makeText(PayActivity.this, "服务器请求错误", Toast.LENGTH_SHORT).show();
                    finish();
                }
           }
        });

    }
}
代码有很多,但是主要的就两步:

1,通过后台提供的url得到6个参数,再加上appid一共7个

2,通过这7个参数调用微信支付的界面

所以

getProcess

这个方法大家可以忽略,是我自己项目要加的判断



接下来,还有一个重要的类,支付完成后的回调类:

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

   private static final String TAG = "WXPayEntryActivity";
   //http://123.56.157.124/loan/api/order/show_order_all_details?userId=28&oid=458

   private IWXAPI api;
   public String process=null;


   int userId;
   int processint;
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      api = WXAPIFactory.createWXAPI(this, HfConstant.APP_ID);
      api.registerApp(HfConstant.APP_ID);
      api.handleIntent(getIntent(), this);

         Intent intent= new Intent(this, HomeActivity.class);
         startActivity(intent);



   }

   @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) {
               int code = resp.errCode;
               String msg = "";
               switch (code) {
                  case 0:
                     msg = "支付成功!";
                     int mOId= (int) SpUtils.get(this,"myOid",0);
                     userId= HfApplication.getInstance().getUserInfo().id;
                     getProcess( userId,mOId);

                     SpUtils.put(getApplicationContext(),"due_status",1);

                     break;
                  case -1:
                     msg = "支付失败!";
                     break;
                  case -2:
                     msg = "您取消了支付!";
                     break;
                  default:
                     msg = "支付失败!";
                     break;
         }
         Toast.makeText(this, msg, Toast.LENGTH_LONG).show();

这个类相对比较简单,代码都是固定的



接下来说说------------坑--------------------

1,首先就是调用微信支付界面,注意调用微信支付一定要用debug.store签名打包,如果你检查自己的参数没有问题的话,但是请求不到支付界面,那八成就是签名的问题,

遇到这个问题,建议下载一个生成签名的工具,查看一下你的项目签名跟注册时的签名是否一致,只有一致的签名才可以调起支付


2,支付回调问题:

另外很揪心的一件事就是支付明明成功了,却不走回调方法,当时本人遇到这个问题,找了好久,最后发现缺少这样一句代码:

// 在支付之前,如果应用没有注册到微信,应该先调用IWXMsg.registerApp将应用注册到微信
//下面这句代码很重要,一定要加上
api.registerApp(HfConstant.APP_ID);
在请求支付的时候加上,我已经重点标记了


以上是我自己遇到的两个坑,当然了,还有其他问题,比如支付回调的类名和路径都是固定的等等,大家细心就可以避免了


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值