unity 安卓支付宝篇

xiaoxiao2021-02-28  11

首先下载支付宝jar放到libs下

然后androidstudio代码

// Unity中调用 public void Pay(final String orderInfo) { //新开一个线程,将orderInfo字串传入到PayTask任务中去 Runnable payRunnable = new Runnable() { @Override public void run() { //新建一个PAyTask对象 PayTask alipay = new PayTask(MainActivity.this); Map<String, String> result = alipay.payV2(orderInfo, true); Log.i("msp", result.toString()); 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则代表支付成功 把支付结果返回给unity处理 UnityPlayer.UnitySendMessage("Canvas","AliPayCallback", resultStatus); //这里可以进行弹窗提示 if (TextUtils.equals(resultStatus, RESULT_SUCCESS)) { Toast.makeText(MainActivity.this, TIP_PAY_SUCCESS, Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, TIP_PAY_FAILED, Toast.LENGTH_SHORT).show(); } break; } default: break; } }; }

然后在xml里添加

<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- alipay sdk begin --> <activity android:name="com.alipay.sdk.app.H5PayActivity" android:configChanges="orientation|keyboardHidden|navigation|screenSize" android:exported="false" android:screenOrientation="behind" android:windowSoftInputMode="adjustResize|stateHidden" > </activity> <activity android:name="com.alipay.sdk.app.H5AuthActivity" android:configChanges="orientation|keyboardHidden|navigation" android:exported="false" android:screenOrientation="behind" android:windowSoftInputMode="adjustResize|stateHidden" > </activity> <!-- alipay sdk end -->

添加混淆规则

应用工程的proguard-rules.pro里添加以下相关规则:

-libraryjars libs/alipaySdk-20170922.jar -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 { <fields>; <methods>; } -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.** { *;}

最后unity里调用

/// <summary> /// 支付宝开始支付接口 /// </summary> /// <param name="payInfo">支付信息</param> /// <returns></returns> public IEnumerator AliPay(PayInfo payInfo) { Dictionary<string, string> headdress = new Dictionary<string, string> { { "Cookie", PlayerPrefs.GetString(SaveUserInfoOnPrefas.UserCookiePHPSESSID) }//这里是后台登录交互过程中保存的cookie信息 确定用户 }; string data = "";//添加与后台交互信息 byte[] bs = System.Text.Encoding.UTF8.GetBytes(data); WWW www = new WWW(StaticData.APICheckRecepitIsValid, bs, headdress);//请求后台地址,传参,包含cookie的头 yield return www; if (www.error != null) { Debug.Log(www.error); } else { var userOrder = JsonMapper.ToObject(www.text); if (userOrder["errno"].ToString() == "0") { string orderInfo = userOrder["result"]["data"].ToString(); #if UNITY_ANDROID && !UNITY_EDITOR AndroidJavaClass javaClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); currentActivity = javaClass.GetStatic<AndroidJavaObject>("currentActivity"); #endif currentActivity.Call("Pay", orderInfo);//后台获取orderInfo发送给支付宝 } else { } } }

如果不在android里处理弹窗,可以放到unity里

AndroidJavaClass PayResult = new AndroidJavaClass("android.widget.Toast"); PayResult.CallStatic<AndroidJavaObject>("makeText", currentActivity, "支付成功或失败", 0).Call("show");
转载请注明原文地址: https://www.6miu.com/read-1100193.html

最新回复(0)