I'm using SwiftyStoreKit to perform subscriptions:
private func subscribeUser(withId subscriptionId: String) {
SwiftyStoreKit.purchaseProduct(subscriptionId, atomically: false) { result in
switch result {
case .success(let product):
if product.needsFinishTransaction {
SwiftyStoreKit.finishTransaction(product.transaction)
}
// Get the receipt
SwiftyStoreKit.fetchReceipt(forceRefresh: true) { result in
switch result {
case .success(let receiptData):
// Encrypt the receipt
let encryptedReceipt = receiptData.base64EncodedString(options: [])
// Set up verification url
let url = "https://myUrl/verifyReceipt"
// Send receipt to server
APIConnect.verifyReceipt(
url: url,
params: ["receipt" : encryptedReceipt],
completion: self.processSubscription)
case .error(let error):
print("SUBSCRIPTIONPAYMENTVC | subscribeUser | Fetch receipt failed: (error)")
}
}
case .error(let error):
switch error.code {
// alert user accordingly
}
}
}
}
After the receipt is sent to our server and verified, the response is handled:
func processSubscription(receiptJSON: JSON) {
print("SUBSCRIPTIONPAYMENTVC | verifySubscription | Done Verifying Subscription")
// Check response
if receiptJSON["status"] > 0 {
// alert user accordingly
} else {
// Get data from receipt
let lastReceiptIndex = receiptJSON["latest_receipt_info"].arrayValue.count - 1
// Set up varibles
productId = receiptJSON["latest_receipt_info"][lastReceiptIndex]["product_id"].stringValue
originalTransactionId = receiptJSON["latest_receipt_info"][lastReceiptIndex]["original_transaction_id"].stringValue
startDate = receiptJSON["latest_receipt_info"][lastReceiptIndex]["original_purchase_date_ms"].stringValue
endDate = receiptJSON["latest_receipt_info"][lastReceiptIndex]["expires_date_ms"].stringValue
latestReceipt = receiptJSON["latest_receipt"].stringValue
// Add user to database
addUserToDB()
}
}
However, wouldn't the user have already been charged even though processSubscription could receive a status > 0? Am I calling receipt verification at a wrong time? Where exactly does the purchase take place and how can it be blocked by the verification?
question from:
https://stackoverflow.com/questions/65849245/swiftystorekit-when-to-perform-receipt-validation 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…