版权声明:本文为原创文章,转载请声明http://www.cnblogs.com/unityExplorer/p/8405044.html
支付宝的sdk接入方式和微信支付比较类似,大部分的工作也基本是由服务端去做,客户端只负责支付部分
调用支付宝支付的java核心代码其实只有一行:alipay.payV2()(如果不需要支付的结果的话。。。)
public class AliPay { private static final int SDK_PAY_FLAG = 1; @SuppressLint("HandlerLeak") private static 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")) { UnityPlayer.UnitySendMessage("ThirdPartySdkManager", "AliPayCallback", "true"); } else { UnityPlayer.UnitySendMessage("ThirdPartySdkManager", "AliPayCallback", "false"); } break; } default: break; } } }; //收到来自服务端返回的orderInfo后,向支付宝发起支付 public static void SendPay(final String orderInfo,final Context context) { Runnable payRunnable = new Runnable() { @Override public void run() { PayTask alipay = new PayTask((Activity) context); 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(); } }
值得一提的是,相对于微信支付,支付宝支付除了需要订单信息外,还需要传入context参数,context就是当前的Activity
下面附上c#调用代码
public void SendAliPay(string info) { AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"); AndroidJavaObject utils = new AndroidJavaObject("com.my.ugcf.alipay.AliPay"); utils.CallStatic("SendPay", info, currentActivity); }
恩。。。写到这里,突然发现这几个sdk的ios版忘了写了。。。后面补上。。。