Last active
July 15, 2023 16:35
-
-
Save wisaruthk/c7daf2bb760b2ff1083a to your computer and use it in GitHub Desktop.
iOS In App Purchase Example with appusername
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// FVRInAppPurchaseVC.swift | |
// favroutes | |
// | |
// Created by wisaruthk on 10/8/2558 BE. | |
// Copyright © 2558 kojo. All rights reserved. | |
// | |
import UIKit | |
import StoreKit | |
class FVRInAppPurchaseVC: UIViewController, SKProductsRequestDelegate, SKPaymentTransactionObserver { | |
@IBOutlet weak var purchaseButton: UIButton! | |
@IBOutlet weak var restorePurchaseButton:UIButton! | |
var product_id:String?; | |
var appusername:String?; | |
override func viewDidLoad() { | |
product_id = "item4"; | |
appusername = "[email protected]"; | |
super.viewDidLoad() | |
SKPaymentQueue.defaultQueue().addTransactionObserver(self) | |
self.title = "In App Purchase" | |
// | |
} | |
override func viewWillAppear(animated: Bool) { | |
super.viewWillAppear(animated) | |
// | |
} | |
override func viewDidDisappear(animated: Bool) { | |
super.viewDidDisappear(animated) | |
SKPaymentQueue.defaultQueue().removeTransactionObserver(self) | |
} | |
@IBAction func buyConsumable(sender: AnyObject) { | |
print("About to fetch the products"); | |
// We check that we are allow to make the purchase. | |
if (SKPaymentQueue.canMakePayments()) | |
{ | |
let productID:Set<String> = Set<String>(arrayLiteral: self.product_id!) | |
let productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID); | |
productsRequest.delegate = self; | |
productsRequest.start(); | |
print("Fething Products"); | |
}else{ | |
print("can't make purchases"); | |
} | |
} | |
@IBAction func restorePurchase(sender:AnyObject){ | |
print("About to restore purchase"); | |
// For IAP with appusername | |
SKPaymentQueue.defaultQueue().restoreCompletedTransactionsWithApplicationUsername(self.appusername) | |
// For default | |
// SKPaymentQueue.defaultQueue().restoreCompletedTransactions(); | |
} | |
// Helper Methods | |
func buyProduct(product: SKProduct){ | |
print("Sending the Payment Request to Apple"); | |
// For IAP with appusername | |
let payment:SKMutablePayment = SKMutablePayment(product: product) | |
payment.applicationUsername = self.appusername!; | |
print("payment request with appusername = \(payment.applicationUsername)"); | |
// For default | |
//var payment = SKPayment(product: product) | |
SKPaymentQueue.defaultQueue().addPayment(payment); | |
} | |
// Delegate Methods for IAP | |
func productsRequest (request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) { | |
print("got the request from Apple") | |
let count : Int = response.products.count | |
if (count>0) { | |
let validProducts = response.products | |
let validProduct: SKProduct = response.products[0] as SKProduct | |
if (validProduct.productIdentifier == self.product_id) { | |
print(validProduct.localizedTitle) | |
print(validProduct.localizedDescription) | |
print(validProduct.price) | |
buyProduct(validProduct); | |
} else { | |
print(validProduct.productIdentifier) | |
} | |
} else { | |
print("nothing") | |
} | |
} | |
func request(request: SKRequest!, didFailWithError error: NSError!) { | |
print("Request fail with error \(error.localizedDescription)"); | |
} | |
// Delegate for SKPaymentTransactionObserver | |
func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) | |
{ | |
print("Received Payment Transaction Response from Apple"); | |
for transaction:SKPaymentTransaction in transactions { | |
if let trans:SKPaymentTransaction = transaction { | |
switch trans.transactionState { | |
case .Purchased: | |
print("Product Purchased"); | |
//For IAP With appusername | |
print("tran with appuser = \(trans.payment.applicationUsername)") | |
SKPaymentQueue.defaultQueue().finishTransaction(trans) | |
break; | |
case .Failed: | |
print("Purchased Failed with \(trans.error)"); | |
SKPaymentQueue.defaultQueue().finishTransaction(trans) | |
break; | |
// case .Restored: | |
//[self restoreTransaction:transaction]; | |
default: | |
break; | |
} | |
} | |
} | |
} | |
// Sent when an error is encountered while adding transactions from the user's purchase history back to the queue. | |
func paymentQueue(queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: NSError){ | |
print("restore completed but failed with error \(error.localizedDescription)"); | |
} | |
// Sent when all transactions from the user's purchase history have successfully been added back to the queue. | |
func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue){ | |
print("restore completed with queue \(queue.transactions[0].payment.productIdentifier)"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment