6

Hi I'm trying to do some server integration with my IAPs in my iOS app. The params for what I'm sending to the server require: transaction_ID, product_ID, and pennyCost.

The first two were easy enough to figure out but I can't find the cost. I do have some NSData for the receipt but I can't figure out how to get that into any form I can use. This is my code so far.

- (void)completeTransaction:(SKPaymentTransaction *)transaction {
 NSLog(@"completeTransaction...");

 purchaseComplete = 2;

 NSString *transaction_key = transaction.transactionIdentifier;
 NSString *productID = transaction.payment.productIdentifier;
 NSNumber *pennyCost = transaction.payment.product.pennyCost;

 NSData *rec = [self receiptForPaymentTransaction:transaction];


}

The transaction.payment.product.pennyCost line is invalid.

Any help would be awesome thanks!

Sean Calkins
  • 312
  • 2
  • 11
  • The price is part of the `SKProduct` used to create the `SKPayment` you sent. – rmaddy May 18 '16 at 23:15
  • Yes, but I don't have access to the product in the completeTransaction as far as I can tell. – Sean Calkins May 18 '16 at 23:39
  • 1
    @SeanCalkins that's correct. If you want to capture the cost of an IAP you must do so when you submit the purchase request. You can use the `SKProduct` from the `SKTransaction` to retrieve the *current price* of that product, but this may not be the price that applied when the purchase was made – Paulw11 May 18 '16 at 23:45

1 Answers1

12

SKPayment doesn't have a price, oddly enough. The price is defined on the SKProduct after retrieving the product using an SKProductRequest.

What you need to do is keep a reference to the SKProduct instances returned from the SKProductRequest. Once you submit a payment to the SKPaymentQueue and you get back your response, use the productId of the SKPayment to find the matching SKProduct.

Now it's possible that you will receive a payment transaction later, after your app has been restarted. In that case, you will have to make a new SKProductRequest to get the SKProduct so you can get the price.

The bad part about all of this is that the price you get for the product could different from what the user is actually charged since you might update the product's pricing in between the time you received the payment transaction and the time you request the product info.

rmaddy
  • 314,917
  • 42
  • 532
  • 579