創建app內購買項目
消耗型項目:對於消耗型App內購買項目,用戶每次下載時都必須進行購買。一次性服務通常屬於消耗型項目,例如釣魚App 中的魚餌。
非消耗型項目:對於非消耗型App內購買項目,用戶僅需要購買一次。不會過期或隨使用而減少的服務通常為非消耗型項目,例如游戲App 的新跑道。
自動續訂訂閱:通過自動續訂訂閱,用戶可以購買指定時間期限內的更新和動態內容。除非用戶取消選擇,否則訂閱(例如雜志訂閱等)會自動續訂。
免費訂閱:通過免費訂閱,開發者可以將免費訂閱內容放入“報刊雜志”。用戶注冊免費訂閱后,該訂閱內容將會出現在與該用戶Apple ID 關聯的所有設備上。請注意,免費訂閱不會過期,並且僅在支持報刊雜志功能的 App 中提供。
非續訂訂閱:非續訂訂閱允許有時限性的營銷服務。對於 App 內購買項目中的限時訪問內容,就需使用非續訂訂閱。例如,導航App 中語音導航功能的一周訂閱,或者年度訂閱已存檔的視頻或音頻的在線目錄。
測試環境
在sandbox中驗證receipt:https://sandbox.itunes.apple.com/verifyReceipt
在生產環境中驗證receipt:https://buy.itunes.apple.com/verifyReceipt
那么如何自動的識別收據是否是sandbox receipt呢?
識別沙盒環境下收據的方法有兩種:
- 根據收據字段 environment = sandbox。
- 根據收據驗證接口返回的狀態碼。
如果status=21007,則表示當前的收據為沙盒環境下收據, t進行驗證。
蘋果反饋的狀態碼:
- 21000 App Store無法讀取你提供的JSON數據
- 21002 收據數據不符合格式
- 21003 收據無法被驗證
- 21004 你提供的共享密鑰和賬戶的共享密鑰不一致
- 21005 收據服務器當前不可用
- 21006 收據是有效的,但訂閱服務已經過期。當收到這個信息時,解碼后的收據信息也包含在返回內容中
- 21007 收據信息是測試用(sandbox),但卻被發送到產品環境中驗證
- 21008 收據信息是產品環境中使用,但卻被發送到測試環境中驗證
注意:
在verifyWithRetry方法中,首先向向真實環境驗證票據,如果是21007則向沙盒環境驗證;但是在消耗品類型的測試中,使用沙盒票據在真實環境中驗證票據得到返回碼:21002.所以下面代碼在真實環境運行時,沙盒測試消耗型商品得不到正確的驗證結果。
/*
verifyWithRetry the receipt
*/
IAPVerifier.verifyWithRetry = function(receipt, isBase64, cb) {
var encoded = null, receiptData = {};
if (isBase64) {
encoded = receipt;
} else {
encoded = new Buffer(receipt).toString('base64');
}
receiptData['receipt-data'] = encoded;
var options = this.requestOptions();
return this.verify(receiptData, options, (function(_this) {
return function(error, data) {
if (error) return cb(error);
if ((21007 === (data != null ? data.status : void 0)) && (_this.productionHost == _this.host)) {
var options_this.requestOptions();
// 指向沙盒測試環境再次驗證
options.host = 'sandbox.itunes.apple.com/verifyReceipt';
return _this.verify(receiptData, options, function(err, data) {
return cb(err, data);
});
} else {
return cb(err, data);
}
};
})(this));
};
/*
verify the receipt data
*/
IAPVerifier.verify = function(data, options, cb) {
var post_data, request;
post_data = JSON.stringify(data);
options.headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': post_data.length
};
var request = https.request(options, (function(_this) {
return function(response) {
var response_chunk = [];
response.on('data', function(data) {
if (response.statusCode !== 200) {
return cb(new Error("response.statusCode != 200"));
}
response_chunk.push(data);
});
return response.on('end', function() {
var responseData, totalData;
totalData = response_chunk.join('');
try {
responseData = JSON.parse(totalData);
} catch (_error) {
return cb(_error);
}
return cb(null, responseData);
});
};
})(this));
request.write(post_data);
request.end();
request.on('error', function (exp) {
console.log('problem with request: ' + exp.message);
});
};
IAPVerifier.requestOptions = function() {
return options = {
host: 'buy.itunes.apple.com',
port: 443,
path: '/verifyReceipt',
method: "POST",
rejectUnauthorized: false/*不加:返回證書不受信任CERT_UNTRUSTED*/
};
};
建議:
為保證審核的通過,需要在客戶端或server進行雙重驗證,即,先以線上交易驗證地址進行驗證,如果蘋果正式驗證服務器的返回驗證碼code為21007,則再一次連接沙盒測試服務器進行驗證即可。在應用提審時,蘋果IAP提審驗證時是在沙盒環境的進行的,即:蘋果在審核App時,只會在sandbox環境購買,其產生的購買憑證,也只能連接蘋果的測試驗證服務器,如果沒有做雙驗證,需要特別注意此問題,否則會被拒。