In-App Purchases in iOS 6 Tutorial: Consumables and Receipt Validation

Discussion of the official tutorials published on raywenderlich.com. Please only discuss the official tutorials here - for general questions, use the General Discussion forum instead.

In-App Purchases in iOS 6 Tutorial: Consumables and Receipt Validation

Postby rwenderlich » Thu Dec 13, 2012 11:00 am

This is the official thread to discuss the following blog post: In-App Purchases in iOS 6 Tutorial: Consumables and Receipt Validation
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ray Wenderlich
Blog: http://www.raywenderlich.com
Twitter: http://twitter.com/rwenderlich
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
User avatar
rwenderlich
Team Member
Site Admin
 
Posts: 2168
Joined: Thu Dec 23, 2010 4:14 pm
Has thanked: 28 times
Been thanked: 349 times

Re: In-App Purchases in iOS 6 Tutorial: Consumables and Rece

Postby PhillyNJ » Sat Dec 15, 2012 12:39 pm

I always appreciate a great tutorial, but I must ask why you use those graphics of the characters (e.g. Random rage face) - Image??

Just curious, but I despise these images for some reason. IMHO, they take aways from the tutorial...
PhillyNJ
Baby Hacker
 
Posts: 5
Joined: Sat Jan 14, 2012 12:32 am
Has thanked: 0 time
Been thanked: 0 time

Re: In-App Purchases in iOS 6 Tutorial: Consumables and Rece

Postby rwenderlich » Sat Dec 15, 2012 4:12 pm

Heh, they are love them or hate them I guess. Personally I like them because I think they help make the tutorial a bit more amusing/lighthearted - otherwise tutorials can get pretty dry and technical. Maybe I'll run a poll sometimes to see what people think :]
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
Ray Wenderlich
Blog: http://www.raywenderlich.com
Twitter: http://twitter.com/rwenderlich
- - - - - - - - - - - - - - - - - - - - - - - - - - - -
User avatar
rwenderlich
Team Member
Site Admin
 
Posts: 2168
Joined: Thu Dec 23, 2010 4:14 pm
Has thanked: 28 times
Been thanked: 349 times

Re: In-App Purchases in iOS 6 Tutorial: Consumables and Rece

Postby jarocep » Mon Dec 17, 2012 8:49 pm

Hi Ray,
As always, Excellent Tutorial!

I have a little question: When I use the code in this tutorial for receipt validation, ¿Do I have to check the "Uses Encryption" box in iTunes Connect?

Thanks,
Jarocep
jarocep
Hacker
 
Posts: 12
Joined: Sun Sep 11, 2011 10:42 pm
Has thanked: 0 time
Been thanked: 1 time

Re: In-App Purchases in iOS 6 Tutorial: Consumables and Rece

Postby Abhay » Wed Dec 26, 2012 7:56 am

great tutorial,

i have a question,
i have non-consumable product,how do i send request using skpayment for MultipleProduct Id's in IOS.In my project I have 5 products. So I need to buy 3 products to send payment request for those particular productID's, do I need to loop it like below.
for (int i;i<[productsID's count];i++)
{
skmutablepayment *payment =[skmutablepayment paymentwithproductidentifier:
[product objetatindex:i]];
}
I need the payment only for specific products which I have selected. How do I pass those selected id's to skpayment?

your help is greatly appreciated.
Abhay
n00b
 
Posts: 1
Joined: Wed Dec 26, 2012 7:35 am
Has thanked: 0 time
Been thanked: 0 time

Re: In-App Purchases in iOS 6 Tutorial: Consumables and Rece

Postby tomDev » Sat Jan 05, 2013 3:57 am

Hi, thank you so much for the tutorial, everything works great, but instead of a table view I would like to create a simple IBAction button.

I tried several ways but I couldn't make it work. Just a simple button to start the process.

I'm trying like this:

Code: Select all
- (IBAction)button10Coins:(SKProduct *)product {

     SKPayment * payment = [SKPayment paymentWithProduct:@"com.companyname.10coins"];
     [[SKPaymentQueue defaultQueue] addPayment:payment];
 }


Or this:

Code: Select all
-(IBAction)buyProductButton:(id)sender {

    SKProduct *product = _products;

    NSLog(@"Buying %@...", product.productIdentifier);
    [[RageIAPHelper sharedInstance] buyProduct:product]; }


But doesn't work. Any ideas? I would reallly appreciate an answer, it can't be so difficult. This is the ONLY thing missing on a 3 months project.
tomDev
n00b
 
Posts: 3
Joined: Sat Jan 05, 2013 3:33 am
Has thanked: 0 time
Been thanked: 0 time

Re: In-App Purchases in iOS 6 Tutorial: Consumables and Rece

Postby DroolGames » Fri Jan 11, 2013 6:19 am

Hey,

I was just wondering what if I wanted to do a simple button to purchase an item within my view controller. I have some code:

- (IBAction)purchase {

SKPayment *payment = [SKPayment paymentWithProductIdentifier:@"com.500k"];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];

status.text = @"Please wait...";

}


- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {

SKProduct *validProduct = nil;
int count = [response.products count];
if (count >0) {
validProduct = [response.products objectAtIndex:0];
} else if (!validProduct) {

NSLog(@"No products avaiable");
}
}

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {

for (SKPaymentTransaction *transaction in transactions) {

switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
status.text = @"Processing...";
break;

case SKPaymentTransactionStatePurchased:


[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
[self purchaseCOMPLETE];
status.text = @"Purchase complete";

break;

case SKPaymentTransactionStateRestored:
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
status.text = @"";

break;

case SKPaymentTransactionStateFailed:
if (transaction.error.code != SKErrorPaymentCancelled) {
NSLog (@"An error encountered");
}

[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
status.text = @"Purchase error";
break;
}

}
}

-(void)purchaseCOMPLETE {

self.currentMoneyStore = self.currentMoneyStore + 500000;
moneyLabelStore.text = [NSString stringWithFormat: @"$ %lld", self.currentMoneyStore];
}

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

if ([SKPaymentQueue canMakePayments]) {

NSLog(@"Parental-controls are disbaled");

SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:@"com.500k"]];
productsRequest.delegate = self;
[productsRequest start];

} else {

NSLog(@"Parental-controls are enabled");
}

}


