微信支付

//微信 依赖
implementation 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:+'
//  商户APP工程中引入微信JAR包,调用API前,需要先向微信注册您的APPID
         api = WXAPIFactory.createWXAPI(this, null);
// 将该app注册到微信
          api.registerApp("xxxxxxxxxxxxxxxxxxx");
 PayReq req = new PayReq();
 req.appId = payBean.getAppId();
 req.partnerId = payBean.getPartnerId();
 //预支付订单
 req.prepayId = payBean.getPrepayId();
 req.nonceStr = payBean.getNonceStr();
 req.timeStamp = payBean.getTimeStamp()+"";
 req.packageValue = payBean.getPackageValue();
 req.sign = payBean.getSign();
 // 在支付之前,如果应用没有注册到微信,应该先调用IWXMsg.registerApp将应用注册到微信
 //3.调用微信支付sdk支付方法
// api.sendReq(req);

 

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
    
    private IWXAPI api;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.pay_result);
        api = WXAPIFactory.createWXAPI(this, "xxxxxxxxxxxxxx");
        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) {

        Toast.makeText(getApplicationContext(), "成功", Toast.LENGTH_LONG).show();

    }
    //支付结果
    @Override
    public void onResp(BaseResp resp) {
        if (TextUtils.isEmpty(resp.openId)) {
            Log.i("xml", "啥也没有");
        }
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            //  AlertDialog.Builder builder = new AlertDialog.Builder(this);
            //builder.setTitle("微信支付提示");
            switch (resp.errCode) {
                case 0://支付成功h
                    Toast.makeText(getApplicationContext(), "支付成功", Toast.LENGTH_SHORT).show();//builder.setMessage("支付成功.");
                    break;
                case -1://支付失败,一般是后端签名失败等问题
                    Toast.makeText(getApplicationContext(), "支付失败", Toast.LENGTH_SHORT).show();
                    // builder.setMessage("支付失败,请清理微信缓存或重新安装.");
                    break;

                case -2://用户取消了支付

                    Toast.makeText(getApplicationContext(), "支付已取消.", Toast.LENGTH_SHORT).show();
                    //builder.setMessage("支付已取消.");
                    break;
            }

            //  builder.show();
        }


        finish();
    }
}

你可能感兴趣的:(studio)