Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 125 lines (95 sloc) 4.206 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
//
// ILSimTransactionSimulator.m
// SimStoreKit
//
// Created by ∞ on 13/07/11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ILSimTransactionSimulator.h"
#if kILSimAllowSimulatedStoreKit

#if TARGET_OS_IPHONE

#import <UIKit/UIKit.h>
#import <objc/runtime.h>

static char kILSimDefaultTransactionSimulator_iOSCompletionHandlerToken = 0;
static void* const kILSimDefaultTransactionSimulator_iOSCompletionHandlerKey = &kILSimDefaultTransactionSimulator_iOSCompletionHandlerToken;

@interface ILSimDefaultTransactionSimulator_iOS : NSObject <ILSimTransactionSimulator, UIAlertViewDelegate>
@end

@implementation ILSimDefaultTransactionSimulator_iOS

- (void) simulateTransaction:(ILSimSKPaymentTransaction *)transaction product:(ILSimSKProduct*) p forQueue:(ILSimSKPaymentQueue *)queue completionHandler:(void (^)(NSError *))block;
{
UIAlertView* a = [[UIAlertView new] autorelease];
a.delegate = self;
objc_setAssociatedObject(a, kILSimDefaultTransactionSimulator_iOSCompletionHandlerKey, block, OBJC_ASSOCIATION_COPY_NONATOMIC);

NSNumberFormatter* numberFormatter = [[NSNumberFormatter new] autorelease];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[numberFormatter setLocale:p.priceLocale];
NSString* formattedPrice = [numberFormatter stringFromNumber:p.price];

a.title = [NSString stringWithFormat:@"SIMULATED: Would you like to buy %d x '%@' at %@?", transaction.payment.quantity, p.localizedTitle, formattedPrice];
[a addButtonWithTitle:@"Succeed Transaction"];
[a addButtonWithTitle:@"Fail Transaction"];
a.cancelButtonIndex = [a addButtonWithTitle:@"Cancel"];
[a show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
{
void (^completionHandler)(NSError*) = objc_getAssociatedObject(alertView, kILSimDefaultTransactionSimulator_iOSCompletionHandlerKey);
if (!completionHandler)
return;

switch (buttonIndex) {
case 0:
completionHandler(nil);
break;
case 1:
completionHandler([NSError errorWithDomain:kILSimSKErrorDomain code:kILSimSKErrorUnknown userInfo:nil]);
break;
case 2:
default:
completionHandler([NSError errorWithDomain:kILSimSKErrorDomain code:kILSimSKErrorPaymentCancelled userInfo:nil]);
break;
}
}

@end

#elif TARGET_OS_MAC && !TARGET_OS_IPHONE

#import <Cocoa/Cocoa.h>

@interface ILSimDefaultTransactionSimulator_Mac : NSObject <ILSimTransactionSimulator>
@end

@implementation ILSimDefaultTransactionSimulator_Mac

- (void) simulateTransaction:(ILSimSKPaymentTransaction *)transaction product:(ILSimSKProduct*) p forQueue:(ILSimSKPaymentQueue *)queue completionHandler:(void (^)(NSError *))block;
{
NSAlert* a = [[NSAlert new] autorelease];

NSNumberFormatter* numberFormatter = [[NSNumberFormatter new] autorelease];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[numberFormatter setLocale:p.priceLocale];
NSString* formattedPrice = [numberFormatter stringFromNumber:p.price];

[a setMessageText:[NSString stringWithFormat:@"Simulated StoreKit: Would you like to buy %d x '%@' at %@?", transaction.payment.quantity, p.localizedTitle, formattedPrice]];

[a addButtonWithTitle:@"Succeed Transaction"];
[a addButtonWithTitle:@"Cancel"];
[a addButtonWithTitle:@"Fail Transaction"];

NSInteger result = [a runModal];
switch (result) {
case NSAlertFirstButtonReturn:
block(nil);
break;

case NSAlertSecondButtonReturn:
block([NSError errorWithDomain:kILSimSKErrorDomain code:kILSimSKErrorUnknown userInfo:nil]);
break;

case NSAlertThirdButtonReturn:
block([NSError errorWithDomain:kILSimSKErrorDomain code:kILSimSKErrorUnknown userInfo:nil]);
break;
}
}

@end

#endif


id <ILSimTransactionSimulator> ILSimDefaultTransactionSimulator() {
static id simulator = nil; if (!simulator) {
#if TARGET_OS_IPHONE
simulator = [ILSimDefaultTransactionSimulator_iOS new];
#elif TARGET_OS_MAC && !TARGET_OS_IPHONE
simulator = [ILSimDefaultTransactionSimulator_Mac new];
#endif
}

return simulator;
}


#endif // kILSimAllowSimulatedStoreKit
Something went wrong with that request. Please try again.