Unity IAP with JS (InvalidCastException)

I’m trying to implement Unity IAP using JS and I get this error:
InvalidCastException: Cannot cast from source type to destination type.

The problem is this line (concretely “this”):
UnityPurchasing.Initialize(this, builder);

I use the sample Store from https://docs.unity3d.com/ScriptReference/Purchasing.Product.html
I’ve just added “import UnityEngine.Purchasing” that was missing:

#pragma strict

import UnityEngine.Purchasing;

public class MyStoreClass extends MonoBehaviour {
    static var kProductID100Currency: String = "virtualcurrency_100";
    var m_StoreController: IStoreController;
    function Start() {
        var builder: ConfigurationBuilder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
        builder.AddProduct(kProductID100Currency, ProductType.Consumable);
        UnityPurchasing.Initialize(this, builder);
    }
    public function PurchaseCurrency() {
        if (m_StoreController != null) {
            // Fetch the currency Product reference from Unity Purchasing
            var product: Product = m_StoreController.products.WithID(kProductID100Currency);
            if (product != null && product.availableToPurchase) {
                m_StoreController.InitiatePurchase(product);
            }
        }
    }
    public function OnInitialized(controller: IStoreController, extensions: IExtensionProvider) {
        m_StoreController = controller;
    }
    public function OnInitializeFailed(error: InitializationFailureReason) {
    }
    public function ProcessPurchase(e: PurchaseEventArgs) {
        if (String.Equals(e.purchasedProduct.definition.id, kProductID100Currency, StringComparison.Ordinal)) {
            Debug.Log("Purchased 100 coins");
        }
        return PurchaseProcessingResult.Complete;
    }
    public function OnPurchaseFailed(item: Product, r: PurchaseFailureReason) {
    }
}

The C# example runs without errors. So my configuration seems to be OK.
In the C# example, MyStoreClass class derives from IStoreListener

 public class Purchaser : MonoBehaviour, IStoreListener {

Could this be the problem? How I do that with JS?

Have you had any luck with this? I too am trying to get this to work but am stuck on the same error you’re getting.

No, I haven’t. Sorry.
Maybe you can use the Unity IAP C# script in combination with SendMenssage() from your JS scripts.

This is the reason I’m migrating to C#…the JS script reference and support by Unity and other plugins is very poor.
I liked JS much more…it was faster to type :(…

Your class needs to implement the IStoreListener interface:

public class MyStoreClass extends MonoBehaviour implements IStoreListener {
...
1 Like