Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jarnik committed Jul 26, 2013
1 parent 6e2de9d commit b016840
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 182 deletions.
12 changes: 8 additions & 4 deletions ouya_iap.hxproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,23 @@
</output>
<!-- Other classes to be compiled into your SWF -->
<classpaths>
<class path="Source" />
<class path="src" />
<class path="C:\HaxeToolkit\haxe/lib\openfl/1,0,4/" />
<class path="C:\HaxeToolkit\haxe/lib\openfl-ouya/1,0,2/" />
<class path="bin/android/haxe" />
</classpaths>
<!-- Build options -->
<build>
<option directives="" />
<option directives="tools=1&#xA;openfl_ver=1.0.4&#xA;openfl&#xA;display&#xA;android&#xA;mobile&#xA;android" />
<option flashStrict="False" />
<option mainClass="com.example.ouya_iap" />
<option mainClass="ApplicationMain" />
<option enabledebug="False" />
<option additional="" />
<option additional="--no-output&#xA;--macro allowPackage(&quot;flash&quot;)" />
</build>
<!-- haxelib libraries -->
<haxelib>
<library name="openfl" />
<library name="openfl-ouya" />
</haxelib>
<!-- Class files to compile (other referenced classes will automatically be included) -->
<compileTargets>
Expand Down
131 changes: 131 additions & 0 deletions src/IAPHandler.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.utils.ByteArray;
import haxe.io.Bytes;
import haxe.crypto.BaseCode;

import openfl.Assets;

#if android
import openfl.utils.JNI;
import tv.ouya.console.api.OuyaController;
import tv.ouya.console.api.OuyaFacade;
import openfl.events.JoystickEvent;
#end

class IAPHandler
{

public var initCall:Dynamic;
public var requestProductListCall:Dynamic;
public var getProductListIDsCall:Dynamic;
public var requestReceiptsCall:Dynamic;
public var getReceiptProductIDsCall:Dynamic;
public var requestPurchaseCall:Dynamic;
public var ouyaFacadeObject:Dynamic;

public function new( ouyaFacadeObject:Dynamic, DERKeyPath:String )
{
this.ouyaFacadeObject = ouyaFacadeObject;

#if android

// bind methods to JNI
trace("=================== JNI linking methods...");
initCall = openfl.utils.JNI.createStaticMethod
("com.jarnik.iaptest.OUYA_IAP", "init", "(Lorg/haxe/nme/HaxeObject;Ltv/ouya/console/api/OuyaFacade;Ljava/lang/String;)V", true);
requestProductListCall = openfl.utils.JNI.createStaticMethod
("com.jarnik.iaptest.OUYA_IAP", "requestProductList", "([Ljava/lang/String;)V", true);
getProductListIDsCall = openfl.utils.JNI.createStaticMethod
("com.jarnik.iaptest.OUYA_IAP", "getProductListIDs", "()Ljava/lang/String;", true);
requestReceiptsCall = openfl.utils.JNI.createStaticMethod
("com.jarnik.iaptest.OUYA_IAP", "requestReceipts", "()V", true);
getReceiptProductIDsCall = openfl.utils.JNI.createStaticMethod
("com.jarnik.iaptest.OUYA_IAP", "getReceiptProductIDs", "()Ljava/lang/String;", true);
requestPurchaseCall = openfl.utils.JNI.createStaticMethod
("com.jarnik.iaptest.OUYA_IAP", "requestPurchase", "(Ljava/lang/String;)V", true);
trace("=================== JNI methods linked!");
var appKey:ByteArray = Assets.getBytes( DERKeyPath );

// I don't know how to pass ByteArray to JNI, let's use Base64 encoding
var BASE:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var base64:BaseCode = new BaseCode( Bytes.ofString( BASE ) );
var appKey64:String = base64.encodeBytes( appKey ).toString();

// init the IAP
var params:Array<Dynamic> = [this, ouyaFacadeObject, appKey64 ];
initCall( params );
#end
}

public function requestProductList( products:Array<String> ):Void {
trace("requesting " + products.join(" "));
requestProductListCall( [products] );
}

public function requestPurchase( product:String ):Void {
trace("purchasing " + product );
requestPurchaseCall( [product] );
}

public function requestReceipts():Void {
trace("requestReceipts");
requestReceiptsCall( [] );
}

public function getProductListIDs():String {
// will return list of received product identifiers, delimited by space character
return getProductListIDsCall();
}

public function getReceiptProductIDs():String {
// will return list of purchased products identifiers, delimited by space character
return getReceiptProductIDsCall();
}

// ==================================== CALLBACKS - OVERRIDE THESE! =======================

// ==== Product List
public function onProductListReceived()
{
var p:Array<String> = getProductListIDs().split(" ");
trace("=== onProductListReceived! " + p.join(" "));

requestReceipts();
}
public function onProductListFailed(error:String)
{
trace("=== onProductListFailed! "+error);
}

// ==== Purchasing
public function onPurchaseSuccess(productID:String)
{
trace("=== onPurchaseSuccess! "+productID);
}
public function onPurchaseFailed(error:String)
{
trace("=== onPurchaseFailed! "+error);
}
public function onPurchaseCancelled()
{
trace("=== onPurchaseCancelled! ");
}

// ==== Receipt List
public function onReceiptsReceived()
{
var p:Array<String> = getReceiptProductIDs().split(" ");
trace("=== onReceiptsReceived! "+p.join("x"));
}
public function onReceiptsFailed( error:String )
{
trace("=== onReceiptsFailed "+error);
}
public function onReceiptsCancelled()
{
trace("=== onReceiptsCancelled ");
}
}


