用户取消应用购买。如何结束交易

时间:2010-12-21 23:54:22

标签: iphone xcode in-app-purchase

我对iAP有一个小问题。我的代码大部分都在工作。只有当用户取消购买时,设备才会认为结束并保留它。因此,如果你再次购买它,你将购买2个产品......这很糟糕,因为他们只想要一个。我一定错过了一些代码。我将附上一个控制台日志,以便您了解最新情况。

2010-12-21 15:47:29.364[204:307] Parental-controls are disabled (I launch the iAP)
2010-12-21 15:47:33.816[204:307] Win (There are products)
2010-12-21 15:47:37.043[204:307] Buying... (I pressed buy)
2010-12-21 15:47:40.661[204:307] FailedTransaction (Essentially I canceled)
2010-12-21 15:47:40.674[204:307] DefaultAction (which is to dismiss the modal view)
2010-12-21 15:47:42.738[204:307] Parental-controls are disabled (reopen the iAP view)
2010-12-21 15:47:44.440[204:307] Win
2010-12-21 15:47:46.939[204:307] Buying...
2010-12-21 15:47:46.945[204:307] Buying...
2010-12-21 15:47:48.414[204:307] FailedTransaction
2010-12-21 15:47:48.427[204:307] DefaultAction
2010-12-21 15:47:48.437[204:307] FailedTransaction
2010-12-21 15:47:48.449[204:307] DefaultAction
2010-12-21 15:47:50.622[204:307] Parental-controls are disabled
2010-12-21 15:47:51.314[204:307] Win
2010-12-21 15:47:51.883[204:307] Buying...(see how it compounds?)
2010-12-21 15:47:51.889[204:307] Buying...
2010-12-21 15:47:51.896[204:307] Buying...
2010-12-21 15:48:10.764[204:307] Posted5
2010-12-21 15:48:10.782[204:307] Yipeee!
2010-12-21 15:48:10.810[204:307] Posted5 (It gave the users 15 credits!)
2010-12-21 15:48:10.869[204:307] Yipeee!
2010-12-21 15:48:10.897 204:307] Posted5
2010-12-21 15:48:10.912[204:307] Yipeee!

以下是我正在运行的代码:

#import "PurchaserViewController.h"

@implementation PurchaserViewController


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"yayadaadul.com/checkcredits.php"]]];

    if ([SKPaymentQueue canMakePayments]) {
        NSLog(@"Parental-controls are disabled");

        SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:@"com.company.name.song"]];
        productsRequest.delegate = self;
        [productsRequest start];
    } else {
        NSLog(@"Parental-controls are enabled");
    }
}

- (IBAction)purchase {


    SKPayment *payment = [SKPayment paymentWithProductIdentifier:@"com.changedwe.worksinxcode.song"];
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
}

- (IBAction)stop {
    [player stop];
}

- (IBAction)play {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *file = [NSString stringWithFormat:@"%@/Song.mp3", documentsDirectory];

    player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:file] error:nil];
    [player play];
}

- (void)downloadFromURL:(NSURL *)url {
    NSData *data = [NSData dataWithContentsOfURL:url];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *file = [NSString stringWithFormat:@"%@/Song.mp3", documentsDirectory];

    [data writeToFile:file atomically:YES];
}

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
    SKProduct *validProduct = nil;
    int count = [response.products count];
    if (count > 0) {
        validProduct = [response.products objectAtIndex:0];
        NSLog(@"Win");

    } else if (!validProduct) {
        NSLog(@"No products available");


    }
}
-(IBAction)home {
    [self dismissModalViewControllerAnimated:YES];

}


- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
    for (SKPaymentTransaction *transaction in transactions) {
        switch (transaction.transactionState) {
            case SKPaymentTransactionStatePurchasing:
                NSLog(@"Buying...");
                break;
                break;

            case SKPaymentTransactionStatePurchased:
                [self downloadFromURL:[NSURL URLWithString:@""]];

                NSString *post =[NSString stringWithFormat:@"username=lol&cval=5"];

                NSString *hostStr = @"http://mysite.com/subdom/ccredit.php?";
                hostStr = [hostStr stringByAppendingString:post];
                NSLog(@"Posted5");


                NSLog(@"Yipeee!");
                [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://Action to Post.php?"]]];



                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];

                break;

            case SKPaymentTransactionStateRestored:
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                NSLog(@"Restored");

                break;

            case SKPaymentTransactionStateFailed:
                NSLog(@"FailedTransaction");
            //  [self finishTransaction:transaction wasSuccessful:NO];

                if (transaction.error.code != SKErrorPaymentCancelled) {
                    NSLog(@"An error encounterd");
                }

                //You need to clear their purchases here


                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                NSLog(@"DefaultAction");


                [self dismissModalViewControllerAnimated:YES];

                break;
        }
    }
}

2 个答案:

答案 0 :(得分:2)

是。您需要实现SKRequest的成功和失败委托方法。请参阅文档。 http://developer.apple.com/library/ios/#documentation/StoreKit/Reference/SKRequestDelegate/Reference/Reference.html#//apple_ref/doc/uid/TP40008416

- (void)requestDidFinish:(SKRequest *)request
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error

答案 1 :(得分:2)

每次执行purchase操作时,您都会继续将自己添加为事务观察者。按一下,一切正常。再次按下它,你的观察者委托方法被调用两次,两者都用于第二次交易。