I have 2 issues :
After purchasing a subscription I receive the "You are all set Your purchase was successful" but in the SKPaymentTransactionObserver paymentQueue:updatedTransactions: I receive the SKPaymentTransactionStateFailed state with "Cannot connect to iTunes Store" error.
Some time when the purchase is initiated i receive a Confirm Purchase pop up but also the paymentQueue:updatedTransactions: is called with fail state
NOTE: I'm testing on simulator with Network Link Conditioner ( HighLatencyDNS custom profile 500ms)
Also I have notice this issue in the networks with high DNS latency more than 0.5 sec
Add Payment to queue:
- (BOOL)internalBeginPurchaseFor:(NSString *)productId
{
if ([SKPaymentQueue canMakePayments]) {
SKProduct *product;
if ((product = self.products[productId])) {
SKPayment *payment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];
return YES;
}
}
return NO;
}
Processing transaction state:
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions
{
for (SKPaymentTransaction *transaction in transactions)
{
NSString *transactionIdentifier = transaction.transactionIdentifier;
NSString *transactionDetails = #"";
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
NSLog(#"User is purchasing");
transactionDetails = #"User purchasing";
break;
case SKPaymentTransactionStatePurchased:
NSLog(#"User purchased");
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
[self executeHandlerForTransaction:transaction andSuccessFlag:YES];
transactionDetails = #"User purchased";
break;
case SKPaymentTransactionStateFailed:
NSLog(#"failed to purchase");
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
[self executeHandlerForTransaction:transaction andSuccessFlag:NO];
transactionDetails = #"Failed to purchase";
break;
case SKPaymentTransactionStateRestored:
NSLog(#"restored transaction");
transactionDetails = #"Restored transaction";
break;
case SKPaymentTransactionStateDeferred:
NSLog(#"pending for transaction");
transactionDetails = #"Pending for transaction";
break;
}
NSString *stringData = [NSString stringWithFormat:#"%# for transaction Identifier %#",transactionDetails, transactionIdentifier];
[EventLogger logEventOfType:PURCHASE_PLAN withData:stringData andNotes:#"APIAPurchase"];
}
}
any suggestions ?
Related
Hopefully this is enough information to help me solve my problem.
I am setting up In App Purchases via a templated source code that was given to me. In my MKStoreManager.m file, where I set up the featureID's for my in app purchases (Ex: static NSString *featureAId = #"com.logannat.myfirstgameTier1";)
When clicking on the actual button in the game, a method
- (void) buyFeature:(SKProduct*) product
{
if ([SKPaymentQueue canMakePayments])
{
SKPayment *payment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
}
ends up getting called. I set a break point, and found the error to be in the last line of that code.
The variables that show up in the break point state are:
self = (MKStoreManager *)"reference number"
product = (SKProduct *) nil
and
payment = (SKPayment *) "reference number"
I can not seem to figure out what the problem is here. Any help would be much appreciated, thank you.
i do not have swift Right now so i am giving you Xcode code please convert the syntax accordingly its not so difficult.
so here is the complete life cycle for the in app purchased.
in your header file call these delegates and setup the bool for checking the status of you purchased
like that
#import <UIKit/UIKit.h>
BOOL arefavorite_access;
BOOL areAdsRemoved;
#interface ViewController : UIViewController<SKProductsRequestDelegate, SKPaymentTransactionObserver>{
}
- (IBAction)purchase;
- (IBAction)restore;
- (IBAction)tapsRemoveAds:(id)sender;
#end
one you need to hockup this tapsRemoveAds method on touchupinside of you button selector ant it will start working.
- (IBAction)tapsRemoveAds:(id)sender{
UIButton*btn=(UIButton*)sender;
NSString *featureAId = #"com.logannat.myfirstgameTier1";
NSLog(#"User requests to remove ads");
if([SKPaymentQueue canMakePayments]){
NSLog(#"User can make payments");
SKProductsRequest *productsRequest;
if (btn.tag==1) {
productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject: featureAId]];
_protype= featureAId;
}
productsRequest.delegate = self;
[productsRequest start];
}
else{
NSLog(#"User cannot make payments due to parental controls");
//this is called the user cannot make payments, most likely due to parental controls
}
}
product request method
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
SKProduct *validProduct = nil;
int count = [response.products count];
if(count > 0){
validProduct = [response.products objectAtIndex:0];
NSLog(#"Products Available!");
[self purchase:validProduct];
}
else if(!validProduct){
NSLog(#"No products available");
//this is called if your product id is not valid, this shouldn't be called unless that happens.
}
}
- (IBAction)purchase:(SKProduct *)product{
SKPayment *payment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
- (IBAction) restore{
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue {
NSLog(#"%#",queue );
NSLog(#"Restored Transactions are once again in Queue for purchasing %#",[queue transactions]);
NSMutableArray *purchasedItemIDs = [[NSMutableArray alloc] init];
NSLog(#"received restored transactions: %i", queue.transactions.count);
for (SKPaymentTransaction *transaction in queue.transactions) {
NSString *productID = transaction.payment.productIdentifier;
[purchasedItemIDs addObject:productID];
NSLog (#"product id is %#" , productID);
_protype=productID;
[self doRemoveAds];
// here put an if/then statement to write files based on previously purchased items
// example if ([productID isEqualToString: #"youruniqueproductidentifier]){write files} else { nslog sorry}
}
if(queue.transactions.count==0){
[Utilities showOKAlertWithTitle:#"Restore purchase" message:#"you have no product available for restoration"];
}
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{
for(SKPaymentTransaction *transaction in transactions){
switch(transaction.transactionState){
case SKPaymentTransactionStatePurchasing: NSLog(#"Transaction state -> Purchasing");
//called when the user is in the process of purchasing, do not add any of your own code here.
break;
case SKPaymentTransactionStatePurchased:
//this is called when the user has successfully purchased the package (Cha-Ching!)
[self doRemoveAds]; //you can add your code for what you want to happen when the user buys the purchase here, for this tutorial we use removing ads
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
NSLog(#"Transaction state -> Purchased");
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
break;
case SKPaymentTransactionStateRestored:
NSLog(#"Transaction state -> Restored");
_protype=transaction.payment.productIdentifier;
[self doRemoveAds];
//add the same code as you did from SKPaymentTransactionStatePurchased here
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
break;
case SKPaymentTransactionStateFailed:
//called when the transaction does not finish
if(transaction.error.code == SKErrorPaymentCancelled){
NSLog(#"Transaction state -> Cancelled");
//the user cancelled the payment ;(
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
break;
}
}
}
- (void)doRemoveAds{
// // [self.bannerView setAlpha:0];
NSString *featureAId = #"com.logannat.myfirstgameTier1";
NSString *featureAIdnumber2 = #"com.logannat.myfirstgameTier2";
if([_protype isEqualToString:featureAId]){
arefavorite_access = YES;
[[NSUserDefaults standardUserDefaults] setBool:arefavorite_access forKey:frareFavoriteRemoved];
[[NSUserDefaults standardUserDefaults] synchronize];
}
else if([_protype isEqualToString: featureAIdnumber2]){
areAdsRemoved = YES;
[[NSUserDefaults standardUserDefaults] setBool:areAdsRemoved forKey:frareAdsRemoved];
[[NSUserDefaults standardUserDefaults] synchronize];
}
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
// [self showpopUp:self];
}
if this answer helps you please vote up and accept the answer
I create IAP in itune connect (non-consumable) to access view controller but I test in iPhone it not pop up to purchase page
my IAP product ID is "Egayov"
my app id is "my.com.th.myapp"
this is my code
- (void)viewDidLoad {
[super viewDidLoad];
if([SKPaymentQueue canMakePayments]){
NSLog(#"can buy all IAP");
SKProductsRequest *productRequest = [[SKProductsRequest alloc]initWithProductIdentifiers:[NSSet setWithObject:#"my.com.th.myapp"]];
productRequest.delegate = self;
[productRequest start];
}
else{
NSLog(#"Parental controll on");
}
}
-(IBAction)Buyvoyage{
SKPayment *vo = [SKPayment paymentWithProductIdentifier:#"my.com.th.myapp"];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:vo];
}
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
SKProduct *vaildproduct = nil;
int count = [response.products count];
if (count>0) {
vaildproduct = [response.products objectAtIndex:0];
}
if (!vaildproduct){
NSLog(#"no iap %#",vaildproduct);
}
}
-(void)paymentQueue:(SKPaymentQueue *)queue removedTransactions:(NSArray *)transactions{
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
break;
case SKPaymentTransactionStatePurchased:
[self checkiap];
break;
case SKPaymentTransactionStateRestored:
[[SKPaymentQueue defaultQueue]finishTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
if (transaction.error.code != SKErrorPaymentCancelled) {
NSLog(#"Payment Failed");
}
[[SKPaymentQueue defaultQueue]finishTransaction:transaction];
break;
}
}
}
in productrequest function it go to else condition
I had the same problem and honestly saying it wasn't solved. It's much easier to implement IAPs with Parse. It takes just few lines of code and it really works. Read this, I'm sure it will help you:
http://blog.parse.com/announcements/in-app-purchase/
When I opened app it's popup's iTunes login with out any interaction of user.That popup contains my old test user account mail id sometimes my current testuser mail id aslo.I heard that i have to call
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
I called it several times where ever I thought it's needed.But still it's asking password.
And here is my code:
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
for(SKPaymentTransaction *transaction in transactions)
{
NSLog(#"Updated transaction %#",transaction);
switch (transaction.transactionState)
{
case SKPaymentTransactionStateFailed:
[self errorWithTransaction:transaction];
[[SKPaymentQueue defaultQueue]finishTransaction:transaction];
break;
case SKPaymentTransactionStatePurchasing:
NSLog(#"Purchasing...");
break;
case SKPaymentTransactionStatePurchased:
{
if(no need to download)
{
// I will download the content later by restoring transactions.
[[SKPaymentQueue defaultQueue]finishTransaction:transaction];
}
else
{
[[SKPaymentQueue defaultQueue] startDownloads:transaction.downloads];
}
}
case SKPaymentTransactionStateRestored:
{
if(no need to download content)
{
[[SKPaymentQueue defaultQueue]finishTransaction:transaction];
}
else
{
[[SKPaymentQueue defaultQueue] startDownloads:transaction.downloads];
}
}
break;
default:
break;
}
}
-(void) paymentQueue:(SKPaymentQueue *)queue updatedDownloads:(NSArray *)downloads
{
for (SKDownload *download in downloads)
{
switch (download.downloadState)
{
case SKDownloadStateActive:
{
// showing download progress
}
case SKDownloadStateCancelled:
{
[[SKPaymentQueue defaultQueue] finishTransaction:download.transaction];
}
case SKDownloadStateFailed:
{
// showing alert and finish transactions
[[SKPaymentQueue defaultQueue] finishTransaction:download.transaction];
}break;
case SKDownloadStateFinished:
{
// processing content and finished the transactions
[[SKPaymentQueue defaultQueue] finishTransaction:download.transaction];
}break;
}
}
}
Is this correct and is there any place to call finishTransaction: method.Please let me know is any...
thanks in advance..
I used info from this brilliant page to add in-app purchases.
However I get the NSLog No Product Available. I have checked to make sure I have added the correct Product ID. I am at a loss to what to do and have been at this for several hours.
This is for a game built on Cocos2d and I can't figure out what is wrong. Sorry for the abundance of code.
I have added <SKProductsRequestDelegate, SKPaymentTransactionObserver> and <StoreKit/StoreKit.h>
.m
#define kRemoveAdsProductIdentifier #"COINS1000"
#implementation shopCoins
- (void)buy500Coins{
NSLog(#"User requests to remove ads");
if([SKPaymentQueue canMakePayments]){
NSLog(#"User can make payments");
SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:kRemoveAdsProductIdentifier]];
productsRequest.delegate = self;
[productsRequest start];
}
else{
NSLog(#"User cannot make payments due to parental controls");
//this is called the user cannot make payments, most likely due to parental controls
}
}
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
SKProduct *validProduct = nil;
int count = [response.products count];
if(count > 0){
validProduct = [response.products objectAtIndex:0];
NSLog(#"Products Available!");
[self purchase:validProduct];
}
else if(!validProduct){
NSLog(#"No products available");
//this is called if your product id is not valid, this shouldn't be called unless that happens.
}
}
- (IBAction)purchase:(SKProduct *)product{
SKPayment *payment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
- (IBAction) restore{
//this is called when the user restores purchases, you should hook this up to a button
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
- (void) paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
NSLog(#"received restored transactions: %i", queue.transactions.count);
for (SKPaymentTransaction *transaction in queue.transactions)
{
if(SKPaymentTransactionStateRestored){
NSLog(#"Transaction state -> Restored");
//called when the user successfully restores a purchase
[self doRemoveAds];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
}
}
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{
for(SKPaymentTransaction *transaction in transactions){
switch (transaction.transactionState){
case SKPaymentTransactionStatePurchasing: NSLog(#"Transaction state -> Purchasing");
//called when the user is in the process of purchasing, do not add any of your own code here.
break;
case SKPaymentTransactionStatePurchased:
//this is called when the user has successfully purchased the package (Cha-Ching!)
[self doRemoveAds]; //you can add your code for what you want to happen when the user buys the purchase here, for this tutorial we use removing ads
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
NSLog(#"Transaction state -> Purchased");
break;
case SKPaymentTransactionStateRestored:
NSLog(#"Transaction state -> Restored");
//add the same code as you did from SKPaymentTransactionStatePurchased here
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
//called when the transaction does not finnish
if(transaction.error.code != SKErrorPaymentCancelled){
NSLog(#"Transaction state -> Cancelled");
//the user cancelled the payment ;(
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
}
}
}
- (void)doRemoveAds{
NSLog(#"Bitches");
}
If it's a debug build you probably need to add a sandbox user on iTunes connect this will allow you to use IAP that have not been reviewed yet. Note this user should match your iTunes connect account.
I am working on an app with in-app-purchase up to the transaction part.
I have a purchaseViewController to call up getProductInfo while launch:
-(void)getProductInfo: (CC3DeviceCameraOverlayUIViewController *) viewController
{
_homeViewController = viewController;
if ([SKPaymentQueue canMakePayments])
{
SKProductsRequest *request = [[SKProductsRequest alloc]
initWithProductIdentifiers:
[NSSet setWithObject:self.productID]];
request.delegate = self;
[request start];
}
else
_productDescription.text =
#"Please enable In App Purchase in Settings";
}
which is successful. Then I have a button to call buyProduct:
- (IBAction)buyProduct:(id)sender {
SKPayment *payment = [SKPayment paymentWithProduct:_product];
if(_myQueue == nil) {
_myQueue = [SKPaymentQueue defaultQueue];
}
[_myQueue addPayment:payment];
NSLog(#"buyproduct happening %#", _product.localizedTitle);
}
which also successful because I see the NSLog and the product title.
But after this, I always get a transaction failed! when the purchasing process up to
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
for (SKPaymentTransaction *transaction in transactions)
{
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchased:
// [self unlockFeature];
NSLog(#"Transaction purchased");
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
if (_product.downloadable) {
[[SKPaymentQueue defaultQueue] startDownloads:transaction.downloads];
}
break;
case SKPaymentTransactionStateRestored:
if (transaction.downloads) {
[[SKPaymentQueue defaultQueue] startDownloads:transaction.downloads];
NSLog(#"Transaction downloading now...");
} else {
// unlock features
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
NSLog(#"Transaction finish");
}
break;
case SKPaymentTransactionStateFailed:
NSLog(#"Transaction Failed");
[[SKPaymentQueue defaultQueue]
finishTransaction:transaction];
break;
default:
break;
}
}
}
What seems to be wrong and how can I debug this part? Is there a way for me to clear previous transaction test or check if there is any pending transaction interfere with the current test? I am still testing it in sandbox mode. The error was thrown in a quite immediate manner that give me the impression that it did not really go through Apple server and the transaction error happen locally, some sort of previous transaction test conflict or something that I still yet to find out.
One more point to mention is that I am purchasing non-consumable item, which if transaction successful, it will trigger the download(I hope.).