136 changes: 15 additions & 121 deletions src/Main.hx
Original file line number Diff line number Diff line change
Expand Up @@ -16,150 +16,44 @@ import openfl.events.JoystickEvent;
class Main extends Sprite {

public static inline var OUYA_DEVELOPER_ID:String = "a589aa6a-cf50-4f72-9313-0a515e4dab95";
public static inline var PRODUCT_IDENTIFIER:String = "test_sss_full";
public static inline var DER_KEY_PATH:String = "assets/key.der";

#if android
public static var ouyaFacade:OuyaFacade;
#end

private var p:IAP_Handler;
private var handler:MyIAPHandler;

public function new () {

super ();
// Help:
// How do you call Haxe from Java (Android) http://www.openfl.org/forums/general-discussion/how-do-you-call-haxe-java-android/
// writing JNI bindings http://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html#zz-4.3
// JNI elements https://nekonme.googlecode.com/svn/trunk/project/android/JNI.cpp

addChild( new Bitmap( Assets.getBitmapData("assets/OUYA_O.png" ) ) );
super ();

#if android
var getContext = JNI.createStaticMethod ("org.haxe.nme.GameActivity", "getContext", "()Landroid/content/Context;", true);
OuyaController.init ( getContext () );
ouyaFacade = OuyaFacade.getInstance();
ouyaFacade.init( getContext(), OUYA_DEVELOPER_ID );
trace("OUYA controller & facade inited!");
//ShopOUYA.init();

// How do you call Haxe from Java (Android) http://www.openfl.org/forums/general-discussion/how-do-you-call-haxe-java-android/

p = new IAP_Handler( ouyaFacade.__jobject );

p.requestProductList(["test_sss_full", "__DECLINED__THIS_PURCHASE"]);
handler = new MyIAPHandler( ouyaFacade.__jobject, DER_KEY_PATH );
handler.requestProductList(["test_sss_full", "__DECLINED__THIS_PURCHASE"]);

addChild( new Bitmap( Assets.getBitmapData("assets/OUYA_O.png" ) ) );
stage.addEventListener (JoystickEvent.BUTTON_DOWN, stage_onJoystickButtonDown);

#end

}

#if android
private function stage_onJoystickButtonDown( e:JoystickEvent ):Void {
trace("pressed button, will purchase");
p.requestPurchase( "test_sss_full" );
trace("OUYA button pressed, starting purchase");
handler.requestPurchase( PRODUCT_IDENTIFIER );
}
#end

}

