Reputation: 4383
I am working on in app purchase with ios and i have few doubts, that doubts will be helpful for fresher like me so understand In app purchase.
1)I got problem in my app if user "install my app into new device or same device if he remove my app before" at that time when user try to buy already purchase item my code does not callrestoreTransaction
in switch case of updatedTransactions
I get the message that you ve already purchased this tap okay to downlaod it FREE Enviornment sandbox
and it call the SKPaymentTransactionStatePurchased
case but it does not call the SKPaymentTransactionStateRestored
what will the problem in my case..
So i have implementing separate Restore Button to restore all video item already brought by user so just need to know that will it reject my app at apple store?
2) For purchase of item it ask me password only one time and after that it doesn't ask me for password to purchase. it directly display the dialog box with confirm button but my project manager says it should ask for password for every item purchase.
It ask for password every time when i try to restore the Purchase..strange.
3) Currently i am testing in sandbox when i try to purchase with real apple id it display purchase failed(i have to use test account to test purchase as apple document says) but my project manager says that it should ask for new test username if you are testing in sandbox (as document said you have to sign out from setting manually but my project manager want to it should do automatically) ,
So just need to ask that is it possible to sign-out and display sign box by coding ( i know its not possible but for information i ask)
4)Currently my app is working in sandbox environment but do i need to change something for real purchase for my app?..or apple will automatically change sandbox to real purchase when apple validate my app and sign it and available on app store?
5)i am validating transaction on my own server so i am sending sandbox 1 if i am on sandbox environment otherwise i have to send 0 ( currently i hardcode sandbox value to 1)so is there any method to detect environment is sandbox or real?
Here is my purchase code and Restore button code any help is appreciated
Purchase code
- (IBAction)PaymentButton:(id)sender {
loadingHUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
loadingHUD.labelText = NSLocalizedString(@"Loading", nil);
[loadingHUD show:YES];
[self startPurchase];// call the restore Purchase method
//[loadingHUD showWhileExecuting:@selector(startPurchase) onTarget:self withObject:nil animated:YES];// call the restore Purchase method
}
- (void)startPurchase {
if([SKPaymentQueue canMakePayments]) {
NSLog(@"IN-APP:can make payments");
[self requestProductData];
}
else {
NSLog(@"IN-APP:can't make payments");
loadingHUD.hidden=YES;
}
}
- (void)requestProductData {
NSLog(@"IN-APP:requestProductData");
SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers: [NSSet setWithObject:myIdentifier]];
request.delegate = self;
[request start];
NSLog(@"IN-APP:requestProductData END");
NSLog(@"Productdata is %@",myIdentifier);
}
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
@try {
SKProduct *product = [response.products objectAtIndex:0];
SKPayment *newPayment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addPayment:newPayment];
NSLog(@"IN-APP:productsRequest END");
}
@catch (NSException *exception) {
// Failed to purchase Hide the progress bar and Display Error Dialog
loadingHUD.hidden=YES;
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Error in Product id can not purchase" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
}
- (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;
}
}
}
- (void) completeTransaction: (SKPaymentTransaction *)transaction
{
NSLog(@"Transaction Completed");
// Finally, remove the transaction from the payment queue.
[self verifyReceipt:transaction]; // Call the verifyReceipt method to send transaction.bytes
NSLog(@"Purchase Transaction finish");
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
- (void) restoreTransaction: (SKPaymentTransaction *)transaction
NSLog(@"Transaction Restored %@",transaction.originalTransaction.payment.productIdentifier);
// You can create a method to record the transaction.
// [self recordTransaction: transaction];
loadingHUD.hidden=YES;
// You should make the update to your app based on what was purchased and inform user.
// [self provideContent: transaction.payment.productIdentifier];
// Finally, remove the transaction from the payment queue.
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
- (void) failedTransaction: (SKPaymentTransaction *)transaction
{
loadingHUD.hidden=YES;// hide loadingHUD
if (transaction.error.code != SKErrorPaymentCancelled)
{
// Display an error here.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Purchase Unsuccessful"
message:@"Your purchase failed. Please try again."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
For restore it simple
-(void)startRestore
{
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
- (void) paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
if ([queue.transactions count] == 0)
{
HUD.hidden=YES;
UIAlertView *restorealert = [[UIAlertView alloc]
initWithTitle:@"Restore"
message:@"There is no products purchased by you"
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[restorealert show];
}
else
{
NSLog(@"received restored transactions: %i", queue.transactions.count);
for (SKPaymentTransaction *transaction in queue.transactions)
{
NSString *temp = transaction.payment.productIdentifier;
NSString *testID = [temp stringByReplacingOccurrencesOfString:projectIdString withString:@""];
NSString *productID = [testID stringByReplacingOccurrencesOfString:@"." withString:@""]; // remove Dot
NSLog(@"cutted string is %@",productID);
[purchasedItemIDs addObject:productID];
NSLog(@"** Purchased item is %@",purchasedItemIDs);
}
HUD.hidden=YES;
HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
HUD.labelText = NSLocalizedString(@"Restoring", nil);
[HUD showWhileExecuting:@selector(restorePurchasedItem) onTarget:self withObject:nil animated:YES];// call the restore Purchase method
}
}
Upvotes: 4
Views: 4334
Reputation: 846
IMHO you'd better crop this question in some number if different questions.
Anyway, I'll try to answer:
1) That's how it meant to be.
You get transactions with SKPaymentTransactionStateRestored
state only if you call restoreCompletedTransactions
manually. AFAIK, that's the normal practise to create one button (for example, "Restore purchases") for that.
2) Can't say anything about that. Normally, every time, when your app is going to make a purchase (and take some user's money), it should ask user for password.
3) AFAIK, no. You work with Apple servers through iTunes/AppStore apps. It's their business to remember user's iTunes account. And I don't think they give you any way to make current user logged out. Your project manager should understand it :-)
4), 5) There is no difference in production/sandbox environments until you try to verify the receipt. If you talk about work with server, I hope, you use server to verify receipts.
At device's side, all what you do is working with StoreKit
framework. You don't define any URLs to Apple servers, you just use framework's classes and call it's methods. AFAIK, you don't need to make any changes to your code for sandbox and production support at the same time.
But at your server's side, there is difference: when your app is in production already, you should send HTTP POST requests to https://buy.itunes.apple.com/verifyReceipt
. At the other hand, when your app is still in the sandbox, use https://sandbox.itunes.apple.com/verifyReceipt
.
How to handle that? Watch WWDC 2012 video 308 about Auto-Renewable subscriptions. They suggest 2 ways:
1) Smart server. When your app send receipts to your server, it also passes some parameter, to let server know, which Apple's server to use. As I see, you already use this method.
2) Reactive server. Your server always send receipts to Apple's production server. You check the response, and if the status is 21007
(following the documentation, "This receipt is a sandbox receipt, but it was sent to the production service for verification."), you send the same request to Apple's sandbox server.
Upvotes: 2
Reputation: 2579
Use this code, it might help you....
//To Show Alert of InAppPurchase
- (void)showAlert
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Click buy to
purchase full tracks and other functionalities of the application." delegate:self
cancelButtonTitle:nil otherButtonTitles:nil, nil];
[alert addButtonWithTitle:@"Buy"];
[alert addButtonWithTitle:@"Restore Transaction"];
[alert addButtonWithTitle:@"Cancel"];
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:
(NSInteger)buttonIndex{
if(buttonIndex==0)
{
// For buy an item
[self Declarations];
}
else if(buttonIndex == 1)
{
//For Restore Previous Transaction
[self restorePreviousTransaction:nil];
}
else
{
//Do something here..
}
}
#pragma mark In App Purchase
-(void)Declarations
{
if ([SKPaymentQueue canMakePayments]) {
NSLog(@"parental functions are disabled");
SKProductsRequest *productRequest = [[SKProductsRequest
alloc]initWithProductIdentifiers:[NSSet
setWithObjects:@"com.tapmobi.careerandsuccess.inapp",nil]];
productRequest.delegate=self;
[productRequest start];
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
}
else
{
NSLog(@"parental functions are enabled");
}
}
- (IBAction)restorePreviousTransaction:(id)sender {
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:
(SKProductsResponse *)response
{
SKProduct *validProduct=nil;
int count = [response.products count];
NSLog(@"number of prouducts present:%d",count);
if(count==0){
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
return;
}
validProduct = [response.products objectAtIndex:0];
NSLog(@"the product is :%@",validProduct.localizedTitle);
SKPayment *skpayment = [SKPayment paymentWithProduct:validProduct];
[[SKPaymentQueue defaultQueue] addPayment:skpayment];
[[SKPaymentQueue defaultQueue]addTransactionObserver:self];
}
-(void)requestDidFinish:(SKRequest *)request
{
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
}
-(void)request:(SKRequest *)request didFailWithError:(NSError *)error
{
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
NSLog(@"Failed to connect with error: %@", [error localizedDescription]);
}
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
NSString *message = [[NSString alloc]init];
BOOL bSuccess = NO;
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState)
{
case SKPaymentTransactionStatePurchasing:
NSLog(@"stuff is getting purchased");
break;
case SKPaymentTransactionStatePurchased:
NSLog(@"purchased properly");
message = @"Thank you.";
[[NSUserDefaults standardUserDefaults] setValue:@"Full Version"
forKey:PURCHASED_KEY];
[[SKPaymentQueue defaultQueue]finishTransaction:transaction];
bSuccess = YES;
break;
case SKPaymentTransactionStateRestored:
[[SKPaymentQueue defaultQueue]finishTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
if (transaction.error.code != SKErrorPaymentCancelled) {
NSLog(@"error happened");
message = @"Purchase is not successfull. Try again later";
}
[[SKPaymentQueue defaultQueue]finishTransaction:transaction];
break;
default:
break;
}
}
if (bSuccess){
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"" message:message
delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}
Happy Coding..
Upvotes: 0