Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Tenfay committed Nov 11, 2024
1 parent 8be899c commit 46e78a6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 47 deletions.
7 changes: 4 additions & 3 deletions DYFStoreDemo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,19 @@ class AppDelegate: UIResponder, UIApplicationDelegate, DYFStoreAppStorePaymentDe
}

// Processes the purchase which was initiated by user from the App Store.
func didReceiveAppStorePurchaseRequest(_ queue: SKPaymentQueue, payment: SKPayment, forProduct product: SKProduct) {
func didReceiveAppStorePurchaseRequest(_ queue: SKPaymentQueue, payment: SKPayment, forProduct product: SKProduct) -> Bool {
if !DYFStore.canMakePayments() {
self.sk_showTipsMessage("Your device is not able or allowed to make payments!")
return
return false
}

// Get account name from your own user system.
let accountName = "Handsome Jon"
// This algorithm is negotiated with server developer.
let userIdentifier = DYFStoreCryptoSHA256(accountName) ?? ""
let userIdentifier = accountName.tx_sha256 ?? ""
DYFStoreLog("userIdentifier: \(userIdentifier)")
SKIAPManager.shared.addPayment(product.productIdentifier, userIdentifier: userIdentifier)
return true
}

func applicationWillResignActive(_ application: UIApplication) {
Expand Down
4 changes: 2 additions & 2 deletions DYFStoreDemo/SKStoreViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class SKStoreViewController: UIViewController, UITableViewDelegate, UITableViewD
// Get account name from your own user system.
let accountName = "Handsome Jon"
// This algorithm is negotiated with server developer.
let userIdentifier = DYFStoreCryptoSHA256(accountName) ?? ""
let userIdentifier = accountName.tx_sha256 ?? ""
DYFStoreLog("userIdentifier: \(userIdentifier)")
SKIAPManager.shared.restorePurchases(userIdentifier)
}
Expand Down Expand Up @@ -70,7 +70,7 @@ class SKStoreViewController: UIViewController, UITableViewDelegate, UITableViewD
// Get account name from your own user system.
let accountName = "Handsome Jon"
// This algorithm is negotiated with server developer.
let userIdentifier = DYFStoreCryptoSHA256(accountName) ?? ""
let userIdentifier = accountName.tx_sha256 ?? ""
DYFStoreLog("userIdentifier: \(userIdentifier)")
SKIAPManager.shared.addPayment(productIdentifier, userIdentifier: userIdentifier)
}
Expand Down
81 changes: 40 additions & 41 deletions DYFStoreDemo/Sample/SKIAPManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,16 @@ open class SKIAPManager: NSObject, DYFStoreReceiptVerifierDelegate {
return
}