class IAP_Handler
{

public var initCall:Dynamic;
public var requestProductListCall:Dynamic;
public var getProductListIDsCall:Dynamic;
public var requestReceiptsCall:Dynamic;
public var getReceiptProductIDsCall:Dynamic;
public var requestPurchaseCall:Dynamic;
public var ouyaFacadeObject:Dynamic;

public function new( ouyaFacadeObject:Dynamic )
{
this.ouyaFacadeObject = ouyaFacadeObject;
#if android
//var fn = openfl.utils.JNI.createStaticMethod("com.jarnik.iaptest.OUYA_IAP", "purchaseSomething", "(Lorg/haxe/nme/HaxeObject;)V", true);
//fn([this]);

// writing JNI bindings http://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html#zz-4.3
// JNI elements https://nekonme.googlecode.com/svn/trunk/project/android/JNI.cpp
trace("=================== JNI linking");
initCall = openfl.utils.JNI.createStaticMethod
("com.jarnik.iaptest.OUYA_IAP", "init", "(Lorg/haxe/nme/HaxeObject;Ltv/ouya/console/api/OuyaFacade;Ljava/lang/String;)V", true);
// (Lorg/haxe/nme/HaxeObject;Ltv/ouya/console/api/OuyaFacade;[B)V
requestProductListCall = openfl.utils.JNI.createStaticMethod
("com.jarnik.iaptest.OUYA_IAP", "requestProductList", "([Ljava/lang/String;)V", true);
getProductListIDsCall = openfl.utils.JNI.createStaticMethod
("com.jarnik.iaptest.OUYA_IAP", "getProductListIDs", "()Ljava/lang/String;", true);
requestPurchaseCall = openfl.utils.JNI.createStaticMethod
("com.jarnik.iaptest.OUYA_IAP", "requestPurchase", "(Ljava/lang/String;)V", true);
requestReceiptsCall = openfl.utils.JNI.createStaticMethod
("com.jarnik.iaptest.OUYA_IAP", "requestReceipts", "()V", true);
getReceiptProductIDsCall = openfl.utils.JNI.createStaticMethod
("com.jarnik.iaptest.OUYA_IAP", "getReceiptProductIDs", "()Ljava/lang/String;", true);
trace("=================== JNI linked!");
var appKey:ByteArray = Assets.getBytes("assets/key.der");

// I don't know how to pass ByteArray to JNI, let's use Base64
var BASE64:String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var base64:BaseCode = new BaseCode( Bytes.ofString( BASE64 ) );
var appKey64:String = base64.encodeBytes( appKey ).toString();

var params:Array<Dynamic> = [this, ouyaFacadeObject, appKey64 ];
trace("gonna run initCall from Haxe");
initCall( params );
#end
}

public function requestProductList( products:Array<String> ):Void {
trace("requesting " + products.join(" "));
requestProductListCall( [products] );
}

public function requestPurchase( product:String ):Void {
trace("purchasing " + product );
requestPurchaseCall( [product] );
}

public function requestReceipts():Void {
trace("requestReceipts");
requestReceiptsCall( [] );
}

// ==================================== CALLBACKS =======================

public function onProductListReceived()
{
var p:Array<String> = getProductListIDsCall().split(" ");
trace("=== onProductListReceived! >> " + p.join("x"));

requestReceipts();
}
public function onProductListFailed(error:String)
{
trace("=== onProductListFailed! "+error);
}

public function onPurchaseSuccess(productID:String)
{
trace("=== onPurchaseSuccess! "+productID);
}
public function onPurchaseFailed(error:String)
{
trace("=== onPurchaseFailed! "+error);
}
public function onPurchaseCancelled()
{
trace("=== onPurchaseCancelled! ");
}

public function onReceiptsReceived()
{
var p:Array<String> = getReceiptProductIDsCall().split(" ");
trace("=== onReceiptsReceived! >> "+p.join("x"));
}
public function onReceiptsFailed( error:String )
{
trace("=== onReceiptsFailed "+error);
}
public function onReceiptsCancelled()
{
trace("=== onReceiptsCancelled ");
}
}


}
8 changes: 8 additions & 0 deletions src/MyIAPHandler.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

class MyIAPHandler extends IAPHandler
{


}


Loading

0 comments on commit b016840

Please sign in to comment.