【android】轻松集成支付包支付


一、首先去支付宝开发平台下载 alipaySdk-xxxxxxxx.jar包并放入商户应用工程的libs目录下,如下图。

1.png

在app module下的build.gradle下手动添加依赖,如下代码所示:

dependencies {
......
compile files('libs/alipaySdk-20170725.jar')
......
}

二、修改AndroidManifest

在商户应用工程的AndroidManifest.xml文件里面添加声明:

android:name="com.alipay.sdk.app.H5PayActivity"
android:configChanges="orientation|keyboardHidden|navigation|screenSize"
android:exported="false"
android:screenOrientation="behind"
android:windowSoftInputMode="adjustResize|stateHidden" >

android:name="com.alipay.sdk.app.H5AuthActivity"
android:configChanges="orientation|keyboardHidden|navigation"
android:exported="false"
android:screenOrientation="behind"
android:windowSoftInputMode="adjustResize|stateHidden" >

和权限声明:






三、添加混淆

在商户应用工程的proguard-project.txt里添加以下相关规则:

-keep class com.alipay.android.app.IAlixPay{*;}
-keep class com.alipay.android.app.IAlixPay$Stub{*;}
-keep class com.alipay.android.app.IRemoteServiceCallback{*;}
-keep class com.alipay.android.app.IRemoteServiceCallback$Stub{*;}
-keep class com.alipay.sdk.app.PayTask{ public *;}
-keep class com.alipay.sdk.app.AuthTask{ public *;}
-keep class com.alipay.sdk.app.H5PayCallback {
;
;
}
-keep class com.alipay.android.phone.mrpc.core.** { *; }
-keep class com.alipay.apmobilesecuritysdk.** { *; }
-keep class com.alipay.mobile.framework.service.annotation.** { *; }
-keep class com.alipay.mobilesecuritysdk.face.** { *; }
-keep class com.alipay.tscenter.biz.rpc.** { *; }
-keep class org.json.alipay.** { *; }
-keep class com.alipay.tscenter.** { *; }
-keep class com.ta.utdid2.** { *;}
-keep class com.ut.device.** { *;}
至此,开发包开发资源导入完成。


四、开始支付代码

//根据自己的网络框架去请求后天给的一个接口,得到挑用支付宝接口的数据如下:

NetUtils.getInstance().postMonth(PaymeijiaActivity.this, Constant.APARTPAY, NetUtils.getPay(startime, endtime, room_id, "alipay", allmoney, "1"), mHandler, Constant.ONE);

得到返回的数据形式:

String result = alipay_sdk=alipay-sdk-php-20161101&app_id=2017031306200467&biz_content=%7B%22body%22%3A%22%5Cu6d4b%5Cu8bd5%5Cu6570%5Cu636e%22%2C%22subject%22%3A%22App%5Cu652f%5Cu4ed8%5Cu6d4b%5Cu8bd5%22%2C%22out_trade_no%22%3A%221496387386561323%22%2C%22timeout_express%22%3A%2230m%22%2C%22total_amount%22%3A0.01%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%7D&charset=UTF-8&format=json&method=alipay.trade.app.pay¬ify_url=http%3A%2F%2F222.173.28.166%3A8087%2Fnotify_url.php&sign_type=RSA2×tamp=2017-06-02+07%3A09%3A46&version=1.0&sign=H1NJRwc9xtyZrtjd8kCDNwnZ%2FxdE%2FBFeM9sJK%2FXrRNC6npgcgJeA%2FV7xRKIO4AV75drG%2B068iVQUAIjy6poqPC9BTeKa2u4A3EJ6tl0zUO87GD5uKY%2Fb%2BaCHTXGqH3z6toZYa%2BzNBjPKcvydFuq2JRDRT8OTPay8N1GAZ10oDlnhYRNyOFok9ljlq8sc3J9sQioqxEPpIWIygwk0AxD%2FB7dUzKcCBFaN7%2FjAZlOnsyLpu%2FPwbJbxpPxSg9ZYXsx2u86wdLQQgkWVvyzasjHczO%2BgFhcBmpc%2FyTWeBvZmUj3nLrHhZqS6ZiwLkXQG3IiFwy986HiozRwsdxplnaKgyQ%3D%3D
五、调起支付
Runnable payRunnable = new Runnable() {
                        @Override
                        public void run() {
                            // 构造PayTask 对象
                            PayTask alipay = new PayTask(PaymeijiaActivity.this);
                            // 调用支付接口,获取支付结果
                            String result = alipay.pay(result , true);
                            Message msg = new Message();
                            msg.what = Constant.ONE;
                            msg.obj = result;
                            mHandler.sendMessage(msg);
                        }
                    };
                    // 必须异步调用
                    Thread payThreadpay = new Thread(payRunnable);
                    payThreadpay.start();


//支付宝支付结果返回
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data == null) {
            return;
        }
        String msg = "";
        /*
         * 支付控件返回字符串:success、fail、cancel 分别代表支付成功,支付失败,支付取消
         */
        String str = data.getExtras().getString("pay_result");
        Log.v("zftphone", "2 " + data.getExtras().getString("merchantOrderId"));
        if (str.equalsIgnoreCase("success")) {
            msg = "支付成功!";

        } else if (str.equalsIgnoreCase("fail")) {
            msg = "支付失败!";

        } else if (str.equalsIgnoreCase("cancel")) {

            msg = "用户取消了支付";
        }
        //支付完成,处理自己的业务逻辑!
    }

六、支付结果后UI操作
private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case Constant.ONE: {
                    PayResult payResult = new PayResult((String) msg.obj);
                    /**
                     对于支付结果,请商户依赖服务端的异步通知结果。同步通知结果,仅作为支付结束的通知。
                     */
//                    String resultInfo = payResult.getResult();// 同步返回需要验证的信息
                    String resultStatus = payResult.getResultStatus();
                    // 判断resultStatus 为9000则代表支付成功
                    if (TextUtils.equals(resultStatus, "9000")) {
                        // 该笔订单是否真实支付成功,需要依赖服务端的异步通知。
                        Toast.makeText(PaymeijiaActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(PaymeijiaActivity.this, PayOkActivity.class);
                        intent.putExtra("payMoney", payMoney);
                        intent.putExtra("pay", "ok");
                        startActivity(intent);
                        finish();
                    } else if (TextUtils.equals(resultStatus, "6001")) {
                        // 该笔订单是否真实支付成功,需要依赖服务端的异步通知。
                        Toast.makeText(PaymeijiaActivity.this, "用户中途取消", Toast.LENGTH_SHORT).show();
                                    
                    } else {
                        // 该笔订单真实的支付结果,需要依赖服务端的异步通知。
                        Toast.makeText(PaymeijiaActivity.this, "支付失败", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(PaymeijiaActivity.this, PayOkActivity.class);
                        intent.putExtra("pay", "fail");
                        startActivity(intent);
                        finish();
                    }
                    break;
                
		default:
                break;
        }
    }

};

你可能感兴趣的:(android,支付宝,支付)