let transaction = persister.retrieveTransaction(identifier)
if let tx = transaction {
DYFStoreLog("transaction.state: \(tx.state)")
DYFStoreLog("transaction.productIdentifier: \(tx.productIdentifier!)")
DYFStoreLog("transaction.userIdentifier: \(tx.userIdentifier ?? "null")")
DYFStoreLog("transaction.transactionIdentifier: \(tx.transactionIdentifier!)")
DYFStoreLog("transaction.transactionTimestamp: \(tx.transactionTimestamp!)")
DYFStoreLog("transaction.originalTransactionIdentifier: \(tx.originalTransactionIdentifier ?? "null")")
DYFStoreLog("transaction.originalTransactionTimestamp: \(tx.originalTransactionTimestamp ?? "null")")
if let tx = persister.retrieveTransaction(identifier) {
DYFStoreLog("tx.state: \(tx.state)")
DYFStoreLog("tx.productIdentifier: \(tx.productIdentifier!)")
DYFStoreLog("tx.userIdentifier: \(tx.userIdentifier ?? "null")")
DYFStoreLog("tx.transactionIdentifier: \(tx.transactionIdentifier!)")
DYFStoreLog("tx.transactionTimestamp: \(tx.transactionTimestamp!)")
DYFStoreLog("tx.originalTransactionIdentifier: \(tx.originalTransactionIdentifier ?? "null")")
DYFStoreLog("tx.originalTransactionTimestamp: \(tx.originalTransactionTimestamp ?? "null")")
if let receiptData = tx.transactionReceipt!.base64DecodedData() {
DYFStoreLog("transaction.transactionReceipt: \(receiptData)")
DYFStoreLog("tx.transactionReceipt: \(receiptData)")
self.verifyReceipt(receiptData)
}
}
Expand All @@ -176,22 +175,22 @@ open class SKIAPManager: NSObject, DYFStoreReceiptVerifierDelegate {
let info = self.purchaseInfo!
let persister = DYFStoreUserDefaultsPersistence()

let transaction = DYFStoreTransaction()
let tx = DYFStoreTransaction()
if info.state! == .succeeded {
transaction.state = DYFStoreTransactionState.purchased.rawValue
tx.state = DYFStoreTransactionState.purchased.rawValue
} else if info.state! == .restored {
transaction.state = DYFStoreTransactionState.restored.rawValue
tx.state = DYFStoreTransactionState.restored.rawValue
}

transaction.productIdentifier = info.productIdentifier
transaction.userIdentifier = info.userIdentifier
transaction.transactionTimestamp = info.transactionDate?.timestamp()
transaction.transactionIdentifier = info.transactionIdentifier
transaction.originalTransactionTimestamp = info.originalTransactionDate?.timestamp()
transaction.originalTransactionIdentifier = info.originalTransactionIdentifier
tx.productIdentifier = info.productIdentifier
tx.userIdentifier = info.userIdentifier
tx.transactionTimestamp = info.transactionDate?.timestamp()
tx.transactionIdentifier = info.transactionIdentifier
tx.originalTransactionTimestamp = info.originalTransactionDate?.timestamp()
tx.originalTransactionIdentifier = info.originalTransactionIdentifier

transaction.transactionReceipt = data.base64EncodedString()
persister.storeTransaction(transaction)
tx.transactionReceipt = data.base64EncodedString()
persister.storeTransaction(tx)

self.verifyReceipt(data)
} catch let error {
Expand All @@ -215,10 +214,10 @@ open class SKIAPManager: NSObject, DYFStoreReceiptVerifierDelegate {
DYFStoreLog()
self.sk_hideLoading()
self.sk_showAlert(withTitle: NSLocalizedString("Notification", tableName: nil, comment: ""),
message: "Fail to refresh receipt! Please check if your device can access the internet.",
cancelButtonTitle: "Cancel",
cancel: { (cancelAction) in },
confirmButtonTitle: NSLocalizedString("Retry", tableName: nil, comment: ""))
message: "Fail to refresh receipt! Please check if your device can access the internet.",
cancelButtonTitle: "Cancel",
cancel: { (cancelAction) in },
confirmButtonTitle: NSLocalizedString("Retry", tableName: nil, comment: ""))
{ (action) in
self.refreshReceipt()
}
Expand Down Expand Up @@ -265,10 +264,10 @@ open class SKIAPManager: NSObject, DYFStoreReceiptVerifierDelegate {

private func sendNotice(_ message: String) {
self.sk_showAlert(withTitle: NSLocalizedString("Notification", tableName: nil, comment: ""),
message: message,
cancelButtonTitle: nil,
cancel: nil,
confirmButtonTitle: NSLocalizedString("I see!", tableName: nil, comment: ""))
message: message,
cancelButtonTitle: nil,
cancel: nil,
confirmButtonTitle: NSLocalizedString("I see!", tableName: nil, comment: ""))
{ (action) in
DYFStoreLog("alert action title: \(action.title!)")
}
Expand All @@ -288,12 +287,12 @@ open class SKIAPManager: NSObject, DYFStoreReceiptVerifierDelegate {
let identifier = info.transactionIdentifier!

if info.state! == .restored {
let transaction = store.extractRestoredTransaction(identifier)
store.finishTransaction(transaction)
let tx = store.extractRestoredTransaction(identifier)
store.finishTransaction(tx)
} else {
let transaction = store.extractPurchasedTransaction(identifier)
let tx = store.extractPurchasedTransaction(identifier)
// The transaction can be finished only after the client and server adopt secure communication and data encryption and the receipt verification is passed. In this way, we can avoid refreshing orders and cracking in-app purchase. If we were unable to complete the verification, we want `StoreKit` to keep reminding us that there are still outstanding transactions.
store.finishTransaction(transaction)
store.finishTransaction(tx)
}

persister.removeTransaction(identifier)
Expand All @@ -313,10 +312,10 @@ open class SKIAPManager: NSObject, DYFStoreReceiptVerifierDelegate {
if error.code < 21000 {
// After several attempts, you can cancel refreshing receipt.
self.sk_showAlert(withTitle: NSLocalizedString("Notification", tableName: nil, comment: ""),
message: "Fail to verify receipt! Please check if your device can access the internet.",
cancelButtonTitle: "Cancel",
cancel: nil,
confirmButtonTitle: NSLocalizedString("Retry", tableName: nil, comment: ""))
message: "Fail to verify receipt! Please check if your device can access the internet.",
cancelButtonTitle: "Cancel",
cancel: nil,
confirmButtonTitle: NSLocalizedString("Retry", tableName: nil, comment: ""))
{ (action) in
DYFStoreLog("alert action title: \(action.title!)")
self.verifyReceipt(nil)
Expand All @@ -333,12 +332,12 @@ open class SKIAPManager: NSObject, DYFStoreReceiptVerifierDelegate {
let identifier = info.transactionIdentifier!

if info.state! == .restored {
let transaction = store.extractRestoredTransaction(identifier)
store.finishTransaction(transaction)
let tx = store.extractRestoredTransaction(identifier)
store.finishTransaction(tx)
} else {
let transaction = store.extractPurchasedTransaction(identifier)
let tx = store.extractPurchasedTransaction(identifier)
// The transaction can be finished only after the client and server adopt secure communication and data encryption and the receipt verification is passed. In this way, we can avoid refreshing orders and cracking in-app purchase. If we were unable to complete the verification, we want `StoreKit` to keep reminding us that there are still outstanding transactions.
store.finishTransaction(transaction)
store.finishTransaction(tx)
}

persister.removeTransaction(identifier)
Expand Down
2 changes: 1 addition & 1 deletion DYFStoreDemo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class ViewController: UIViewController {
// Get account name from your own user system.
let accountName = "Handsome Jon"
// This algorithm is negotiated with server developer.
let userIdentifier = DYFStoreCryptoSHA256(accountName) ?? ""
let userIdentifier = accountName.tx_sha256 ?? ""
DYFStoreLog("userIdentifier: \(userIdentifier)")
SKIAPManager.shared.addPayment(productId, userIdentifier: userIdentifier)
}
Expand Down

0 comments on commit 46e78a6

Please sign in to comment.