Pay Per Play

Monkey Forums/Monkey Programming/Pay Per Play

luggage(Posted 2011) [#1]
Hi There

If I wanted to add Pay Per Play features to my IOS\Android Monkey game what would be the best way to go about it? Android and iOS have their own API's for it so I wouldn't mind moving them into a single API but I'm not sure where to start. I don't want to mess with the game post build that's for sure.

Any ideas? Suggestions?


xzess(Posted 2011) [#2]
You have to wrap InAppPurchase functions to monkey for that

Here is something i worked on but didn't complete it

You have "just" to forward those values and variables and make it monkey ready

If you have success it would be very nice if you would share it with the community!

// inapppurchase.cpp

#import <Foundation/Foundation.h>


//In App Purchase Functions

void InitStore()
{
	//Your application should add the observer when your application launches.
	MyStoreObserver *observer = [[MyStoreObserver alloc] init];
	[[SKPaymentQueue defaultQueue] addTransactionObserver:observer];
}

void requestProductData()
{
   SKProductsRequest *request= [[SKProductsRequest alloc] initWithProductIdentifiers: [NSSet setWithObject: kMyFeatureIdentifier]];
   request.delegate = self;
   [request start];
}

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
    NSArray *myProduct = response.products;
    // populate UI
    [request autorelease];
}

BOOL isInAppPurchaseAvailable() {
	if ([SKPaymentQueue canMakePayments])
	{
	   return true;
	}
	else
	{
	   return false;
	}
}

void completeTransaction(SKPaymentTransaction *transaction)
{
// Your application should implement these two methods.
    [self recordTransaction: transaction];
    [self provideContent: transaction.payment.productIdentifier];
// Remove the transaction from the payment queue.
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

void restoreTransaction(SKPaymentTransaction *transaction)
{
    [self recordTransaction: transaction];
    [self provideContent: transaction.originalTransaction.payment.productIdentifier];
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

void failedTransaction(SKPaymentTransaction *transaction)
{
    if (transaction.error.code != SKErrorPaymentCancelled)
    {
        // Optionally, display an error here.
    }
    [[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}

void BuyItem()
{
SKPayment *payment = [SKPayment paymentWithProductIdentifier:kMyFeatureIdentifier];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}

void BuyAmount(int quantity)
{
//If your store offers the ability to purchase more than one of a product, 
//you can create a single payment and set the quantity property.

SKMutablePayment *payment = [SKMutablePayment paymentWithProductIdentifier:kMyFeatureIdentifier];
payment.quantity = quantity;
[[SKPaymentQueue defaultQueue] addPayment:payment];
}

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
    for (SKPaymentTransaction *transaction in transactions)
    {
        switch (transaction.transactionState)
        {
            case SKPaymentTransactionStatePurchased:
                [self completeTransaction:transaction];
                break;
            case SKPaymentTransactionStateFailed:
                [self failedTransaction:transaction];
                break;
            case SKPaymentTransactionStateRestored:
                [self restoreTransaction:transaction];
            default:
                break;
        }
    }
}





luggage(Posted 2011) [#3]
Thanks for that, it'll give me something to start with.

How does Monkey sync with the native code? I guess I'd have a version of the functions we need for each supported platform but I can't see where the connection to the .monkey files come. How does trans know to call the cpp functions?


xzess(Posted 2011) [#4]
Yes indeed, if you want to support all targets, you will have to write your functions for all targets in all those languages

To get it into monkey you can do it like this:

//****** native.cpp *******

void MyFunction()
{

}




//****** test.monkey *******
Import "native/native.cpp"  'contains native cpp code

Extern

Function MyFunction()



anawiki(Posted 2011) [#5]
I'll be giving this a try today on iOS. Hopefully it's easier than I think :D


xzess(Posted 2011) [#6]
Plz share results :)


anawiki(Posted 2011) [#7]
Don't hold your breath for it... I am iOS rookie... I didn't move forward a step on the code side... still learning how to setup iTunes connect to be able to test that thing :D