微信支付

2020/12/02 11:24
阅读数 24

微信支付与微信登录 依赖一样

implementation 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+'

微信支付权限

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/>

购买VIP签名参数处理
下单的签名规则:将 userId、commodityId这两个参数的值和’tech’这个单词进行字符串拼接,全部小写就行,然后再进行MD5加密,顺序不可以乱。

例如参与拼接的参数值为: ‘25’+’1001’+’tech’,其中25是userId,1001是commodityId,拼接完后为”251001tech”,然后对这个字符串进行MD5加密,加密方案可参考以下代码。

 /**
 *  MD5加密
 * @param sourceStr
 * @return
 */
public static String MD5(String sourceStr) {
    String result = "";
    try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        md.update(sourceStr.getBytes());
        byte b[] = md.digest();
        int i;
        StringBuffer buf = new StringBuffer("");
        for (int offset = 0; offset < b.length; offset++) {
            i = b[offset];
            if (i < 0)
                i += 256;
            if (i < 16)
                buf.append("0");
            buf.append(Integer.toHexString(i));
        }
        result = buf.toString();
    } catch (NoSuchAlgorithmException e) {
        System.out.println(e);
    }
    return result;
}

加密之后请求接口获取订单号

if (wei.isChecked()) {
             button.setOnClickListener(new View.OnClickListener() {
             @Override
              public void onClick(View v) {
                    StringBuilder sb = new StringBuilder();
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("commodityId", list.get(1).getCommodityId());
                    result = "";
                    sb.append(mmkv.decodeInt("userId"));
                    sb.append(list.get(1).getCommodityId());
                     sb.append("tech");
                     result = sb.toString();
                     String str = MD5(result);
                    map.put("sign", str);
                     p.startQuery(Constant.PAYVIP, map, VipBean.class);
             }
         });
   }

//支付成功的回调

    if (data instanceof PayBean) {
        PayBean pay = (PayBean) data;
        if (pay != null && pay.getStatus().equals("0000")) {
            PayReq req = new PayReq();
            req.appId = pay.getAppId();
            req.nonceStr = pay.getNonceStr();
            req.partnerId = pay.getPartnerId();
            req.prepayId = pay.getPrepayId();
            req.sign = pay.getSign();
            req.timeStamp = pay.getTimeStamp();
            req.packageValue = pay.getPackageValue();
			//去调微信
            WXUtils.reg(this).sendReq(req);
        }

    }

}

老样子在wxapi下新建WXPayEntryActivity
以下是代码:

public class WXPayEntryActivity extends AppCompatActivity implements IWXAPIEventHandler {
    private IWXAPI iwxapi;
    private static String APP_ID = "wxb3852e6a6b7d9516";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        // 隐藏状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //接收到分享以及登录的intent传递handleIntent方法,处理结果
        iwxapi = WXAPIFactory.createWXAPI(this, APP_ID, false);
        iwxapi.handleIntent(getIntent(), this);
    }
    @Override
    public void onReq(BaseReq baseReq) {
    }
    @Override
    public void onResp(BaseResp baseResp) {
        if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            switch (baseResp.errCode) {
                case BaseResp.ErrCode.ERR_OK:
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(WXPayEntryActivity.this, "成功", Toast.LENGTH_SHORT).show();
                            Log.e("dj", "支付成功");
                            WXPayEntryActivity.this.finish();
                        }
                    });
                    break;
                case BaseResp.ErrCode.ERR_USER_CANCEL:
                    Toast.makeText(WXPayEntryActivity.this, "取消支付", Toast.LENGTH_SHORT).show();
                    Log.e("dj", "用户取消支付");
                    WXPayEntryActivity.this.finish();
                    break;

                default:
                    break;
            }
        }
    }
}

清单注册

<activity android:name=".wxapi.WXPayEntryActivity"/>

到这就结束了

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部