124 lines
3.0 KiB
C#
124 lines
3.0 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using GameWrapper.Pay;
|
||
using PhxhSDK;
|
||
using UnityEngine;
|
||
|
||
public class AliPayHandle : MonoBehaviour
|
||
{
|
||
private static AliPayHandle _instance;
|
||
|
||
public static AliPayHandle Instance
|
||
{
|
||
get
|
||
{
|
||
if (_instance is null)
|
||
{
|
||
var go = new GameObject("AliPayHandle");
|
||
_instance = go.AddComponent<AliPayHandle>();
|
||
DontDestroyOnLoad(go);
|
||
}
|
||
|
||
return _instance;
|
||
}
|
||
}
|
||
|
||
private void Awake()
|
||
{
|
||
if (_instance == null)
|
||
{
|
||
_instance = this;
|
||
Initialize();
|
||
DontDestroyOnLoad(gameObject);
|
||
}
|
||
else
|
||
{
|
||
Destroy(gameObject);
|
||
}
|
||
}
|
||
|
||
public void BuyGoods(string currencyID, int count, int itemID, int shopID = 0, int goodsID = 0,
|
||
int index = 0)
|
||
{
|
||
DebugUtil.Log("支付宝支付");
|
||
PayRequest(currencyID, count, itemID);
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 初始化支付服务
|
||
/// </summary>
|
||
public void Initialize()
|
||
{
|
||
#if AliPay
|
||
PaymentServiceLocator.AliPaymentService.Initialize();
|
||
#endif
|
||
}
|
||
|
||
/// <summary>
|
||
/// 检查支付宝是否已初始化
|
||
/// </summary>
|
||
public bool IsInitialized()
|
||
{
|
||
#if AliPay
|
||
return PaymentServiceLocator.AliPaymentService.IsAppInitialized();
|
||
#endif
|
||
return false;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 是否在支付流程中
|
||
/// </summary>
|
||
public bool IsPaying()
|
||
{
|
||
#if AliPay
|
||
return PaymentServiceLocator.AliPaymentService.IsPaying();
|
||
#endif
|
||
return false;
|
||
}
|
||
|
||
private void PayRequest(string currencyID, int count, int itemID)
|
||
{
|
||
#if AliPay
|
||
PaymentServiceLocator.AliPaymentService.BuyGoods(currencyID, count, itemID);
|
||
#endif
|
||
}
|
||
|
||
/// <summary>
|
||
/// 测试用支付按钮,正式环境请调用BuyGoods方法
|
||
/// </summary>
|
||
/// <param name="orderInfo"></param>
|
||
public void AliPayTest(string orderInfo)
|
||
{
|
||
try
|
||
{
|
||
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
|
||
if (unityPlayer == null)
|
||
{
|
||
Debug.LogError("unityPlayer is null");
|
||
return;
|
||
}
|
||
|
||
AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
|
||
AndroidJavaObject utils = new AndroidJavaObject("com.phxh.dev.nld.PayActivity");
|
||
utils.Call("PayDemo", orderInfo, currentActivity, "AliPayHandle", "OnPaymentCallback"); //AliPay
|
||
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
Debug.LogError(e);
|
||
return;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 支付回调 由java层调用
|
||
/// </summary>
|
||
public void OnPaymentCallback(string result)
|
||
{
|
||
DebugUtil.Log("AliPayHandle.OnPaymentCallback result: {0}", result);
|
||
#if AliPay
|
||
PaymentServiceLocator.AliPaymentService.OnPaymentCallback(result);
|
||
#endif
|
||
}
|
||
} |