4

can we assign skpayment applicationusername?

When in app purchase is completed, I got a null applicationUsername in theSKPayment.

How do I assign a value to SKPayment applicationUsername?

Paulw11
  • 108,386
  • 14
  • 159
  • 186
Ankur Patel
  • 477
  • 5
  • 19

3 Answers3

2

You can't assign a value to the property of an SKPayment; When you create the SKMutablePayment instance in order to submit the purchase request, your app can provide an opaque value that represents the application user id. This enables the iTunes store to detect irregular activity.

Despite its name, the applicationUsername property is not meant to hold the actual username.

A suggested approach is shown in the In-App Purchasing Programming Guide

Note that the value you assigned may not be present in the SKPayment you receive on the transaction queue.

Important

The applicationUsername property is not guaranteed to persist between when you add the payment transaction to the queue and when the queue updates the transaction. Do not attempt to use this property for purposes other than providing fraud detection.

Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • The question appears to refer to a completed purchase. As in when he receives SKPaymentTransaction in the paymentQueue: updatedTransactions: method. The returned objects are immutable. – Gregory Furmanek Sep 26 '17 at 16:50
  • Yes, I know. I didn’t say to mutate the returned object. I said you need to assign a value when you submit the purchase – Paulw11 Sep 26 '17 at 20:57
  • I am not sure if the question was correctly asked or my understanding of the English language is failing me but when someone states "When in app purchase is completed" I assume you have received SKPaymentTransaction that includes SKPayment in paymentQueue:updatedTransactions: observer method. An immutable object. – Gregory Furmanek Sep 28 '17 at 16:05
  • Right, the question was asking how you assign a value so that that property isn’t `nil` in the completed transaction; the answer is that you must assign a value before you submit the payment request to Apple as an SKMutablePayment. – Paulw11 Sep 28 '17 at 20:24
1

You can assign it like :

   // MARK: purchase SKProduct from store
    @objc func buy(product: SKProduct, withHandler handler: @escaping ((_ success : Bool , _ error :RiscoIAPManagerError? ) -> Void)) {
        let payment = SKMutablePayment(product: product)
        
        payment.applicationUsername = "applicationUsername"
        SKPaymentQueue.default().add(payment)
        // Keep the completion handler.
        productBuyComplition = handler
    }

And can get it again in

     func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
                    print(transaction.payment.applicationUsername as Any)

}
guru
  • 2,727
  • 3
  • 27
  • 39
-2

You may not assign a read only property. The objects iOS is providing on completed transaction are immutable. To be honest Apple's API fails to properly inform the client apps about the processed transaction.

Gregory Furmanek
  • 364
  • 1
  • 13
  • **@property(nonatomic, copy, readwrite) NSString *applicationUsername NS_AVAILABLE_IOS(7_0);** I think iOS 7 onwards we can assign value to applicationUsername I take property description from SKPayment.h – Ankur Patel Sep 27 '17 at 05:03
  • We are talking about SKPayment, right? Readwrite property is part of SKMutablePayment.h In a copy of the SKPayment.h file you will find on line 44 redeclaration of the applicationUsername property is part of SKMutablePayment which starts on line 42. (Ref: https://github.com/theos/sdks/blob/master/iPhoneOS10.1.sdk/System/Library/Frameworks/StoreKit.framework/Headers/SKPayment.h) – Gregory Furmanek Sep 28 '17 at 15:59
  • sorry you are right oh ya Sorry right I got it. I have problem in below question can you guide me plz? https://stackoverflow.com/questions/46218509/get-blank-response-subscription-status-url-iap-from-apple-server-status-update get Blank Response Subscription Status URL IAP from Apple server, Status Update Notifications, statusUpdateNotification – Ankur Patel Sep 29 '17 at 10:59