// Update is called once per frame void OnGUI() { if (GUILayout.Button("Test Centili Unity Payment")) { print("Request for payment sent."); var paymentRequest = new CentiliPaymentRequest("28550ec26491d4ed1b1de6fd3fe2b92a"); paymentRequest.ClientId = "test"; CentiliPaymentManager.DebugMode = true; CentiliPaymentManager.MakePayment(paymentRequest, CentiliCallback); } }
/// <summary> /// MakePayment coroutine that can wait until the CentiliPaymentResponse is ready /// </summary> private IEnumerator MakePaymentCoroutine(CentiliPaymentRequest request, Action <CentiliPaymentStatus, CentiliPaymentResponse> callback) { // invoke singleton instance creation of this object, because we need it GetInstance(); // Passing data over the bridge between C# here and Java running on Android has // a quirk, that it can't handle NULL values and throws exceptions. Here is a // workaround that uses an extra string to pass which variables are present and which // are absent. string presenceMask = new string(new char[] { request.ApiKey == null ? 'a' : 'p', //request.PackageIndex == null ? 'a' : 'p', 'p', request.LanguageCode == null ? 'a' : 'p', request.Info == null ? 'a' : 'p', request.ClientId == null ? 'a' : 'p', // request.TestModeEnabled == null ? 'a' : 'p', 'p', // request.OfflineModeEnabled == null ? 'a' : 'p', 'p', // request.PendingTransactionHandlingEnabled == null ? 'a' : 'p' 'p' }); // Java side will use this parameter to return data lastCentiliPaymentResponse = null; // A method with a lot of parameters is acceptable here, because other options add even // more bloat and won't help with readability (serializations) GetCurrentActivity().Call("startPayment", new object[] { presenceMask, request.ApiKey == null ? "" : request.ApiKey, // request.PackageIndex == null ? (int)-1 : (int) request.PackageIndex, request.PackageIndex, request.LanguageCode == null ? "EN" : request.LanguageCode, request.Info == null ? "" : request.Info, request.ClientId == null ? "" : request.ClientId, // request.TestModeEnabled == null ? true : request.TestModeEnabled, request.TestModeEnabled, // request.OfflineModeEnabled == null ? true : request.OfflineModeEnabled, request.OfflineModeEnabled, // request.PendingTransactionHandlingEnabled == null ? false : request.PendingTransactionHandlingEnabled request.Price }); // if there is no callback, don't wait for response and return immediately if (callback == null) { yield break; } // wait until we get a result. This variable is set in NotifyResponseReady() // that is called from Android Java runtime while (lastCentiliPaymentResponse == null) { yield return(1); // wait 1 frame } // call the callback and clear the buffer automatically callback(lastCentiliPaymentStatus, lastCentiliPaymentResponse); lastCentiliPaymentResponse = null; }
public void MakePayment(CentiliPaymentRequest request, Action <CentiliPaymentStatus, CentiliPaymentResponse> callback) { StartCoroutine(MakePaymentCoroutine(request, callback)); }