Everything works great but I get the warning (paymentWithProductIdentifier) is deprecated in iOS 5 every time. Would this code still work for my app in I submit a binary? Is there a better way to code this? Please help.
DroolGames
n00b
 
Posts: 2
Joined: Fri Jan 11, 2013 6:12 am
Has thanked: 0 time
Been thanked: 0 time

Re: In-App Purchases in iOS 6 Tutorial: Consumables and Rece

Postby DroolGames » Mon Jan 14, 2013 7:07 am

Hey everyone,

I figured out if someone wanted a single button to make a purchase than they would simply apply this code:


-(IBAction)purchase25k:(id)sender {
SKProductsRequest *request= [[SKProductsRequest alloc]
initWithProductIdentifiers: [NSSet setWithObject: @"com.25k"]];
request.delegate = self;
[request start];


//Added a UIlabel to display status of purchase
status.text = @"Processing..Please wait..";
}


-(IBAction)purchase75k:(id)sender {
SKProductsRequest *request= [[SKProductsRequest alloc]
initWithProductIdentifiers: [NSSet setWithObject: @"com.75k"]];
request.delegate = self;
[request start];

//Added a UIlabel to display status of purchase
status.text = @"Processing..Please wait..";
}

[size=150]Than after you enter your transaction methods as explained in the tutorial IF you have more than one button you can add[/size]

- (void)provideContentForProductIdentifier:(NSString *)productIdentifier {

if ([productIdentifier isEqualToString:@"com.25k"]) {

self.currentMoneyStore = self.currentMoneyStore + 25000;
moneyLabelStore.text = [NSString stringWithFormat: @"$ %lld", self.currentMoneyStore];

status.text = @"Purchase complete";
} else if ([productIdentifier isEqualToString:@"com.75k"]) {

self.currentMoneyStore = self.currentMoneyStore + 75000;
moneyLabelStore.text = [NSString stringWithFormat: @"$ %lld", self.currentMoneyStore];

status.text = @"Purchase complete";

}



email me for any confusion or help

droolgames@gmail.com
DroolGames
n00b
 
Posts: 2
Joined: Fri Jan 11, 2013 6:12 am
Has thanked: 0 time
Been thanked: 0 time

Re: In-App Purchases in iOS 6 Tutorial: Consumables and Rece

Postby ajmccall » Mon Jan 14, 2013 12:55 pm

Hi,

I was wondering about server-side receipt validation and man-in-the-middle attacks. I asked the question on StackOverflow and it might be easier to check it out rather than rehashing it in this forum.

http://stackoverflow.com/questions/14243909/apple-in-app-purchase-receipt-verification

Thanks for the tutorial. Best of luck
Al
ajmccall
n00b
 
Posts: 1
Joined: Mon Jan 14, 2013 12:52 pm
Has thanked: 0 time
Been thanked: 0 time

Re: In-App Purchases in iOS 6 Tutorial: Consumables and Rece

Postby Ralph » Mon Feb 04, 2013 11:22 am

Hi,

thank you for the great tutorials.
Just a small remark.

In the previous tutorial the restoreTransaction method uses "originalTransaction" (transaction.originalTransaction.payment.productIdentifier).

Like so:

Code: Select all
- (void)completeTransaction:(SKPaymentTransaction *)transaction {
    [self provideContentForProductIdentifier:transaction.payment.productIdentifier];
    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}

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


In this new tutorial, "validateReceiptForTransaction" always works with: "transaction.payment.productIdentifier".
"originalTransaction" is not used.

Is this on purpose?


Also, if validate receipt does not succeed, it appears that the following code will get called twice:
Code: Select all
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];

Once in "validateReceiptForTransaction" and once in for example "completeTransaction".
Any harm done by this?

Thanks again.
All the best
Ralph
Ralph
n00b
 
Posts: 1
Joined: Mon Feb 04, 2013 11:04 am
Has thanked: 0 time
Been thanked: 1 time

Next

Return to Official Tutorials

Who is online

Users browsing this forum: No registered users and 14 guests