支付宝支付

·  阅读 489
final String orderInfo = "alipay_sdk=alipay-sdk-java-dynamicVersionNo&app_id=20181119622544&biz_content=%7B%22body%22%3A%22%E6%88%91%E6%98%AF%E6%B5%8B%E8%AF%95%E6%95%B0%E6%8D%AE%22%2C%22out_trade_no%22%3A%221234%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%2C%22subject%22%3A%22App%E6%94%AF%E4%BB%98%E6%B5%8B%E8%AF%95Java%22%2C%22timeout_express%22%3A%2230m%22%2C%22total_amount%22%3A%220.01%22%7D&charset=UTF-8&format=json&method=alipay.trade.app.pay&notify_url=%E5%95%86%E6%88%B7%E5%A4%96%E7%BD%91%E5%8F%AF%E4%BB%A5%E8%AE%BF%E9%97%AE%E7%9A%84%E5%BC%82%E6%AD%A5%E5%9C%B0%E5%9D%80&sign=xQd6TIn3u1mlxe6Ny3Uotgb0%2FPEIsHxijcv0trA9Xrv0edi0fSjfktM7U0slR%2Fql3q2btV5ucbofQ7ekKoeVxwloOrvqpd%2FKL9kPoQn4shVKTNGertcqz1O0FK5em6tHXdBg3wyvRpyKKqo7HkJIo57esFJCIxH%2BTvMTM20T85Q%3D&sign_type=RSA2&timestamp=2018-11-19+14%3A51%3A02&version=1.0";   // 订单信息

Runnable payRunnable = new Runnable() {
    @Override
    public void run() {
        PayTask alipay = new PayTask(MainActivity.this);
        Map<String, String> result = alipay.payV2(orderInfo,true);
        Message msg = new Message();
        msg.what = SDK_PAY_FLAG;
        msg.obj = result;
        mHandler.sendMessage(msg);
    }
};
// 必须异步调用
Thread payThread = new Thread(payRunnable);
payThread.start();
复制代码
@SuppressLint("HandlerLeak")
private Handler mHandler = new Handler() {
    @SuppressWarnings("unused")
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case SDK_PAY_FLAG: {
                @SuppressWarnings("unchecked")
                PayResult payResult = new PayResult((Map<String, String>) msg.obj);
                /**
                 对于支付结果,请商户依赖服务端的异步通知结果。同步通知结果,仅作为支付结束的通知。
                 */
                String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                String resultStatus = payResult.getResultStatus();
                // 判断resultStatus 为9000则代表支付成功
                if (TextUtils.equals(resultStatus, "9000")) {
                    // 该笔订单是否真实支付成功,需要依赖服务端的异步通知。
                    showAlert(MainActivity.this, "支付成功: " + payResult);
                } else {
                    // 该笔订单真实的支付结果,需要依赖服务端的异步通知。
                    showAlert(MainActivity.this, "支付失败: " + payResult);
                }
                break;
            }
            case SDK_AUTH_FLAG: {
                @SuppressWarnings("unchecked")
                AuthResult authResult = new AuthResult((Map<String, String>) msg.obj, true);
                String resultStatus = authResult.getResultStatus();
                // 判断resultStatus 为“9000”且result_code
                // 为“200”则代表授权成功,具体状态码代表含义可参考授权接口文档
                if (TextUtils.equals(resultStatus, "9000") &&TextUtils.equals(authResult.getResultCode(), "200")) {
                    // 获取alipay_open_id,调支付时作为参数extern_token 的value
                    // 传入,则支付账户为该授权账户
                    showAlert(MainActivity.this, "授权成功: " + authResult);
                } else {
                    // 其他状态值则为授权失败
                    showAlert(MainActivity.this, "授权失败: " + authResult);
                }
                break;
            }
            default:
                break;
        }
    };
};
复制代码
private static void showAlert(Context ctx, String info) {
        showAlert(ctx, info, null);
    }

    private static void showAlert(Context ctx, String info, DialogInterface.OnDismissListener onDismiss) {
        new AlertDialog.Builder(ctx)
                .setMessage(info)
                .setPositiveButton("确定", null)
                .setOnDismissListener(onDismiss)
                .show();
    }

    private static void showToast(Context ctx, String msg) {
        Toast.makeText(ctx, msg, Toast.LENGTH_LONG).show();
    }
复制代码
收藏成功!
已添加到「」, 点击更改